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.ryantenney.metrics.spring.MetricParamAnnotationTest.java

@Test
public void meteredParameterMethod() {
    Meter meteredMethod = forMeteredMethod(metricRegistry, MetricParamClass.class, "meteredParameterMethod",
            new BigDecimal(50));
    assertNull(meteredMethod);//from   ww  w  .j  a v a2  s.c  o  m

    metricParamClass.meteredParameterMethod(new BigDecimal(50));
    meteredMethod = forMeteredMethod(metricRegistry, MetricParamClass.class, "meteredParameterMethod",
            new BigDecimal(50));
    assertNotNull(meteredMethod);
    assertEquals(1, meteredMethod.getCount());

    Method method = findMethod(MetricParamClass.class, "meteredParameterMethod");
    String metricName = forMeteredMethod(MetricParamClass.class, method, method.getAnnotation(Metered.class),
            new BigDecimal(50));
    assertEquals("metered-param.50", metricName);
}

From source file:com.longio.spring.LioBootstrap.java

private void bootEndpoints(DefaultListableBeanFactory bf, String name) {
    RootBeanDefinition bd = (RootBeanDefinition) bf.getBeanDefinition(name);
    String fbMethod = bd.getFactoryMethodName();
    String fbName = bd.getFactoryBeanName();
    Object fb = bf.getBean(fbName);

    if (!bf.containsBeanDefinition("longio.connector")) {
        GenericBeanDefinition bdd = new GenericBeanDefinition();
        bdd.setBeanClass(NettyConnector.class);
        bf.registerBeanDefinition("longio.connector", bdd);
    }//from w ww  .  j  av  a  2  s.  c  o  m

    Connector connector = bf.getBean("longio.connector", Connector.class);

    Class<?> fbCls = fb.getClass().getSuperclass();
    Method m;
    try {
        m = fbCls.getDeclaredMethod(fbMethod);
        Boots boots = m.getAnnotation(Boots.class);
        if (boots == null) {
            MethodDispatcher dispatcher = new MethodDispatcher();
            Boot b = m.getAnnotation(Boot.class);
            connector.start(b.port(), dispatcher, b.tt(), b.pt(), b.pkg());
            logger.info("connector start at port [" + b.port() + "] with tt = " + b.tt() + " and pt = " + b.pt()
                    + " for pkg = " + b.pkg());
        } else {
            for (Boot b : boots.value()) {
                MethodDispatcher dispatcher = new MethodDispatcher();
                connector.start(b.port(), dispatcher, b.tt(), b.pt(), b.pkg());
                logger.info("connector start at port [" + b.port() + "] with tt = " + b.tt() + " and pt = "
                        + b.pt() + " for pkg = " + b.pkg());
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

private void doInvalidate(final JoinPoint jp, final Object retVal) throws Throwable {
    if (isCacheDisabled()) {
        LOG.debug("Caching is disabled.");
        return;/*  w w  w .  j a v  a2 s  .  c om*/
    }

    final Method methodToCache = getMethodToCache(jp);
    List<L2InvalidateAssignCache> lAnnotations;

    //        if (methodToCache.getAnnotation(InvalidateAssignCache.class) != null) {
    lAnnotations = Arrays.asList(methodToCache.getAnnotation(L2InvalidateAssignCache.class));
    //        } else {
    //            lAnnotations = Arrays.asList(methodToCache.getAnnotation(InvalidateAssignCaches.class).value());
    //        }

    for (int i = 0; i < lAnnotations.size(); i++) {
        // 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 AnnotationInfo info = getAnnotationInfo(lAnnotations.get(i), methodToCache.getName());
            final String cacheKey = buildCacheKey(info.getAsString(AType.ASSIGN_KEY),
                    info.getAsString(AType.NAMESPACE), info.getAsString(AType.KEY_PREFIX));
            if (cacheKey == null || cacheKey.trim().length() == 0) {
                throw new InvalidParameterException("Unable to find a cache key");
            }
            getCache().invalidateBulk(Arrays.asList(cacheKey));
        } catch (Throwable 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());
            }
        }
    }
}

From source file:com.ebay.jetstream.management.HtmlResourceFormatter.java

protected void formatOperation(Method method) throws IOException {
    PrintWriter pw = getWriter();
    String text = method.getName();
    formatHRef(makePath(getPrefix(), getPath(), "?" + text), text);
    text = method.getAnnotation(ManagedOperation.class).description();
    if (!CommonUtils.isEmptyTrimmed(text)) {
        pw.print(" (" + text + ")");
    }/*  w  w  w.j  a  va 2  s.c  o m*/
    pw.println();
}

From source file:ar.com.jrules.core.service.LoadJRules.java

private void validateExcuteRuleAnnotationParameters() {
    log.debug("Found " + this.clazzWithMethodThatJRulesImplement.size() + " class with JRule implementation");

    for (Class<?> clazz : this.clazzWithMethodThatJRulesImplement) {

        for (Method method : clazz.getMethods()) {

            ExecuteRule executeRuleAnnotation = method.getAnnotation(ExecuteRule.class);

            if (executeRuleAnnotation != null) {

                if (executeRuleAnnotation.ruleClass().length == 0
                        && executeRuleAnnotation.ruleSetName().length == 0) {
                    throw new JRuleConfigurationException(
                            "Found error in ExecuteRule configuration. The method '" + method.getName()
                                    + "' in class " + clazz.getCanonicalName()
                                    + " has not properly declared annotation @ExecuteRule. Remember to declare a 'ruleClass' or 'ruleSetName'");
                }//w  ww.  j  av  a2s.c  o m
            }
        }
    }
}

From source file:com.github.wnameless.spring.routing.RoutingPathResolver.java

private List<Method> getMethodsListWithAnnotation(final Class<?> cls,
          final Class<? extends Annotation> annotationCls) {
      Method[] allMethods = cls.getDeclaredMethods();
      List<Method> annotatedMethods = new ArrayList<Method>();
      for (Method method : allMethods) {
          if (method.getAnnotation(annotationCls) != null) {
              annotatedMethods.add(method);
          }/*w  ww .j ava2  s.  c  o  m*/
      }
      return annotatedMethods;
  }

From source file:com.stevpet.sonar.plugins.dotnet.mscover.parser.XmlParserSubject.java

private void invokePathMatcherMethod(String path, String elementValue, ParserObserver observer, Method method) {
    PathMatcher annos = method.getAnnotation(PathMatcher.class);
    if (annos == null) {
        return;//from   w  ww .ja  va 2 s . com
    }
    if (path.equals(annos.path())) {
        invokeMethod(elementValue, observer, method);
    }

}

From source file:net.paslavsky.springrest.SpringAnnotationPreprocessor.java

@Override
public RestMethodMetadata parse(Class<?> clientClass, Method method) {
    RestMethodMetadata metadata = new RestMethodMetadata();

    RequestMapping classMapping = clientClass.getAnnotation(RequestMapping.class);
    RequestMapping methodMapping = method.getAnnotation(RequestMapping.class);

    metadata.setCommonPath(getPath(classMapping));
    metadata.setAdditionalPath(getPath(methodMapping));
    metadata.setHttpMethod(getHttpMethod(classMapping, methodMapping));
    metadata.setResponseClass(getResponseType(method));
    metadata.setMethodReturnType(method.getReturnType());
    metadata.setRequestHeaderParameters(getParametersWithAnnotation(method, RequestHeader.class));
    metadata.setUriVarParameters(getParametersWithAnnotation(method, PathVariable.class));
    metadata.setQueryParameters(getParametersWithAnnotation(method, RequestParam.class));
    metadata.setRequestParameter(getRequestParameter(method));

    return metadata;
}

From source file:ar.com.allium.rules.core.service.LoadAlliumRules.java

private void validateExcuteRuleAnnotationParameters() {
    log.debug("Found " + this.clazzWithMethodThatAlliumRulesImplement.size()
            + " class with AlliumRule implementation");

    for (Class<?> clazz : this.clazzWithMethodThatAlliumRulesImplement) {

        for (Method method : clazz.getMethods()) {

            ExecuteRule executeRuleAnnotation = method.getAnnotation(ExecuteRule.class);

            if (executeRuleAnnotation != null) {

                if (executeRuleAnnotation.ruleClass().length == 0
                        && executeRuleAnnotation.ruleSetName().length == 0) {
                    throw new AlliumRuleConfigurationException(
                            "Found error in ExecuteRule configuration. The method '" + method.getName()
                                    + "' in class " + clazz.getCanonicalName()
                                    + " has not properly declared annotation @ExecuteRule. Remember to declare a 'ruleClass' or 'ruleSetName'");
                }/*from ww  w  . j  a v  a2  s .c  om*/
            }
        }
    }
}

From source file:com.ryantenney.metrics.spring.MetricParamAnnotationTest.java

@Test
public void timedCollectionParameterMethod() {
    List<String> list = Arrays.asList("1", "2", "3");
    Timer timedMethod = forTimedMethod(metricRegistry, MetricParamClass.class, "timedCollectionParameterMethod",
            list);/*ww  w .j  av  a2 s . co m*/
    assertNull(timedMethod);

    metricParamClass.timedCollectionParameterMethod(list);
    timedMethod = forTimedMethod(metricRegistry, MetricParamClass.class, "timedCollectionParameterMethod",
            list);
    assertNotNull(timedMethod);
    assertEquals(1, timedMethod.getCount());

    Method method = findMethod(MetricParamClass.class, "timedCollectionParameterMethod");
    String metricName = forTimedMethod(MetricParamClass.class, method, method.getAnnotation(Timed.class), list);
    assertEquals("timed-collection-param.size.3", metricName);
}