本文整理匯總了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);
}