6 Replies Latest reply: Sep 2, 2008 7:12 AM by 843793 RSS

    Creating Your Own Annotations - Resource Injection

    843793
      I have Googled for a little while and haven't had much luck finding what I am looking for.

      I am looking to write my own, custom Annotation that provides similar functionality to the EJB 3 @PersistenceContext and related-annotations that allows me to use an Annotation to reduce boilerplate code.

      A good, simple example of what I am trying to do is this (and this should be enough to get me started if I can solve this problem):

      All of my processing classes have:
      protected static final Logger logger = Logger.getLogger(MyClass.class)
      .

      After this has been done, a static code block performs some other Log4J initialization.

      I would like to just do this:
      @Logger
      private static Logger logger;
      Marking the logger field with the @Logger annotation, I want to pull the functionality out from the static initialization block and have it automatically taken care of.

      I have read the following articles, but do not think they cover what I am trying to do (unless I'm missing the idea):
      - http://www.oracle.com/technology/pub/articles/hunter_meta_2.html
      - http://www.oracle.com/technology/pub/articles/hunter_meta_3.html


      Can someone provide me with a link to an article that explains things like this or tell me what I'm missing?
        • 1. Re: Creating Your Own Annotations - Resource Injection
          843793
          Have a look at https://fuse.dev.java.net, its a resource injection library specifically designed for GUI programming but also supports custom types.
          • 2. Re: Creating Your Own Annotations - Resource Injection
            843793
            Perfect.
            With very little effort I was able to find what I needed on that site.

            Thank you.
            • 3. Re: Creating Your Own Annotations - Resource Injection
              843793
              http://forum.java.sun.com/thread.jspa?threadID=5176050&tstart=20
              • 4. Re: Creating Your Own Annotations - Resource Injection
                843793
                So I created my sample annotation and created a factory class that handles running the resource injection. Running the class with the injection called at construction time on a class worked just fine.

                However... how do I request that at runtime all classes have this done automatically?

                Here is my sample annotation:
                @Documented
                @Retention(RetentionPolicy.RUNTIME)
                @Target(ElementType.FIELD)
                public @interface HelloAnnotationWorld
                {
                }
                Here is the method in my injection factory to handle it:
                public static <T> void inject(Class<T> resolveeClass, T resolveeInstance) {
                    HelloAnnotationWorld haw;
                
                    // base
                    Field[] fields = resolveeClass.getFields();
                
                    // get all the fields declared in the class
                    if (fields != null) {
                      for (Field field : fields) {
                        // see if the field has a logger injection
                        if haw = field.getAnnotation(HelloAnnotationWorld.class)) != null) {
                
                          // run the injection
                          try {
                            field.set(resolveeInstance, "Hello, Annotation World!");
                          }
                          catch (IllegalAccessException e) {
                            ExceptionHandler.DEFAULT.handle(e, LOGGER);
                          }
                        }
                      }
                    }
                  }
                The class used to test this is:
                class HAWTest {
                  @HelloAnnotationWorld
                  public String helloWorld;
                
                  public static void main(String[] args) {
                    System.out.println(new HAWTest().helloWorld);
                  }
                }
                Creating a constructor in there that calls the inject(Class<T>, T) method results in everything working just fine. However, I want the class to work just as it is shown above... without calling the inject method directly in the constructor.
                • 5. Re: Creating Your Own Annotations - Resource Injection
                  843793
                  couple of thoughts
                  1) create a base class in your object model that calls inject(), all pertinent classes extend (inject() could be made to resolve annotation and values in a general manner)

                  2) follow the J2EE model, make your own container to run within, define and implement an interface that the container can use to inject the resources

                  3) create a classloader capable of doing the inject (variation of 2)
                  • 6. Re: Creating Your Own Annotations - Resource Injection
                    843793
                    Hi,
                    I am also working for the same from long time . could you please help me out .
                    Really it would be the great help for me. could you send me the complete source code and the compilation command.
                    my mail id is nanimca@gmail.com

                    Thanks and Regards,
                    uday Kiran D