You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Java15AnnotationFinder.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* *******************************************************************
  2. * Copyright (c) 2005 Contributors.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Adrian Colyer Initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.reflect;
  13. import java.lang.annotation.Annotation;
  14. import java.lang.reflect.AccessibleObject;
  15. import java.lang.reflect.Constructor;
  16. import java.lang.reflect.Field;
  17. import java.lang.reflect.Member;
  18. import java.lang.reflect.Method;
  19. import java.util.Collections;
  20. import java.util.HashSet;
  21. import java.util.Set;
  22. import org.aspectj.apache.bcel.classfile.JavaClass;
  23. import org.aspectj.apache.bcel.util.Repository;
  24. import org.aspectj.apache.bcel.util.ClassLoaderRepository;
  25. import org.aspectj.weaver.ResolvedType;
  26. import org.aspectj.weaver.UnresolvedType;
  27. import org.aspectj.weaver.World;
  28. /**
  29. * Find the given annotation (if present) on the given object
  30. *
  31. */
  32. public class Java15AnnotationFinder implements AnnotationFinder {
  33. private Repository bcelRepository;
  34. public Java15AnnotationFinder() {
  35. this.bcelRepository = new ClassLoaderRepository(getClass().getClassLoader());
  36. }
  37. /* (non-Javadoc)
  38. * @see org.aspectj.weaver.reflect.AnnotationFinder#getAnnotation(org.aspectj.weaver.ResolvedType, java.lang.Object)
  39. */
  40. public Object getAnnotation(ResolvedType annotationType, Object onObject) {
  41. try {
  42. Class annotationClass = Class.forName(annotationType.getName());
  43. if (onObject.getClass().isAnnotationPresent(annotationClass)) {
  44. return onObject.getClass().getAnnotation(annotationClass);
  45. }
  46. } catch (ClassNotFoundException ex) {
  47. // just return null
  48. }
  49. return null;
  50. }
  51. public Object getAnnotationFromClass(ResolvedType annotationType, Class aClass) {
  52. try {
  53. Class annotationClass = Class.forName(annotationType.getName());
  54. if (aClass.isAnnotationPresent(annotationClass)) {
  55. return aClass.getAnnotation(annotationClass);
  56. }
  57. } catch (ClassNotFoundException ex) {
  58. // just return null
  59. }
  60. return null;
  61. }
  62. public Object getAnnotationFromMember(ResolvedType annotationType, Member aMember) {
  63. if (!(aMember instanceof AccessibleObject)) return null;
  64. AccessibleObject ao = (AccessibleObject) aMember;
  65. try {
  66. Class annotationClass = Class.forName(annotationType.getName());
  67. if (ao.isAnnotationPresent(annotationClass)) {
  68. return ao.getAnnotation(annotationClass);
  69. }
  70. } catch (ClassNotFoundException ex) {
  71. // just return null
  72. }
  73. return null;
  74. }
  75. public Set getAnnotations(Member onMember) {
  76. if (!(onMember instanceof AccessibleObject)) return Collections.EMPTY_SET;
  77. // here we really want both the runtime visible AND the class visible annotations
  78. // so we bail out to Bcel and then chuck away the JavaClass so that we don't hog
  79. // memory.
  80. try {
  81. JavaClass jc = bcelRepository.loadClass(onMember.getDeclaringClass());
  82. org.aspectj.apache.bcel.classfile.annotation.Annotation[] anns = new org.aspectj.apache.bcel.classfile.annotation.Annotation[0];
  83. if (onMember instanceof Method) {
  84. org.aspectj.apache.bcel.classfile.Method bcelMethod = jc.getMethod((Method)onMember);
  85. anns = bcelMethod.getAnnotations();
  86. } else if (onMember instanceof Constructor) {
  87. org.aspectj.apache.bcel.classfile.Method bcelCons = jc.getMethod((Constructor)onMember);
  88. anns = bcelCons.getAnnotations();
  89. } else if (onMember instanceof Field) {
  90. org.aspectj.apache.bcel.classfile.Field bcelField = jc.getField((Field)onMember);
  91. anns = bcelField.getAnnotations();
  92. }
  93. // the answer is cached and we don't want to hold on to memory
  94. bcelRepository.clear();
  95. if (anns == null) anns = new org.aspectj.apache.bcel.classfile.annotation.Annotation[0];
  96. // convert to our Annotation type
  97. Set<UnresolvedType> annSet = new HashSet<UnresolvedType>();
  98. for (int i = 0; i < anns.length; i++) {
  99. annSet.add(UnresolvedType.forName(anns[i].getTypeName()));
  100. }
  101. return annSet;
  102. } catch (ClassNotFoundException cnfEx) {
  103. // just use reflection then
  104. }
  105. AccessibleObject ao = (AccessibleObject) onMember;
  106. Annotation[] anns = ao.getDeclaredAnnotations();
  107. Set<UnresolvedType> annSet = new HashSet<UnresolvedType>();
  108. for (int i = 0; i < anns.length; i++) {
  109. annSet.add(UnresolvedType.forName(anns[i].annotationType().getName()));
  110. }
  111. return annSet;
  112. }
  113. public ResolvedType[] getAnnotations(Class forClass, World inWorld) {
  114. // here we really want both the runtime visible AND the class visible annotations
  115. // so we bail out to Bcel and then chuck away the JavaClass so that we don't hog
  116. // memory.
  117. try {
  118. JavaClass jc = bcelRepository.loadClass(forClass);
  119. org.aspectj.apache.bcel.classfile.annotation.Annotation[] anns =jc.getAnnotations();
  120. bcelRepository.clear();
  121. if (anns == null) return new ResolvedType[0];
  122. ResolvedType[] ret = new ResolvedType[anns.length];
  123. for (int i = 0; i < ret.length; i++) {
  124. ret[i] = inWorld.resolve(anns[i].getTypeName());
  125. }
  126. return ret;
  127. } catch (ClassNotFoundException cnfEx) {
  128. // just use reflection then
  129. }
  130. Annotation[] classAnnotations = forClass.getAnnotations();
  131. ResolvedType[] ret = new ResolvedType[classAnnotations.length];
  132. for (int i = 0; i < classAnnotations.length; i++) {
  133. ret[i] = inWorld.resolve(classAnnotations[i].annotationType().getName());
  134. }
  135. return ret;
  136. }
  137. }