use of java.lang.reflect.Parameter in project commons by terran4j.
the class DsqlRepositoryProxy method getContext.
private Map<String, Object> getContext(Method method, Object[] args) throws BusinessException {
Map<String, Object> context = new HashMap<>();
// 没有参数的情况。
Parameter[] params = method.getParameters();
if (params == null || params.length == 0) {
return context;
}
// 只有一个参数,并且没有 @Param 注解时,用 query 作默认的 key.
if (params.length == 1 && params[0].getAnnotation(Param.class) == null) {
if (args[0] != null) {
context.put("args", args[0]);
}
}
String[] paramNames = paramNameDiscoverer.getParameterNames(method);
for (int i = 0; i < params.length; i++) {
Parameter param = params[i];
String key = param.getName();
// 利用 Spring 提供的工具,获取参数名。
if (paramNames != null && paramNames.length >= i + 1) {
key = paramNames[i];
}
Param paramAnnotation = param.getAnnotation(Param.class);
if (paramAnnotation != null) {
key = paramAnnotation.value();
}
Object value = args[i];
if (value != null) {
context.put(key, value);
}
}
return context;
}
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 rxlib by RockyLOMO.
the class RestClient method facade.
public static <T> T facade(Class<T> contract, String serverPrefixUrl, BiFunc<String, Boolean> checkResponse) {
RequestMapping baseMapping = contract.getAnnotation(RequestMapping.class);
String prefix = serverPrefixUrl + getFirstPath(baseMapping);
boolean defMethod = isPostMethod(baseMapping);
return proxy(contract, (m, p) -> {
RequestMapping pathMapping = m.getAnnotation(RequestMapping.class);
String path = getFirstPath(pathMapping);
if (Strings.isEmpty(path)) {
path = m.getName();
}
String reqUrl = prefix + path;
boolean doPost = Arrays.isEmpty(pathMapping.method()) ? defMethod : isPostMethod(pathMapping);
Parameter[] parameters = m.getParameters();
Func<Map<String, Object>> getFormData = () -> {
Map<String, Object> data = new HashMap<>();
for (int i = 0; i < parameters.length; i++) {
Parameter parameter = parameters[i];
RequestParam param = parameter.getAnnotation(RequestParam.class);
String name = param != null ? !Strings.isEmpty(param.value()) ? param.value() : param.name() : parameter.getName();
Object val = p.arguments[i];
if (val == null && param != null) {
val = Reflects.changeType(param.defaultValue(), parameter.getType());
}
data.put(name, val);
}
return data;
};
String responseText;
ProceedEventArgs args = new ProceedEventArgs(contract, new Object[1], m.getReturnType().equals(void.class));
HttpClient client = new HttpClient();
try {
if (doPost) {
if (parameters.length == 1 && parameters[0].isAnnotationPresent(RequestBody.class)) {
args.getParameters()[0] = p.arguments[0];
responseText = args.proceed(() -> client.postJson(reqUrl, args.getParameters()[0]).toString());
} else {
Map<String, Object> data = getFormData.invoke();
args.getParameters()[0] = data;
responseText = args.proceed(() -> client.post(reqUrl, data).toString());
}
} else {
Map<String, Object> data = getFormData.invoke();
args.getParameters()[0] = data;
responseText = args.proceed(() -> client.get(HttpClient.buildUrl(reqUrl, data)).toString());
}
if (checkResponse != null && !checkResponse.invoke(responseText)) {
throw new InvalidException("Response status error");
}
} catch (Exception e) {
args.setError(e);
throw e;
} finally {
App.log(args, msg -> {
if (doPost) {
msg.appendLine("POST: %s %s", args.getTraceId(), reqUrl);
} else {
msg.appendLine("GET: %s %s", args.getTraceId(), reqUrl);
}
msg.appendLine("Request:\t%s", toJsonString(args.getParameters()));
msg.append("Response:\t%s", args.getReturnValue());
});
}
if (m.getReturnType().equals(Void.class)) {
return null;
}
return fromJson(responseText, ifNull(RESULT_TYPE.get(), m.getReturnType()));
});
}
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);
}
}
};
}
Aggregations