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:com.netflix.hystrix.contrib.javanica.cache.CacheInvocationContextFactory.java

/**
 * Create {@link CacheInvocationContext} parametrized with {@link CacheRemove} annotation.
 *
 * @param metaHolder the meta holder, see {@link com.netflix.hystrix.contrib.javanica.command.MetaHolder}
 * @return initialized and configured {@link CacheInvocationContext}
 */// ww  w.j av a 2s.co  m
public static CacheInvocationContext<CacheRemove> createCacheRemoveInvocationContext(MetaHolder metaHolder) {
    Method method = metaHolder.getMethod();
    if (method.isAnnotationPresent(CacheRemove.class)) {
        CacheRemove cacheRemove = method.getAnnotation(CacheRemove.class);
        MethodExecutionAction cacheKeyMethod = createCacheKeyAction(cacheRemove.cacheKeyMethod(), metaHolder);
        return new CacheInvocationContext<CacheRemove>(cacheRemove, cacheKeyMethod, metaHolder.getObj(), method,
                metaHolder.getArgs());
    }
    return null;
}

From source file:com.liferay.apio.architect.internal.annotation.representor.processor.TypeProcessor.java

private static void _processMethod(Builder builder, Method method) {
    LinkTo linkTo = method.getAnnotation(LinkTo.class);

    if (linkTo != null) {
        builder.addLinkToFieldData(new FieldData<>(method, linkTo));

        return;//from ww w  .  jav a2s  .  c  o  m
    }

    RelativeURL relativeURL = method.getAnnotation(RelativeURL.class);

    if (relativeURL != null) {
        FieldData<RelativeURL> relativeURLFieldData = new FieldData<>(method, relativeURL);

        builder.addRelativeURLFieldData(relativeURLFieldData);

        return;
    }

    BidirectionalModel bidirectionalModel = method.getAnnotation(BidirectionalModel.class);

    if (bidirectionalModel != null) {
        FieldData<BidirectionalModel> bidirectionalFieldData = new FieldData<>(method, bidirectionalModel);

        builder.addBidirectionalFieldData(bidirectionalFieldData);

        return;
    }

    Class<?> returnTypeClass = method.getReturnType();

    if (returnTypeClass == Optional.class) {
        _addOptionalType(builder, method);
    } else if (returnTypeClass == List.class) {
        _addListType(builder, method, (Class<?>) _getGenericType(method));
    } else {
        _addFields(builder, method, returnTypeClass);
    }
}

From source file:org.openmrs.module.webservices.rest.util.ReflectionUtil.java

/**
 * Find getter method in handler class or any of its superclasses
 * //from  ww  w .ja v  a2 s .  c o  m
 * @param handler
 * @param propName
 * @return
 */
public static <T> Method findPropertyGetterMethod(DelegatingPropertyAccessor<? extends T> handler,
        String propName) {
    String key = handler.getClass().getName().concat(propName);
    Method result = getterMethodCache.get(key);
    if (result != null) {
        return result == nullMethod ? null : result;
    }

    Class<?> clazz = handler.getClass();
    while (clazz != Object.class && result == null) {
        for (Method method : clazz.getMethods()) {
            PropertyGetter ann = method.getAnnotation(PropertyGetter.class);
            if (ann != null && ann.value().equals(propName)) {
                result = method;
                break;
            }
        }
        clazz = clazz.getSuperclass();
    }

    getterMethodCache.put(key, result == null ? nullMethod : result);

    return result;
}

From source file:org.openmrs.module.webservices.rest.util.ReflectionUtil.java

/**
 * Find setter method in handler class or any of its superclasses
 * //from  w  ww . j av a2  s .  c  om
 * @param handler
 * @param propName
 * @return
 */
public static <T> Method findPropertySetterMethod(DelegatingPropertyAccessor<? extends T> handler,
        String propName) {
    String key = handler.getClass().getName().concat(propName);
    Method result = setterMethodCache.get(key);
    if (result != null) {
        return result == nullMethod ? null : result;
    }

    Class<?> clazz = handler.getClass();
    while (clazz != Object.class && result == null) {
        for (Method method : clazz.getMethods()) {
            PropertySetter ann = method.getAnnotation(PropertySetter.class);
            if (ann != null && ann.value().equals(propName)) {
                result = method;
                break;
            }
        }
        clazz = clazz.getSuperclass();
    }

    setterMethodCache.put(key, result == null ? nullMethod : result);

    return result;
}

From source file:com.manydesigns.portofino.buttons.ButtonsLogic.java

public static Button getButtonForMethod(Method method, String list) {
    Button button = method.getAnnotation(Button.class);
    if (button != null && list.equals(button.list())) {
        return button;
    } else {//from ww w . ja  va2 s .c om
        Buttons buttons = method.getAnnotation(Buttons.class);
        if (buttons != null) {
            for (Button b : buttons.value()) {
                if (list.equals(b.list())) {
                    return b;
                }
            }
        }
    }
    return null;
}

From source file:org.kantega.dogmaticmvc.web.DogmaticMVCHandler.java

public static Class getTestClass(Method method) {
    final TestWith testWith = method.getAnnotation(TestWith.class);
    return testWith != null ? testWith.value() : method.getDeclaringClass();
}

From source file:org.openmrs.module.webservices.rest.web.DelegatingCrudResourceTest.java

/**
 * Convenience method that checks of the specified resource class has a method for setting the
 * given property/*www .  ja v  a  2 s .co  m*/
 * 
 * @param propName
 * @param resource
 * @return
 */
@SuppressWarnings("rawtypes")
private static boolean hasSetterMethod(String propName, Class resourceClass) {
    for (Method candidate : resourceClass.getMethods()) {
        PropertySetter ann = candidate.getAnnotation(PropertySetter.class);
        if (ann != null && ann.value().equals(propName)) {
            return true;
        }
    }

    return false;
}

From source file:com.sun.socialsite.business.impl.JPAListenerManagerImpl.java

/**
 * Calls any appropriately-annotated methods in listeners which
 * have registered to receive lifecycle events for a class to
 * which the specified entity belongs./*from  w  ww  .j  ava2 s .c o  m*/
 * @param entity the entity experiencing a lifecycle event.
 * @param annotationClass the annotation class corresponding to the event.
 */
private static void notifyListeners(Object entity, Class<? extends Annotation> annotationClass) {

    //log.trace(String.format("notifyListeners(%s, %s)", entity, annotationClass.getCanonicalName()));

    Collection<Object> listeners = getListeners(entity);
    for (Object listener : listeners) {
        Method[] methods = listener.getClass().getMethods();
        for (int i = 0; i < methods.length; i++) {
            Method method = methods[i];
            if (method.getAnnotation(annotationClass) != null) {
                try {
                    log.debug(String.format("calling %s.%s(%s)", listener, method.getName(), entity));
                    // Handle cases where the listener is a nested class
                    if (Modifier.isPublic(method.getModifiers()))
                        method.setAccessible(true);
                    method.invoke(listener, entity);
                } catch (Throwable t) {
                    // TODO: Catch and Handle individual Exception types
                    log.error("Exception", t);
                }
            }
        }
    }
}

From source file:com.mmj.app.common.notify.NotifyUtils.java

private static void parserAnnotation(Result result, Object obj, Method method,
        Map<EventType, Set<MethodDescriptor>> container) {
    EventConfig eventConfig = method.getAnnotation(EventConfig.class);
    EventType[] events = eventConfig.events();
    if (events == null || events.length == 0) {
        logger.warn("No eventConfig FOUND in ?" + method.getName() + "");
        return;/*from   w ww. j ava 2  s.co m*/
    }
    if (logger.isDebugEnabled()) {
        logger.debug("found ?" + method.getName() + "!");
    }
    for (int i = 0, j = events.length; i < j; i++) {
        Set<MethodDescriptor> methodSet = container.get(events[i]);
        if (methodSet == null) {
            methodSet = new HashSet<MethodDescriptor>();
            container.put(events[i], methodSet);
        }
        MethodDescriptor md = new MethodDescriptor(obj, method);
        methodSet.add(md);
        result.appendMessage(md.toString());
    }
}

From source file:com.zb.app.common.notify.NotifyUtils.java

private static void parserAnnotation(Result result, Object obj, Method method,
        Map<EventType, Set<MethodDescriptor>> container) {
    EventConfig eventConfig = method.getAnnotation(EventConfig.class);
    EventType[] events = eventConfig.events();
    if (events == null || events.length == 0) {
        logger.warn("No eventConfig FOUND in ?" + method.getName() + "");
        return;/*from  ww w . jav a2  s  . com*/
    }
    if (logger.isDebugEnabled()) {
        logger.debug("found ?" + method.getName() + "!");
    }
    for (int i = 0, j = events.length; i < j; i++) {
        Set<MethodDescriptor> methodSet = container.get(events[i]);
        if (methodSet == null) {
            methodSet = new ConcurrentHashSet<MethodDescriptor>();
            container.put(events[i], methodSet);
        }
        MethodDescriptor md = new MethodDescriptor(obj, method);
        methodSet.add(md);
        result.appendMessage(md.toString());
    }
}