Trigger error when using a particular third-party or java.* method or constructor

158 views Asked by At

Is there some Java library that connects to the compiler that can allow you to add a warning or even failure when using any method and output a text recommending something else?

For instance, if I want a user calling Arrays.asList() to get a warning or even failure and recommend another method to use?

2

There are 2 answers

11
Louis Wasserman On BEST ANSWER

You asked for something that plugs into the compiler, so you might try Error Prone, which lets you write custom compile errors by plugging into the compiler API.

(Error Prone, and tools built on top of it that aren't open-sourced yet, are exactly what Google uses for exactly this problem. Source: I sit next to the Error Prone team.)

An example check, to flag usages of Class.getClass(), is here. (That is, you already had a Class object, and then you called getClass() on that, which returns Class.class, which is just silly.)

3
Bhupi On

i believe Eclipse will be good enough to take care of such things .

Eclipse - > preferences ->Java->Compiler ->Error /warnings ,

you can play with options here. but to make to make it more and more generic , you need to use eclipse provided APIs