use of java.lang.reflect.Parameter in project Flash by Minato262.
the class Annotation method annotation.
@SuppressWarnings("unchecked")
@Test
public void annotation() throws InvocationTargetException, IllegalAccessException {
Annotation annotationTest = new Annotation();
Class<Annotation> clazz = (Class<Annotation>) annotationTest.getClass();
RequestMapping annotation = clazz.getAnnotation(RequestMapping.class);
Assert.assertNotEquals(annotation.value(), null);
Assert.assertEquals(annotation.value(), "annotation");
Assert.assertNotEquals(annotation.method(), null);
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(RequestMapping.class)) {
RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
Assert.assertNotEquals(requestMapping, null);
Assert.assertNotEquals(requestMapping.value(), null);
Assert.assertNotEquals(requestMapping.method(), null);
Assert.assertNotEquals(requestMapping.params(), null);
Assert.assertNotEquals(requestMapping.headers(), null);
Assert.assertNotEquals(requestMapping.consumes(), null);
Assert.assertNotEquals(requestMapping.produces(), null);
}
if (method.isAnnotationPresent(ResponseBody.class)) {
ResponseBody responseBody = method.getAnnotation(ResponseBody.class);
Assert.assertNotEquals(responseBody, null);
}
Parameter[] params = method.getParameters();
if (params.length != 0) {
method.invoke(annotationTest, 1L);
}
for (Parameter param : params) {
if (param.isAnnotationPresent(RequestParam.class)) {
RequestParam requestParam = param.getAnnotation(RequestParam.class);
Assert.assertNotEquals(requestParam, null);
}
}
}
System.out.println(annotationTest.getGetId());
System.out.println(annotationTest.getPostId());
System.out.println(annotationTest.getPutId());
System.out.println(annotationTest.getDeleteId());
System.out.println(annotationTest.getHandler());
}
use of java.lang.reflect.Parameter in project jeesuite-libs by vakinge.
the class RequestLoggingInterceptor method getParameterConfigs.
private List<ParameterLogConfig> getParameterConfigs(String className, Method method) {
String fullName = className + method.getName();
List<ParameterLogConfig> configs = parameterLogConfigs.get(fullName);
//
if (configs == null) {
synchronized (parameterLogConfigs) {
Parameter[] parameters = method.getParameters() == null ? new Parameter[0] : method.getParameters();
configs = new ArrayList<>(parameters.length);
ParameterLogConfig config;
for (Parameter parameter : parameters) {
config = new ParameterLogConfig();
if (ServletRequest.class.isAssignableFrom(parameter.getType()) || ServletResponse.class.isAssignableFrom(parameter.getType())) {
config.ignore = true;
configs.add(config);
continue;
}
String paramName = null;
if (parameter.isAnnotationPresent(RequestParam.class)) {
paramName = parameter.getAnnotation(RequestParam.class).value();
if (StringUtils.isBlank(paramName)) {
paramName = parameter.getName();
}
} else if (parameters.length > 1) {
paramName = parameter.getName();
}
config.paramName = paramName;
config.isBody = parameter.getType() != MultipartFile.class && !BeanUtils.isSimpleDataType(parameter.getType());
configs.add(config);
}
parameterLogConfigs.put(fullName, configs);
}
}
return configs;
}
use of java.lang.reflect.Parameter in project markdown-doclet by Abnaxos.
the class ReflectedOptions method processorForMethod.
public static OptionProcessor processorForMethod(Object target, Method method) {
List<ArgumentConverter<?>> converters = new ArrayList<>(method.getParameterCount());
for (Parameter parameter : method.getParameters()) {
ArgumentConverter<?> converter;
OptionConsumer.Converter converterAnnotation = parameter.getAnnotation(OptionConsumer.Converter.class);
if (converterAnnotation != null) {
try {
converter = converterAnnotation.value().newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new ReflectionException("Error instantiating converter for parameter " + parameter + " method " + method);
}
} else {
converter = StandardArgumentConverters.forType(parameter.getParameterizedType());
if (converter == null) {
throw new ReflectionException("No argument converter found for parameter " + parameter.getName() + " of " + method);
}
}
converters.add(converter);
}
return (name, arguments) -> {
if (arguments.size() != converters.size()) {
throw new InvalidOptionArgumentsException("Unexpected argument count: " + arguments.size() + "!=" + converters.size() + "(expeted)");
}
Object[] methodArguments = new Object[arguments.size()];
for (int i = 0; i < arguments.size(); i++) {
methodArguments[i] = converters.get(i).convert(arguments.get(i));
}
try {
method.invoke(target, methodArguments);
} catch (IllegalAccessException e) {
throw new ArgumentsProcessingException(e);
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof InvalidOptionArgumentsException) {
throw (InvalidOptionArgumentsException) e.getTargetException();
} else {
throw new ArgumentsProcessingException(e);
}
}
};
}
use of java.lang.reflect.Parameter in project spring-data-commons by spring-projects.
the class TypeDiscoverer method getParameterTypes.
/*
* (non-Javadoc)
* @see org.springframework.data.util.TypeInformation#getParameterTypes(java.lang.reflect.Constructor)
*/
public List<TypeInformation<?>> getParameterTypes(Constructor<?> constructor) {
Assert.notNull(constructor, "Constructor must not be null!");
List<TypeInformation<?>> parameterTypes = new ArrayList<>(constructor.getParameterCount());
for (Parameter parameter : constructor.getParameters()) {
parameterTypes.add(createInfo(parameter.getParameterizedType()));
}
return parameterTypes;
}
use of java.lang.reflect.Parameter in project spring-native by spring-projects-experimental.
the class ParameterWriter method writeParameterValue.
private void writeParameterValue(Builder code, Object value, Supplier<ResolvableType> parameterTypeSupplier) {
if (value == null) {
code.add("null");
return;
}
ResolvableType parameterType = parameterTypeSupplier.get();
if (parameterType.isArray()) {
code.add("new $T { ", parameterType.toClass());
code.add(writeAll(Arrays.asList(ObjectUtils.toObjectArray(value)), (item) -> parameterType.getComponentType()));
code.add(" }");
} else if (value instanceof List) {
List<?> list = (List<?>) value;
if (list.isEmpty()) {
code.add("$T.emptyList()", Collections.class);
} else {
Class<?> listType = (value instanceof ManagedList ? ManagedList.class : List.class);
code.add("$T.of(", listType);
ResolvableType collectionType = parameterType.as(List.class).getGenerics()[0];
code.add(writeAll(list, (item) -> collectionType));
code.add(")");
}
} else if (value instanceof Set) {
Set<?> set = (Set<?>) value;
if (set.isEmpty()) {
code.add("$T.emptySet()", Collections.class);
} else {
Class<?> setType = (value instanceof ManagedSet ? ManagedSet.class : Set.class);
code.add("$T.of(", setType);
ResolvableType collectionType = parameterType.as(Set.class).getGenerics()[0];
code.add(writeAll(set, (item) -> collectionType));
code.add(")");
}
} else if (value instanceof Map) {
Map<?, ?> map = (Map<?, ?>) value;
if (map.size() <= 10) {
code.add("$T.of(", Map.class);
List<Object> parameters = new ArrayList<>();
map.forEach((mapKey, mapValue) -> {
parameters.add(mapKey);
parameters.add(mapValue);
});
code.add(writeAll(parameters, ResolvableType::forInstance));
code.add(")");
}
} else if (value instanceof Character) {
String result = '\'' + characterLiteralWithoutSingleQuotes((Character) value) + '\'';
code.add(result);
} else if (isPrimitiveOrWrapper(value)) {
code.add("$L", value);
} else if (value instanceof String) {
code.add("$S", value);
} else if (value instanceof Enum) {
Enum<?> enumValue = (Enum<?>) value;
code.add("$T.$N", enumValue.getClass(), enumValue.name());
} else if (value instanceof Class) {
code.add("$T.class", value);
} else if (value instanceof ResolvableType) {
code.add(this.typeWriter.generateTypeFor((ResolvableType) value));
} else if (value instanceof BeanDefinition) {
this.innerBeanDefinitionWriter.accept((BeanDefinition) value, code);
} else if (value instanceof BeanReference) {
code.add("new $T($S)", RuntimeBeanReference.class, ((BeanReference) value).getBeanName());
} else {
throw new IllegalArgumentException("Parameter of type " + parameterType + " is not supported");
}
}
Aggregations