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.azaptree.services.eventbus.impl.EventBusServiceImpl.java

private void checkSubscribeMethodExists(final Object eventHandler) {
    for (final Method m : eventHandler.getClass().getMethods()) {
        if (m.getAnnotation(Subscribe.class) != null) {
            return;
        }/*from  ww w .  j a  v  a 2 s. c  om*/
    }

    throw new IllegalArgumentException("eventHandler has no methods annotated with @Subscribe");
}

From source file:com.threewks.thundr.search.gae.meta.SearchMetadata.java

private String determineName(Method method) {
    SearchIndex annotation = method.getAnnotation(SearchIndex.class);
    String name = annotation == null ? null : annotation.value();
    if (StringUtils.isBlank(name)) {
        name = method.getName();/* w w w . j a v  a 2s. c om*/
        if (name.startsWith("is")) {
            name = WordUtils.uncapitalize(name.substring(2));
        } else if (name.startsWith("get")) {
            name = WordUtils.uncapitalize(name.substring(3));
        }
    }
    return name;
}

From source file:com.excilys.spring.mom.annotation.MOMAnnotationProcessing.java

@Override
public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {
    final Class<?> clazz = bean.getClass();
    MOMController classAnnotation = clazz.getAnnotation(MOMController.class);

    // If the bean is annotated with @MOMController
    if (classAnnotation != null) {
        LOGGER.debug("Found @MOMController annotated class : {}", clazz);

        ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() {
            public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
                MOMMapping methodAnnotation = method.getAnnotation(MOMMapping.class);

                // If the method is annotated with @MOMMapping
                if (methodAnnotation != null) {
                    String topic = resolveProperty(methodAnnotation.topic());
                    MOMMappingConsum consum = methodAnnotation.consumes();

                    LOGGER.debug("Configuring @MOMMapping({}) method {}", consum, method);

                    try {
                        momClient.subscribe(topic, new MOMMethodHandler(method, bean, consum));
                    } catch (NotConnectedException e) {
                        LOGGER.error("Can't subscribe to topic {}", topic, e);
                    } catch (SocketException e) {
                        LOGGER.error("Can't subscribe to topic {}", topic, e);
                    }/*w ww .  j  a v  a 2 s .  c  o m*/
                }
            }
        });
    }

    return bean;
}

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

private String getSystemId(Method m) {
    if (m.isAnnotationPresent(Iri.class))
        return m.getAnnotation(Iri.class).value();
    Class<?> dclass = m.getDeclaringClass();
    String mame = m.getName();/*from   w w  w. j  a v  a2  s . co m*/
    if (dclass.isAnnotationPresent(Iri.class)) {
        String url = dclass.getAnnotation(Iri.class).value();
        if (url.indexOf('#') >= 0)
            return url.substring(0, url.indexOf('#') + 1) + mame;
        return url + "#" + mame;
    }
    String name = dclass.getSimpleName() + ".class";
    URL url = dclass.getResource(name);
    if (url != null)
        return url.toExternalForm() + "#" + mame;
    return "java:" + dclass.getName() + "#" + mame;
}

From source file:com.atlassian.plugins.studio.storage.examples.it.IntegrationTestServlet.java

private Method[] getTests(Object instance) {
    return Iterables.toArray(Iterables.<Method>filter(
            Arrays.<Method>asList(instance.getClass().getDeclaredMethods()), new Predicate<Method>() {
                @Override//from   w w w . ja  v a 2 s . com
                public boolean apply(@Nullable Method input) {
                    ToolkitTest marker = input.getAnnotation(ToolkitTest.class);
                    return marker != null && !marker.ignore();
                }
            }), Method.class);
}

From source file:com.spstudio.session.filter.SessionAOP.java

private UserSessionType getSessionType(ProceedingJoinPoint pj) {
    // ? Method  
    MethodSignature joinPointObject = (MethodSignature) pj.getSignature();
    Method method = joinPointObject.getMethod();
    boolean flag = method.isAnnotationPresent(UserSession.class);
    if (flag) {/*from  w  w w  .  j  a  v a  2  s .  c  o m*/
        UserSession annotation = method.getAnnotation(UserSession.class);
        return annotation.value();
    }
    return null;
}

From source file:org.flite.cach3.aop.L2UpdateAssignCacheAdvice.java

@AfterReturning(pointcut = "updateL2Assign()", returning = "retVal")
public Object cacheUpdateL2Assign(final JoinPoint jp, final Object retVal) throws Throwable {
    // If we've disabled the caching programmatically (or via properties file) just flow through.
    if (isCacheDisabled()) {
        LOG.debug("Caching is disabled.");
        return retVal;
    }/* w  w  w .  j av a2 s  . c om*/

    // This is injected caching.  If anything goes wrong in the caching, LOG the crap outta it,
    // but do not let it surface up past the AOP injection itself.
    try {
        final Method methodToCache = getMethodToCache(jp);
        final L2UpdateAssignCache annotation = methodToCache.getAnnotation(L2UpdateAssignCache.class);
        final AnnotationInfo info = getAnnotationInfo(annotation, methodToCache.getName());
        final String cacheKey = buildCacheKey(info.getAsString(AType.ASSIGN_KEY),
                info.getAsString(AType.NAMESPACE), info.getAsString(AType.KEY_PREFIX));

        final int dataIndex = info.getAsInteger(AType.DATA_INDEX, -2).intValue();
        final Object dataObject = dataIndex == -1 ? retVal
                : CacheBase.getIndexObject(dataIndex, jp.getArgs(), methodToCache.toString());
        final Object submission = (dataObject == null) ? new PertinentNegativeNull() : dataObject;
        boolean cacheable = true;
        if (submission instanceof CacheConditionally) {
            cacheable = ((CacheConditionally) submission).isCacheable();
        }
        if (cacheable) {
            getCache().setBulk(ImmutableMap.of(cacheKey, submission),
                    info.<Duration>getAsType(AType.WINDOW, null));
        }
    } catch (Exception ex) {
        if (LOG.isDebugEnabled()) {
            LOG.warn("Caching on " + jp.toShortString() + " aborted due to an error.", ex);
        } else {
            LOG.warn("Caching on " + jp.toShortString() + " aborted due to an error: " + ex.getMessage());
        }
    }

    return retVal;
}

From source file:com.flipkart.polyguice.dropwiz.DropConfigProvider.java

private Object getValueFromMethods(String path, Class<?> type, Object inst) throws Exception {
    Method[] methods = type.getDeclaredMethods();
    for (Method method : methods) {
        JsonProperty ann = method.getAnnotation(JsonProperty.class);
        if (ann != null) {
            String annName = ann.value();
            if (StringUtils.isBlank(annName)) {
                annName = ann.defaultValue();
            }/*from  w w w.j  a  v  a2  s  . c  o  m*/
            if (StringUtils.isBlank(annName)) {
                annName = getNameFromMethod(method);
            }
            if (StringUtils.equals(path, annName)) {
                boolean accessible = method.isAccessible();
                if (!accessible) {
                    method.setAccessible(true);
                }
                Object value = method.invoke(inst);
                if (!accessible) {
                    method.setAccessible(false);
                }
                return value;
            }
        }
    }
    return null;
}

From source file:org.jtheque.ui.utils.AbstractController.java

/**
 * Generate the cache of methods./* w  w  w.  j a  va  2s . c  o  m*/
 */
private void generateCache() {
    Method[] methods = getClass().getMethods();

    for (Method method : methods) {
        if (method.isAnnotationPresent(Action.class)) {
            Action action = method.getAnnotation(Action.class);

            methodCache.put(action.value(), method);
        }
    }
}

From source file:com.lyncode.jtwig.functions.repository.FunctionResolver.java

public FunctionResolver store(Object instance) {
    for (Method method : instance.getClass().getDeclaredMethods()) {
        JtwigFunction annotation = method.getAnnotation(JtwigFunction.class);
        if (annotation != null) {
            addFunction(instance, method, annotation.name());
            for (String name : annotation.aliases()) {
                addFunction(instance, method, name);
            }//from  ww  w.j a v a2 s  .c  o m
        }

    }
    return this;
}