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;
}
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;
}
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;
}
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;
}
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();
}
Aggregations