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

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

Introduction

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

Prototype

public static Map<String, Object> getAnnotationAttributes(Annotation annotation) 

Source Link

Document

Retrieve the given annotation's attributes as a Map , preserving all attribute types.

Usage

From source file:org.focusns.common.validation.ValidationHelper.java

/**
 * ??//from   w w  w. ja  va  2  s . c om
 * 
 * @param constraintDescriptor
 * @return
 */
private static List<String> getConstraintParams(ConstraintDescriptor<?> constraintDescriptor) {
    Annotation constraintInstance = constraintDescriptor.getAnnotation();
    Class<?> constraintClass = constraintDescriptor.getAnnotation().annotationType();
    //
    List<String> params = new ArrayList<String>();
    //
    Method valueMethod = ClassUtils.getMethodIfAvailable(constraintClass, "value", (Class<?>[]) null);
    if (valueMethod != null) {
        String value = String.valueOf(ReflectionUtils.invokeMethod(valueMethod, constraintInstance));
        params.add("value:" + value);
    }
    Map<String, Object> annotationAttrs = AnnotationUtils.getAnnotationAttributes(constraintInstance);
    for (String key : annotationAttrs.keySet()) {
        if ("message".equals(key) || "payload".equals(key)) {
            continue;
        }
        //
        String value = "";
        if ("groups".equals(key)) {
            List<String> groupNames = new ArrayList<String>();
            Class[] groupClasses = (Class[]) annotationAttrs.get(key);
            for (Class groupClass : groupClasses) {
                groupNames.add(groupClass.getName());
            }
            value = StringUtils.collectionToDelimitedString(groupNames, "|");
        } else {
            value = String.valueOf(annotationAttrs.get(key));
        }
        //
        if (StringUtils.hasText(value)) {
            params.add(key + ":" + "'" + value + "'");
        }
    }
    //
    return params;
}

From source file:us.swcraft.springframework.cache.aerospike.config.annotation.AerospikeCacheConfiguration.java

public void setImportMetadata(AnnotationMetadata importMetadata) {
    Map<String, Object> enableAttrMap = importMetadata
            .getAnnotationAttributes(EnableAerospikeCacheManager.class.getName());
    AnnotationAttributes enableAttrs = AnnotationAttributes.fromMap(enableAttrMap);
    if (enableAttrs == null) {
        // search parent classes
        Class<?> currentClass = ClassUtils.resolveClassName(importMetadata.getClassName(), beanClassLoader);
        for (Class<?> classToInspect = currentClass; classToInspect != null; classToInspect = classToInspect
                .getSuperclass()) {/*from  w  w w  . j av a 2 s  . co m*/
            EnableAerospikeCacheManager enableWebSecurityAnnotation = AnnotationUtils
                    .findAnnotation(classToInspect, EnableAerospikeCacheManager.class);
            if (enableWebSecurityAnnotation == null) {
                continue;
            }
            enableAttrMap = AnnotationUtils.getAnnotationAttributes(enableWebSecurityAnnotation);
            enableAttrs = AnnotationAttributes.fromMap(enableAttrMap);
        }
    }
    defaultTimeToLiveInSeconds = enableAttrs.getNumber("defaultTimeToLiveInSeconds");
    defaultNamespace = enableAttrs.getString("defaultNamespace");
    defaultCacheName = enableAttrs.getString("defaultCacheName");
    compression = enableAttrs.getEnum("compression");
    serializerClass = enableAttrs.getClass("serializerClass");

    cachesConfiguration = enableAttrs.getAnnotationArray("caches");
}

From source file:us.swcraft.springframework.session.aerospike.config.annotation.web.http.AerospikeHttpSessionConfiguration.java

public void setImportMetadata(AnnotationMetadata importMetadata) {
    Map<String, Object> enableAttrMap = importMetadata
            .getAnnotationAttributes(EnableAerospikeHttpSession.class.getName());
    AnnotationAttributes enableAttrs = AnnotationAttributes.fromMap(enableAttrMap);
    if (enableAttrs == null) {
        // search parent classes
        Class<?> currentClass = ClassUtils.resolveClassName(importMetadata.getClassName(), beanClassLoader);
        for (Class<?> classToInspect = currentClass; classToInspect != null; classToInspect = classToInspect
                .getSuperclass()) {/* www .  j  av  a 2 s.com*/
            EnableAerospikeHttpSession enableWebSecurityAnnotation = AnnotationUtils
                    .findAnnotation(classToInspect, EnableAerospikeHttpSession.class);
            if (enableWebSecurityAnnotation == null) {
                continue;
            }
            enableAttrMap = AnnotationUtils.getAnnotationAttributes(enableWebSecurityAnnotation);
            enableAttrs = AnnotationAttributes.fromMap(enableAttrMap);
        }
    }
    maxInactiveIntervalInSeconds = enableAttrs.getNumber("maxInactiveIntervalInSeconds");
    namespace = enableAttrs.getString("namespace");
    setname = enableAttrs.getString("setname");
}

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

/**
 * Inspects the relationship resource and returns meta data about it
 * /* www  .  ja  va 2s  .co  m*/
 * @param annot RelationshipResource
 * @param resource Class<?>
 */
private static List<ResourceMetadata> inspectRelationship(RelationshipResource annot, Class<?> resource) {
    Map<String, Object> annotAttribs = AnnotationUtils.getAnnotationAttributes(annot);
    String urlPath = String.valueOf(annotAttribs.get("name"));
    String entityPath = findEntityNameByAnnotationAttributes(annotAttribs);
    String relationshipKey = ResourceDictionary.resourceKey(entityPath, urlPath);
    Api api = inspectApi(resource);
    List<ResourceMetadata> metainfo = new ArrayList<ResourceMetadata>();

    MetaHelper helper = new MetaHelper(resource);
    findOperation(RelationshipResourceAction.Create.class, HttpMethod.POST, helper);
    findOperation(RelationshipResourceAction.Read.class, HttpMethod.GET, helper);
    findOperation(RelationshipResourceAction.ReadById.class, HttpMethod.GET, helper);
    findOperation(RelationshipResourceAction.Update.class, HttpMethod.PUT, helper);
    findOperation(RelationshipResourceAction.Delete.class, HttpMethod.DELETE, helper);

    findOperation(RelationshipResourceAction.CreateWithResponse.class, HttpMethod.POST, helper);
    findOperation(RelationshipResourceAction.ReadWithResponse.class, HttpMethod.GET, helper);
    findOperation(RelationshipResourceAction.ReadByIdWithResponse.class, HttpMethod.GET, helper);
    findOperation(RelationshipResourceAction.UpdateWithResponse.class, HttpMethod.PUT, helper);
    findOperation(RelationshipResourceAction.DeleteWithResponse.class, HttpMethod.DELETE, helper);

    findOperation(MultiPartRelationshipResourceAction.Create.class, HttpMethod.POST, helper);

    boolean noAuth = resource.isAnnotationPresent(WebApiNoAuth.class);
    if (noAuth) {
        throw new IllegalArgumentException(
                "@WebApiNoAuth should not be on all (relationship resource class) - only on methods: "
                        + urlPath);
    }

    Set<Class<? extends ResourceAction>> apiNoAuth = helper.apiNoAuth;

    if (resource.isAnnotationPresent(WebApiDeleted.class)) {
        metainfo.add(new ResourceMetadata(relationshipKey, RESOURCE_TYPE.RELATIONSHIP, null,
                inspectApi(resource), ALL_RELATIONSHIP_RESOURCE_INTERFACES, apiNoAuth, entityPath));
    } else {
        metainfo.add(new ResourceMetadata(relationshipKey, RESOURCE_TYPE.RELATIONSHIP, helper.operations,
                inspectApi(resource), helper.apiDeleted, apiNoAuth, entityPath));
    }

    inspectAddressedProperties(api, resource, relationshipKey, metainfo);
    inspectOperations(api, resource, relationshipKey, metainfo);
    return metainfo;
}

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

/**
 * Inspects the method and returns meta data about its operations
 * @param aMethod Method/*from ww  w.ja v  a  2s. co  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//from   w  w  w . j  a v  a  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

/**
 * @param paramAnot Annotation// w w  w .j av  a 2s  .c  o  m
 * @param resource Class<?>
 * @param aMethod Method
 * @return ResourceParameter
 */
private static ResourceParameter findResourceParameter(Annotation paramAnot, Class<?> resource,
        Method aMethod) {
    Map<String, Object> annotAttribs = AnnotationUtils.getAnnotationAttributes(paramAnot);
    ResourceParameter.KIND paramKind = (ResourceParameter.KIND) annotAttribs.get("kind");
    Class<?> dType = String.class;
    if (ResourceParameter.KIND.HTTP_BODY_OBJECT.equals(paramKind)) {
        dType = ResourceInspectorUtil.determineType(resource, aMethod);
    }
    return ResourceParameter.valueOf(String.valueOf(annotAttribs.get("name")),
            String.valueOf(annotAttribs.get("title")), String.valueOf(annotAttribs.get("description")),
            (Boolean) annotAttribs.get("required"), paramKind, (Boolean) annotAttribs.get("allowMultiple"),
            dType);
}

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

/**
 * Finds the name of the entity using its annotation.
 * @param entityAnnot EntityResource/*w w  w. j a  v  a 2s.  c  o m*/
 * @return the entity name/path
 */
protected static String findEntityName(EntityResource entityAnnot) {
    Map<String, Object> annotAttribs = AnnotationUtils.getAnnotationAttributes(entityAnnot);
    String urlPath = String.valueOf(annotAttribs.get("name"));
    return urlPath;
}

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

/**
 * For a given class, looks for @EmbeddedEntityResource annotations, using the annotation produce
 * a Map of the property name key and the entity key
 * @return A map of property key name and a value of the entity path name
 *//* ww  w  .j a  v  a  2  s.c  om*/
public static Map<String, Pair<String, Method>> findEmbeddedResources(Class<?> anyClass) {
    Map<String, Pair<String, Method>> embeds = new HashMap<String, Pair<String, Method>>();
    List<Method> annotatedMethods = ResourceInspectorUtil.findMethodsByAnnotation(anyClass,
            EmbeddedEntityResource.class);
    if (annotatedMethods != null && !annotatedMethods.isEmpty()) {
        for (Method annotatedMethod : annotatedMethods) {
            Annotation annot = AnnotationUtils.findAnnotation(annotatedMethod, EmbeddedEntityResource.class);
            if (annot != null) {
                Map<String, Object> annotAttribs = AnnotationUtils.getAnnotationAttributes(annot);
                String entityPath = findEntityNameByAnnotationAttributes(annotAttribs);
                String key = String.valueOf(annotAttribs.get("propertyName"));
                embeds.put(key, new Pair<String, Method>(entityPath, annotatedMethod));
            }
        }

    }
    return embeds;
}

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

/**
 * Finds operations on an entity//from  w w w  . j a  v a  2  s  . co m
 * @param entityPath path to the entity
 * @param anyClass resource clause
 * @return The operations
 */
private static Map<String, Pair<ResourceOperation, Method>> findOperations(String entityPath,
        Class<?> anyClass) {
    Map<String, Pair<ResourceOperation, Method>> embeds = new HashMap<String, Pair<ResourceOperation, Method>>();
    List<Method> annotatedMethods = ResourceInspectorUtil.findMethodsByAnnotation(anyClass, Operation.class);
    if (annotatedMethods != null && !annotatedMethods.isEmpty())
        for (Method annotatedMethod : annotatedMethods) {
            //validateOperationMethod(annotatedMethod, anyClass);
            Annotation annot = AnnotationUtils.findAnnotation(annotatedMethod, Operation.class);
            if (annot != null) {
                Map<String, Object> annotAttribs = AnnotationUtils.getAnnotationAttributes(annot);
                String actionName = String.valueOf(annotAttribs.get("value"));
                String actionPath = ResourceDictionary.propertyResourceKey(entityPath, actionName);
                ResourceOperation ro = inspectOperation(anyClass, annotatedMethod, HttpMethod.POST);
                embeds.put(actionPath, new Pair<ResourceOperation, Method>(ro, annotatedMethod));
            }
        }
    return embeds;
}