Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a Java Annotation with IDE contextual behaviour

I've created an Annotation

/**
 * Highlights this method is declared in XML
 */
public @interface FromXML {
}

I'm using this on methods that look like this:

@FromXML
public void onSomethingClick(View v){

}

The v variable is needed by the Android reflection system to call this method. However the input var v is unused, so my IDE warns me of this. I like this warning and want it on for the rest of my code.

enter image description here

To hide the warning I could do

@SuppressWarnings("unused")
@FromXML
public void onSomethingClick(View v){

}

or I could add a param tag

But I would rather that the IDE (eclipse) reads my @FromXML annotation and doesn't give me the warning.

I know you can't extend an Annotation, but thats basically what I want to do.

Is there a way I can add my annotation to be recognised as a 'suppress warnings' annotation.

or

Is there a way to code my annotation to 'act like' suppress warnings?

like image 244
Blundell Avatar asked Nov 26 '12 11:11

Blundell


1 Answers

You can always create a plugin for Eclipse, that would scan through the source and find these annotations in your case @FromXML and add an extra annotation in your case @SuppressWarnings.

You could create a Command for it and when that command is fired you would run this plugin.

Creating Eclipse Plugin

Hope this helps.

UPDATE - IT WAS A FLUKE CANNOT BE DONE USING THIS (TRIED IT):

Or Using AspectJ for removing the warnings Adding warnings in Eclipse using AspectJ

This tutorial uses AspectJ for adding warnings to eclipse if developer uses System.out.println() in the code. So the reverse can be done to remove the warning when annotation is present.

UPDATE 2: There is a way in Eclipse to create custom annotation processor or editting the bundeled annotation processor (that generates the unused variable warning). So will have to tweak that processor in a custom way.

Some great links:

Tutorials for Eclipse Annotation processor development

Java Development Tools - Annotation Processing Tools

like image 77
Narendra Pathai Avatar answered Sep 25 '22 06:09

Narendra Pathai