Create a Java Annotation with IDE contextual behaviour

為{幸葍}努か 提交于 2020-01-01 02:51:33

问题


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.

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?


回答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




回答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:

public interface MyInterface {
    @FromXML
    public void onSomethingClick(View v);
}

MyClass.java

public MyClass implements MyInterface {

    @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:

public abstract class MyClassUsingOnSomethingClick {

/*
 * All the class implementation can be here as the normal class.
 */

    @FromXML
    public abstract void onSomethingClick(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:

MyClassUsingOnSomethingClick a = new MyClassUsingOnSomethingClick() {
    @Override
    @FromXML
    public void onSomethingClick(View v) {}
};

2 - Create a factory method in your abstract MyClassUsingOnSomethingClick:

public static final MyClassUsingOnSomethingClick createEmptyMyClassUsingOnSomethingClick() {
    return new MyClassUsingOnSomethingClick() {
        @Override
        @FromXML
        public void onSomethingClick(View v) {}
    };
}

// and then, create with: :
MyClassUsingOnSomethingClick a = 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)



回答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.



来源:https://stackoverflow.com/questions/13563820/create-a-java-annotation-with-ide-contextual-behaviour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!