1

I'm trying to do some preprocessing of my code by using annotations. On loading i want to make a "virtual" copy of the class and it's functions. But at compile/build time i have to do some "precompilation" using the annotations. I need code in my process-function of a subclass of the AbstractProcessor of java. It shall add the annotation "@Named" with special arguments(depending on the defined name in the annotation @SpecialClass and the methods/fields names) to every method/field that has neither @Invisible nor already @Named means to every field/method without any annotation. The process-function of AbstractProcessor gets called if the @SpecialClass-Annotation is detected.

I'd like to compile this:

@SpecialClass(name="Class1")
public class Test{

    public int var1;

    @Invisible
    public int priv;

    @Named(name="variable", namespace="Class1")
    public int var2;

    public void funcA(...){...}

    @Invisible
    public void funcB(...){...}

    @Named(name="funcX", namespace="Class1")
    public void funcC(...){...}
}

to this:

@SpecialClass(name="Class1")
public class Test{

    @Named(name="var1", namespace="Class1")
    public int var1;

    @Invisible
    public int priv;

    @Named(name="variable", namespace="Class1")
    public int var2;

    @Named(name="funcA", namespace="Class1")
    public void funcA(...){...}

    @Invisible
    public void funcB(...){...}

    @Named(name="funcX", namespace="Class1")
    public void funcC(...){...}
}

My problem at this time is that i don't know how to add an annotation to a classobject (to the fields/methods) Also i don't understand the operation of the 2 parameters of the process-function.

I already found this topic: Adding programmatic annotations to a Java class but noone really gave a concrete example. That's why i'm asking here again hoping that somebody can describe the way it works.

Thank you in advance.

ADDED INFORMATION:

Actually i didn't want to ask exactly that because I don't want guys thinking that I just want to have the solution of you but when I asked for an example I hoped that someone can solve it for me. Later when code gets loaded into the VM I'm checking every class for the annotation @Named. If I found one I'll register the function or field in an object that represents the namespace. This means if I want to call a function by this nickname I just pick the "object" and look for the nick of the method. I need to complete the methods and fields with the annotation at compile time instead of runtime because it could be that the real method names get obfuscated. So the methods and fields at runtime maybe have senseless names. That's why I wanted to complete the code before it gets obfuscated -> at compiletime.

Community
  • 1
  • 1
rapus95
  • 31
  • 4
  • Your question is not very clear but IMO, you're looking in the wrong direction here. Look at using at the [behavioral design patterns](http://stackoverflow.com/q/350404/1530938) instead of this convoluted (and most likely impossible) use of annotations you're contemplating – kolossus Aug 19 '13 at 19:21
  • Actually added some information that will describe the problem better. But already thanks for the link. I just didn't understand in which way it could support the operation. Can you give me an example? :/ Sorry that I don't understand it at the moment and that I'm asking for more precise information. – rapus95 Aug 20 '13 at 00:22

0 Answers0