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:no.met.jtimeseries.service.ServiceDescriptionGenerator.java

/**
 * @param method The method to get information about.
 * @return Information about all the parameters to web service call to the method.
 *//*  ww  w.  j a v  a 2  s  .  c o  m*/
private static List<ParameterInfo> getParameters(Method method) {

    List<ParameterInfo> parameters = new ArrayList<ParameterInfo>();
    Annotation[][] paramAnnotations = method.getParameterAnnotations();
    for (Annotation[] annotationForParam : paramAnnotations) {

        ParameterInfo pi = new ParameterInfo();
        for (Annotation a : annotationForParam) {
            if (a instanceof QueryParam) {
                pi.name = ((QueryParam) a).value();
            } else if (a instanceof DefaultValue) {
                pi.defaultValue = ((DefaultValue) a).value();
            }
        }

        if (pi.name != null) {
            parameters.add(pi);
        }
    }

    return parameters;
}

From source file:com.espertech.esper.metrics.jmx.CommonJMXUtil.java

private static MBeanParameterInfo[] extractParameterInfo(Method m) {
    Class<?>[] types = m.getParameterTypes();
    Annotation[][] annotations = m.getParameterAnnotations();
    MBeanParameterInfo[] params = new MBeanParameterInfo[types.length];
    for (int i = 0; i < params.length; i++) {
        boolean hasAnnotation = false;
        for (int j = 0; j < annotations[i].length; j++) {
            if (annotations[i][j] instanceof JmxParam) {
                JmxParam param = (JmxParam) annotations[i][j];
                params[i] = new MBeanParameterInfo(param.name(), types[i].getName(), param.description());
                hasAnnotation = true;//from  w w w.ja  v a2s .  c  o m
                break;
            }
        }
        if (!hasAnnotation) {
            params[i] = new MBeanParameterInfo("", types[i].getName(), "");
        }
    }

    return params;
}

From source file:org.mule.config.expression.ExpressionAnnotationsHelper.java

public static ExpressionTransformer getTransformerForMethodWithAnnotations(Method method, MuleContext context)
        throws TransformerException, InitialisationException {
    ExpressionTransformer trans = new ExpressionTransformer();
    trans.setMuleContext(context);//from w  w w .  j  a  v a2  s  . c  o  m

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

    for (int i = 0; i < annotations.length; i++) {
        Annotation[] annotation = annotations[i];
        for (int j = 0; j < annotation.length; j++) {
            Annotation ann = annotation[j];
            ExpressionArgument arg = parseAnnotation(ann, method.getParameterTypes()[i], context);

            if (arg != null) {
                trans.addArgument(arg);
            }
        }
    }
    trans.initialise();
    return trans;
}

From source file:org.springframework.data.gemfire.function.GemfireFunctionUtils.java

/**
 * Determine the order position of a an annotated method parameter
 *
 * @param method the {@link Method} instance
 * @param targetAnnotationType the annotation
 * @param requiredTypes an array of valid parameter types for the annotation
 * @return the parameter position or -1 if the annotated parameter is not found
 *//* w ww .  j  a  v a 2  s.co m*/
public static int getAnnotationParameterPosition(Method method, Class<?> targetAnnotationType,
        Class<?>[] requiredTypes) {

    int position = -1;

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

    if (parameterAnnotations.length > 0) {
        Class<?>[] parameterTypes = method.getParameterTypes();

        List<Class<?>> requiredTypesList = Arrays.asList(requiredTypes);

        for (int index = 0; index < parameterAnnotations.length; index++) {
            Annotation[] annotations = parameterAnnotations[index];

            if (annotations.length > 0) {
                for (Annotation annotation : annotations) {
                    if (annotation.annotationType().equals(targetAnnotationType)) {
                        Assert.state(position < 0, String.format(
                                "Method %s signature cannot contain more than one parameter annotated with type %s",
                                method.getName(), targetAnnotationType.getName()));

                        boolean isRequiredType = false;

                        for (Class<?> requiredType : requiredTypesList) {
                            if (requiredType.isAssignableFrom(parameterTypes[index])) {
                                isRequiredType = true;
                                break;
                            }
                        }

                        Assert.isTrue(isRequiredType, String.format(
                                "Parameter of type %s annotated with %s must be assignable from one of type %s in method %s",
                                parameterTypes[index], targetAnnotationType.getName(),
                                StringUtils.arrayToCommaDelimitedString(requiredTypes), method.getName()));

                        position = index;
                    }
                }
            }
        }
    }

    return position;
}

From source file:io.swagger.jaxrs.ParameterExtractor.java

public static List<Parameter> getParameters(Swagger swagger, Class cls, Method method) {
    List<Parameter> parameters = new ArrayList<>();
    Type[] genericParameterTypes = method.getGenericParameterTypes();
    Annotation[][] paramAnnotations = method.getParameterAnnotations();
    for (int i = 0; i < genericParameterTypes.length; i++) {
        Type type = null;/*from   ww w  .  j a v a 2  s.c  o  m*/
        if (SynapseEndpointServiceMarker.class.isAssignableFrom(cls)) {
            //an RPC style endpoint (implements SynapseEndpointServiceMarker)
            type = TypeFactory.defaultInstance().constructType(ParameterWrapper.class, cls);
        } else if (cls.getAnnotation(Service.class) != null) {
            //an endpoint with @Service annotation
            if (genericParameterTypes[i] instanceof ParameterizedType && Request.class
                    .isAssignableFrom((Class<?>) ((ParameterizedType) genericParameterTypes[i]).getRawType())) {
                //if Request or Event Object
                type = TypeFactory.defaultInstance().constructType(
                        ((ParameterizedType) genericParameterTypes[i]).getActualTypeArguments()[0], cls);
            } else {
                type = TypeFactory.defaultInstance().constructType(genericParameterTypes[i], cls);
            }

        }

        extractParameters(swagger, parameters, type, Arrays.asList(paramAnnotations[i]));
    }
    return parameters;
}

From source file:org.movealong.junitfu.MethodMockBinder.java

private static Collection<? extends MockBinder> createMockBinders(Method method) {
    if (method.getAnnotation(Inject.class) != null) {
        HashSet<MockBinder> mockBinders = new HashSet<MockBinder>();
        for (int parameterIndex = 0; parameterIndex < method.getParameterTypes().length; parameterIndex++) {
            for (Annotation annotation : method.getParameterAnnotations()[parameterIndex]) {
                if (annotation.annotationType() == Mock.class) {
                    Class<?> mockClass = method.getParameterTypes()[parameterIndex];
                    String mockName = defaultIfEmpty(((Mock) annotation).value(),
                            uncapitalize(mockClass.getSimpleName()));
                    mockBinders.add(new SimpleMockBinder(mockClass, mockName));
                }// www .  ja  v a  2 s  .c  o m
            }
        }
        return mockBinders;
    } else {
        return Collections.emptyList();
    }
}

From source file:com.reactivetechnologies.platform.rest.WebbitRestServerBean.java

/**
 * /*w  w w  .ja va 2 s  . co  m*/
 * @param method
 * @return
 */
private static MethodDetail createMethodDetail(Method method) {
    MethodDetail m = new MethodDetail(method);
    Annotation[][] mAnnots = method.getParameterAnnotations();
    int i = 0;
    for (Annotation[] annots : mAnnots) {
        for (Annotation annot : annots) {
            if (annot.annotationType() == QueryParam.class) {
                m.setQueryParam(i, ((QueryParam) annot).value());
            }
            if (annot.annotationType() == PathParam.class) {
                m.setPathParam(i, ((PathParam) annot).value());
            }
        }
        i++;
    }
    return m;
}

From source file:com.sf.ddao.orm.RSMapperFactoryRegistry.java

public static int findParameter(Method method) {
    final Annotation[][] parameterAnnotations = method.getParameterAnnotations();
    for (int i = 0, parameterAnnotationsLength = parameterAnnotations.length; i < parameterAnnotationsLength; i++) {
        Annotation[] annotations = parameterAnnotations[i];
        for (Annotation annotation : annotations) {
            if (annotation.annotationType().isAssignableFrom(UseRSMapper.class)) {
                return i;
            }/*from   w ww.  ja  v a  2 s . c  o m*/
        }
    }
    return -1;
}

From source file:jp.go.nict.langrid.client.soap.io.SoapRequestWriter.java

public static void writeSoapRequest(OutputStream os, String namespace, Iterable<RpcHeader> headers,
        Method method, Object... args) throws IOException, IllegalAccessException, InvocationTargetException {
    Service sa = method.getDeclaringClass().getAnnotation(Service.class);
    Map<String, Object> bindings = new HashMap<String, Object>();
    Class<?>[] paramTypes = method.getParameterTypes();
    Annotation[][] paramAnnots = method.getParameterAnnotations();
    List<Encoder> params = new ArrayList<Encoder>();
    int n = paramTypes.length;
    for (int i = 0; i < n; i++) {
        String name = EncoderUtil.getParameterName(paramAnnots[i]);
        if (name == null)
            name = "in" + i;
        params.add(Encoders.create(3, name, paramTypes[i], args[i]));
    }/*from   w  ww. ja va  2 s  . c  om*/
    String ns = namespace;
    if (ns == null) {
        if (sa != null && sa.namespace().length() > 0) {
            ns = sa.namespace();
        } else {
            String sn = method.getDeclaringClass().getSimpleName();
            if (sn.endsWith("Service")) {
                sn = sn.substring(0, sn.length() - 7);
            }
            ns = "servicegrid:servicetype:nict.nlp:" + sn;
        }
    }
    bindings.put("namespace", ns);
    bindings.put("headers", escapeValueXml(headers));
    bindings.put("methodName", method.getName());
    bindings.put("parameters", params);

    StreamUtil.writeString(os, jmte.transform(template, bindings), "UTF-8");
}

From source file:org.silverpeas.core.util.annotation.AnnotationUtil.java

/**
 * Provides a centralized way to extract annotated method parameter values.
 * @param method the intercepted method that will be invoked.
 * @return the map with keys of Annotation class and values of object list.
 * @throws Exception//from w  ww  .j  a v a 2 s  .  c o m
 */
@SuppressWarnings("unchecked")
private static Map<Class<Annotation>, List<Object>> extractMethodAnnotatedParameterValues(Method method,
        Object[] parameterValues) throws Exception {

    // Initializing the results
    Map<Class<? extends Annotation>, List<Object>> results = new LinkedHashMap<>();

    Annotation[][] annotations = method.getParameterAnnotations();
    for (int i = 0; i < annotations.length; i++) {
        Annotation[] parameterAnnotations = annotations[i];
        if (parameterAnnotations.length > 0) {
            for (Annotation parameterAnnotation : parameterAnnotations) {
                Object parameterValue = parameterValues[i];
                if (parameterValue != null) {

                    if (parameterAnnotation.annotationType().isAssignableFrom(SourcePK.class)
                            || parameterAnnotation.annotationType().isAssignableFrom(TargetPK.class)) {

                        if (parameterValue instanceof Collection) {
                            for (Object value : ((Collection) parameterValue)) {
                                addPKParameterValue(results, parameterAnnotation.annotationType(), value);
                            }
                        } else {
                            addPKParameterValue(results, parameterAnnotation.annotationType(), parameterValue);
                        }

                    } else if (parameterAnnotation.annotationType().isAssignableFrom(Language.class)) {

                        String language = null;
                        if (parameterValue instanceof String) {
                            language = (String) parameterValue;
                        } else if (parameterValue instanceof Locale) {
                            language = ((Locale) parameterValue).getLanguage();
                        }
                        MapUtil.putAddList(results, parameterAnnotation.annotationType(), language);

                    } else {
                        MapUtil.putAddList(results, parameterAnnotation.annotationType(), parameterValue);
                    }
                }
            }
        }
    }

    return (Map) results;
}