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:eu.tripledframework.eventstore.infrastructure.ReflectionObjectConstructor.java

private Method getEventHandlerMethods(DomainEvent event) {
    Set<Method> methods = Arrays.stream(targetClass.getMethods())
            .filter(constructor -> constructor.isAnnotationPresent(ConstructionHandler.class))
            .collect(Collectors.toSet());
    for (Method method : methods) {
        ConstructionHandler annotation = method.getAnnotation(ConstructionHandler.class);
        if (annotation.value().equals(event.getClass())) {
            return method;
        }//  w  w w  .  j  a  v  a  2  s.  co  m
    }
    return null;
}

From source file:com.kixeye.chassis.bootstrap.AppMetadata.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private Method findLifecycleMethod(final Class lifecycleAnnotationClass) {
    Set<Method> methods = ReflectionUtils.getMethods(declaringClass, new Predicate<Method>() {
        @Override//from w w w  . ja  va 2s  . c o  m
        public boolean apply(Method input) {
            return input != null && input.getAnnotation(lifecycleAnnotationClass) != null;
        }
    });
    if (methods.isEmpty()) {
        return null;
    }
    if (methods.size() > 1) {
        throw new BootstrapException("Found multiple " + lifecycleAnnotationClass.getSimpleName()
                + " methods in class " + declaringClass.getSimpleName() + ". Only 1 is allowed.");
    }
    return methods.iterator().next();
}

From source file:net.firejack.platform.core.validation.NotBlankProcessor.java

@Override
public List<Constraint> generate(Method readMethod, String property, Map<String, String> params) {
    List<Constraint> constraints = null;
    Annotation annotation = readMethod.getAnnotation(NotBlank.class);
    if (annotation != null) {
        NotBlank notBlank = (NotBlank) annotation;
        boolean generateConstraint;
        if (params != null && StringUtils.isNotBlank(params.get("id"))) {
            generateConstraint = ArrayUtils.contains(notBlank.modes(), ValidationMode.UPDATE);
        } else {/*w  ww.j a v a  2  s .  c  o m*/
            generateConstraint = ArrayUtils.contains(notBlank.modes(), ValidationMode.CREATE);
        }
        if (generateConstraint) {
            Constraint constraint = new Constraint(notBlank.annotationType().getSimpleName());
            String errorMessage = MessageResolver.messageFormatting(notBlank.msgKey(), Locale.ENGLISH,
                    property); //TODO need to set real locale
            constraint.setErrorMessage(errorMessage);
            constraints = new ArrayList<Constraint>();
            constraints.add(constraint);
        }
    }
    return constraints;
}

From source file:com.billing.ng.plugin.Parameters.java

/**
 * Returns the field name of javabeans properties annotated with the
 * {@link Parameter} annotation./*from  w  w w  .  j  a v a  2s . c  o  m*/
 *
 * @param type plugin class with parameter annotations
 * @return map of collected parameter field names and the associated annotation
 */
public Map<String, Parameter> getAnnotatedFields(Class<T> type) {
    Map<String, Parameter> fields = new HashMap<String, Parameter>();

    // collect public member methods of the class, including those defined on the interface
    // or those inherited from a super class or super interface.
    for (Method method : type.getMethods()) {
        Parameter annotation = method.getAnnotation(Parameter.class);
        if (annotation != null) {
            if (method.getName().startsWith("get") || method.getName().startsWith("set")) {
                fields.put(ClassUtils.getFieldName(method.getName()), annotation);
            }
        }
    }

    // collection all field annotations, including private fields that
    // we can to access via a public accessor method
    Class klass = type;
    while (klass != null) {
        for (Field field : klass.getDeclaredFields()) {
            Parameter annotation = field.getAnnotation(Parameter.class);
            if (annotation != null) {
                fields.put(field.getName(), annotation);
            }
        }

        // try the super class
        klass = klass.getSuperclass();
    }

    return fields;
}

From source file:com.openmeap.web.form.ParameterMapBuilder.java

private Method methodForFormName(Class<?> clazz, String formName) {

    for (Method method : clazz.getMethods()) {
        Parameter param = method.getAnnotation(Parameter.class);
        if (param != null) {
            if (formNameForMethod(param, method).equals(formName)) {
                return method;
            }/*  w  w  w  .  jav  a 2s .c  o  m*/
        }
    }
    return null;
}

From source file:net.firejack.platform.core.validation.MatchProcessor.java

@Override
public List<Constraint> generate(Method readMethod, String property, Map<String, String> params) {
    List<Constraint> constraints = null;
    Match match = readMethod.getAnnotation(Match.class);
    if (match != null) {
        Constraint constraint = new Constraint(match.annotationType().getSimpleName());
        String errorMessage = MessageResolver.messageFormatting(match.msgKey(), Locale.ENGLISH, property,
                match.example()); //TODO need to set real locale
        constraint.setErrorMessage(errorMessage);
        List<RuleParameter> ruleParameters = new ArrayList<RuleParameter>();
        RuleParameter expressionParam = new RuleParameter("expression", match.expression());
        ruleParameters.add(expressionParam);
        constraint.setParams(ruleParameters);
        constraints = new ArrayList<Constraint>();
        constraints.add(constraint);// w ww .  j a  v a  2s.  c o  m
    }
    return constraints;
}

From source file:org.callimachusproject.rewrite.RedirectAdviceFactory.java

private String[] getCommands(Method method) {
    if (method.isAnnotationPresent(canonical.class))
        return method.getAnnotation(canonical.class).value();
    if (method.isAnnotationPresent(alternate.class))
        return method.getAnnotation(alternate.class).value();
    if (method.isAnnotationPresent(describedby.class))
        return method.getAnnotation(describedby.class).value();
    if (method.isAnnotationPresent(resides.class))
        return method.getAnnotation(resides.class).value();
    if (method.isAnnotationPresent(moved.class))
        return method.getAnnotation(moved.class).value();
    return null;//from   w  ww .j av  a2  s . c  o  m
}

From source file:com.trigonic.utils.spring.cmdline.CommandLineMetaData.java

private void populateOptionMethods(Class<?> beanClass) {
    Class<?> superClass = beanClass.getSuperclass();
    if (!superClass.equals(Object.class)) {
        populateOptionMethods(superClass);
    }/*from  w w  w  .  j  a v a  2  s .  c om*/
    for (Method method : beanClass.getDeclaredMethods()) {
        Option option = method.getAnnotation(Option.class);
        if (option != null) {
            options.put(option, new OptionPropertyHandler(option,
                    getPropertyForMethod("@Option", option, method), beanClass));
        }
    }
}

From source file:net.java.javabuild.ExecuteMojo.java

private void processClass(String className) throws MalformedURLException, ClassNotFoundException,
        InstantiationException, IllegalAccessException, InvocationTargetException, IOException {
    Class<?> theClass = classLoader.loadClass(className);
    if (theClass.isAnnotationPresent(Builder.class)) {
        Object instance = theClass.newInstance();
        Method[] methods = theClass.getDeclaredMethods();
        for (int j = 0; j < methods.length; j++) {
            Method method = methods[j];
            Execute execute = method.getAnnotation(Execute.class);
            if (execute != null) {
                buildPlan.addMethodExecution(execute.phase(), instance, method);
            }/*  w  w w  .  j  a  va2s.  c om*/
        }
    }
}

From source file:com.trigonic.utils.spring.cmdline.CommandLineMetaData.java

private void populateOperandMethods(Class<?> beanClass) {
    Class<?> superClass = beanClass.getSuperclass();
    if (!superClass.equals(Object.class)) {
        populateOperandMethods(superClass);
    }// ww  w  .j a  v a 2s  .c om
    for (Method method : beanClass.getDeclaredMethods()) {
        Operand operand = method.getAnnotation(Operand.class);
        if (operand != null) {
            operands.put(operand, new OperandPropertyHandler(operand,
                    getPropertyForMethod("@Operand", operand, method), beanClass));
        }
    }
}