Search in sources :

Example 81 with Parameter

use of java.lang.reflect.Parameter in project datarouter by hotpads.

the class DefaultDecoder method decode.

@Override
public Object[] decode(HttpServletRequest request, Method method) {
    Map<String, String[]> queryParams = request.getParameterMap();
    Parameter[] parameters = method.getParameters();
    long bodyParamCount = countRequestBodyParam(parameters);
    if (queryParams.size() + bodyParamCount + getOptionalParameterCount(parameters) < parameters.length) {
        return null;
    }
    String body = null;
    if (bodyParamCount >= 1) {
        body = RequestTool.getBodyAsString(request);
        if (StringTool.isEmpty(body)) {
            return null;
        }
    }
    Object[] args = new Object[parameters.length];
    for (int i = 0; i < parameters.length; i++) {
        Parameter parameter = parameters[i];
        String parameterName = parameter.getName();
        Type parameterType = parameter.getParameterizedType();
        {
            Param parameterAnnotation = parameter.getAnnotation(Param.class);
            if (parameterAnnotation != null) {
                if (!parameterAnnotation.value().isEmpty()) {
                    parameterName = parameterAnnotation.value();
                }
            }
        }
        if (parameter.isAnnotationPresent(RequestBody.class)) {
            args[i] = decode(body, parameterType);
        } else if (parameter.isAnnotationPresent(RequestBodyString.class)) {
            args[i] = body;
        } else {
            String[] queryParam = queryParams.get(parameterName);
            // pre-emptively try to check if the parameter is actually a form-encoded array and normalize the name
            boolean isArray = parameterType instanceof Class && ((Class<?>) parameterType).isArray();
            if (isArray && queryParam == null && !parameterName.endsWith("[]")) {
                parameterName += "[]";
                queryParam = queryParams.get(parameterName);
            }
            boolean isOptional = OptionalParameter.class.isAssignableFrom(parameter.getType());
            if (queryParam == null && !isOptional) {
                return null;
            }
            boolean isFormEncodedArray = queryParam != null && (queryParam.length > 1 || parameterName.endsWith("[]")) && isArray;
            if (isFormEncodedArray) {
                Class<?> componentClass = ((Class<?>) parameterType).getComponentType();
                Object typedArray = Array.newInstance(componentClass, queryParam.length);
                for (int index = 0; index < queryParam.length; index++) {
                    Array.set(typedArray, index, decode(queryParam[index], componentClass));
                }
                args[i] = typedArray;
                continue;
            }
            String parameterValue = queryParam == null ? null : queryParam[0];
            args[i] = isOptional ? OptionalParameter.makeOptionalParameter(parameterValue, parameterType) : decode(parameterValue, parameterType);
        }
    }
    return args;
}
Also used : OptionalParameter(io.datarouter.web.handler.types.optional.OptionalParameter) Type(java.lang.reflect.Type) TraceSpanGroupType(io.datarouter.instrumentation.trace.TraceSpanGroupType) Parameter(java.lang.reflect.Parameter) OptionalParameter(io.datarouter.web.handler.types.optional.OptionalParameter)

Example 82 with Parameter

use of java.lang.reflect.Parameter in project common-project by yz-java.

the class HttpParamsValidateAdvisor method invoke.

@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
    Method method = methodInvocation.getMethod();
    Object[] arguments = methodInvocation.getArguments();
    Parameter[] parameters = method.getParameters();
    for (int i = 0; i < parameters.length; i++) {
        ParamsValidate annotation = parameters[i].getAnnotation(ParamsValidate.class);
        if (annotation != null) {
            Set<ConstraintViolation<Object>> violationSet = ValidatorConfiguration.validator().validate(arguments[i]);
            for (ConstraintViolation model : violationSet) {
                logger.warn(model.getPropertyPath() + model.getMessage());
                return JSON.toJSONString(new ResponseMessage(-1, model.getPropertyPath() + model.getMessage()));
            }
        }
    }
    Object proceed = null;
    try {
        proceed = methodInvocation.proceed();
    } catch (Exception e) {
        throw e;
    }
    return proceed;
}
Also used : ParamsValidate(com.yz.common.web.annotations.ParamsValidate) ConstraintViolation(javax.validation.ConstraintViolation) Parameter(java.lang.reflect.Parameter) Method(java.lang.reflect.Method) ResponseMessage(com.yz.common.core.message.ResponseMessage) InvocationTargetException(java.lang.reflect.InvocationTargetException) HandlerException(com.yz.common.core.exception.HandlerException)

Example 83 with Parameter

use of java.lang.reflect.Parameter in project openwebbeans by apache.

the class ObserverMethodConfiguratorImpl method read.

@Override
public ObserverMethodConfigurator<T> read(Method method) {
    this.qualifiers = getQualifiers(Arrays.asList(method.getAnnotations()));
    this.beanClass = method.getDeclaringClass();
    for (Parameter parameter : method.getParameters()) {
        Observes observes = parameter.getAnnotation(Observes.class);
        ObservesAsync observesAsync = parameter.getAnnotation(ObservesAsync.class);
        if (observes != null || observesAsync != null) {
            observedType = parameter.getParameterizedType();
            if (observes != null) {
                this.reception = observes.notifyObserver();
                this.transactionPhase = observes.during();
            } else {
                this.reception = observesAsync.notifyObserver();
                this.transactionPhase = TransactionPhase.IN_PROGRESS;
                this.async = true;
            }
            Priority prio = parameter.getAnnotation(Priority.class);
            if (prio != null) {
                this.priority = prio.value();
            }
            break;
        }
    }
    // X TODO CDI-2.0
    return this;
}
Also used : ObservesAsync(javax.enterprise.event.ObservesAsync) Observes(javax.enterprise.event.Observes) Priority(javax.annotation.Priority) Parameter(java.lang.reflect.Parameter) AnnotatedParameter(javax.enterprise.inject.spi.AnnotatedParameter)

Example 84 with Parameter

use of java.lang.reflect.Parameter in project java-artefacts by ConsumerDataStandardsAustralia.

the class ReqParamNamesDiscoverer method doGetParameterNames.

private static String[] doGetParameterNames(Executable executable) {
    Parameter[] parameters = executable.getParameters();
    String[] parameterNames = new String[parameters.length];
    for (int i = 0; i < parameters.length; ++i) {
        Parameter param = parameters[i];
        String paramName = param.getName();
        if (param.isAnnotationPresent(RequestParam.class)) {
            RequestParam requestParamAnnotation = param.getAnnotation(RequestParam.class);
            if (!Strings.isNullOrEmpty(requestParamAnnotation.value())) {
                paramName = requestParamAnnotation.value();
            }
        }
        parameterNames[i] = paramName;
    }
    return parameterNames;
}
Also used : RequestParam(org.springframework.web.bind.annotation.RequestParam) Parameter(java.lang.reflect.Parameter)

Example 85 with Parameter

use of java.lang.reflect.Parameter in project felix-dev by apache.

the class Helpers method getLambdaParameterName.

/**
 * Extracts the first parameter of a lambda.
 */
public static String getLambdaParameterName(SerializableLambda lambda, int index) {
    SerializedLambda serialized = getSerializedLambda(lambda);
    Method m = getLambdaMethod(serialized, lambda.getClass().getClassLoader());
    Parameter p = m.getParameters()[index];
    if (Objects.equals("arg0", p.getName())) {
        throw new IllegalStateException("Can'f find lambda method name (Please check you are using javac -parameters option).");
    }
    return p.getName();
}
Also used : Parameter(java.lang.reflect.Parameter) Method(java.lang.reflect.Method) SerializedLambda(java.lang.invoke.SerializedLambda)

Aggregations

Parameter (java.lang.reflect.Parameter)771 Method (java.lang.reflect.Method)276 ArrayList (java.util.ArrayList)142 Annotation (java.lang.annotation.Annotation)95 List (java.util.List)90 Type (java.lang.reflect.Type)77 HashMap (java.util.HashMap)75 Map (java.util.Map)72 Constructor (java.lang.reflect.Constructor)60 Test (org.junit.jupiter.api.Test)57 Arrays (java.util.Arrays)53 InvocationTargetException (java.lang.reflect.InvocationTargetException)52 Executable (java.lang.reflect.Executable)50 Field (java.lang.reflect.Field)45 Collectors (java.util.stream.Collectors)45 ParameterizedType (java.lang.reflect.ParameterizedType)42 Optional (java.util.Optional)42 Test (org.junit.Test)42 Set (java.util.Set)37 Modifier (java.lang.reflect.Modifier)29