Example usage for java.lang.reflect Method getParameterAnnotations

List of usage examples for java.lang.reflect Method getParameterAnnotations

Introduction

In this page you can find the example usage for java.lang.reflect Method getParameterAnnotations.

Prototype

@Override
public Annotation[][] getParameterAnnotations() 

Source Link

Usage

From source file:at.ac.tuwien.infosys.jcloudscale.utility.ReflectionUtil.java

public static boolean[] findByRefParams(Method method) {

    Annotation[][] paramAnnotations = method.getParameterAnnotations();
    Class<?>[] paramTypes = method.getParameterTypes();
    return findByRefParams(paramAnnotations, paramTypes);

}

From source file:com.ryantenney.metrics.spring.Util.java

static String resolveName(String explicitName, Method method, Object... arguments) {
    //single arg methods where the arg is null need to be initialized with one null element, since the array comes in as null sometimes
    Object[] args;// w ww.j  av  a 2  s  . com
    if (arguments == null) {
        args = new Object[] { null };
    } else {
        args = arguments;
    }
    String resolvedName = explicitName;
    MetricParam mp;
    int i = 0;
    Annotation[][] parametersAnnotations = method.getParameterAnnotations();
    for (Annotation[] parameterAnnotations : parametersAnnotations) {
        for (Annotation parameterAnnotation : parameterAnnotations) {
            if (parameterAnnotation != null && parameterAnnotation instanceof MetricParam && i < args.length) {
                mp = (MetricParam) parameterAnnotation;
                String token = mp.value();
                if (StringUtils.isEmpty(token)) {
                    token = Integer.toString(i);
                }
                token = "{" + token + "}";
                String value = "";
                if (mp.collection()) {
                    int size = 0;
                    if (args[i] != null) {
                        size = ((Collection<?>) args[i]).size();
                    }
                    value = "size." + Integer.toString(size);
                } else {
                    if (args[i] != null) {
                        value = args[i].toString();
                    } else {
                        value = "null";
                    }
                }
                resolvedName = explicitName.replace(token, value);
            }
        }
        i++;
    }
    return resolvedName;
}

From source file:org.seedstack.seed.core.utils.SeedReflectionUtils.java

/**
 * Checks if an annotation is present on a set of methods (itself or on at lest one of its parameters) set.
 *
 * @param methods         the methods where the annotation is searched.
 * @param annotationClass the annotation to check for.
 * @return true if the annotation is present, false otherwise.
 *//* w  w w .j  a va2s  .  c  o m*/
public static boolean isPresent(Set<Method> methods, Class<? extends Annotation> annotationClass) {
    for (Method method : methods) {
        if (method.getAnnotation(annotationClass) != null) {
            return true;
        }

        for (Annotation[] paramAnnotations : method.getParameterAnnotations()) {
            if (paramAnnotations != null) {
                for (Annotation paramAnnotation : paramAnnotations) {
                    if (paramAnnotation.annotationType().equals(annotationClass)) {
                        return true;
                    }
                }
            }
        }
    }

    return false;
}

From source file:io.neba.core.resourcemodels.factory.ModelInstantiator.java

/**
 * @return all public methods annotated with @Inject. Fails if a public method
 * annotated with @Inject does not take exactly one argument.
 *//*  w ww  .ja  va  2 s. c  om*/
@Nonnull
private static ModelServiceSetter[] resolveServiceSetters(@Nonnull Class<?> modelType) {
    List<ModelServiceSetter> serviceSetters = new ArrayList<>();
    for (Method method : modelType.getMethods()) {
        if (isStatic(method.getModifiers())) {
            continue;
        }
        if (!annotations(method).containsName(INJECT_ANNOTATION_NAME)) {
            continue;
        }

        if (method.getParameterCount() != 1) {
            throw new InvalidModelException("The method " + method
                    + " is annotated with @Inject and must thus take exactly one argument.");
        }

        Filter filter = findFilterAnnotation(method.getParameterAnnotations()[0]);
        Type serviceType = method.getGenericParameterTypes()[0];
        ServiceDependency serviceDependency = new ServiceDependency(serviceType, modelType, filter);
        serviceSetters.add(new ModelServiceSetter(serviceDependency, method));
    }
    return serviceSetters.toArray(new ModelServiceSetter[0]);
}

From source file:org.switchyard.component.resteasy.util.ClientInvoker.java

private static String createSubResourcePath(String base, Method method) {
    if (!base.endsWith("/")) {
        base = base + "/";
    }//from   w  w w  .  ja  va 2  s . com
    String path = method.getAnnotation(Path.class).value();
    if (path.startsWith("/")) {
        path = path.substring(1);
    }
    Annotation[][] params = method.getParameterAnnotations();
    int index = 1;
    for (Annotation[] param : params) {
        for (Annotation a : param) {
            if (a instanceof PathParam) {
                String name = ((PathParam) a).value();
                path = path.replace("{" + name + "}", "%" + index + "$s");
                break;
            }
        }
        index++;
    }
    return base + path;
}

From source file:com.astamuse.asta4d.data.InjectUtil.java

private final static List<TargetInfo> createMethodTarget(Method method)
        throws InstantiationException, IllegalAccessException {
    Class<?>[] types = method.getParameterTypes();
    List<TargetInfo> targetList = new ArrayList<>();
    if (types.length == 0) {
        return targetList;
    }/*from  w  w w .ja v a2  s . c om*/
    Annotation[][] annotations = method.getParameterAnnotations();
    String[] parameterNames = paranamer.lookupParameterNames(method);
    TargetInfo target;
    ContextData cd;
    ContextDataSet cdSet;
    for (int i = 0; i < types.length; i++) {
        target = new TargetInfo();
        target.type = types[i];
        target.isContextDataHolder = ContextDataHolder.class.isAssignableFrom(target.type);

        cd = ConvertableAnnotationRetriever.retrieveAnnotation(ContextData.class, annotations[i]);
        cdSet = ConvertableAnnotationRetriever.retrieveAnnotation(ContextDataSet.class,
                target.type.getAnnotations());
        target.name = cd == null ? "" : cd.name();
        target.scope = cd == null ? "" : cd.scope();
        target.typeUnMatch = cd == null ? TypeUnMacthPolicy.EXCEPTION : cd.typeUnMatch();
        if (StringUtils.isEmpty(target.name)) {
            target.name = parameterNames[i];
        }

        if (cdSet == null) {
            target.contextDataSetFactory = null;
        } else {
            target.contextDataSetFactory = cdSet.factory().newInstance();
            target.isContextDataSetSingletonInContext = cdSet.singletonInContext();
        }

        target.fixForPrimitiveType();
        targetList.add(target);
    }
    return targetList;
}

From source file:com.taobao.rpc.doclet.RPCAPIInfoHelper.java

public static RPCAPIInfo getAPIInfo(String realPath, Method method) {
    RPCAPIInfo apiInfo = new RPCAPIInfo();

    ClassInfo classInfo = RPCAPIDocletUtil.getClassInfo(method.getDeclaringClass().getName());
    MethodInfo methodInfo = null;/*from w  ww  .jav a2 s .  com*/

    if (classInfo != null) {
        methodInfo = classInfo.getMethodInfo(method.getName());
    }

    if (methodInfo != null) {

        apiInfo.setComment(methodInfo.getComment());
    }

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

    Security securityAnnotation = method.getAnnotation(Security.class);
    ResourceMapping resourceMapping = method.getAnnotation(ResourceMapping.class);

    Object returnType = null;

    returnType = buildTypeStructure(method.getReturnType(), method.getGenericReturnType(), null);

    boolean checkCSRF = false;
    if (securityAnnotation != null) {
        checkCSRF = securityAnnotation.checkCSRF();
    }

    apiInfo.setPattern(realPath.replaceAll("///", "/"));
    apiInfo.setCheckCSRF(checkCSRF);
    apiInfo.setResponseMimes(StringUtils.arrayToDelimitedString(resourceMapping.produces(), ","));
    apiInfo.setHttpMethod(StringUtils.arrayToDelimitedString(resourceMapping.method(), ","));

    List<RPCAPIInfo.Parameter> parameters = new ArrayList<RPCAPIInfo.Parameter>();

    RPCAPIInfo.Parameter parameter = null;
    LocalVariableTableParameterNameDiscoverer nameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
    String[] paramNames = nameDiscoverer.getParameterNames(method);
    int i = 0;

    Class<?>[] parameterTypes = method.getParameterTypes();

    Type[] genericParameterTypes = method.getGenericParameterTypes();

    Class<?> paramType = null;

    Type genericType;

    for (int k = 0; k < parameterTypes.length; k++) {

        paramType = parameterTypes[k];
        genericType = genericParameterTypes[k];

        Annotation[] pAnnotations = parameterAnnotations[i];

        if (HttpServletRequest.class.isAssignableFrom(paramType)
                || HttpServletResponse.class.isAssignableFrom(paramType)
                || ErrorContext.class.isAssignableFrom(paramType)) {
            continue;
        } // end if

        String realParamName = paramNames[k];

        if (pAnnotations.length == 0) {

            parameter = apiInfo.new Parameter();
            parameter.setName(realParamName);
            parameter.setType(buildTypeStructure(paramType, genericType, null));
            parameters.add(parameter);

            setParameterComment(parameter, realParamName, methodInfo);

            continue;
        } // end if

        for (Annotation annotation : pAnnotations) {
            parameter = apiInfo.new Parameter();
            setParameterComment(parameter, realParamName, methodInfo);

            if (annotation instanceof RequestParam) {
                RequestParam requestParam = (RequestParam) annotation;
                parameter.setName(requestParam.name());
                parameter.setType(buildTypeStructure(paramType, genericType, null));
                if (!StringUtil.isBlank(requestParam.defaultValue())) {
                    parameter.setDefaultValue(requestParam.defaultValue().trim());
                }
                parameters.add(parameter);
            } else if (annotation instanceof PathParam) {
                PathParam pathParam = (PathParam) annotation;
                parameter.setName(pathParam.name());
                parameter.setType(buildTypeStructure(paramType, genericType, null));
                if (!StringUtil.isBlank(pathParam.defaultValue())) {
                    parameter.setDefaultValue(pathParam.defaultValue().trim());
                }
                parameters.add(parameter);
            } else if (annotation instanceof JsonParam) {
                JsonParam pathParam = (JsonParam) annotation;
                parameter.setName(pathParam.value());
                parameter.setType(buildTypeStructure(paramType, genericType, null));
                parameters.add(parameter);
            } else if (annotation instanceof RequestParams) {
                parameter.setName(realParamName);
                parameter.setType(buildTypeStructure(paramType, genericType, null));
                parameters.add(parameter);
            } else if (annotation instanceof File) {
                File file = (File) annotation;
                parameter.setName(file.value());
                parameter.setType("");
                parameters.add(parameter);
            } // end if
        } // end for
        i++;
    } // end for
    apiInfo.setParmeters(parameters);
    apiInfo.setReturnType(returnType);
    return apiInfo;
}

From source file:ca.uhn.fhir.rest.method.MethodUtil.java

public static Integer findParamAnnotationIndex(Method theMethod, Class<?> toFind) {
    int paramIndex = 0;
    for (Annotation[] annotations : theMethod.getParameterAnnotations()) {
        for (int annotationIndex = 0; annotationIndex < annotations.length; annotationIndex++) {
            Annotation nextAnnotation = annotations[annotationIndex];
            Class<? extends Annotation> class1 = nextAnnotation.getClass();
            if (toFind.isAssignableFrom(class1)) {
                return paramIndex;
            }//from   www.j  a v a 2s  .  c o  m
        }
        paramIndex++;
    }
    return null;
}

From source file:com.sitewhere.web.rest.documentation.RestDocumentationGenerator.java

/**
 * Parse method parameters./*from w ww  .j a  va  2  s.  c o m*/
 * 
 * @param method
 * @return
 * @throws SiteWhereException
 */
protected static List<ParsedParameter> parseParameters(Method method) throws SiteWhereException {
    List<ParsedParameter> parsed = new ArrayList<ParsedParameter>();

    Paranamer paranamer = new BytecodeReadingParanamer();
    String[] paramNames = paranamer.lookupParameterNames(method);

    if (paramNames.length > 0) {
        int i = 0;
        for (Annotation[] annotations : method.getParameterAnnotations()) {
            RequestParam request = null;
            PathVariable path = null;
            ApiParam api = null;
            Concerns concerns = null;
            for (Annotation annotation : annotations) {
                if (annotation instanceof RequestParam) {
                    request = (RequestParam) annotation;
                } else if (annotation instanceof PathVariable) {
                    path = (PathVariable) annotation;
                } else if (annotation instanceof ApiParam) {
                    api = (ApiParam) annotation;
                } else if (annotation instanceof Concerns) {
                    concerns = (Concerns) annotation;
                }
            }
            if (request != null) {
                ParsedParameter param = new ParsedParameter();
                param.setType(ParameterType.Request);
                param.setName(paramNames[i]);
                param.setRequired(request.required());
                if (api != null) {
                    param.setRequired(api.required());
                    param.setDescription(api.value());
                }
                if (concerns != null) {
                    param.getConcerns().addAll(Arrays.asList(concerns.values()));
                }
                parsed.add(param);
            } else if (path != null) {
                ParsedParameter param = new ParsedParameter();
                param.setType(ParameterType.Path);
                param.setName(paramNames[i]);
                param.setRequired(true);
                if (api != null) {
                    param.setDescription(api.value());
                }
                if (concerns != null) {
                    param.getConcerns().addAll(Arrays.asList(concerns.values()));
                }
                parsed.add(param);
            }
            i++;
        }
    }

    return parsed;
}

From source file:org.firesoa.common.util.JSR181ParameterNameDiscover.java

public String[] getParameterNames(Method method) {
    Annotation[][] annotations = method.getParameterAnnotations();
    int paramNum = annotations.length;
    String[] paramNames = new String[paramNum];
    if (paramNum == 0)
        return paramNames;
    for (int i = 0; i < paramNum; i++) {
        Annotation[] annotationArr = annotations[i];
        if (annotationArr.length == 0) {

            //WebParam ????WebParam?WebParam???
            paramNames[i] = null;//from   ww  w  . jav a  2  s . co  m
            continue;
        }
        javax.jws.WebParam obj = (javax.jws.WebParam) this.findWebParam(annotationArr);
        if (obj == null) {
            paramNames[i] = null;
            continue;
        }
        String name = obj.name();
        if (StringUtils.isEmpty(name)) {
            paramNames[i] = null;
            continue;
        }
        paramNames[i] = name;
    }

    return paramNames;
}