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:MyAnno.java

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

    try {//from w ww .ja va 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.");
    }
}

From source file:JavaScriptUtils.java

public static void eval(Method method) throws ScriptException {
    JavaScript script = method.getAnnotation(JavaScript.class);
    eval(script);//www.j  a va2  s.co m
}

From source file:Main.java

public static <T extends Annotation> boolean hasObjectMethodWithAnnotation(final Object o,
        final Class<T> annotation) {
    final Class<?>[] interfaces = o.getClass().getInterfaces();
    for (final Class<?> anInterface : interfaces) {
        for (final Method method : anInterface.getMethods()) {
            if (method.getAnnotation(annotation) != null) {
                return true;
            }/*from   w w w.  j  av  a2 s.  c  o  m*/
        }

    }
    return false;
}

From source file:pl.bristleback.server.bristle.conf.resolver.action.ActionResolvingUtils.java

public static String resolveActionName(Method actionMethod) {
    Action actionAnnotation = actionMethod.getAnnotation(Action.class);
    if (actionAnnotation == null || StringUtils.isBlank(actionAnnotation.name())) {
        return actionMethod.getName();
    }/*from   w w  w.  j  a v  a2 s.co m*/
    validateActionNameFromAnnotationValue(actionAnnotation.name());
    return actionAnnotation.name();
}

From source file:io.lavagna.common.QueryFactory.java

private static QueryTypeAndQuery extractQueryAnnotation(Class<?> clazz, String activeDb, Method method) {
    Query q = method.getAnnotation(Query.class);
    QueriesOverride qs = method.getAnnotation(QueriesOverride.class);

    Assert.isTrue(q != null, String.format("missing @Query annotation for method %s in interface %s",
            method.getName(), clazz.getSimpleName()));
    // only one @Query annotation, thus we return the value without checking the database
    if (qs == null) {
        return new QueryTypeAndQuery(q.type(), q.value());
    }/*w  w  w  .  j a  va  2 s.  c o m*/

    for (QueryOverride query : qs.value()) {
        if (query.db().equals(activeDb)) {
            return new QueryTypeAndQuery(q.type(), query.value());
        }
    }

    return new QueryTypeAndQuery(q.type(), q.value());
}

From source file:io.devcon5.pageobjects.tx.TransactionHelper.java

/**
 * Determines the transaction name of the method. The method must be annotated with {@link Transaction} otherwise
 * the empty optional is returned. The name is derived from the value of the {@link Transaction} annotation or from
 * the mehtod name. If the declaring class denotes a transaction itself, it's name prefixes the method transaction.
 *
 * @param method/* w  w  w  .  j  a v  a 2  s. c om*/
 *         the method for which the transaction name should be determined
 *
 * @return the name of the transaction or the empty optional if the method denotes no transaction
 */
public static Optional<String> getTxName(Object object, final Method method) {

    return Optional.ofNullable(method.getAnnotation(Transaction.class))
            .map(t -> getClassTxName(object.getClass()).map(ctx -> ctx + '_').orElse("")
                    + (isEmpty(t.value()) ? method.getName() : t.value()));
}

From source file:elaborate.jaxrs.JAXUtils.java

static String descriptionOf(Method method) {
    APIDesc annotation = method.getAnnotation(APIDesc.class);
    return (annotation != null) ? annotation.value() : "";
}

From source file:elaborate.jaxrs.JAXUtils.java

static ImmutableList<String> requestContentTypesOf(Method method) {
    Consumes annotation = method.getAnnotation(Consumes.class);
    return annotation != null ? ImmutableList.copyOf(annotation.value()) : ImmutableList.<String>of();
}

From source file:elaborate.jaxrs.JAXUtils.java

static ImmutableList<String> responseContentTypesOf(Method method) {
    Produces annotation = method.getAnnotation(Produces.class);
    return annotation != null ? ImmutableList.copyOf(annotation.value()) : ImmutableList.<String>of();
}

From source file:cn.hxh.springside.test.groups.GroupsTestRunner.java

/**
 * ???./*from www  .  j a  v a 2  s .co m*/
 * ??Groupstrue.
 */
public static boolean shouldRun(Class<?> testClass) {
    Method[] methods = testClass.getMethods();
    for (Method method : methods) {
        if (method.getAnnotation(Test.class) != null && method.getAnnotation(Ignore.class) == null
                && shouldRun(method)) {
            return true;
        }
    }

    return false;
}