How do I declare a method from a Java API as deprecated for my local project?

78 views Asked by At

I am attempting to add functionality to my class, CustomList, which extends JList. Now all of the current usages of CustomList call the non-overridden method setModel(Listmodel<E> lm) from JList. I am stuck on an older version of Java, Java 1.6, which does not allow me to directly override this method due to erasure problems. All of my research has brought me to the conclusion that I must rename this method and replace all the current usages with the newly named method.

What I would like to do is give some warning to other people working with CustomList that they need to call this method rather than JList.setModel(ListModel<E> lm). I am unsure of how to locally deprecate a native method for my project.

A simple example outlining my new method is shown below:

public class CustomList<E> extends JList {
    private ArrayList list;
    public void newSetModel (ListModel<E> lm){
        super.setModel(lm);
        list = new ArrayList<Boolean> (lm.getSize());
    }   
}

Please note that I cannot simply make JList generic in the extends to allow an override since I am stuck with Java 1.6

1

There are 1 answers

1
Sean Patrick Floyd On BEST ANSWER

There is no sane way to do that in Java.

Ideally, you have a build / CI process, and in that you can use tools for Policy Enforcement. Look at Checkstyle & PMD, perhaps one of them will allow you to blacklist a specific method call.

Failing that, you can use a low-level technology like asm or aspectj to write your own check, but it won't be easy. Or you can go old school and write a test that parses source files and looks for the bad method call, but that's very error-prone.

I'd say your best bet is letting your other team members know. Do you guys do code reviews? You should.