vendredi 29 décembre 2017

How to call all methods annotated @someAnnotation if this methods are in other package and class?

I fave an annotation in package "fillers":

  package fillers;
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface someAnnotation {
    }

I have methods:

package fillers;
      @someAnnotation
        public static void randomized() {
            System.out.println("one");
            }
        }

        @someAnnotation
        public static void sortedArray() {
           System.out.println("two");
        }

And I want to invoke all annotated methods from another package. And I did next:

package analyzer;
public class Analyzer {
     public static void runAllAnnotatedWith() throws Exception {
         Reflections reflections = new Reflections(new ConfigurationBuilder()
      .setUrls(ClasspathHelper.forPackage("someAnnotation.ArrayFiller"))
                 .setScanners(new MethodAnnotationsScanner()));
         Set<Class<? extends ArrayFiller>> subTypes = reflections.getSubTypesOf(ArrayFiller.class);
         Set<Method> methods = reflections.getMethodsAnnotatedWith(Filler.class);

         for (Method m : methods) {
             // for simplicity, invokes methods as static without parameters
             m.invoke(null);
         }
     }
}

And i got this:

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Predicate
    at analyzer.Analyzer.runAllAnnotatedWith(Analyzer.java:22)
    at Main.main(Main.java:11)
Caused by: java.lang.ClassNotFoundException:.......

In my mind, exception is becouse of wrong reflection. But I don`t understand fow to fix it.





Aucun commentaire:

Enregistrer un commentaire