下面列出了java.lang.reflect.AccessibleObject#getAnnotations ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Unpacks all annotations that are present at the given object.
* Some annotations (e.g. {@link MinCardinality}) may be packed in a container annotation (e.g. {@link MinCardinalities}).
* In contrast to {@link AccessibleObject#getAnnotations()} these contained annotations are unpacked and the container
* is omitted.
* @param object The object for which to get the annotations.
* @return The unpacked annotations of the object.
*/
private Collection<Annotation> unpackAnnotations(AccessibleObject object) {
Collection<Annotation> annotations = new HashSet<>();
for (Annotation annotation : object.getAnnotations()) {
// Unpack container annotations:
if(annotation.getClass().equals(MinCardinalities.class)) {
annotations.addAll(unpackAnnotations(object, MinCardinality.class));
} else if(annotation.getClass().equals(MaxCardinalities.class)) {
annotations.addAll(unpackAnnotations(object, MaxCardinality.class));
} else if(annotation.getClass().equals(Cardinalities.class)) {
annotations.addAll(unpackAnnotations(object, Cardinality.class));
} else { // Add any directly annotated (non-container) annotation:
annotations.add(annotation);
}
}
return annotations;
}
@Override
boolean isCallerSensitive(AccessibleObject o) {
for(Annotation a: o.getAnnotations()) {
if(String.valueOf(a).equals(CALLER_SENSITIVE_ANNOTATION_STRING)) {
return true;
}
}
if (o instanceof Method) {
// JDK7 hack
Method method = (Method) o;
final String className = method.getDeclaringClass().getName();
final String methodName = method.getName();
if (className.equals("java.security.AccessController") && methodName.equals("doPrivileged")) {
return true;
}
if (className.equals("java.lang.Class") && methodName.equals("forName")) {
return true;
}
}
return false;
}
@Override
boolean isCallerSensitive(final AccessibleObject o) {
for(final Annotation a: o.getAnnotations()) {
if(String.valueOf(a).equals(CALLER_SENSITIVE_ANNOTATION_STRING)) {
return true;
}
}
return false;
}
@Nullable
private AnnotationAttributes findAutowiredAnnotation(AccessibleObject ao) {
if (ao.getAnnotations().length > 0) { // autowiring annotations have to be local
for (Class<? extends Annotation> type : this.autowiredAnnotationTypes) {
AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(ao, type);
if (attributes != null) {
return attributes;
}
}
}
return null;
}
@Override
boolean isCallerSensitive(final AccessibleObject o) {
for(final Annotation a: o.getAnnotations()) {
if(String.valueOf(a).equals(CALLER_SENSITIVE_ANNOTATION_STRING)) {
return true;
}
}
return false;
}
@Override
boolean isCallerSensitive(final AccessibleObject o) {
for(final Annotation a: o.getAnnotations()) {
if(String.valueOf(a).equals(CALLER_SENSITIVE_ANNOTATION_STRING)) {
return true;
}
}
return false;
}
private AnnotationAttributes findAutowiredAnnotation(AccessibleObject ao) {
if (ao.getAnnotations().length > 0) {
for (Class<? extends Annotation> type : this.autowiredAnnotationTypes) {
AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(ao, type);
if (attributes != null) {
return attributes;
}
}
}
return null;
}
@Override
boolean isCallerSensitive(final AccessibleObject o) {
for(final Annotation a: o.getAnnotations()) {
if(String.valueOf(a).equals(CALLER_SENSITIVE_ANNOTATION_STRING)) {
return true;
}
}
return false;
}
@Override
boolean isCallerSensitive(final AccessibleObject o) {
for(final Annotation a: o.getAnnotations()) {
if(String.valueOf(a).equals(CALLER_SENSITIVE_ANNOTATION_STRING)) {
return true;
}
}
return false;
}
@Override
boolean isCallerSensitive(AccessibleObject o) {
for(Annotation a: o.getAnnotations()) {
if(String.valueOf(a).equals(CALLER_SENSITIVE_ANNOTATION_STRING)) {
return true;
}
}
return false;
}
private AnnotationAttributes findAutowiredAnnotation(AccessibleObject ao) {
if (ao.getAnnotations().length > 0) {
for (Class<? extends Annotation> type : this.autowiredAnnotationTypes) {
AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(ao, type);
if (attributes != null) {
return attributes;
}
}
}
return null;
}
@Override
boolean isCallerSensitive(AccessibleObject o) {
for(Annotation a: o.getAnnotations()) {
if(String.valueOf(a).equals(CALLER_SENSITIVE_ANNOTATION_STRING)) {
return true;
}
}
return false;
}
/**
* Scans all annotation except @Test, and generates map.
*
* @param MethodOrFileld
* @return
* @throws Exception
*/
public static Map<String, Object> getMetadata(AccessibleObject methodOrFileld, boolean excludeTest) {
Map<String, Object> metadata = new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER);
metadata.putAll(metadataFromClass(methodOrFileld));
try {
Annotation[] allAnnotations = methodOrFileld.getAnnotations();
for (Annotation annotation : allAnnotations) {
if (excludeTest && annotation instanceof Test)
continue;
Method[] annotationMethods = annotation.annotationType().getDeclaredMethods();
for (Method annotationMethod : annotationMethods) {
Object objVal = annotationMethod.invoke(annotation);
if (annotation instanceof MetaData) {
@SuppressWarnings("unchecked")
Map<String, Object> map = new Gson().fromJson((String) objVal, Map.class);
metadata.putAll(map);
} else {
metadata.put(annotationMethod.getName(), objVal);
}
}
}
} catch (Exception e) {
logger.error(e);
}
return metadata;
}
@Override
boolean isCallerSensitive(final AccessibleObject o) {
for(final Annotation a: o.getAnnotations()) {
if(String.valueOf(a).equals(CALLER_SENSITIVE_ANNOTATION_STRING)) {
return true;
}
}
return false;
}
public void test_getAnnotations() throws Exception {
AccessibleObject ao = SubTestClass.class.getMethod("annotatedMethod");
Annotation[] annotations = ao.getAnnotations();
assertEquals(2, annotations.length);
Set<Class<?>> ignoreOrder = new HashSet<Class<?>>();
ignoreOrder.add(annotations[0].annotationType());
ignoreOrder.add(annotations[1].annotationType());
assertTrue("Missing @AnnotationRuntime0",
ignoreOrder.contains(AnnotationRuntime0.class));
assertTrue("Missing @AnnotationRuntime1",
ignoreOrder.contains(AnnotationRuntime1.class));
}
private Inject findReferenceAnnotation(AccessibleObject accessibleObject) {
if (accessibleObject.getAnnotations().length > 0) {
Inject inject = getMergedAnnotation(accessibleObject, Inject.class);
return inject;
}
return null;
}