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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. private ClassLoader classLoader;
  35. private World world;
  36. // must have no-arg constructor for reflective construction
  37. public Java15AnnotationFinder() {
  38. }
  39. public void setClassLoader(ClassLoader aLoader) {
  40. this.bcelRepository = new ClassLoaderRepository(aLoader);
  41. this.classLoader = aLoader;
  42. }
  43. public void setWorld(World aWorld) {
  44. this.world = aWorld;
  45. }
  46. /* (non-Javadoc)
  47. * @see org.aspectj.weaver.reflect.AnnotationFinder#getAnnotation(org.aspectj.weaver.ResolvedType, java.lang.Object)
  48. */
  49. public Object getAnnotation(ResolvedType annotationType, Object onObject) {
  50. try {
  51. Class<? extends Annotation> annotationClass = (Class<? extends Annotation>) Class.forName(annotationType.getName(),false,classLoader);
  52. if (onObject.getClass().isAnnotationPresent(annotationClass)) {
  53. return onObject.getClass().getAnnotation(annotationClass);
  54. }
  55. } catch (ClassNotFoundException ex) {
  56. // just return null
  57. }
  58. return null;
  59. }
  60. public Object getAnnotationFromClass(ResolvedType annotationType, Class aClass) {
  61. try {
  62. Class<? extends Annotation> annotationClass = (Class<? extends Annotation>) Class.forName(annotationType.getName(),false,classLoader);
  63. if (aClass.isAnnotationPresent(annotationClass)) {
  64. return aClass.getAnnotation(annotationClass);
  65. }
  66. } catch (ClassNotFoundException ex) {
  67. // just return null
  68. }
  69. return null;
  70. }
  71. public Object getAnnotationFromMember(ResolvedType annotationType, Member aMember) {
  72. if (!(aMember instanceof AccessibleObject)) return null;
  73. AccessibleObject ao = (AccessibleObject) aMember;
  74. try {
  75. Class annotationClass = Class.forName(annotationType.getName(),false,classLoader);
  76. if (ao.isAnnotationPresent(annotationClass)) {
  77. return ao.getAnnotation(annotationClass);
  78. }
  79. } catch (ClassNotFoundException ex) {
  80. // just return null
  81. }
  82. return null;
  83. }
  84. public Set getAnnotations(Member onMember) {
  85. if (!(onMember instanceof AccessibleObject)) return Collections.EMPTY_SET;
  86. // here we really want both the runtime visible AND the class visible annotations
  87. // so we bail out to Bcel and then chuck away the JavaClass so that we don't hog
  88. // memory.
  89. try {
  90. JavaClass jc = bcelRepository.loadClass(onMember.getDeclaringClass());
  91. org.aspectj.apache.bcel.classfile.annotation.Annotation[] anns = new org.aspectj.apache.bcel.classfile.annotation.Annotation[0];
  92. if (onMember instanceof Method) {
  93. org.aspectj.apache.bcel.classfile.Method bcelMethod = jc.getMethod((Method)onMember);
  94. anns = bcelMethod.getAnnotations();
  95. } else if (onMember instanceof Constructor) {
  96. org.aspectj.apache.bcel.classfile.Method bcelCons = jc.getMethod((Constructor)onMember);
  97. anns = bcelCons.getAnnotations();
  98. } else if (onMember instanceof Field) {
  99. org.aspectj.apache.bcel.classfile.Field bcelField = jc.getField((Field)onMember);
  100. anns = bcelField.getAnnotations();
  101. }
  102. // the answer is cached and we don't want to hold on to memory
  103. bcelRepository.clear();
  104. if (anns == null) anns = new org.aspectj.apache.bcel.classfile.annotation.Annotation[0];
  105. // convert to our Annotation type
  106. Set<ResolvedType> annSet = new HashSet<ResolvedType>();
  107. for (int i = 0; i < anns.length; i++) {
  108. annSet.add(UnresolvedType.forName(anns[i].getTypeName()).resolve(world));
  109. }
  110. return annSet;
  111. } catch (ClassNotFoundException cnfEx) {
  112. // just use reflection then
  113. }
  114. AccessibleObject ao = (AccessibleObject) onMember;
  115. Annotation[] anns = ao.getDeclaredAnnotations();
  116. Set<UnresolvedType> annSet = new HashSet<UnresolvedType>();
  117. for (int i = 0; i < anns.length; i++) {
  118. annSet.add(UnresolvedType.forName(anns[i].annotationType().getName()).resolve(world));
  119. }
  120. return annSet;
  121. }
  122. public ResolvedType[] getAnnotations(Class forClass, World inWorld) {
  123. // here we really want both the runtime visible AND the class visible annotations
  124. // so we bail out to Bcel and then chuck away the JavaClass so that we don't hog
  125. // memory.
  126. try {
  127. JavaClass jc = bcelRepository.loadClass(forClass);
  128. org.aspectj.apache.bcel.classfile.annotation.Annotation[] anns =jc.getAnnotations();
  129. bcelRepository.clear();
  130. if (anns == null) return new ResolvedType[0];
  131. ResolvedType[] ret = new ResolvedType[anns.length];
  132. for (int i = 0; i < ret.length; i++) {
  133. ret[i] = inWorld.resolve(anns[i].getTypeName());
  134. }
  135. return ret;
  136. } catch (ClassNotFoundException cnfEx) {
  137. // just use reflection then
  138. }
  139. Annotation[] classAnnotations = forClass.getAnnotations();
  140. ResolvedType[] ret = new ResolvedType[classAnnotations.length];
  141. for (int i = 0; i < classAnnotations.length; i++) {
  142. ret[i] = inWorld.resolve(classAnnotations[i].annotationType().getName());
  143. }
  144. return ret;
  145. }
  146. }