Example usage for java.lang.reflect Method getAnnotations

List of usage examples for java.lang.reflect Method getAnnotations

Introduction

In this page you can find the example usage for java.lang.reflect Method getAnnotations.

Prototype

public Annotation[] getAnnotations() 

Source Link

Usage

From source file:X.java

public static void main(String[] args) {
    try {/*from ww  w .  jav  a 2s .  com*/
        Class<?> clazz = Class.forName("X");
        X x = (X) clazz.newInstance();
        Class[] argTypes = { String.class };
        Method method = clazz.getMethod("objectMethod", argTypes);
        Annotation[] annos = method.getAnnotations();
        for (Annotation anno : annos) {
            System.out.println(anno);
        }
        System.out.println();

    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:org.janusgraph.TestBed.java

/**
 * @param args/*w  w w  .j  av a2  s .com*/
 * @throws java.io.IOException
 */
public static void main(String[] args) throws Exception {
    Method method = TestBed.class.getMethod("getInt", int.class, int.class);
    AnnotatedType rt = method.getAnnotatedReturnType();
    System.out.println(rt.getType());
    System.out.println(rt.getAnnotations().length);
    System.out.println(method.getAnnotations().length);
    for (int i = 0; i < method.getAnnotations().length; i++) {
        System.out.println(method.getAnnotations()[i]);
    }

    //        String[] s = {"a","b","c","d","e","f","g","h","i","x","u"};
    //        int len = s.length;
    //        Random random = new Random();
    //
    //        Context c = new Context(new ObserverManager(),Observer.NO_OP);
    //        //Warmup
    //        for (int i = 0; i < 1000000000; i++) {
    //            c.observe(s[1],s[2]);
    //        }
    //        long before = System.nanoTime();
    //        for (int i = 0; i < 1000000000; i++) {
    //            c.observe(s[1],s[2]);
    //        }
    //        long total = System.nanoTime()-before;
    //        System.out.println("Total time: " + total/1000000);

    System.exit(0);

    final ScheduledExecutorService exe = new ScheduledThreadPoolExecutor(1, new RejectedExecutionHandler() {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            r.run();
        }
    });
    ScheduledFuture future = exe.scheduleWithFixedDelay(new Runnable() {
        AtomicInteger atomicInt = new AtomicInteger(0);

        @Override
        public void run() {
            try {
                for (int i = 0; i < 10; i++) {
                    exe.submit(new Runnable() {

                        private final int number = atomicInt.incrementAndGet();

                        @Override
                        public void run() {
                            try {
                                Thread.sleep(150);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            System.out.println(number);
                        }
                    });
                    System.out.println("Submitted: " + i);
                    //                    doSomethingExpensive(20);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }, 0, 1, TimeUnit.SECONDS);
    Thread.sleep(10000);
    //        future.get(1,TimeUnit.SECONDS);
    System.out.println("Cancel: " + future.cancel(false));
    System.out.println("Done: " + future.isDone());
    exe.shutdown();
    //        Thread.sleep(2000);
    System.out.println("Terminate: " + exe.awaitTermination(5, TimeUnit.SECONDS));
    System.out.println("DONE");
    NonBlockingHashMapLong<String> id1 = new NonBlockingHashMapLong<String>(128);
    ConcurrentHashMap<Long, String> id2 = new ConcurrentHashMap<Long, String>(128, 0.75f, 2);

}

From source file:MyAnno.java

@What(description = "test method")
@MyAnno(str = "Testing", val = 100)
public static void myMeth() throws Exception {
    Main ob = new Main();
    Annotation annos[] = ob.getClass().getAnnotations();
    System.out.println("All annotations for Meta2:");
    for (Annotation a : annos) {
        System.out.println(a);/*www.j  a  v  a 2 s  . com*/
    }
    Method m = ob.getClass().getMethod("myMeth");
    annos = m.getAnnotations();
    for (Annotation a : annos) {
        System.out.println(a);
    }

}

From source file:MyAnno.java

@What(description = "An annotation test method")
@MyAnno(str = "Testing", val = 100)
public static void myMeth() {
    MyClass ob = new MyClass();

    try {//from www  . jav  a  2  s  .c o  m
        Annotation annos[] = ob.getClass().getAnnotations();
        for (Annotation a : annos)
            System.out.println(a);

        Method m = ob.getClass().getMethod("myMeth");
        annos = m.getAnnotations();
        for (Annotation a : annos)
            System.out.println(a);

    } catch (NoSuchMethodException exc) {
        System.out.println("Method Not Found.");
    }
}

From source file:MyAnno.java

@What(description = "An annotation test method")
@MyAnno(str = "Testing", val = 100)
public static void myMeth() {
    Meta2 ob = new Meta2();

    try {//from w  ww  .j  av  a 2  s  .c om
        Annotation annos[] = ob.getClass().getAnnotations();

        System.out.println("All annotations for Meta2:");
        for (Annotation a : annos)
            System.out.println(a);

        Method m = ob.getClass().getMethod("myMeth");
        annos = m.getAnnotations();

        System.out.println("All annotations for myMeth:");
        for (Annotation a : annos)
            System.out.println(a);

    } catch (NoSuchMethodException exc) {
        System.out.println("Method Not Found.");
    }
}

From source file:MyAnno.java

@What(description = "An annotation test method")
@MyAnno(str = "Testing", val = 100)
public static void myMeth() {
    MainClass ob = new MainClass();

    try {/*  w w  w.  j  a v  a 2  s  .c  om*/
        Annotation annos[] = ob.getClass().getAnnotations();

        System.out.println("All annotations for Meta2:");
        for (Annotation a : annos)
            System.out.println(a);

        System.out.println();

        Method m = ob.getClass().getMethod("myMeth");
        annos = m.getAnnotations();

        System.out.println("All annotations for myMeth:");
        for (Annotation a : annos)
            System.out.println(a);

    } catch (NoSuchMethodException exc) {
        System.out.println("Method Not Found.");
    }
}

From source file:com.yahoo.bard.webservice.web.filters.QueryParameterNormalizationFilter.java

/**
 * Determine whether or not a method is annotated as a JAX-RS endpoint.
 *
 * @param method  Method to check//from  w w  w  .  ja v  a2 s . c o  m
 * @return True if annotated as JAX-RS endpoint, false otherwise
 */
private static boolean isWebEndpoint(Method method) {
    return Stream.of(method.getAnnotations())
            .anyMatch(annotation -> annotation.annotationType().isAnnotationPresent(HttpMethod.class));
}

From source file:com.opensymphony.xwork2.util.AnnotationUtils.java

/**
 * For the given <code>Class</code> get a collection of the the {@link AnnotatedElement}s 
 * that match the given <code>annotation</code>s or if no <code>annotation</code>s are 
 * specified then return all of the annotated elements of the given <code>Class</code>. 
 * Includes only the method level annotations.
 * //w w w  .  j a v a 2  s.  c o  m
 * @param clazz The {@link Class} to inspect
 * @param annotation the {@link Annotation}s to find
 * @return A {@link Collection}&lt;{@link AnnotatedElement}&gt; containing all of the
 *  method {@link AnnotatedElement}s matching the specified {@link Annotation}s
 */
public static Collection<Method> getAnnotatedMethods(Class clazz, Class<? extends Annotation>... annotation) {
    Collection<Method> toReturn = new HashSet<>();

    for (Method m : clazz.getMethods()) {
        if (ArrayUtils.isNotEmpty(annotation) && isAnnotatedBy(m, annotation)) {
            toReturn.add(m);
        } else if (ArrayUtils.isEmpty(annotation) && ArrayUtils.isNotEmpty(m.getAnnotations())) {
            toReturn.add(m);
        }
    }

    return toReturn;
}

From source file:org.silverpeas.core.util.annotation.AnnotationUtil.java

/**
 * Provides a centralized way to extract annotation of a method.
 * @param method the intercepted method that will be invoked.
 * @param <A> the type of an annotation.
 * @return the map with keys of Annotation class and values of object list.
 * @throws Exception/*w w w.jav a2s  . c o m*/
 */
@SuppressWarnings("unchecked")
private static <A extends Annotation> Map<Class<A>, A> extractMethodAnnotations(Method method)
        throws Exception {

    // Initializing the results
    Map<Class<? extends Annotation>, Annotation> results = new LinkedHashMap<>();

    for (Annotation annotation : method.getAnnotations()) {
        results.put(annotation.annotationType(), annotation);
    }

    return (Map) results;
}

From source file:ch.aonyx.broker.ib.api.util.AnnotationUtils.java

private static boolean isInterfaceWithAnnotatedMethods(final Class<?> iface) {
    synchronized (ANNOTATED_INTERFACE_CACHE) {
        final Boolean flag = ANNOTATED_INTERFACE_CACHE.get(iface);
        if (flag != null) {
            return flag;
        }//from w ww  .  j av  a2 s  .co  m
        boolean found = false;
        for (final Method ifcMethod : iface.getMethods()) {
            if (ifcMethod.getAnnotations().length > 0) {
                found = true;
                break;
            }
        }
        ANNOTATED_INTERFACE_CACHE.put(iface, found);
        return found;
    }
}