Example usage for java.lang.reflect Method isAnnotationPresent

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

Introduction

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

Prototype

@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) 

Source Link

Usage

From source file:com.flowpowered.cerealization.config.annotated.AnnotatedObjectConfiguration.java

private void load(ConfigurationNodeSource source, Object object, String[] path, Set<Member> members)
        throws ConfigurationException {
    final Set<Method> methods = new HashSet<Method>();
    for (Member member : members) {
        if (member instanceof Method) {
            final Method method = (Method) member;
            if (method.isAnnotationPresent(Load.class)) {
                methods.add(method);/*w  w  w  .  j  av a 2  s  .  co m*/
            }
            continue;
        }
        final Field field = (Field) member;
        field.setAccessible(true);
        String[] fieldPath = field.getAnnotation(Setting.class).value();
        if (fieldPath.length == 0) {
            fieldPath = new String[] { field.getName() };
        }
        final ConfigurationNode fieldNode = source.getNode(ArrayUtils.addAll(path, fieldPath));
        final Object value = fieldNode.getTypedValue(field.getGenericType());
        try {
            if (value != null) {
                field.set(object, value);
            } else {
                fieldNode.setValue(field.getGenericType(), field.get(object));
            }
        } catch (IllegalAccessException ex) {
            throw new ConfigurationException(ex);
        }
    }
    invokeMethods(methods, object, source.getNode(path));
}

From source file:com.alta189.beaker.CommonEventManager.java

/**
 * Registers all the {@link EventHandler}s contained in the
 * Listener//from  w w w.  j a  va  2 s.  co m
 *
 * @param listener registration, not null
 * @param owner owner of the listener, not null
 * @throws com.alta189.beaker.exceptions.EventRegistrationException
 */
@Override
public void registerListener(Listener listener, Named owner) throws EventRegistrationException {
    try {
        Validate.notNull(listener, "Listener cannot be null");
        Validate.notNull(owner, "Owner cannot be null");
        Validate.notNull(owner.getName(), "Owner's name cannot be null");
        Validate.notEmpty(owner.getName(), "Owner's name cannot be empty");

        for (Method method : listener.getClass().getDeclaredMethods()) {
            if (!method.isAnnotationPresent(EventHandler.class)) {
                continue;
            }

            EventHandler handler = method.getAnnotation(EventHandler.class);
            Validate.notNull(handler.ignoreCancelled(), "ignoredCancelled cannot be null");
            Validate.notNull(handler.priority(), "Priority cannot be null");

            if (!Modifier.isPublic(method.getModifiers())) {
                throw new EventRegistrationException("Method has to be public");
            }

            if (Modifier.isStatic(method.getModifiers())) {
                throw new EventRegistrationException("Method cannot be static");
            }

            if (method.getParameterTypes().length != 1) {
                throw new EventRegistrationException("Method cannot have more than one parameter");
            }

            if (!Event.class.isAssignableFrom(method.getParameterTypes()[0])) {
                throw new EventRegistrationException("Method's parameter type has to extend class");
            }

            EventExecutor executor = new AnnotatedEventExecutor(listener, method);
            HandlerRegistration registration = new HandlerRegistration(executor, handler.priority(),
                    handler.ignoreCancelled(), owner);

            List<HandlerRegistration> list = getRegistrationList(
                    (Class<? extends Event>) method.getParameterTypes()[0], true);
            list.add(registration);
            sortList(list);
        }
    } catch (EventRegistrationException e) {
        throw e;
    } catch (Exception e) {
        throw new EventRegistrationException(e);
    }
}

From source file:com.netflix.hystrix.contrib.javanica.command.AbstractHystrixCommandFactory.java

CommandAction createFallbackAction(MetaHolder metaHolder,
        Collection<HystrixCollapser.CollapsedRequest<Object, Object>> collapsedRequests) {
    String fallbackMethodName = metaHolder.getHystrixCommand().fallbackMethod();
    CommandAction fallbackAction = null;
    if (StringUtils.isNotEmpty(fallbackMethodName)) {
        try {//from w  w w . ja  v  a 2s.co m
            Method fallbackMethod = metaHolder.getObj().getClass().getDeclaredMethod(fallbackMethodName,
                    metaHolder.getParameterTypes());
            if (fallbackMethod.isAnnotationPresent(HystrixCommand.class)) {
                fallbackMethod.setAccessible(true);
                MetaHolder fmMetaHolder = MetaHolder.builder().obj(metaHolder.getObj()).method(fallbackMethod)
                        .args(metaHolder.getArgs()).defaultCollapserKey(metaHolder.getDefaultCollapserKey())
                        .defaultCommandKey(fallbackMethod.getName())
                        .defaultGroupKey(metaHolder.getDefaultGroupKey())
                        .hystrixCollapser(metaHolder.getHystrixCollapser())
                        .hystrixCommand(fallbackMethod.getAnnotation(HystrixCommand.class)).build();
                fallbackAction = new LazyCommandExecutionAction(GenericHystrixCommandFactory.getInstance(),
                        fmMetaHolder, collapsedRequests);
            } else {
                fallbackAction = new MethodExecutionAction(metaHolder.getObj(), fallbackMethod,
                        metaHolder.getArgs());
            }
        } catch (NoSuchMethodException e) {
            throw Throwables.propagate(e);
        }
    }
    return fallbackAction;
}

From source file:org.androidtransfuse.processor.ManifestManager.java

private <T extends Annotation> T findAnnotation(Class<T> annotationClass, Method... methods) {
    T annotation = null;//from  www .  j a  v a 2s .  c  o  m
    if (methods != null) {
        for (Method method : methods) {
            if (annotation == null && method != null && method.isAnnotationPresent(annotationClass)) {
                annotation = method.getAnnotation(annotationClass);
            }
        }
    }
    return annotation;
}

From source file:com.katsu.jpa.dao.JpaDao.java

private boolean isOneToManyOrManyToManyMethod(Method m) {
    return (m.isAnnotationPresent(javax.persistence.OneToMany.class)
            || m.isAnnotationPresent(javax.persistence.ManyToMany.class))
            && (m.getName().startsWith(GET) || m.getName().startsWith(IS)) && m.getParameterTypes().length == 0;
}

From source file:org.spout.api.util.config.annotated.AnnotatedObjectConfiguration.java

@Override
public void save(ConfigurationNodeSource source) throws ConfigurationException {
    for (Entry<Object, Set<Member>> entry : objectMembers.entrySet()) {
        final Object object = entry.getKey();
        final String[] objectPath = objectPaths.get(object);
        final Set<Method> methods = new HashSet<Method>();
        for (Member member : entry.getValue()) {
            if (member instanceof Method) {
                final Method method = (Method) member;
                if (method.isAnnotationPresent(Save.class)) {
                    methods.add(method);
                }//from   w  w w.  j  a va 2s .c  o  m
                continue;
            }
            final Field field = (Field) member;
            field.setAccessible(true);
            String[] fieldPath = field.getAnnotation(Setting.class).value();
            if (fieldPath.length == 0) {
                fieldPath = new String[] { field.getName() };
            }
            final ConfigurationNode fieldNode = source.getNode(ArrayUtils.addAll(objectPath, fieldPath));
            try {
                fieldNode.setValue(field.getGenericType(), field.get(object));
            } catch (IllegalAccessException ex) {
                throw new ConfigurationException(ex);
            }
        }
        invokeMethods(methods, object, source.getNode(objectPath));
    }
}

From source file:com.emc.ecs.sync.config.ConfigWrapper.java

public ConfigWrapper(Class<C> targetClass) {
    try {/*from   ww w  .j a  va  2  s  . c  o m*/
        this.targetClass = targetClass;
        if (targetClass.isAnnotationPresent(StorageConfig.class))
            this.uriPrefix = targetClass.getAnnotation(StorageConfig.class).uriPrefix();
        if (targetClass.isAnnotationPresent(FilterConfig.class))
            this.cliName = targetClass.getAnnotation(FilterConfig.class).cliName();
        if (targetClass.isAnnotationPresent(Label.class))
            this.label = targetClass.getAnnotation(Label.class).value();
        if (targetClass.isAnnotationPresent(Documentation.class))
            this.documentation = targetClass.getAnnotation(Documentation.class).value();
        BeanInfo beanInfo = Introspector.getBeanInfo(targetClass);
        for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {
            if (descriptor.getReadMethod().isAnnotationPresent(Option.class)) {
                propertyMap.put(descriptor.getName(), new ConfigPropertyWrapper(descriptor));
            }
        }
        for (MethodDescriptor descriptor : beanInfo.getMethodDescriptors()) {
            Method method = descriptor.getMethod();
            if (method.isAnnotationPresent(UriParser.class)) {
                if (method.getReturnType().equals(Void.TYPE) && method.getParameterTypes().length == 1
                        && method.getParameterTypes()[0].equals(String.class)) {
                    uriParser = method;
                } else {
                    log.warn("illegal signature for @UriParser method {}.{}", targetClass.getSimpleName(),
                            method.getName());
                }
            } else if (method.isAnnotationPresent(UriGenerator.class)) {
                if (method.getReturnType().equals(String.class) && method.getParameterTypes().length == 0) {
                    uriGenerator = method;
                } else {
                    log.warn("illegal signature for @UriGenerator method {}.{}", targetClass.getSimpleName(),
                            method.getName());
                }
            }
        }
        if (propertyMap.isEmpty())
            log.info("no @Option annotations found in {}", targetClass.getSimpleName());
    } catch (IntrospectionException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.spout.api.util.config.annotated.AnnotatedObjectConfiguration.java

@Override
public void load(ConfigurationNodeSource source) throws ConfigurationException {
    for (Entry<Object, Set<Member>> entry : objectMembers.entrySet()) {
        final Object object = entry.getKey();
        final String[] objectPath = objectPaths.get(object);
        final Set<Method> methods = new HashSet<Method>();
        for (Member member : entry.getValue()) {
            if (member instanceof Method) {
                final Method method = (Method) member;
                if (method.isAnnotationPresent(Load.class)) {
                    methods.add(method);
                }/*from  w w  w  . j  a va 2 s . c  o  m*/
                continue;
            }
            final Field field = (Field) member;
            field.setAccessible(true);
            String[] fieldPath = field.getAnnotation(Setting.class).value();
            if (fieldPath.length == 0) {
                fieldPath = new String[] { field.getName() };
            }
            final ConfigurationNode fieldNode = source.getNode(ArrayUtils.addAll(objectPath, fieldPath));
            final Object value = fieldNode.getTypedValue(field.getGenericType());
            try {
                if (value != null) {
                    field.set(object, value);
                } else {
                    fieldNode.setValue(field.getGenericType(), field.get(object));
                }
            } catch (IllegalAccessException ex) {
                throw new ConfigurationException(ex);
            }
        }
        invokeMethods(methods, object, source.getNode(objectPath));
    }
}

From source file:org.wso2.carbon.registry.reporting.services.ReportingAdminService.java

public String[] getAttributeNames(String className) throws Exception {
    List<String> output = new LinkedList<String>();
    Method[] declaredMethods = RegistryUtils.loadClass(className).getDeclaredMethods();
    for (Method method : declaredMethods) {
        if (method.isAnnotationPresent(Property.class)) {
            String name = method.getName();
            if (name.startsWith("set")) {
                name = name.substring("set".length());
            }/*from  ww w .  j ava 2  s  . c  o  m*/
            output.add(name.substring(0, 1).toLowerCase() + name.substring(1));
        }
    }
    return output.toArray(new String[output.size()]);
}

From source file:org.wso2.carbon.registry.reporting.services.ReportingAdminService.java

public String[] getMandatoryAttributeNames(String className) throws Exception {
    List<String> output = new LinkedList<String>();
    Method[] declaredMethods = RegistryUtils.loadClass(className).getDeclaredMethods();
    for (Method method : declaredMethods) {
        if (method.isAnnotationPresent(Property.class) && method.getAnnotation(Property.class).mandatory()) {
            String name = method.getName();
            if (name.startsWith("set")) {
                name = name.substring("set".length());
            }/*  w  ww  .j a  va2 s  .co  m*/
            output.add(name.substring(0, 1).toLowerCase() + name.substring(1));
        }
    }
    return output.toArray(new String[output.size()]);
}