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:org.flite.cach3.aop.InvalidateMultiCacheAdvice.java

private void doInvalidate(final JoinPoint jp, final Object retVal) throws Throwable {
    if (isCacheDisabled()) {
        LOG.debug("Caching is disabled.");
        return;//www  .  ja  v a2 s  .  com
    }

    final MemcachedClientIF cache = getMemcachedClient();
    final Method methodToCache = getMethodToCache(jp);

    List<InvalidateMultiCache> lAnnotations;

    if (methodToCache.getAnnotation(InvalidateMultiCache.class) != null) {
        lAnnotations = Arrays.asList(methodToCache.getAnnotation(InvalidateMultiCache.class));
    } else {
        lAnnotations = Arrays.asList(methodToCache.getAnnotation(InvalidateMultiCaches.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 List<Object> keyObjects = (List<Object>) getIndexObject(info.getAsInteger(AType.KEY_INDEX),
                    retVal, jp.getArgs(), methodToCache.toString());
            final List<String> baseKeys = UpdateMultiCacheAdvice.getBaseKeys(keyObjects,
                    info.getAsString(AType.KEY_TEMPLATE), retVal, jp.getArgs(), factory, methodStore);
            for (final String base : baseKeys) {
                final String cacheKey = buildCacheKey(base, info.getAsString(AType.NAMESPACE),
                        info.getAsString(AType.KEY_PREFIX));
                cache.delete(cacheKey);
            }

            // Notify the observers that a cache interaction happened.
            final List<InvalidateMultiCacheListener> listeners = getPertinentListeners(
                    InvalidateMultiCacheListener.class, info.getAsString(AType.NAMESPACE));
            if (listeners != null && !listeners.isEmpty()) {
                for (final InvalidateMultiCacheListener listener : listeners) {
                    try {
                        listener.triggeredInvalidateMultiCache(info.getAsString(AType.NAMESPACE),
                                info.getAsString(AType.KEY_PREFIX, null), baseKeys, retVal, jp.getArgs());
                    } catch (Exception ex) {
                        LOG.warn("Problem when triggering a listener.", ex);
                    }
                }
            }
        } 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.jayway.jaxrs.hateoas.DefaultHateoasContext.java

private String[] getConsumes(Method method) {
    if (method.isAnnotationPresent(Consumes.class)) {
        return method.getAnnotation(Consumes.class).value();
    }/*from   w  w w. j a v a2  s  .  c om*/

    return DEFAULT_MEDIA_TYPE;
}

From source file:com.jayway.jaxrs.hateoas.DefaultHateoasContext.java

private String[] getProduces(Method method) {
    if (method.isAnnotationPresent(Produces.class)) {
        return method.getAnnotation(Produces.class).value();
    }/*from  w ww  . j a va 2  s.  c om*/

    return DEFAULT_MEDIA_TYPE;
}

From source file:io.ingenieux.lambada.maven.ServeMojo.java

public void loadPathHandlers() throws Exception {
    final Set<Method> methodsAnnotatedWith = extractRuntimeAnnotations(ApiGateway.class);

    getLog().info("There are " + methodsAnnotatedWith.size() + " found methods.");

    for (Method m : methodsAnnotatedWith) {
        ApiGateway a = m.getAnnotation(ApiGateway.class);
        PathHandler pathHandler = new PathHandler();

        pathHandler.setPath(a.path());//w  w w  .ja  va  2  s  .  c o  m
        pathHandler.setMethod(m);

        boolean bStatic = Modifier.isStatic(m.getModifiers());

        // TODO: ApiGateway
        getLog().info("Class: " + m.getDeclaringClass().getName());

        if (!bStatic) {
            try {
                pathHandler.setInstance(m.getDeclaringClass().newInstance());
            } catch (Exception exc) {
                throw new RuntimeException("Unable to create class " + m.getDeclaringClass(), exc);
            }
        }

        pathHandler.setMethodType(a.method());

        getLog().info("Added path handler: " + pathHandler);

        pathHandlers.add(pathHandler.build());
    }

}

From source file:candr.yoclip.option.OptionPropertiesSetterTest.java

@Test
public void getNames() throws NoSuchMethodException {
    final Method setter = TestCase.class.getDeclaredMethod("setProperty", String.class, String.class);
    final OptionProperties optionProperties = setter.getAnnotation(OptionProperties.class);
    final OptionPropertiesSetter<TestCase> test = new OptionPropertiesSetter<TestCase>(optionProperties,
            setter);/* w  ww.j a v  a2s .  co m*/
    assertThat("property name", test.getNames()[0], is("P"));
}

From source file:candr.yoclip.option.OptionPropertiesSetterTest.java

@Test
public void testGetUsage() throws NoSuchMethodException {
    final Method setter = TestCase.class.getDeclaredMethod("setProperty", String.class, String.class);
    final OptionProperties optionProperties = setter.getAnnotation(OptionProperties.class);
    final OptionPropertiesSetter<TestCase> test = new OptionPropertiesSetter<TestCase>(optionProperties,
            setter);// ww  w.j  a v a  2  s. c o m
    assertThat("usage", test.getUsage(), is("test usage"));
}

From source file:candr.yoclip.option.OptionPropertiesSetterTest.java

@Test
public void testIsProperties() throws NoSuchMethodException {
    final Method setter = TestCase.class.getDeclaredMethod("setProperty", String.class, String.class);
    final OptionProperties optionProperties = setter.getAnnotation(OptionProperties.class);
    final OptionPropertiesSetter<TestCase> test = new OptionPropertiesSetter<TestCase>(optionProperties,
            setter);// www . j a va  2 s  . c o m
    assertTrue("expected to be properties", test.isProperties());
}

From source file:candr.yoclip.option.OptionPropertiesSetterTest.java

@Test
public void testGetPropertyDescriptions() throws NoSuchMethodException {
    Method setter = TestCase.class.getDeclaredMethod("setProperty", String.class, String.class);
    OptionProperties optionProperties = setter.getAnnotation(OptionProperties.class);
    OptionPropertiesSetter<TestCase> test = new OptionPropertiesSetter<TestCase>(optionProperties, setter);

    final List<Pair<String, String>> propertyDescriptions = test.getPropertyDescriptions();
    assertThat("propertyDescriptions size", propertyDescriptions.size(), is(2));

    assertThat("description[0] rhs", propertyDescriptions.get(0).getLeft(), is("foo"));
    assertThat("description[0] lhs", propertyDescriptions.get(0).getRight(), is("foo property"));

    assertThat("description[1] rhs", propertyDescriptions.get(1).getLeft(), is("bar"));
    assertThat("description[1] lhs", propertyDescriptions.get(1).getRight(), is("bar\nproperty"));

    setter = TestCase.class.getDeclaredMethod("setPropertyNoDescription", String.class, String.class);
    optionProperties = setter.getAnnotation(OptionProperties.class);
    test = new OptionPropertiesSetter<TestCase>(optionProperties, setter);
    assertTrue("did not expect property descriptions", test.getPropertyDescriptions().isEmpty());
}

From source file:candr.yoclip.option.OptionPropertiesSetterTest.java

@Test(expected = OptionsParseException.class)
public void testSetOptionWithBadValue() throws NoSuchMethodException {
    final Method setter = TestCase.class.getDeclaredMethod("setProperty", String.class, String.class);
    final OptionProperties optionProperties = setter.getAnnotation(OptionProperties.class);
    final OptionPropertiesSetter<TestCase> test = new OptionPropertiesSetter<TestCase>(optionProperties,
            setter);/* www.j  ava 2s . c  o m*/
    test.setOption(mock(TestCase.class), "value");
}

From source file:kenh.expl.impl.BaseFunction.java

/**
 * Find the method with name <code>process</code> or <code>@Processing</code>
 *///from  ww  w.  ja va 2s  .  c o m
@Override
public Object invoke(Object... params) throws UnsupportedExpressionException {
    Method[] methods = this.getClass().getMethods();

    for (Method method : methods) {
        String name = method.getName();
        Class[] classes = method.getParameterTypes();
        Annotation a = method.getAnnotation(Processing.class);

        if ((name.equals(METHOD) || a != null) && params.length == classes.length) {

            logger.trace("Method: " + method.toGenericString());
            boolean find = true;
            Object[] objs = new Object[params.length];
            for (int i = 0; i < params.length; i++) {
                Class class1 = params[i].getClass();
                Class class2 = classes[i];

                if (class2.isAssignableFrom(class1) || class2 == Object.class) {
                    objs[i] = params[i];

                } else if (class1 == String.class) {
                    try {
                        Object obj = Environment.convert((String) params[i], class2);
                        if (obj == null) {
                            logger.trace("Failure(Convert failure[" + (i + 1) + "-" + class1 + "," + class2
                                    + "]): " + method.toGenericString());
                            find = false;
                            break;
                        } else {
                            objs[i] = obj;
                        }
                    } catch (Exception e) {
                        logger.trace("Failure(Convert exception[" + (i + 1) + "-" + e.getMessage() + "]): "
                                + method.toGenericString());
                        find = false;
                        break;
                        //UnsupportedExpressionException ex = new UnsupportedExpressionException(e);
                        //throw ex;
                    }
                } else {
                    logger.trace("Failure(Class unmatched[" + (i + 1) + "]): " + method.toGenericString());
                    find = false;
                    break;
                }
            }
            if (find) {
                try {
                    return method.invoke(this, objs);
                } catch (Exception e) {
                    if (e instanceof UnsupportedExpressionException)
                        throw (UnsupportedExpressionException) e;
                    else
                        throw new UnsupportedExpressionException(e);
                }
            }
        }
    }

    String paramStr = "";
    for (Object param : params) {
        paramStr += param.getClass().getCanonicalName() + ", ";
    }
    paramStr = StringUtils.defaultIfBlank(StringUtils.chop(StringUtils.trimToEmpty(paramStr)), "<NO PARAM>");

    UnsupportedExpressionException e = new UnsupportedExpressionException(
            "Can't find the method to process.[" + paramStr + "]");
    throw e;
}