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:com.springframework.core.annotation.AnnotationUtils.java

static boolean isInterfaceWithAnnotatedMethods(Class<?> iface) {
    Boolean flag = annotatedInterfaceCache.get(iface);
    if (flag != null) {
        return flag.booleanValue();
    }/* ww  w.  java2 s  . c  om*/
    Boolean found = Boolean.FALSE;
    for (Method ifcMethod : iface.getMethods()) {
        try {
            if (ifcMethod.getAnnotations().length > 0) {
                found = Boolean.TRUE;
                break;
            }
        } catch (Exception ex) {
            // Assuming nested Class values not resolvable within annotation attributes...
            logIntrospectionFailure(ifcMethod, ex);
        }
    }
    annotatedInterfaceCache.put(iface, found);
    return found.booleanValue();
}

From source file:de.forsthaus.webui.util.GFCBaseCtrl.java

/**
 * With this method we get the @Secured Annotation for a method.<br>
 * Captured the method call and check if it's allowed. <br>
 * sample: @Secured({"rightName"})/*from   w  ww.  j  a  v a 2  s .com*/
 * 
 * @param mtd
 */
private void isAllowed(Method mtd) {
    Annotation[] annotations = mtd.getAnnotations();
    for (Annotation annotation : annotations) {
        if (annotation instanceof Secured) {
            Secured secured = (Secured) annotation;
            for (String rightName : secured.value()) {
                if (!userWorkspace.isAllowed(rightName)) {
                    throw new SecurityException("Call of this method is not allowed! Missing right: \n\n"
                            + "needed RightName: " + rightName + "\n\n" + "Method: " + mtd);
                }
            }
            return;
        }
    }
}

From source file:org.qifu.base.interceptor.QueryParamInspectInterceptor.java

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
    Method method = ((HandlerMethod) handler).getMethod();
    Annotation[] actionMethodAnnotations = method.getAnnotations();
    log(method, actionMethodAnnotations, request);
    return true;/*from   ww  w  .j  av  a 2s .c o  m*/
}

From source file:com.sf.ddao.chain.ChainInvocationHandler.java

public void init(Class<?> iFace) {
    List<Command> classLevelList = new ArrayList<Command>();
    addChainMemebers(iFace, classLevelList);
    final Method[] methods = iFace.getMethods();
    for (Method method : methods) {
        List<Command> list = new ArrayList<Command>(classLevelList.size() + method.getAnnotations().length);
        list.addAll(classLevelList);/*ww w.  j  a  v a2  s.  c o m*/
        addChainMemebers(method, list);
        map.put(method, new MethodInvocationHandler(iFace, method, list));
    }
}

From source file:Main.java

public static void dumpMethod(final Method method) {
    final StringBuilder builder = new StringBuilder();
    builder.append("------------------------------\n");
    builder.append("MethodName: ").append(method.getName()).append("\n");
    builder.append("ParameterTypes:{");
    for (Class<?> cls : method.getParameterTypes()) {
        builder.append(cls.getName()).append(", ");
    }/*from   w w  w .  j  av  a  2  s  .c  o  m*/
    builder.append("}\n");
    builder.append("GenericParameterTypes:{");
    for (Type cls : method.getGenericParameterTypes()) {
        builder.append(cls.getClass()).append(", ");
    }
    builder.append("}\n");
    builder.append("TypeParameters:{");
    for (TypeVariable<Method> cls : method.getTypeParameters()) {
        builder.append(cls.getName()).append(", ");
    }
    builder.append("}\n");
    builder.append("DeclaredAnnotations:{");
    for (Annotation cls : method.getDeclaredAnnotations()) {
        builder.append(cls).append(", ");
    }
    builder.append("}\n");
    builder.append("Annotations:{");
    for (Annotation cls : method.getAnnotations()) {
        builder.append(cls).append(", ");
    }
    builder.append("}\n");
    builder.append("ExceptionTypes:{");
    for (Class<?> cls : method.getExceptionTypes()) {
        builder.append(cls.getName()).append(", ");
        ;
    }
    builder.append("}\n");
    builder.append("ReturnType: ").append(method.getReturnType());
    builder.append("\nGenericReturnType: ").append(method.getGenericReturnType());
    builder.append("\nDeclaringClass: ").append(method.getDeclaringClass());
    builder.append("\n");

    System.out.println(builder.toString());
}

From source file:org.apache.hadoop.metrics2.lib.MetricsSourceBuilder.java

private void add(Object source, Method method) {
    for (Annotation annotation : method.getAnnotations()) {
        if (!(annotation instanceof Metric))
            continue;
        factory.newForMethod(source, method, (Metric) annotation, registry);
        hasAtMetric = true;/*from  www  .j  ava 2  s  .  com*/
    }
}

From source file:org.eclipse.ecr.core.api.TransactionalCoreSessionWrapper.java

protected boolean needsRollback(Method method, Throwable t) {
    for (Annotation annotation : method.getAnnotations()) {
        if (annotation.annotationType() == NoRollbackOnException.class) {
            return false;
        }/*from  w  w w . jav  a  2  s.  co  m*/
    }
    return true;
}

From source file:org.apache.hadoop.hbase.util.ParamFormatHelper.java

/**
 * General constructor which allows you to specify the context, clazz and
 * formatType individually//from w ww.  j  ava 2s.co m
 * @param context the context to use when calling ParamFormatter instances
 * @param clazz the Class to reflect over to get the ParamFormat annotations
 * @param formatType which formatTypes to use (ignores others)
 */
public ParamFormatHelper(TContext context, Class<?> clazz, ParamFormat.FormatTypes formatType) {
    this.context = context;

    for (Method method : clazz.getMethods()) {
        for (Annotation ann : method.getAnnotations()) {

            if (!(ann instanceof ParamFormat))
                continue;
            ParamFormat paramFormat = (ParamFormat) ann;
            if (paramFormat.formatType() != formatType)
                continue;

            try {
                // getMethod throws if it wasn't derived from ParamFormatter<TContext>
                paramFormat.clazz().getMethod("getMap", Object[].class, clazz);

                // The generic constraint in the annotation checks this...
                @SuppressWarnings("unchecked")
                ParamFormatter<TContext> newFormatter = (ParamFormatter<TContext>) paramFormat.clazz()
                        .newInstance();

                paramFormatters.put(method, newFormatter);
            } catch (InstantiationException e) {
                String msg = "Can not create " + ((ParamFormat) ann).clazz().getName()
                        + " make sure it's public (and static if nested)";
                throw new RuntimeException(msg, e);
            } catch (NoSuchMethodException e) {
                // This exception is thrown if the class clazz was derived
                //  from a different TContext
            } catch (IllegalAccessException e) {
                String msg = "Can not create " + ((ParamFormat) ann).clazz().getName()
                        + " make sure it's public (and static if nested)";
                throw new RuntimeException(msg, e);
            }
        }
    }

}

From source file:org.motechproject.server.omod.advice.RunAsUserAdvice.java

private boolean hasAuthAnnotation(Method method) {
    for (Annotation annotation : method.getAnnotations()) {
        if (annotation instanceof RunAsUser) {
            return true;
        }/*from w w w.  j ava  2 s  . com*/
    }
    return false;
}

From source file:io.wcm.tooling.netbeans.sightly.completion.classLookup.RequestAttributeResolver.java

private Set<String> getAttributesFromClassLoader(String clazzname) {
    final Set<String> ret = new LinkedHashSet<>();
    try {/*from   www. j  av  a 2 s .c  o  m*/
        Class clazz = classPath.getClassLoader(true).loadClass(clazzname);
        for (Method method : clazz.getDeclaredMethods()) {
            for (Annotation annotation : method.getAnnotations()) {
                if (annotation.annotationType().getName().equals(REQUEST_ATTRIBUTE_CLASSNAME)) {
                    ret.add(method.getName());
                }
            }
        }
        for (Field field : clazz.getDeclaredFields()) {
            for (Annotation annotation : field.getAnnotations()) {
                if (annotation.annotationType().getName().equals(REQUEST_ATTRIBUTE_CLASSNAME)) {
                    ret.add(field.getName());
                }
            }
        }
    } catch (ClassNotFoundException cnfe) {
        LOGGER.log(Level.FINE, "Could not resolve class " + clazzname, cnfe);
    }
    return ret;
}