Example usage for java.lang.reflect Method getAnnotation

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

Introduction

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

Prototype

public <T extends Annotation> T getAnnotation(Class<T> annotationClass) 

Source Link

Usage

From source file:MyAnnotation.java

public static void main(String[] a) throws Exception {
    Main ob = new Main();
    Class c = ob.getClass();/*from  w  w w.j  a  va  2  s . c  om*/
    Method m = c.getMethod("myMethod");
    MyAnnotation anno = m.getAnnotation(MyAnnotation.class);
    System.out.println(anno.stringValue() + " " + anno.intValue());
}

From source file:net.openhft.chronicle.wire.benchmarks.ComparisonMain.java

public static void main(String... args) throws Exception {
    Affinity.setAffinity(2);//from  w  ww .java  2s.co  m
    if (Jvm.isDebug()) {
        ComparisonMain main = new ComparisonMain();
        for (Method m : ComparisonMain.class.getMethods()) {
            main.s = null;
            main.sb.setLength(0);
            main.mhe.wrap(main.directBuffer, 0).blockLength(0);
            main.buf = null;

            if (m.getAnnotation(Benchmark.class) != null) {
                m.invoke(main);
                String s = main.s;
                if (s != null) {
                    System.out.println("Test " + m.getName() + " used " + s.length() + " chars.");
                    System.out.println(s);
                } else if (main.sb.length() > 0) {
                    System.out.println("Test " + m.getName() + " used " + main.sb.length() + " chars.");
                    System.out.println(main.sb);
                } else if (main.mhd.wrap(main.directBuffer, 0).blockLength() > 0) {
                    int len = main.mhd.wrap(main.directBuffer, 0).blockLength() + main.mhd.encodedLength();
                    System.out.println("Test " + m.getName() + " used " + len + " chars.");
                    System.out.println(new NativeBytesStore<>(main.directBuffer.addressOffset(), len)
                            .bytesForRead().toHexString());
                } else if (main.buf != null) {
                    System.out.println("Test " + m.getName() + " used " + main.buf.length + " chars.");
                    System.out.println(Bytes.wrapForRead(main.buf).toHexString());
                } else if (main.bytes.writePosition() > 0) {
                    main.bytes.readPosition(0);
                    System.out
                            .println("Test " + m.getName() + " used " + main.bytes.readRemaining() + " chars.");
                    System.out.println(main.bytes.toHexString());
                }
            }
        }
    } else {
        int time = Boolean.getBoolean("longTest") ? 30 : 2;
        System.out.println("measurementTime: " + time + " secs");
        Options opt = new OptionsBuilder().include(ComparisonMain.class.getSimpleName())
                //                    .warmupIterations(5)
                .measurementIterations(5).forks(10).mode(Mode.SampleTime)
                .measurementTime(TimeValue.seconds(time)).timeUnit(TimeUnit.NANOSECONDS).build();

        new Runner(opt).run();
    }
}

From source file:MyAnno.java

@MyAnno()
public static void myMeth() throws Exception {
    Main ob = new Main();
    Class c = ob.getClass();/*ww  w .  ja  v a2s  .  c  o m*/
    Method m = c.getMethod("myMeth");
    MyAnno anno = m.getAnnotation(MyAnno.class);
    System.out.println(anno.str() + " " + anno.val());
}

From source file:MyAnno.java

@MyAnno()
public static void myMeth() {
    Meta3 ob = new Meta3();

    try {//ww  w.  j a  v a2 s . c om
        Class c = ob.getClass();

        Method m = c.getMethod("myMeth");

        MyAnno anno = m.getAnnotation(MyAnno.class);

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

From source file:MyAnno.java

@MyAnno(str = "Annotation Example", val = 100)
public static void myMeth() {
    MainClass ob = new MainClass();

    try {/*from  w w  w.  ja  v  a2s.  c om*/
        Class c = ob.getClass();

        Method m = c.getMethod("myMeth");

        MyAnno anno = m.getAnnotation(MyAnno.class);

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

From source file:MySingle.java

@MySingle(100)
public static void myMeth() {
    Main ob = new Main();
    try {// ww  w. j ava  2s.  c  o m
        Method m = ob.getClass().getMethod("myMeth");
        MySingle anno = m.getAnnotation(MySingle.class);
        System.out.println(anno.value()); // displays 100
    } catch (NoSuchMethodException exc) {
        System.out.println("Method Not Found.");
    }
}

From source file:MyAnno.java

@MyAnno(str = "Annotation Example", val = 100)
public static void myMeth() {
    Meta ob = new Meta();

    try {/* w  ww  .j a va2  s. co m*/
        Class c = ob.getClass();

        Method m = c.getMethod("myMeth");

        MyAnno anno = m.getAnnotation(MyAnno.class);

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

From source file:MySingle.java

@MySingle(100)
public static void myMeth() {
    Single ob = new Single();

    try {//  w w  w.j a  v a  2s  .  c  om
        Method m = ob.getClass().getMethod("myMeth");

        MySingle anno = m.getAnnotation(MySingle.class);

        System.out.println(anno.value()); // displays 100

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

From source file:Main.java

public static Boolean hasAnnotation(Method method, Class<? extends Annotation> annotation) {
    return method.getAnnotation(annotation) != null;
}

From source file:MyAnno.java

@MyAnno(str = "Two Parameters", val = 19)
public static void myMeth(String str, int i) {
    Meta ob = new Meta();

    try {//  w w w. ja v  a 2 s  .co  m
        Class c = ob.getClass();

        Method m = c.getMethod("myMeth", String.class, int.class);

        MyAnno anno = m.getAnnotation(MyAnno.class);

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