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:org.mule.module.extension.internal.introspection.MuleExtensionAnnotationParser.java

static List<ParameterDescriptor> parseParameters(Method method) {
    String[] paramNames = getParamNames(method);

    if (ArrayUtils.isEmpty(paramNames)) {
        return ImmutableList.of();
    }/*from   w  w  w  .j  a v  a 2s .c o m*/

    DataType[] parameterTypes = IntrospectionUtils.getMethodArgumentTypes(method);
    Annotation[][] parameterAnnotations = method.getParameterAnnotations();

    List<ParameterDescriptor> parameterDescriptors = new LinkedList<>();

    for (int i = 0; i < paramNames.length; i++) {
        Map<Class<? extends Annotation>, Annotation> annotations = toMap(parameterAnnotations[i]);

        if (annotations.containsKey(org.mule.extension.annotations.ParameterGroup.class)) {
            parseGroupParameters(parameterTypes[i], parameterDescriptors);
        } else {
            ParameterDescriptor parameterDescriptor = doParseParameter(paramNames[i], parameterTypes[i],
                    annotations);
            if (parameterDescriptor != null) {
                parameterDescriptors.add(parameterDescriptor);
            }
        }
    }

    return parameterDescriptors;
}

From source file:ch.digitalfondue.npjt.QueryType.java

private static SqlParameterSource extractParameters(Method m, Object[] args,
        Collection<ParameterConverter> parameterConverters) {

    Annotation[][] parameterAnnotations = m.getParameterAnnotations();
    if (parameterAnnotations == null || parameterAnnotations.length == 0) {
        return new EmptySqlParameterSource();
    }/*  ww w.  j  av a  2s .c  o  m*/

    MapSqlParameterSource ps = new MapSqlParameterSource();
    Class<?>[] parameterTypes = m.getParameterTypes();
    for (int i = 0; i < args.length; i++) {
        String name = parameterName(parameterAnnotations[i]);
        if (name != null) {
            Object arg = args[i];
            Class<?> parameterType = parameterTypes[i];

            boolean hasAccepted = false;
            for (ParameterConverter parameterConverter : parameterConverters) {
                if (parameterConverter.accept(parameterType)) {
                    hasAccepted = true;
                    parameterConverter.processParameter(name, arg, parameterType, ps);
                    break;
                }
            }

            if (!hasAccepted) {
                throw new IllegalStateException(
                        "Was not able to find a ParameterConverter able to process object: " + arg
                                + " with class " + parameterType);
            }
        }
    }

    return ps;
}

From source file:com.vityuk.ginger.DefaultLocalization.java

private static int indexOfParameterAnnotation(Method method, Class<? extends Annotation> annotationType) {
    Annotation[][] parametersAnnotations = method.getParameterAnnotations();
    for (int i = 0, n = parametersAnnotations.length; i < n; i++) {
        Annotation[] parameterAnnotations = parametersAnnotations[i];
        for (Annotation parameterAnnotation : parameterAnnotations) {
            if (parameterAnnotation.annotationType().equals(annotationType)) {
                return i;
            }// w  w  w.j a v  a  2s  .com
        }
    }
    return -1;
}

From source file:com.wavemaker.runtime.server.json.JSONUtils.java

public static List<FieldDefinition> getParameterTypes(Method m, JSONArray params, TypeState typeState) {

    List<FieldDefinition> fieldDefinitions = new ArrayList<FieldDefinition>();
    for (Type type : m.getGenericParameterTypes()) {
        fieldDefinitions.add(ReflectTypeUtils.getFieldDefinition(type, typeState, false, null));
    }//  w w w .  j a v  a 2  s  .co m

    Annotation[][] paramAnnotations = m.getParameterAnnotations();

    for (int i = 0; i < paramAnnotations.length; i++) {
        for (Annotation ann : paramAnnotations[i]) {
            if (ann instanceof JSONParameterTypeField) {

                JSONParameterTypeField paramTypeField = (JSONParameterTypeField) ann;
                int pos = paramTypeField.typeParameter();
                String typeString = (String) params.get(pos);

                try {
                    Class<?> newType = org.springframework.util.ClassUtils.forName(typeString);

                    if (Collection.class.isAssignableFrom(newType)) {
                        throw new WMRuntimeException(MessageResource.JSONUTILS_PARAMTYPEGENERIC, i,
                                m.getName());
                    }

                    fieldDefinitions.set(i,
                            ReflectTypeUtils.getFieldDefinition(newType, typeState, false, null));
                } catch (ClassNotFoundException e) {
                    throw new WMRuntimeException(MessageResource.JSONPARAMETER_COULD_NOTLLOAD_TYPE, e,
                            typeString, m.getName(), i);
                } catch (LinkageError e) {
                    throw new WMRuntimeException(e);
                }
            }
        }
    }

    return fieldDefinitions;
}

From source file:org.dcm4che3.conf.core.util.ConfigIterators.java

private static List<AnnotatedSetter> processAnnotatedSetters(Class clazz) {
    List<AnnotatedSetter> list;
    list = new ArrayList<AnnotatedSetter>();

    // scan all methods including superclasses, assume each is a config-setter
    for (Method m : clazz.getMethods()) {
        AnnotatedSetter annotatedSetter = new AnnotatedSetter();
        annotatedSetter.setParameters(new ArrayList<AnnotatedConfigurableProperty>());

        Annotation[][] parameterAnnotations = m.getParameterAnnotations();
        Type[] genericParameterTypes = m.getGenericParameterTypes();

        // if method is no-arg, then it is not a setter
        boolean thisMethodIsNotASetter = true;

        for (int i = 0; i < parameterAnnotations.length; i++) {

            thisMethodIsNotASetter = false;

            AnnotatedConfigurableProperty property = new AnnotatedConfigurableProperty();
            property.setAnnotations(annotationsArrayToMap(parameterAnnotations[i]));
            property.setType(genericParameterTypes[i]);

            annotatedSetter.getParameters().add(property);

            // make sure all the parameters of this setter-wannabe are annotated
            if (property.getAnnotation(ConfigurableProperty.class) == null) {
                thisMethodIsNotASetter = true;
                break;
            }/*from  ww w.j  ava2 s .co m*/
        }

        // filter out non-setters
        if (thisMethodIsNotASetter)
            continue;

        list.add(annotatedSetter);
        annotatedSetter.setAnnotations(annotationsArrayToMap(m.getAnnotations()));
        annotatedSetter.setMethod(m);
    }

    configurableSettersCache.put(clazz, list);
    return list;
}

From source file:org.cleandroid.core.di.DependencyManager.java

public static Object[] getMethodDependencies(Method m, Object context, Object... extras) {

    if (methodParamsMap.containsKey(m))
        return methodParamsMap.get(m);

    Class<?>[] paramsTypes = m.getParameterTypes();
    Annotation[][] paramsAnnotations = m.getParameterAnnotations();
    Object[] params = new Object[paramsTypes.length];

    for (int i = 0; i < paramsTypes.length; i++) {
        Class<?> cl = paramsTypes[i];
        Annotation[] annotations = paramsAnnotations[i];

        for (Object extra : extras) {
            if (annotations.length > i) {
                if (annotations[i].equals(CView.class))
                    continue;
            }/*from w w w  .j a v a2 s.com*/
            if (cl.isInstance(extra)) {
                params[i] = extra;
                break;
            }
        }

        for (Annotation a : annotations) {
            Object dep = getDependencyFromAnnotation(a, context, cl);
            if (dep != null)
                params[i] = dep;
        }

    }
    methodParamsMap.put(m, params);
    return params;
}

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}
 *//*  w w  w.j a  v  a  2s  .c o m*/
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:net.ymate.platform.validation.Validates.java

/**
 * @param targetMethod ?//  ww w . ja  v a  2  s.  c  om
 * @param fieldNames ????
 * @return ??
 */
public static PairObject<Validation, Map<String, ValidateRule[]>> loadValidateRule(Method targetMethod,
        String[] fieldNames) {
    Map<String, ValidateRule[]> _returnValue = null;
    Validation _validation = targetMethod.getAnnotation(Validation.class);
    if (_validation != null) {
        _returnValue = new HashMap<String, ValidateRule[]>();
        Annotation[][] _paramAnnotations = targetMethod.getParameterAnnotations();
        for (int _idx = 0; _idx < targetMethod.getParameterTypes().length; _idx++) {
            Annotation[] _annotations = _paramAnnotations[_idx];
            for (Annotation _annotation : _annotations) {
                if (_annotation instanceof Validate) {
                    Validate _validate = (Validate) _annotation;
                    if (_validate.isModel()) {
                        Map<String, ValidateRule[]> _modelVMap = loadValidateRule(
                                targetMethod.getParameterTypes()[_idx]).getValue();
                        if (_modelVMap != null) {
                            _returnValue.putAll(_modelVMap);
                        }
                    } else {
                        _returnValue.put(StringUtils.defaultIfEmpty(_validate.name(), fieldNames[_idx]),
                                _validate.value());
                    }
                    break;
                }
            }
        }
    }
    return new PairObject<Validation, Map<String, ValidateRule[]>>(_validation, _returnValue);
}

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

/**
 * Try to determine parameter names for a given method. This will check {@link ParamName} attributes and debugging
 * symbols; if no name can be found, a default "arg-&lt;position>" name will be used.
 * // w w  w.ja  v  a  2  s. c  o  m
 * This will also continue working of method has been loaded by a non-default classloader.
 * 
 * @param method The method to introspect.
 * @return The names of the parameters in an ordered list.
 */
public static List<String> getParameterNames(Method method) {

    int numParams = method.getParameterTypes().length;
    List<String> ret = new ArrayList<String>(numParams);
    Annotation[][] paramAnnotations = method.getParameterAnnotations();
    Class<?> paramNameClass = ClassLoaderUtils.loadClass(ParamName.class.getName(),
            method.getDeclaringClass().getClassLoader());

    String[] methodParameterNames;

    try {
        AdaptiveParanamer ap = new AdaptiveParanamer();
        methodParameterNames = ap.lookupParameterNames(method);
        ap = null;
    } catch (ParameterNamesNotFoundException e) {
        logger.info("No parameter names found for method " + method.getName());
        methodParameterNames = new String[numParams];
    }

    for (int i = 0; i < numParams; i++) {
        String paramName = null;

        if (paramName == null) {
            for (Annotation ann : paramAnnotations[i]) {
                if (paramNameClass.isAssignableFrom(ann.annotationType())) {
                    try {
                        Method nameMethod = paramNameClass.getMethod("name");
                        paramName = (String) nameMethod.invoke(ann);
                    } catch (SecurityException e) {
                        throw new WMRuntimeException(e);
                    } catch (NoSuchMethodException e) {
                        throw new WMRuntimeException(e);
                    } catch (IllegalAccessException e) {
                        throw new WMRuntimeException(e);
                    } catch (InvocationTargetException e) {
                        throw new WMRuntimeException(e);
                    }

                    break;
                }
            }
        }

        if (paramName == null && methodParameterNames != null) {
            paramName = methodParameterNames[i];
        }

        if (paramName == null) {
            logger.warn("no parameter name information for parameter " + i + ", method: " + method.getName());
            paramName = "arg-" + (i + 1);
        }

        ret.add(paramName);
    }

    return ret;
}

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

/**
 * Finds all parameter annotations of a method including its ancestors.
 *
 * @param method from which the search starts.
 * @return a set of methods grabed from parent classes and interfaces.
 *//*from  ww w .j ava2  s. c o  m*/
public static Set<Annotation[][]> allParametersAnnotationsFromAncestors(final Method method) {

    Set<Method> methodsFromAncestors = methodsFromAncestors(method);

    Set<Annotation[][]> parametersAnnotations = new HashSet<Annotation[][]>();

    for (Method m : methodsFromAncestors) {
        Annotation[][] as = m.getParameterAnnotations();
        if (as != null) {
            parametersAnnotations.add(as);
        }
    }

    return parametersAnnotations;
}