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.reactivetechnologies.platform.rest.WebbitRestServerBean.java

/**
 * /*from   w  w w. java 2  s  .c  o  m*/
 * @param restletClass
 * @return
 * @throws InstantiationException
 * @throws IllegalAccessException
 */
static JAXRSInstanceMetadata scanJaxRsClass(Class<?> restletClass)
        throws InstantiationException, IllegalAccessException {
    final JAXRSInstanceMetadata proxy = new JAXRSInstanceMetadata(restletClass.newInstance());
    if (restletClass.isAnnotationPresent(Path.class)) {
        String rootUri = restletClass.getAnnotation(Path.class).value();
        if (rootUri == null)
            rootUri = "";
        proxy.setRootUri(rootUri);
    }
    ReflectionUtils.doWithMethods(restletClass, new MethodCallback() {

        @Override
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            String subUri = "";
            if (method.isAnnotationPresent(Path.class)) {
                subUri = method.getAnnotation(Path.class).value();
            }
            if (method.isAnnotationPresent(GET.class)) {
                MethodDetail m = createMethodDetail(method);
                proxy.addGetMethod(proxy.getRootUri() + subUri, m);
            }
            if (method.isAnnotationPresent(POST.class)) {
                MethodDetail m = createMethodDetail(method);
                proxy.addPostMethod(proxy.getRootUri() + subUri, m);
            }
            if (method.isAnnotationPresent(DELETE.class)) {
                MethodDetail m = createMethodDetail(method);
                proxy.addDelMethod(proxy.getRootUri() + subUri, m);
            }

        }
    }, new MethodFilter() {

        @Override
        public boolean matches(Method method) {
            return (method.isAnnotationPresent(GET.class) || method.isAnnotationPresent(POST.class)
                    || method.isAnnotationPresent(DELETE.class));
        }
    });

    return proxy;
}

From source file:com.aionengine.gameserver.restrictions.RestrictionsManager.java

public synchronized static void activate(Restrictions restriction) {
    for (Method method : restriction.getClass().getMethods()) {
        RestrictionMode mode = RestrictionMode.parse(method);

        if (mode == null)
            continue;

        if (method.getAnnotation(DisabledRestriction.class) != null)
            continue;

        Restrictions[] restrictions = RESTRICTIONS[mode.ordinal()];

        if (!ArrayUtils.contains(restrictions, restriction))
            restrictions = (Restrictions[]) ArrayUtils.add(restrictions, restriction);

        Arrays.sort(restrictions, mode);

        RESTRICTIONS[mode.ordinal()] = restrictions;
    }/* w  w w . j av a 2 s .c o m*/
}

From source file:cn.hxh.springside.test.groups.GroupsTestRunner.java

/**
 * ??Groups?./* w ww.j  a  va 2  s. com*/
 * @Groups?GroupsALL@Groupstrue.
 */
public static boolean shouldRun(Method testMethod) {
    //?Groups
    if (groups == null) {
        initGroups();
    }
    //groupstrue
    if (groups.contains(Groups.ALL)) {
        return true;
    }

    //?Groups annotation, Groups??true.
    Groups groupsAnnotation = testMethod.getAnnotation(Groups.class);
    if ((groupsAnnotation == null) || groups.contains(groupsAnnotation.value())) {
        return true;
    }

    return false;
}

From source file:dk.netdesign.common.osgi.config.Attribute.java

private static String getAttributeName(Method classMethod) {
    String attributeName = classMethod.getName().replaceAll("^get", "");
    if (classMethod.isAnnotationPresent(Property.class)) {
        Property methodProperty = classMethod.getAnnotation(Property.class);
        if (!methodProperty.name().isEmpty()) {
            attributeName = methodProperty.name();
        }/*w w w.j a va  2 s .c om*/
    }
    return attributeName;
}

From source file:com.nridge.core.base.field.data.DataBeanBag.java

/**
 * Accepts a POJO containing one or more public annotated get
 * methods and creates a DataBag from them.  The DataBeanObject2
 * test class provides a reference example.
 *
 * @param anObject POJO instance.//from  w w  w  . java2 s  .c  o m
 *
 * @return Data bag instance populated with field information.
 *
 * @throws NSException Thrown if object is null.
 * @throws IllegalAccessException Thrown if access is illegal.
 * @throws InvocationTargetException Thrown if target execution fails.
 */
public static DataBag fromMethodsToBag(Object anObject)
        throws NSException, IllegalAccessException, InvocationTargetException {
    DataField dataField;
    BeanField beanField;
    boolean isPublicAccess, isAnnotationPresent;

    if (anObject == null)
        throw new NSException("Object is null");

    DataBag dataBag = new DataBag(anObject.toString());
    Class<?> objClass = anObject.getClass();
    Method[] methodArray = objClass.getDeclaredMethods();
    for (Method objMethod : methodArray) {
        isPublicAccess = Modifier.isPublic(objMethod.getModifiers());
        isAnnotationPresent = objMethod.isAnnotationPresent(BeanField.class);
        if ((isAnnotationPresent) && (isPublicAccess)) {
            beanField = objMethod.getAnnotation(BeanField.class);
            dataField = reflectMethod(anObject, beanField, objMethod);
            dataBag.add(dataField);
        }
    }

    return dataBag;
}

From source file:ei.ne.ke.cassandra.cql3.EntitySpecificationUtils.java

/**
 * @param embeddedType//from  www  .  j  a va  2 s .  c  o  m
 * @return the column mapping of the {@link java.reflect.Field} annotated with
 * {@link javax.persistence.EmbeddedId}.
 *
 * @see http://docs.oracle.com/javaee/6/api/index.html?javax/persistence/EmbeddedId.html
 */
public static <T> List<String> getCompoundKeyColumnNames(Class<T> embeddedType) {
    Preconditions.checkNotNull(embeddedType);
    List<String> columns = Lists.newArrayList();
    for (Field embeddedField : embeddedType.getDeclaredFields()) {
        javax.persistence.Column c = embeddedField.getAnnotation(javax.persistence.Column.class);
        if (c == null) {
            continue;
        }
        String normalizedName = normalizeCqlElementName(c.name());
        if (columns.contains(normalizedName)) {
            throw new IllegalStateException(String.format("Duplicate column name '%s'", normalizedName));
        }
        columns.add(normalizedName);
    }
    for (Method embeddedMethod : embeddedType.getDeclaredMethods()) {
        javax.persistence.Column c = embeddedMethod.getAnnotation(javax.persistence.Column.class);
        if (c == null) {
            continue;
        }
        String normalizedName = normalizeCqlElementName(c.name());
        if (columns.contains(normalizedName)) {
            throw new IllegalStateException(String.format("Duplicate column name '%s'", normalizedName));
        }
        columns.add(normalizedName);
    }
    return columns;
}

From source file:RssAtomGenerationTest.java

public static <T> T convertObject(final T destination, final Object source, FeedType type, Object... context) {
    Map<Class, Object> contentMap = new HashMap<Class, Object>();
    for (Object o : context) {
        contentMap.put(o.getClass(), o);
    }/*from ww w.j  a v a2 s  .  co m*/
    final Method[] methods = source.getClass().getMethods();
    for (Method method : methods) {
        final boolean refPresent = method.isAnnotationPresent(SyndicationRefs.class);
        if (refPresent || (method.isAnnotationPresent(SyndicationElement.class)
                && method.getAnnotation(SyndicationElement.class).type().equals(type))) {
            SyndicationElement annotation = null;
            if (refPresent) {
                final SyndicationElement[] value = method.getAnnotation(SyndicationRefs.class).value();
                for (SyndicationElement element : value) {
                    if (element.type().equals(type)) {
                        annotation = element;
                        break;
                    }
                }
                if (annotation == null) {
                    continue;
                }
            } else {
                annotation = method.getAnnotation(SyndicationElement.class);
            }
            //final SyndicationElement annotation =
            final String name = annotation.name();
            try {
                final Object initValue = method.invoke(source);
                Object value = null;
                if (!(annotation.converter().isAssignableFrom(NoopConverter.class))) {
                    final Converter converter = annotation.converter().newInstance();
                    value = converter.convert(initValue);
                }
                if (!(annotation.transformer().isAssignableFrom(NoopTransformer.class))) {
                    contentMap.put(initValue.getClass(), initValue);
                    final Class<?> transformer = annotation.transformer();
                    final Method[] transformerMethods = transformer.getMethods();
                    for (Method transformerMethod : transformerMethods) {
                        if (transformerMethod.isAnnotationPresent(ContextTransformable.class)) {
                            final Class<?>[] parameterTypes = transformerMethod.getParameterTypes();
                            List<Object> parameters = new ArrayList<Object>();
                            for (Class clazz : parameterTypes) {
                                if (contentMap.containsKey(clazz)) {
                                    parameters.add(contentMap.get(clazz));
                                } else {
                                    boolean found = false;
                                    for (Object obj : contentMap.values()) {
                                        if (clazz.isInstance(obj)) {
                                            parameters.add(obj);
                                            found = true;
                                            break;
                                        }
                                    }
                                    if (!found) {
                                        parameters.add(null);
                                    }
                                }
                            }
                            if (Modifier.isStatic(transformerMethod.getModifiers())) {
                                value = transformerMethod.invoke(null, parameters.toArray());
                            } else {
                                value = transformerMethod.invoke(transformer.newInstance(),
                                        parameters.toArray());
                            }
                            break;
                        }
                    }

                }
                BeanUtils.setProperty(destination, name, value);
            } catch (Exception e) {
                log.error("test", e);
                e.printStackTrace();
            }

        }
    }
    return destination;
}

From source file:com.aionemu.gameserver.restrictions.RestrictionsManager.java

public synchronized static void activate(Restrictions restriction) {
    for (Method method : restriction.getClass().getMethods()) {
        RestrictionMode mode = RestrictionMode.parse(method);

        if (mode == null) {
            continue;
        }//  w w w .  j av a2 s. c om

        if (method.getAnnotation(DisabledRestriction.class) != null) {
            continue;
        }

        Restrictions[] restrictions = RESTRICTIONS[mode.ordinal()];

        if (!ArrayUtils.contains(restrictions, restriction)) {
            restrictions = (Restrictions[]) ArrayUtils.add(restrictions, restriction);
        }

        Arrays.sort(restrictions, mode);

        RESTRICTIONS[mode.ordinal()] = restrictions;
    }
}

From source file:dk.netdesign.common.osgi.config.Attribute.java

private static Class getMethodReturnType(Method classMethod) throws InvalidMethodException {
    Class methodReturnType = classMethod.getReturnType();
    if (classMethod.isAnnotationPresent(Property.class)) {
        Property methodProperty = classMethod.getAnnotation(Property.class);
        if (List.class.isAssignableFrom(methodReturnType) && methodProperty.type() == void.class) {
            throw new InvalidMethodException("Could not create handler for method " + classMethod.getName()
                    + ". Lists must be accompanied by a returnType");
        }// w ww .ja  va  2  s.  co  m
    }
    return methodReturnType;
}

From source file:org.openmrs.module.webservices.docs.ResourceDocCreator.java

/**
 * Generates {@link ResourceOperation}s corresponding to the supported http methods
 * /* ww w .j av a 2 s.  com*/
 * @param url
 * @param clazz
 * @param supportsSearching specified if the controller the method belongs supports searching
 * @return
 */
private static List<ResourceOperation> getResourceOperations(String url, Class<?> clazz,
        boolean supportsSearching) {
    List<ResourceOperation> resourceOperations = new ArrayList<ResourceOperation>();
    Method[] methods = clazz.getMethods();
    for (Method method : methods) {
        RequestMapping antn = (RequestMapping) method.getAnnotation(RequestMapping.class);
        if (antn == null)
            continue;

        String requestMethod = antn.method()[0].name();

        if (requestMethod.equals("TRACE")) {
            //Skip TRACE, which is used to disable a method in HL7MessageController.
            continue;
        }

        String operationUrl = requestMethod + " " + url;

        if (antn.value().length > 0)
            operationUrl += antn.value()[0];

        String paramString = null;
        for (String param : antn.params()) {
            if (paramString == null)
                paramString = param;
            else
                paramString += ("&" + param);
        }

        if (paramString != null)
            operationUrl += "?" + paramString;

        resourceOperations.add(new ResourceOperation(operationUrl,
                getMethodDescription(antn, requestMethod, method, supportsSearching)));
    }

    Collections.sort(resourceOperations);

    return resourceOperations;
}