Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate compiler warning with user defined annotation

Is it possible for make the compiler generate a warning when it encounters a user defined annotaion? Something similar to the @Deprecated annotation?

Thanks

like image 450
Varun Achar Avatar asked Jul 31 '11 13:07

Varun Achar


People also ask

What is @deprecated annotation in Java?

@Deprecated @Deprecated annotation indicates that the marked element is deprecated and should no longer be used. The compiler generates a warning whenever a program uses a method, class, or field with the @Deprecated annotation.

What is the @override annotation used for?

The @Override annotation denotes that the child class method overrides the base class method. For two reasons, the @Override annotation is useful. If the annotated method does not actually override anything, the compiler issues a warning. It can help to make the source code more readable.


3 Answers

Based on your original question and comments, I assume you're trying to do the following:

  • Mark code as incomplete (with a compiler warning) so other developers do not use it yet.
  • Identify the incomplete code in your IDE at a later point in time.

I don't believe you can mark the code with a compiler warning. The @Deprecated tag is baked into the compiler. A more common way of indicating a method is incomplete is by throwing an exception:

throw new UnsupportedOperationException("Not implemented yet");

The effect isn't realized until runtime, but the other developers should be unit testing their code.

As for identifying the incomplete code I would still refer back to my original comment. Use the TODO comment tag and Eclipse will build a task list for you. If your list is cluttered with auto-generated code that hasn't been cleaned up, you can use FIXME, XXX, or define your own. You should then be able to filter your list.

like image 102
jkeeler Avatar answered Oct 15 '22 07:10

jkeeler


I've asked the Lombok people to look at providing this functionality https://github.com/peichhorn/lombok-pg/issues/114 and it is now implemented ;-) https://github.com/peichhorn/lombok-pg/wiki/%40Warning

like image 2
fommil Avatar answered Oct 15 '22 07:10

fommil


It's possible via annotation processors api

Here is Lightweight Javac Warning Annotation library https://github.com/pushtorefresh/javac-warning-annotation

Usage:

// some code...

@Warning("This method should be refactored")
public void someCodeWhichYouNeedAtTheMomentButYouWantToRefactorItLater() {
    // bad stuff going on here...
}
like image 1
Artem Zinnatullin Avatar answered Oct 15 '22 05:10

Artem Zinnatullin