本文整理汇总了Java中java.lang.reflect.Parameter.getAnnotations方法的典型用法代码示例。如果您正苦于以下问题:Java Parameter.getAnnotations方法的具体用法?Java Parameter.getAnnotations怎么用?Java Parameter.getAnnotations使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.reflect.Parameter
的用法示例。
在下文中一共展示了Parameter.getAnnotations方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getParamName
import java.lang.reflect.Parameter; //导入方法依赖的package包/类
/**
* 获取字段的命名
*
* @param parameter
* 要获取命名的字段
* @return 指定字段的命名
*/
private static String getParamName(Parameter parameter) throws ParamterNoNamingException {
Annotation[] annotations = parameter.getAnnotations();
GeneralParam generalParam = null;
Context context = null;
for (Annotation annotation : annotations) {
if (annotation instanceof GeneralParam) {
generalParam = (GeneralParam) annotation;
break;
} else if (annotation instanceof Context) {
context = (Context) annotation;
break;
}
}
if (generalParam != null && !generalParam.value().trim().isEmpty()) {
return generalParam.value();
} else if (context != null) {
return "Context";
} else {
throw new ParamterNoNamingException(parameter);
}
}
示例2: pathCompiler
import java.lang.reflect.Parameter; //导入方法依赖的package包/类
public static Pattern pathCompiler(String path, Method method) {
Parameter[] parameters = method.getParameters();
for (Parameter parameter : parameters) {
if (parameter.getAnnotations().length == 0) {
continue;
}
Annotation annotation = parameter.getAnnotations()[0];
if (annotation instanceof PathVariable) {
//如果是字符串
if (parameter.getType() == String.class) {
path = path.replace("{" + parameter.getName() + "}", "[0-9\\d\\D]*");
}
//如果是数字
else if (parameter.getType() == Integer.class
|| parameter.getType() == Long.class) {
path = path.replace("{" + parameter.getName() + "}", "[0-9]*");
}
}
}
return Pattern.compile(path);
}
示例3: of
import java.lang.reflect.Parameter; //导入方法依赖的package包/类
/**
* Create key for single method/constructor parameter.
*
* @param parameter
* Parameter to build key for.
* @return Created key.
*/
public static Key of(Parameter parameter) {
Annotation[] annotations = parameter.getAnnotations();
if (!parameter.getType().isAssignableFrom(Supplier.class)) {
return new Key(parameter.getParameterizedType(), false,
parameter.getType(), findBindingAnnotation(annotations));
}
Type type = parameter.getParameterizedType();
if (type instanceof ParameterizedType) {
Type[] args = ((ParameterizedType) type).getActualTypeArguments();
if (args.length > 0 && args[0] instanceof Class) {
return Key.of(args[0], true, annotations);
}
}
throw new InjectorException("Unable to determine parameter type for " + parameter);
}
示例4: isContextParam
import java.lang.reflect.Parameter; //导入方法依赖的package包/类
/**
* 判断参数是否是Context参数
*
* @param parameter
* 参数
* @return 如果返回true说明此参数是Context类型
*/
private static boolean isContextParam(Parameter parameter) {
Annotation[] annotations = parameter.getAnnotations();
for (Annotation annotation : annotations) {
if (annotation instanceof Context) {
return true;
}
}
return false;
}
示例5: processAnnotationOnMethod
import java.lang.reflect.Parameter; //导入方法依赖的package包/类
private void processAnnotationOnMethod(Method method, MethodMetadata.Builder requestTemplateBuilder) {
AnnotatedTypeMetadata methodMetadata = new StandardMethodMetadata(method);
Parameter[] parameters = method.getParameters();
processRequestMappingAnnotation(methodMetadata, requestTemplateBuilder);
for (int i = 0; i < parameters.length; i++) {
Parameter parameter = parameters[i];
processAnnotationsOnParameter(requestTemplateBuilder, parameter, i);
if (parameter.getAnnotations().length == 0) {
requestTemplateBuilder.body(i, parameter.getParameterizedType());
}
}
}
示例6: processAnnotationsOnParameter
import java.lang.reflect.Parameter; //导入方法依赖的package包/类
private void processAnnotationsOnParameter(MethodMetadata.Builder requestTemplateBuilder, Parameter parameter, int i) {
if (parameter.getAnnotations() != null) {
for (Annotation annotation : parameter.getAnnotations()) {
AnnotatedParameterProcessor annotatedParameterProcessor;
if (annotation != null && (annotatedParameterProcessor = annotatedArgumentProcessors.get(annotation.annotationType())) != null) {
annotatedParameterProcessor.processAnnotation(requestTemplateBuilder, annotation, i, parameter.getParameterizedType());
}
}
}
}
示例7: processAnnotationsOnParams
import java.lang.reflect.Parameter; //导入方法依赖的package包/类
/**
* Process annotations on params.
*
* @param requestTemplate the request template
* @param method the method
*/
protected void processAnnotationsOnParams(RestyRequestTemplate requestTemplate, Method method) {
for (int i = 0; i < method.getParameters().length; i++) {
Parameter parameter = method.getParameters()[i];
// 处理PathVariable
PathVariable pathVariable = findMergedAnnotation(parameter, PathVariable.class);
if (pathVariable != null) {
PathVariableData pathVariableData = new PathVariableData();
pathVariableData.setIndex(i);
pathVariableData.setName("{" + (StringUtils.isNotEmpty(pathVariable.name()) ? pathVariable.name() : parameter.getName()) + "}");
pathVariableData.setRequired(pathVariable.required());
requestTemplate.addPathVariable(pathVariableData);
continue;
}
// 处理RequestParam
RequestParam requestParam = findMergedAnnotation(parameter, RequestParam.class);
if (requestParam != null) {
RequestParamData requestParamData = new RequestParamData();
requestParamData.setIndex(i);
requestParamData.setName(StringUtils.isNotEmpty(requestParam.name()) ? requestParam.name() : parameter.getName());
requestParamData.setRequired(requestParam.required());
requestParamData.setDefaultValue(resolveDefaultValue(requestParam.defaultValue()));
requestTemplate.addRequestParam(requestParamData);
continue;
}
RequestHeader requestHeader = findMergedAnnotation(parameter, RequestHeader.class);
if (requestHeader != null) {
RequestHeaderData requestHeaderData = new RequestHeaderData();
requestHeaderData.setIndex(i);
requestHeaderData.setName(StringUtils.isNotEmpty(requestHeader.name()) ? requestHeader.name() : parameter.getName());
requestHeaderData.setRequired(requestHeader.required());
requestHeaderData.setDefaultValue(resolveDefaultValue(requestHeader.defaultValue()));
requestTemplate.addRequestHeader(requestHeaderData);
continue;
}
// 处理RequestBody和无注解参数
RequestBody requestBody = findMergedAnnotation(parameter, RequestBody.class);
if ((requestBody != null || parameter.getAnnotations().length == 0)
&& parameter.getType() != RestyFuture.class) {
RequestBodyData requestBodyData = new RequestBodyData();
requestBodyData.setIndex(i);
requestBodyData.setName(parameter.getName());
requestBodyData.setDefaultValue(null);
if (requestBody != null) {
requestBodyData.setRequired(requestBody.required());
} else {
requestBodyData.setRequired(false);
}
requestTemplate.addRequestBody(requestBodyData);
}
}
}
示例8: doResponse
import java.lang.reflect.Parameter; //导入方法依赖的package包/类
/**
* 解析请求并返回响应
*/
private static void doResponse(HttpServletRequest request, HttpServletResponse response) throws Exception {
String url = request.getRequestURI();
RequestMethod requestMethod = RequestMethod.getEnum(request.getMethod());
log.info("{} {}", requestMethod, url);
MethodDetail methodDetail = UrlMappingPool.getInstance().getMap(url, requestMethod);
// 如果找不到对应的匹配规则
if (methodDetail == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
response.getWriter().print(Constants.NOT_FOUND);
return;
}
Class clazz = methodDetail.getClazz();
Object object = BeansPool.getInstance().getObject(clazz);
if (object == null)
throw new RuntimeException("can't find bean for " + clazz);
Map<String, String> requestParam = new HashMap<>();
request.getParameterMap().forEach((k, v) -> {
requestParam.put(k, v[0]);
});
List<String> params = new ArrayList<>(); // 最终的方法参数
Method method = methodDetail.getMethod();
// 获取方法的所有的参数
Parameter[] parameters = method.getParameters();
for (Parameter parameter : parameters) {
String name = null;
// 获取参数上所有的注解
Annotation[] annotations = parameter.getAnnotations();
for (Annotation annotation : annotations) {
if (annotation.annotationType() == Param.class) {
Param param = (Param) annotation;
name = param.value();
break;
}
}
// 如果请求参数中存在这个参数就把该值赋给方法参数,否则赋值null
params.add(requestParam.getOrDefault(name, null));
}
Object result = method.invoke(object, params.toArray());
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().print(result);
}