Results 1 to 3 of 3
  1. #1
    milan.nosal is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default Reflection tool for working with annotations

    Hello,
    I am looking for a tool, that provides some functionality for working with annotations by reflection. Something with methods for getting all annotations of concrete annotation type, and so on, functionality, that is provided by APT annotation processor. Do anybody knows about something? I have founded a project Google Reflections, that could be the one, but I have some problems to make it work (probably my own unpracticality). So if there is anyone that has some expierence with it, I would appreciate some walkthrough for start (rather through email).
    Thanks,
    Milan Nosal

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    I keep using the java reflection API to find the annotations where I need to work with them, for example,

    Java Code:
      /**
       * Find the declared methods in a class that have the specified annotation.
       * @param ref
       * @param annotationClass
       * @return
       */
      public static Collection<Method> findMethodsWithAnnotation(Class<?> ref, Class<? extends Annotation> annotationClass) {
        Collection<Method> results = new ArrayList<Method>();
    
        Method[] methods = ref.getMethods();
        for (int i = 0; i < methods.length; i++) {
          Method method = methods[i];
          Annotation annotation = method.getAnnotation(annotationClass);
          if (annotation != null) {
            results.add(method);
          }
        }
        return results;
      }
    If you want to "find all classes that have an annotation" on the entire classpath, I have found the project "scannotation" helpful. This is important for if you have a fairly large code base, such as many many jar files in your application, as they use a mechanism to read the .class file structure from within a given package starting point to find annotations outside of the class loader; doing it with the class loader can waste the permgen memory space by loading classes that are not ever used in your application.

  3. #3
    milan.nosal is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks for prompt answer. I am looking for something like scannotation. Scanotation is not exactly what I need, but it seems to be really helpful.

Similar Threads

  1. Annotations
    By yogesh_SC in forum Advanced Java
    Replies: 1
    Last Post: 04-02-2009, 09:33 PM
  2. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  3. Annotations
    By javarishi in forum Advanced Java
    Replies: 1
    Last Post: 05-02-2008, 10:51 AM
  4. Array Reflection: Multi Array Reflection
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:08 PM
  5. @ annotations
    By mew in forum New To Java
    Replies: 1
    Last Post: 12-12-2007, 11:04 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •