Using Java annotations - code generation

I am using java 6 api annotation processing. I have completed the following excellent tutorial on creating an annotation processor that displays a message at build time:

http://kerebus.com/2011/02/using-java-6-processors-in-eclipse/

However, in my case, I have a simple class:

import java.lang.annotation.ElementType; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(value = ElementType.METHOD) public @interface Criteria { String id(); double width(); double height(); } 

As you can see, the above annotation is made available to the JVM at runtime using the Save meta annotation. I use this “Criteria” annotation in the source code of another class to annotate a method, for example:

 @Criteria(id = "fooBar", width = 22, height = 10 ) public void fooStream() { System.out.println("foo stream method"); } 

At run time, I want to include the "fooStream" method in another class, ONLY if the variables that are being passed match the values ​​of the elements in the @Criteria annotation, namely "width" and "height". My question is: how can I use the fooStream method and inject it into another class at runtime? Is it possible? I'm not looking for code examples, just the answers to the two above questions. In addition, the link at the top shows an example of creating code using instances of "JavaFileObject" and "Writer", where the generated code is passed as a string.

+4
source share
2 answers

If you want to change the runtime of classes , you can use your own class loader and intercept class loads, inspect what you want, and generate new bytecode using the asm library instead of the original classes. It is not very difficult, but you must be sure that you need this.

0
source

I do not believe that Java supports a runtime mutation, which means changing members in a given class that you would need to drop to the compile-time preprocessor or bytecode modification circuit.

I could point you in a better direction if I understood the “why” behind this question, but at the same time, dynamic proxy classes can lead you to where you want to be ( JavaWorld article ).

From the documentation:

A dynamic proxy class is a class that implements a list of interfaces specified at runtime, so a method call through one of the interfaces on the class instance will be encoded and sent to another object via the interface form. Thus, a dynamic proxy class can be used to create a safe type of proxy object for a list of interfaces without requiring preliminary generation of a proxy class, for example, with compilation tools. A method call to an instance of a dynamic proxy class is sent to a single method in a call to the handler instance, and they are encoded using the java.lang.reflect.Method object definition of the method that was called and an array of type Object containing arguments.

Here's an example when using Spring to add dynamic proxies based on custom annotations. I think this is probably the closest thing to the behavior you are after.

+4
source

All Articles