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.cuubez.visualizer.resource.ResourceMetaDataScanner.java

public static boolean isSubResource(Method method) {

    if (method.getAnnotation(GET.class) != null || method.getAnnotation(POST.class) != null
            || method.getAnnotation(PUT.class) != null || method.getAnnotation(DELETE.class) != null
            || method.getAnnotation(HEAD.class) != null || method.getAnnotation(OPTIONS.class) != null) {

        return true;
    }/*from w w  w . ja v a2s . co m*/

    return false;

}

From source file:com.sf.ddao.orm.RSMapperFactoryRegistry.java

public static RSMapper createReusable(Method method) {
    final UseRSMapper useRSMapper = method.getAnnotation(UseRSMapper.class);
    if (useRSMapper != null) {
        try {/*from   ww w  .j  a v a2  s  .  c  o m*/
            return useRSMapper.value().newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    Class<?> returnClass = method.getReturnType();
    if (returnClass.isArray()) {
        Class itemType = returnClass.getComponentType();
        RowMapperFactory rowMapperFactory = getRowMapperFactory(itemType);
        return new ArrayRSMapper(rowMapperFactory, itemType);
    }
    Type returnType = method.getGenericReturnType();
    if (Collection.class.isAssignableFrom(returnClass)) {
        Type[] actualTypeArguments = ((ParameterizedType) returnType).getActualTypeArguments();
        Type itemType = actualTypeArguments[0];
        return getCollectionORMapper(itemType);
    }
    if (Map.class.isAssignableFrom(returnClass)) {
        Type[] actualTypeArguments = ((ParameterizedType) returnType).getActualTypeArguments();
        Type keyType = actualTypeArguments[0];
        Type valueType = actualTypeArguments[1];
        RowMapperFactory keyMapperFactory = getScalarMapper(keyType, 1, true);
        RowMapperFactory valueMapperFactory = getRowMapperFactory(valueType, 2);
        return new MapRSMapper(keyMapperFactory, valueMapperFactory);
    }
    return new SingleRowRSMapper(getRowMapperFactory(returnType));
}

From source file:com.dianping.avatar.cache.util.CacheAnnotationUtils.java

/**
 * Generate {@link CacheKey} instance by {@link Method} and arguments.The
 * method should be annotated by {@link Cache}
 *//*from ww w.  j  a v  a2s  .c  om*/
public static CacheKey getCacheKey(final Method method, final Object[] args) {

    if (method == null || args == null) {
        throw new IllegalArgumentException("method/argus must not be null.");
    }

    Cache cache = method.getAnnotation(Cache.class);

    if (cache == null) {
        return null;
    }

    Annotation[][] annos = method.getParameterAnnotations();

    final List<OrderedField> orderedParams = new ArrayList<OrderedField>();

    for (int i = 0; i < annos.length; i++) {
        Annotation[] paramAnnos = annos[i];
        for (int j = 0; j < paramAnnos.length; j++) {
            Annotation paramAnno = paramAnnos[j];
            if (paramAnno instanceof CacheParam) {
                CacheParam cacheParam = (CacheParam) paramAnno;
                orderedParams.add(new OrderedField(args[i], i, cacheParam.order()));
                break;
            }
        }
    }

    Collections.sort(orderedParams);

    Object[] values = new Object[orderedParams.size()];

    for (int i = 0; i < orderedParams.size(); i++) {
        OrderedField oField = orderedParams.get(i);

        values[i] = oField.field;
    }

    return new CacheKey(cache.category(), values);
}

From source file:com.wavemaker.runtime.server.ServerUtils.java

/**
 * Get a list of methods to be exposed to the client. This will obey restrictions from {@link ExposeToClient} and
 * {@link HideFromClient}./* w  w w .  j  ava  2s. c om*/
 * 
 * @param klass The class to examine.
 * @return A list of methods to expose to the client in the specified class.
 */
@SuppressWarnings("unchecked")
public static List<Method> getClientExposedMethods(Class<?> klass) {

    List<Method> allMethods = ClassUtils.getPublicMethods(klass);
    List<Method> ret = new ArrayList<Method>(allMethods.size());
    ClassLoader cl = klass.getClassLoader();

    Class<Annotation> hideFromClient = (Class<Annotation>) ClassLoaderUtils
            .loadClass(HideFromClient.class.getCanonicalName(), cl);
    Class<Annotation> exposeToClient = (Class<Annotation>) ClassLoaderUtils
            .loadClass(ExposeToClient.class.getCanonicalName(), cl);

    if (klass.getAnnotation(hideFromClient) != null) {
        for (Method meth : allMethods) {
            if (meth.getAnnotation(exposeToClient) != null) {
                ret.add(meth);
            }
        }
    } else {
        for (Method meth : allMethods) {
            if (meth.getAnnotation(hideFromClient) == null) {
                ret.add(meth);
            }
        }
    }

    return ret;
}

From source file:de.micromata.genome.tpsb.GroovyExceptionInterceptor.java

protected static void collectMethods(Class<?> cls, Map<String, Method> collected) {
    for (Method m : cls.getDeclaredMethods()) {
        if ((m.getModifiers() & Modifier.PUBLIC) != Modifier.PUBLIC) {
            continue;
        }//from w  ww.  j  a v a2  s.com
        if ((m.getModifiers() & Modifier.STATIC) == Modifier.STATIC) {
            continue;
        }
        if (m.getAnnotation(TpsbIgnore.class) != null) {
            continue;
        }
        if (ignoreMethods.contains(m.getName()) == true) {
            continue;
        }
        if (m.getReturnType().isPrimitive() == true) {
            continue;
        }
        String sm = methodToString(m);
        if (collected.containsKey(sm) == true) {
            continue;
        }
        collected.put(sm, m);
    }
    Class<?> scls = cls.getSuperclass();
    if (scls == Object.class) {
        return;
    }
    collectMethods(scls, collected);
}

From source file:cherry.example.web.util.ViewNameUtil.java

public static String fromMethodCall(Object invocationInfo) {

    MethodInvocationInfo info = (MethodInvocationInfo) invocationInfo;
    Method method = info.getControllerMethod();
    Class<?> type = method.getDeclaringClass();

    UriComponentsBuilder ucb = UriComponentsBuilder.newInstance();
    String typePath = getMappedPath(type.getAnnotation(RequestMapping.class));
    if (StringUtils.isNotEmpty(typePath)) {
        ucb.path(typePath);//from w ww.  j  a  va2s .co  m
    }

    String methodPath = getMappedPath(method.getAnnotation(RequestMapping.class));
    if (StringUtils.isNotEmpty(methodPath)) {
        ucb.pathSegment(methodPath);
    }

    String path = ucb.build().getPath();
    if (path.startsWith("/")) {
        return path.substring(1);
    }
    return path;
}

From source file:com.haulmont.cuba.core.config.ConfigUtil.java

/**
 * Search for an annotation on a configuration interface method. In
 * addition to searching the method itself, the {@link #getGetMethod
 * plain get method} is also searched, as can the {@link
 * #getMethodType method type} be./*from   w w  w.  j  a va  2  s.  c o m*/
 *
 * @param configInterface  The configuration interface.
 * @param method           The method.
 * @param annotationType   The annotation type of interest.
 * @param searchMethodType Whether to search the method type.
 * @return The annotation, or null.
 */
public static <T extends Annotation> T getAnnotation(Class<?> configInterface, Method method,
        Class<T> annotationType, boolean searchMethodType) {
    T annotation = method.getAnnotation(annotationType);
    if (annotation == null) {
        Method getMethod = getGetMethod(configInterface, method);
        if (getMethod != null) {
            annotation = getMethod.getAnnotation(annotationType);
        }
        if ((annotation == null) && searchMethodType) {
            String methodName = method.getName();
            if (ACCESS_RE.matcher(methodName).matches()) {
                // Is the annotation present on the method type?
                Class<?> methodType = getMethodType(method);
                annotation = methodType.getAnnotation(annotationType);
            }
        }
    }
    return annotation;
}

From source file:it.tidalwave.northernwind.aspect.DebugProfilingAspect.java

@Nonnull
private static <T extends Annotation> T getAnnotation(final @Nonnull ProceedingJoinPoint pjp,
        final @Nonnull Class<T> annotationClass) throws NoSuchMethodException {
    final MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
    Method method = methodSignature.getMethod();

    if (method.getDeclaringClass().isInterface()) // FIXME && annotation inheritance -- FIXME also ancestor class
    {// w ww. j  a  va 2  s.  c  om
        final String methodName = pjp.getSignature().getName();
        method = pjp.getTarget().getClass().getDeclaredMethod(methodName, method.getParameterTypes());
    }

    return method.getAnnotation(annotationClass);
}

From source file:net.kamhon.ieagle.util.CollectionUtil.java

private static Object processForNumber(Object object, String propertyName, Object propValue) {
    BeanWrapper wrapper = new BeanWrapperImpl(object);
    PropertyDescriptor descriptor = wrapper.getPropertyDescriptor(propertyName);
    Method method = descriptor.getReadMethod();
    TypeConversion typeConversion = method.getAnnotation(TypeConversion.class);
    if (typeConversion != null) {
        String convertor = typeConversion.converter();
        if (convertor.equalsIgnoreCase(FrameworkConst.STRUTS_DECIMAL_CONVERTER)) {
            DecimalFormat df = new DecimalFormat(FrameworkConst.DEFAULT_DECIMAL_FORMAT);
            return df.format(propValue);
        } else {//from  w  ww.  j  a va2 s  .  c  o m
            return propValue;
        }
    } else {
        if (propValue instanceof Double || propValue instanceof Float) {
            DecimalFormat df = new DecimalFormat(FrameworkConst.DEFAULT_DECIMAL_FORMAT);
            return df.format(propValue);
        } else {
            return propValue;
        }
    }
}

From source file:de.hasait.clap.impl.CLAPClassNode.java

private static <T extends Annotation> T getAnnotation(final PropertyDescriptor pPropertyDescriptor,
        final Class<T> pAnnotationClass) {
    final Method writeMethod = pPropertyDescriptor.getWriteMethod();
    if (writeMethod != null) {
        final T annotation = writeMethod.getAnnotation(pAnnotationClass);
        if (annotation != null) {
            return annotation;
        }/*w ww . j a  v a2s . c o  m*/
    }

    final Method readMethod = pPropertyDescriptor.getReadMethod();
    if (readMethod != null) {
        final T annotation = readMethod.getAnnotation(pAnnotationClass);
        if (annotation != null) {
            return annotation;
        }
    }

    return null;
}