Example usage for org.springframework.core.annotation AnnotationUtils findAnnotation

List of usage examples for org.springframework.core.annotation AnnotationUtils findAnnotation

Introduction

In this page you can find the example usage for org.springframework.core.annotation AnnotationUtils findAnnotation.

Prototype

@Nullable
public static <A extends Annotation> A findAnnotation(Class<?> clazz, @Nullable Class<A> annotationType) 

Source Link

Document

Find a single Annotation of annotationType on the supplied Class , traversing its interfaces, annotations, and superclasses if the annotation is not directly present on the given class itself.

Usage

From source file:morph.plugin.views.annotation.AnnotationMethodHandlerAdapter.java

public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {

    if (AnnotationUtils.findAnnotation(handler.getClass(), SessionAttributes.class) != null) {
        // Always prevent caching in case of session attribute management.
        checkAndPrepare(request, response, this.cacheSecondsForSessionAttributeHandlers, true);
        // Prepare cached set of session attributes names.
    } else {/* w  ww. j  av a2 s.c  o m*/
        // Uses configured default cacheSeconds setting.
        checkAndPrepare(request, response, true);
    }

    // Execute invokeHandlerMethod in synchronized block if required.
    if (this.synchronizeOnSession) {
        HttpSession session = request.getSession(false);
        if (session != null) {
            Object mutex = WebUtils.getSessionMutex(session);
            synchronized (mutex) {
                return invokeHandlerMethod(request, response, handler);
            }
        }
    }

    return invokeHandlerMethod(request, response, handler);
}

From source file:com.haulmont.cuba.core.sys.MetadataLoader.java

protected void initMetaAnnotations(MetaClass metaClass) {
    for (Annotation annotation : metaClass.getJavaClass().getAnnotations()) {
        MetaAnnotation metaAnnotation = AnnotationUtils.findAnnotation(annotation.getClass(),
                MetaAnnotation.class);
        if (metaAnnotation != null) {
            String name = annotation.annotationType().getName();

            Map<String, Object> attributes = new LinkedHashMap<>(
                    AnnotationUtils.getAnnotationAttributes(metaClass.getJavaClass(), annotation));
            metaClass.getAnnotations().put(name, attributes);

            Object propagateToSubclasses = attributes.get("propagateToSubclasses");
            if (propagateToSubclasses == null || Boolean.TRUE.equals(propagateToSubclasses)) {
                for (MetaClass descMetaClass : metaClass.getDescendants()) {
                    Annotation descAnnotation = descMetaClass.getJavaClass()
                            .getAnnotation(annotation.annotationType());
                    if (descAnnotation == null) {
                        descMetaClass.getAnnotations().put(name, attributes);
                    }/*w ww . java 2 s.c  o m*/
                }
            }
        }
    }
}

From source file:com.github.hateoas.forms.spring.SpringActionDescriptor.java

private static RequestMethod getHttpMethod(final Method method) {
    RequestMapping methodRequestMapping = AnnotationUtils.findAnnotation(method, RequestMapping.class);
    RequestMethod requestMethod;// ww w  .j  av a 2 s. c  o  m
    if (methodRequestMapping != null) {
        RequestMethod[] methods = methodRequestMapping.method();
        if (methods.length == 0) {
            requestMethod = RequestMethod.GET;
        } else {
            requestMethod = methods[0];
        }
    } else {
        requestMethod = RequestMethod.GET; // default
    }
    return requestMethod;
}

From source file:com.github.hateoas.forms.spring.SpringActionDescriptor.java

private static String getConsumes(final Method method) {
    RequestMapping methodRequestMapping = AnnotationUtils.findAnnotation(method, RequestMapping.class);
    if (methodRequestMapping != null) {
        StringBuilder sb = new StringBuilder();
        for (String consume : methodRequestMapping.consumes()) {
            sb.append(consume).append(",");
        }//www.ja v a  2  s  .co m
        if (sb.length() > 1) {
            sb.setLength(sb.length() - 1);
            return sb.toString();
        }
    }
    return null;
}

From source file:com.github.hateoas.forms.spring.SpringActionDescriptor.java

private static String getProduces(final Method method) {
    RequestMapping methodRequestMapping = AnnotationUtils.findAnnotation(method, RequestMapping.class);
    if (methodRequestMapping != null) {
        StringBuilder sb = new StringBuilder();
        for (String produce : methodRequestMapping.produces()) {
            sb.append(produce).append(",");
        }/*  ww  w. j  a  v a  2  s  .  c o m*/
        if (sb.length() > 1) {
            sb.setLength(sb.length() - 1);
            return sb.toString();
        }
    }
    return null;
}

From source file:com.haulmont.cuba.core.sys.MetaModelLoader.java

protected void loadPropertyAnnotations(MetaProperty metaProperty, AnnotatedElement annotatedElement) {
    for (Annotation annotation : annotatedElement.getAnnotations()) {
        MetaAnnotation metaAnnotation = AnnotationUtils.findAnnotation(annotation.getClass(),
                MetaAnnotation.class);
        if (metaAnnotation != null) {
            Map<String, Object> attributes = new LinkedHashMap<>(
                    AnnotationUtils.getAnnotationAttributes(annotatedElement, annotation));
            metaProperty.getAnnotations().put(annotation.annotationType().getName(), attributes);
        }//from  www  . j a v  a2 s . c  o  m
    }

    com.haulmont.chile.core.annotations.MetaProperty metaPropertyAnnotation = annotatedElement
            .getAnnotation(com.haulmont.chile.core.annotations.MetaProperty.class);
    if (metaPropertyAnnotation != null) {
        String[] related = metaPropertyAnnotation.related();
        if (!(related.length == 1 && related[0].equals(""))) {
            metaProperty.getAnnotations().put("relatedProperties", Joiner.on(',').join(related));
        }
    }

    loadBeanValidationAnnotations(metaProperty, annotatedElement);
}

From source file:com.github.hateoas.forms.spring.SpringActionDescriptor.java

private Cardinality getCardinality(final Method invokedMethod, final RequestMethod httpMethod,
        final Type genericReturnType) {
    Cardinality cardinality;/* www  . ja v a2  s . co m*/
    ResourceHandler resourceAnn;
    if (SPRING_4_2) {
        resourceAnn = AnnotationUtils.findAnnotation((AnnotatedElement) invokedMethod, ResourceHandler.class);
    } else {
        resourceAnn = AnnotationUtils.findAnnotation(invokedMethod, ResourceHandler.class);
    }
    if (resourceAnn != null) {
        cardinality = resourceAnn.value();
    } else {
        if (RequestMethod.POST == httpMethod || containsCollection(genericReturnType)) {
            cardinality = Cardinality.COLLECTION;
        } else {
            cardinality = Cardinality.SINGLE;
        }
    }
    return cardinality;
}

From source file:org.alfresco.rest.framework.core.ResourceInspector.java

/**
 * Inspects the method and returns meta data about its operations
 * @param aMethod Method/*from  www .j  ava2 s .  c  o m*/
 * @param httpMethod HttpMethod
 * @return ResourceOperation
 */
public static ResourceOperation inspectOperation(Class<?> resource, Method aMethod, HttpMethod httpMethod) {
    Annotation annot = AnnotationUtils.findAnnotation(aMethod, WebApiDescription.class);
    List<ResourceParameter> parameters = new ArrayList<ResourceParameter>();
    parameters.addAll(inspectParameters(resource, aMethod, httpMethod));

    if (annot != null) {
        Map<String, Object> annotAttribs = AnnotationUtils.getAnnotationAttributes(annot);
        String title = String.valueOf(annotAttribs.get("title"));
        String desc = String.valueOf(annotAttribs.get("description"));
        Integer success = (Integer) annotAttribs.get("successStatus");
        return new ResourceOperation(httpMethod, title, desc, parameters,
                validSuccessCode(httpMethod, success));
    } else {
        return new ResourceOperation(httpMethod, "Missing @WebApiDescription annotation",
                "This method should be annotated with @WebApiDescription", parameters,
                validSuccessCode(httpMethod, ResourceOperation.UNSET_STATUS));
    }
}

From source file:org.alfresco.rest.framework.core.ResourceInspector.java

/**
 * Inspects the Method to find any @WebApiParameters and @WebApiParam
 * @param resource the class// www.j a  va 2 s. c  o  m
 * @param aMethod the method
 * @param httpMethod HttpMethod
 * @return a List of parameters
 */
private static List<ResourceParameter> inspectParameters(Class<?> resource, Method aMethod,
        HttpMethod httpMethod) {
    List<ResourceParameter> params = new ArrayList<ResourceParameter>();
    Annotation annot = AnnotationUtils.findAnnotation(aMethod, WebApiParameters.class);
    if (annot != null) {
        Map<String, Object> annotAttribs = AnnotationUtils.getAnnotationAttributes(annot);
        WebApiParam[] apiParams = (WebApiParam[]) annotAttribs.get("value");
        for (int i = 0; i < apiParams.length; i++) {
            params.add(findResourceParameter(apiParams[i], resource, aMethod));
        }
    } else {
        Annotation paramAnot = AnnotationUtils.findAnnotation(aMethod, WebApiParam.class);
        if (paramAnot != null) {
            params.add(findResourceParameter(paramAnot, resource, aMethod));
        }
    }

    //Setup default parameters
    switch (httpMethod) {
    case POST:
        if (paramsCount(params, ResourceParameter.KIND.URL_PATH) == 0) {
            params.add(ResourceParameter.ENTITY_PARAM);
        }
        if (paramsCount(params, ResourceParameter.KIND.HTTP_BODY_OBJECT) == 0) {
            inspectBodyParamAndReturnType(resource, aMethod, params);
        }
        break;
    case PUT:
        int urlPathForPut = paramsCount(params, ResourceParameter.KIND.URL_PATH);
        if (urlPathForPut == 0) {
            params.add(ResourceParameter.ENTITY_PARAM);
        }
        if (RelationshipResourceAction.Update.class.isAssignableFrom(resource) && urlPathForPut < 2) {
            params.add(ResourceParameter.RELATIONSHIP_PARAM);
        }
        if (paramsCount(params, ResourceParameter.KIND.HTTP_BODY_OBJECT) == 0) {
            inspectBodyParamAndReturnType(resource, aMethod, params);
        }
        break;
    case GET:
        int urlPathForGet = paramsCount(params, ResourceParameter.KIND.URL_PATH);
        if (urlPathForGet == 0 && (EntityResourceAction.ReadById.class.isAssignableFrom(resource)
                && READ_BY_ID_METHODNAME.equals(aMethod.getName()))) {
            params.add(ResourceParameter.ENTITY_PARAM);
        } else if (RelationshipResourceAction.ReadById.class.isAssignableFrom(resource)
                || RelationshipResourceAction.Read.class.isAssignableFrom(resource)) {
            //Its a RelationshipResourceAction
            if (urlPathForGet == 0) {
                params.add(ResourceParameter.ENTITY_PARAM);
            }
            //This method is what we are inspecting not what the class implements.
            if (READ_BY_ID_METHODNAME.equals(aMethod.getName()) && urlPathForGet < 2) {
                params.add(ResourceParameter.RELATIONSHIP_PARAM);
            }
        }
        if (!READ_BY_ID_METHODNAME.equals(aMethod.getName())) {
            params.add(ResourceParameter.SKIP_PARAM);
            params.add(ResourceParameter.MAX_ITEMS_PARAM);
            params.add(ResourceParameter.PROPS_PARAM);
        }
        break;
    case DELETE:
        int urlPathForDelete = paramsCount(params, ResourceParameter.KIND.URL_PATH);
        if (urlPathForDelete == 0) {
            params.add(ResourceParameter.ENTITY_PARAM);
        }
        //Add relationship param ?
        if (RelationshipResourceAction.Delete.class.isAssignableFrom(resource) && urlPathForDelete < 2) {
            params.add(ResourceParameter.RELATIONSHIP_PARAM);
        }
    }

    return params;
}

From source file:org.alfresco.rest.framework.core.ResourceInspector.java

/**
 * Finds the name of the entity using its annotation.
 * @return the entity name/path// w  ww .  j av  a 2  s.  c o m
 */
protected static String findEntityNameByAnnotationAttributes(Map<String, Object> annotAttribs) {
    Class<?> entityResourceRef = (Class<?>) annotAttribs.get("entityResource");
    EntityResource entityAnnot = AnnotationUtils.findAnnotation(entityResourceRef, EntityResource.class);
    return findEntityName(entityAnnot);
}