introspectionmodeller.java
来自「resetful样式的ws样例,一种面向资源的webservices服务」· Java 代码 · 共 545 行 · 第 1/2 页
JAVA
545 行
* Return a list of specific meta-annotations decared on annotations of * a method. */ private static final <T extends Annotation> List<T> getMetaAnnotations( Method m, Class<T> annotation) { List <T> ma = new ArrayList<T>(); for (Annotation a : m.getAnnotations()) { if (a.annotationType().getAnnotation(annotation) != null) { ma.add(a.annotationType().getAnnotation(annotation)); } } return ma; } private static final void workOutSubResourceLocatorsList( AbstractResource resource, MethodList methodList, boolean isEncoded) { for (AnnotatedMethod m : methodList.hasNotMetaAnnotation(HttpMethod.class). hasAnnotation(Path.class)) { final Path mPathAnnotation = m.getAnnotation(Path.class); final AbstractSubResourceLocator subResourceLocator = new AbstractSubResourceLocator( m.getMethod(), new UriPathValue( mPathAnnotation.value(), mPathAnnotation.encode(), mPathAnnotation.limited())); processParameters(subResourceLocator, m, isEncoded); resource.getSubResourceLocators().add(subResourceLocator); } } private static final void processParameters( Parameterized parametrized, Constructor ctor, boolean isEncoded) { processParameters( ctor.toString(), parametrized, ((null != ctor.getAnnotation(Encoded.class)) || isEncoded), ctor.getParameterTypes(), ctor.getGenericParameterTypes(), ctor.getParameterAnnotations()); } private static final void processParameters( Parameterized parametrized, AnnotatedMethod method, boolean isEncoded) { processParameters( method.toString(), parametrized, ((null != method.getAnnotation(Encoded.class)) || isEncoded), method.getParameterTypes(), method.getGenericParameterTypes(), method.getParameterAnnotations()); } private static final void processParameters( String nameForLogging, Parameterized parametrized, boolean isEncoded, Class[] parameterTypes, Type[] genericParameterTypes, Annotation[][] parameterAnnotations) { for (int i = 0; i < parameterTypes.length; i++) { Parameter parameter = createParameter( nameForLogging, i + 1, isEncoded, parameterTypes[i], genericParameterTypes[i], parameterAnnotations[i]); if (null != parameter) { parametrized.getParameters().add(parameter); } else { // clean up the parameters parametrized.getParameters().removeAll(parametrized.getParameters()); break; } } } private static interface ParamAnnotationHelper<T extends Annotation> { public String getValueOf(T a); public Parameter.Source getSource(); } private static Map<Class, ParamAnnotationHelper> createParamAnotHelperMap() { Map<Class, ParamAnnotationHelper> m = new WeakHashMap<Class, ParamAnnotationHelper>(); m.put(Context.class, new ParamAnnotationHelper<Context>() { public String getValueOf(Context a) { return null; } public Parameter.Source getSource() { return Parameter.Source.CONTEXT; } }); m.put(HeaderParam.class, new ParamAnnotationHelper<HeaderParam>() { public String getValueOf(HeaderParam a) { return a.value(); } public Parameter.Source getSource() { return Parameter.Source.HEADER; } }); m.put(CookieParam.class, new ParamAnnotationHelper<CookieParam>() { public String getValueOf(CookieParam a) { return a.value(); } public Parameter.Source getSource() { return Parameter.Source.COOKIE; } }); m.put(MatrixParam.class, new ParamAnnotationHelper<MatrixParam>() { public String getValueOf(MatrixParam a) { return a.value(); } public Parameter.Source getSource() { return Parameter.Source.MATRIX; } }); m.put(QueryParam.class, new ParamAnnotationHelper<QueryParam>() { public String getValueOf(QueryParam a) { return a.value(); } public Parameter.Source getSource() { return Parameter.Source.QUERY; } }); m.put(PathParam.class, new ParamAnnotationHelper<PathParam>() { public String getValueOf(PathParam a) { return a.value(); } public Parameter.Source getSource() { return Parameter.Source.PATH; } }); return Collections.unmodifiableMap(m); } private final static Map<Class, ParamAnnotationHelper> ANOT_HELPER_MAP = createParamAnotHelperMap(); @SuppressWarnings("unchecked") private static final Parameter createParameter( String nameForLogging, int order, boolean isEncoded, Class<?> paramClass, Type paramType, Annotation[] annotations) { if (null == annotations) { return null; } Annotation paramAnnotation = null; Parameter.Source paramSource = null; String paramName = null; boolean paramEncoded = isEncoded; String paramDefault = null; /** * Create a parameter from the list of annotations. * Unknown annotated parameters are also supported, and in such a * cases the last unrecognized annotation is taken to be that * associated with the parameter. */ for (Annotation annotation : annotations) { if (ANOT_HELPER_MAP.containsKey(annotation.annotationType())) { ParamAnnotationHelper helper = ANOT_HELPER_MAP.get(annotation.annotationType()); if (null != paramSource) { if (LOGGER.isLoggable(Level.WARNING)) { LOGGER.warning(ImplMessages.AMBIGUOUS_PARAMETER(nameForLogging, Integer.toString(order))); } } paramAnnotation = annotation; paramSource = helper.getSource(); paramName = helper.getValueOf(annotation); } else if (Encoded.class == annotation.annotationType()) { paramEncoded = true; } else if (DefaultValue.class == annotation.annotationType()) { paramDefault = ((DefaultValue) annotation).value(); } else { paramAnnotation = annotation; paramSource = Source.UNKNOWN; paramName = getValue(annotation); } } if (paramAnnotation == null) { paramSource = Parameter.Source.ENTITY; } return new Parameter(annotations, paramAnnotation, paramSource, paramName, paramType, paramClass, paramEncoded, paramDefault); } private static final String getValue(Annotation a) { try { Method m = a.annotationType().getMethod("value"); if (m.getReturnType() != String.class) return null; return (String)m.invoke(a); } catch (Exception ex) { } return null; } private static void logNonPublicMethods(final Class resourceClass) { assert null != resourceClass; if (!LOGGER.isLoggable(Level.WARNING)) { return; // does not make sense to check when logging is disabled anyway } final MethodList declaredMethods = new MethodList( getDeclaredMethods(resourceClass)); // non-public resource methods for (AnnotatedMethod m : declaredMethods.hasMetaAnnotation(HttpMethod.class). hasNotAnnotation(Path.class).isNotPublic()) { LOGGER.warning(ImplMessages.NON_PUB_RES_METHOD(m.getMethod().toGenericString())); } // non-public subres methods for (AnnotatedMethod m : declaredMethods.hasMetaAnnotation(HttpMethod.class). hasAnnotation(Path.class).isNotPublic()) { LOGGER.warning(ImplMessages.NON_PUB_SUB_RES_METHOD(m.getMethod().toGenericString())); } // non-public subres locators for (AnnotatedMethod m : declaredMethods.hasNotMetaAnnotation(HttpMethod.class). hasAnnotation(Path.class).isNotPublic()) { LOGGER.warning(ImplMessages.NON_PUB_SUB_RES_LOC(m.getMethod().toGenericString())); } } private static List<Method> getDeclaredMethods(final Class _c) { final List<Method> ml = new ArrayList<Method>(); AccessController.doPrivileged(new PrivilegedAction<Object>() { Class c = _c; public Object run() { while (c != Object.class && c != null) { for (Method m : c.getDeclaredMethods()) ml.add(m); c = c.getSuperclass(); } return null; } }); return ml; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?