Example usage for java.lang.reflect Parameter getAnnotations

List of usage examples for java.lang.reflect Parameter getAnnotations

Introduction

In this page you can find the example usage for java.lang.reflect Parameter getAnnotations.

Prototype

public Annotation[] getAnnotations() 

Source Link

Usage

From source file:com.adaptc.mws.plugins.testing.transformations.TestMixinTransformation.java

private static Parameter[] copyParameters(Parameter[] parameterTypes) {
    Parameter[] newParameterTypes = new Parameter[parameterTypes.length];
    for (int i = 0; i < parameterTypes.length; i++) {
        Parameter parameterType = parameterTypes[i];
        Parameter newParameter = new Parameter(nonGeneric(parameterType.getType()), parameterType.getName(),
                parameterType.getInitialExpression());
        newParameter.addAnnotations(parameterType.getAnnotations());
        newParameterTypes[i] = newParameter;
    }/*from  w w  w . ja v  a  2  s  .  c o m*/
    return newParameterTypes;
}

From source file:ca.oson.json.util.ObjectUtil.java

public static String[] getParameterNames(Parameter[] parameters) {
    int length = parameters.length;
    String[] parameterNames = new String[length];

    for (int i = 0; i < length; i++) {
        Parameter parameter = parameters[i];

        String parameterName = null;
        for (Annotation annotation : parameter.getAnnotations()) { //getDeclaredAnnotations
            String name = getName(annotation);
            if (name != null) {
                parameterName = name;/*from www .  j  a  v  a2 s .c o m*/
                if (annotation instanceof ca.oson.json.annotation.FieldMapper) {
                    break;
                }
            }
        }

        if (parameterName == null) {
            parameterName = parameter.getName();
        }

        parameterNames[i] = parameterName;
    }

    return parameterNames;
}

From source file:com.teradata.tempto.internal.ReflectionInjectorHelper.java

private Object createInstanceWithFields(Parameter[] parameters) {
    DynamicType.Builder<Object> objectBuilder = new ByteBuddy().subclass(Object.class).modifiers(PUBLIC);
    for (Parameter parameter : parameters) {
        objectBuilder = objectBuilder.defineField(parameter.getName(), parameter.getType(), PUBLIC)
                .annotateField(ArrayUtils.add(parameter.getAnnotations(), INJECT_ANNOTATION));
    }/* ww w . j  ava  2  s . c o  m*/
    try {
        Class<?> createdClass = objectBuilder.make()
                .load(getSystemClassLoader(), ClassLoadingStrategy.Default.INJECTION).getLoaded();
        return createdClass.getConstructor().newInstance();
    } catch (ReflectiveOperationException e) {
        throw new RuntimeException(e);
    }
}

From source file:nl.kpmg.lcm.server.documentation.MethodDocumentator.java

private String addParameters(Method method, String description) {
    String parameters = "";
    int counter = 1;
    for (Parameter param : method.getParameters()) {
        String parameterString = StringUtils.replace(parameterTemplate, "{NUMBER}", Integer.toString(counter));
        String annotationString = "";
        for (Annotation annotation : param.getAnnotations()) {
            annotationString += annotation.annotationType().getSimpleName();
        }// www .j  a  v a2  s. c  om

        if (annotationString.length() > 0) {
            annotationString = annotationString + ", ";
            parameterString = StringUtils.replace(parameterString, "{ANNOTATION}", annotationString);
        } else {
            parameterString = StringUtils.replace(parameterString, "{ANNOTATION}", "Payload, ");
        }

        parameterString = StringUtils.replace(parameterString, "{TYPE}",
                "type: " + param.getType().getSimpleName());
        parameters += parameterString;
        counter++;
    }
    if (parameters.length() > 0) {
        parameters = "<br> " + parameters;
    } else {
        parameters = "none.";
    }
    description = StringUtils.replace(description, "{PARAMETERS}", parameters);
    return description;
}

From source file:io.stallion.restfulEndpoints.ResourceToEndpoints.java

public List<JavaRestEndpoint> convert(EndpointResource resource) {
    Class cls = resource.getClass();
    List<JavaRestEndpoint> endpoints = new ArrayList<>();

    // Get defaults from the resource

    Role defaultMinRole = Settings.instance().getUsers().getDefaultEndpointRoleObj();
    MinRole minRoleAnno = (MinRole) cls.getAnnotation(MinRole.class);
    if (minRoleAnno != null) {
        defaultMinRole = minRoleAnno.value();
    }//from   w ww . j a  v  a2s  .co  m

    String defaultProduces = "text/html";
    Produces producesAnno = (Produces) cls.getAnnotation(Produces.class);
    if (producesAnno != null && producesAnno.value().length > 0) {
        defaultProduces = producesAnno.value()[0];
    }

    Path pathAnno = (Path) cls.getAnnotation(Path.class);
    if (pathAnno != null) {
        basePath += pathAnno.value();
    }

    Class defaultJsonViewClass = null;
    DefaultJsonView jsonView = (DefaultJsonView) cls.getAnnotation(DefaultJsonView.class);
    if (jsonView != null) {
        defaultJsonViewClass = jsonView.value();
    }

    for (Method method : cls.getDeclaredMethods()) {
        JavaRestEndpoint endpoint = new JavaRestEndpoint();
        endpoint.setRole(defaultMinRole);
        endpoint.setProduces(defaultProduces);
        if (defaultJsonViewClass != null) {
            endpoint.setJsonViewClass(defaultJsonViewClass);
        }

        Log.finer("Resource class method: {0}", method.getName());
        for (Annotation anno : method.getDeclaredAnnotations()) {
            if (Path.class.isInstance(anno)) {
                Path pth = (Path) anno;
                endpoint.setRoute(getBasePath() + pth.value());
            } else if (GET.class.isInstance(anno)) {
                endpoint.setMethod("GET");
            } else if (POST.class.isInstance(anno)) {
                endpoint.setMethod("POST");
            } else if (DELETE.class.isInstance(anno)) {
                endpoint.setMethod("DELETE");
            } else if (PUT.class.isInstance(anno)) {
                endpoint.setMethod("PUT");
            } else if (Produces.class.isInstance(anno)) {
                endpoint.setProduces(((Produces) anno).value()[0]);
            } else if (MinRole.class.isInstance(anno)) {
                endpoint.setRole(((MinRole) anno).value());
            } else if (XSRF.class.isInstance(anno)) {
                endpoint.setCheckXSRF(((XSRF) anno).value());
            } else if (JsonView.class.isInstance(anno)) {
                Class[] classes = ((JsonView) anno).value();
                if (classes == null || classes.length != 1) {
                    throw new UsageException("JsonView annotation for method " + method.getName()
                            + " must have exactly one view class");
                }
                endpoint.setJsonViewClass(classes[0]);
            }
        }
        if (!empty(endpoint.getMethod()) && !empty(endpoint.getRoute())) {
            endpoint.setJavaMethod(method);
            endpoint.setResource(resource);
            endpoints.add(endpoint);
            Log.fine("Register endpoint {0} {1}", endpoint.getMethod(), endpoint.getRoute());
        } else {
            continue;
        }
        int x = -1;
        for (Parameter param : method.getParameters()) {
            x++;
            RequestArg arg = new RequestArg();
            for (Annotation anno : param.getAnnotations()) {
                arg.setAnnotationInstance(anno);
                Log.finer("Param Annotation is: {0}, {1}", anno, anno.getClass().getName());
                if (BodyParam.class.isInstance(anno)) {
                    BodyParam bodyAnno = (BodyParam) (anno);
                    arg.setType("BodyParam");
                    if (empty(bodyAnno.value())) {
                        arg.setName(param.getName());
                    } else {
                        arg.setName(((BodyParam) anno).value());
                    }
                    arg.setAnnotationClass(BodyParam.class);
                    arg.setRequired(bodyAnno.required());
                    arg.setEmailParam(bodyAnno.isEmail());
                    arg.setMinLength(bodyAnno.minLength());
                    arg.setAllowEmpty(bodyAnno.allowEmpty());
                    if (!empty(bodyAnno.validationPattern())) {
                        arg.setValidationPattern(Pattern.compile(bodyAnno.validationPattern()));
                    }
                } else if (ObjectParam.class.isInstance(anno)) {
                    ObjectParam oParam = (ObjectParam) anno;
                    arg.setType("ObjectParam");
                    arg.setName("noop");
                    if (oParam.targetClass() == null || oParam.targetClass().equals(Object.class)) {
                        arg.setTargetClass(param.getType());
                    } else {
                        arg.setTargetClass(oParam.targetClass());
                    }
                    arg.setAnnotationClass(ObjectParam.class);
                } else if (MapParam.class.isInstance(anno)) {
                    arg.setType("MapParam");
                    arg.setName("noop");
                    arg.setAnnotationClass(MapParam.class);
                } else if (QueryParam.class.isInstance(anno)) {
                    arg.setType("QueryParam");
                    arg.setName(((QueryParam) anno).value());
                    arg.setAnnotationClass(QueryParam.class);
                } else if (PathParam.class.isInstance(anno)) {
                    arg.setType("PathParam");
                    arg.setName(((PathParam) anno).value());
                    arg.setAnnotationClass(PathParam.class);
                } else if (DefaultValue.class.isInstance(anno)) {
                    arg.setDefaultValue(((DefaultValue) anno).value());
                } else if (NotNull.class.isInstance(anno)) {
                    arg.setRequired(true);
                } else if (Nullable.class.isInstance(anno)) {
                    arg.setRequired(false);
                } else if (NotEmpty.class.isInstance(anno)) {
                    arg.setRequired(true);
                    arg.setAllowEmpty(false);
                } else if (Email.class.isInstance(anno)) {
                    arg.setEmailParam(true);
                }
            }
            if (StringUtils.isEmpty(arg.getType())) {
                arg.setType("ObjectParam");
                arg.setName(param.getName());
                arg.setTargetClass(param.getType());
                arg.setAnnotationClass(ObjectParam.class);
            }

            if (StringUtils.isEmpty(arg.getName())) {
                arg.setName(param.getName());
            }
            endpoint.getArgs().add(arg);
        }
    }
    return endpoints;
}

From source file:com.phoenixnap.oss.ramlapisync.parser.SpringMvcResourceParser.java

/**
 * Checks if a parameter has any metadata that has been attached using the RequestParam data which identifies it as
 * an API request parameter/*  ww  w. j  a  v a2 s .c  om*/
 * @param param The Parameter to be checked
 * @return If true the parameter should be added to the api
 */
protected boolean shouldAddParameter(Parameter param) {
    for (Annotation annotation : param.getAnnotations()) {
        if ((annotation.getClass().equals(RequestParam.class))
                || (annotation.getClass().equals(RequestBody.class))
                || (annotation.getClass().equals(PathVariable.class))) {
            return true;
        }
    }
    return false;
}

From source file:org.springframework.cloud.iot.coap.method.ResolvableMethod.java

private String formatParameter(Parameter param) {
    Annotation[] annot = param.getAnnotations();
    return annot.length > 0
            ? Arrays.stream(annot).map(this::formatAnnotation).collect(joining(",", "[", "]")) + " " + param
            : param.toString();// ww  w. ja va 2 s  .  c om
}

From source file:org.springframework.messaging.handler.invocation.ResolvableMethod.java

private String formatParameter(Parameter param) {
    Annotation[] anns = param.getAnnotations();
    return (anns.length > 0
            ? Arrays.stream(anns).map(this::formatAnnotation).collect(joining(",", "[", "]")) + " " + param
            : param.toString());//from   w w  w  .  j av a 2 s  . c  o  m
}