Skip to content Skip to sidebar Skip to footer

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 o

Solution 1:

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

Solution 2:

I think you could create an interface defining this method. That way, your class will override the method and there should not be any warning.

FromXML.java:

public@interface FromXML {
}

MyInterface.java:

publicinterfaceMyInterface {
    @FromXMLpublicvoidonSomethingClick(View v);
}

MyClass.java

publicMyClassimplementsMyInterface {

    @Override@FromXML
    public void onSomethingClick(View v){
    }
}

EDIT : Another solution could be to define your method as abstract. Indeed, as I understand your code, your methods are just declaration (Implementations are in a XML file). So, your problem is more a design problem than an IDE problem (your IDE is just right about the warning). The reality is that your method is abstract and is defined somewhere else.

Thus, defining your method as abstract will solve the problem but you'll have to make the class abstract:

publicabstractclassMyClassUsingOnSomethingClick {

/*
 * All the class implementation can be here as the normal class.
 */@FromXMLpublicabstractvoidonSomethingClick(View v);

}

I know you'll say that this solution make it impossible to create object easily but you'll have two solutions then:

1 - Create your objects inline:

MyClassUsingOnSomethingClicka=newMyClassUsingOnSomethingClick() {
    @Override@FromXMLpublicvoidonSomethingClick(View v) {}
};

2 - Create a factory method in your abstract MyClassUsingOnSomethingClick:

publicstaticfinal MyClassUsingOnSomethingClick createEmptyMyClassUsingOnSomethingClick() {
    returnnewMyClassUsingOnSomethingClick() {
        @Override@FromXMLpublicvoidonSomethingClick(View v) {}
    };
}

// and then, create with: :MyClassUsingOnSomethingClicka= MyClassUsingOnSomethingClick.createEmptyMyClassUsingOnSomethingClick();

Even is I understand that you would prefer a faster solution, I believe that this solution is the cleanest because:

  1. It respects the Object Oriented Programming philosophy.
  2. It is not specific to an IDE.
  3. It avoids Annotation Processing Tool (which, in my opinion should be used very wisely)

Solution 3:

Ok I can't do this in any easy way or form.

Looking at the annotation package http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/annotation/package-summary.html

I can't extend an annotation: Why is not possible to extend annotations in Java?

I can't implement another annotation java.lang.Override

I can't mimic / mock / pretend to be @Override

If I add @Override to my @FromXML it is NOT inherited down the chain.

The only way would be to create an Eclipse plugin that recognises my annotation and stops the warning. Shame because I can't find an easy way to do this.

I also went down the route of creating an interface for my @FromXML entry points, this was very nice and communicated my Activity was of a type and therefore I didn't need the annotation anymore, perhaps this design change is the answer.

Post a Comment for "Create A Java Annotation With Ide Contextual Behaviour"