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.

ReflectionBasedReferenceTypeDelegate.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  8. *
  9. * Contributors:
  10. * Adrian Colyer Initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.reflect;
  13. import java.lang.reflect.Constructor;
  14. import java.lang.reflect.Field;
  15. import java.lang.reflect.Member;
  16. import java.lang.reflect.Method;
  17. import java.net.URL;
  18. import java.net.URLClassLoader;
  19. import java.util.Collection;
  20. import java.util.Collections;
  21. import org.aspectj.weaver.AjAttribute.WeaverVersionInfo;
  22. import org.aspectj.weaver.AnnotationAJ;
  23. import org.aspectj.weaver.AnnotationTargetKind;
  24. import org.aspectj.weaver.ConcreteTypeMunger;
  25. import org.aspectj.weaver.ISourceContext;
  26. import org.aspectj.weaver.ReferenceType;
  27. import org.aspectj.weaver.ReferenceTypeDelegate;
  28. import org.aspectj.weaver.ResolvedMember;
  29. import org.aspectj.weaver.ResolvedType;
  30. import org.aspectj.weaver.SourceContextImpl;
  31. import org.aspectj.weaver.TypeVariable;
  32. import org.aspectj.weaver.UnresolvedType;
  33. import org.aspectj.weaver.WeakClassLoaderReference;
  34. import org.aspectj.weaver.WeaverStateInfo;
  35. import org.aspectj.weaver.World;
  36. import org.aspectj.weaver.patterns.Declare;
  37. import org.aspectj.weaver.patterns.PerClause;
  38. /**
  39. * @author colyer A delegate for a resolved type that uses runtime type information (java.lang.reflect) to answer questions. This
  40. * class uses only Java 1.4 features to answer questions. In a Java 1.5 environment use the
  41. * Java5ReflectionBasedReferenceTypeDelegate subtype.
  42. */
  43. public class ReflectionBasedReferenceTypeDelegate implements ReferenceTypeDelegate {
  44. private static final ClassLoader bootClassLoader = new URLClassLoader(new URL[0]);// ReflectionBasedReferenceTypeDelegate.class.
  45. // getClassLoader();
  46. protected Class myClass = null;
  47. protected WeakClassLoaderReference classLoaderReference = null;
  48. protected World world;
  49. private ReferenceType resolvedType;
  50. private ResolvedMember[] fields = null;
  51. private ResolvedMember[] methods = null;
  52. private ResolvedType[] interfaces = null;
  53. public ReflectionBasedReferenceTypeDelegate(Class forClass, ClassLoader aClassLoader, World inWorld, ReferenceType resolvedType) {
  54. initialize(resolvedType, forClass, aClassLoader, inWorld);
  55. }
  56. /** for reflective construction only */
  57. public ReflectionBasedReferenceTypeDelegate() {
  58. }
  59. public void initialize(ReferenceType aType, Class<?> aClass, ClassLoader aClassLoader, World aWorld) {
  60. this.myClass = aClass;
  61. this.resolvedType = aType;
  62. this.world = aWorld;
  63. this.classLoaderReference = new WeakClassLoaderReference((aClassLoader != null) ? aClassLoader : bootClassLoader);
  64. }
  65. public Class<?> getClazz() {
  66. return this.myClass;
  67. }
  68. protected Class getBaseClass() {
  69. return this.myClass;
  70. }
  71. protected World getWorld() {
  72. return this.world;
  73. }
  74. public ReferenceType buildGenericType() {
  75. throw new UnsupportedOperationException("Shouldn't be asking for generic type at 1.4 source level or lower");
  76. }
  77. public boolean isAspect() {
  78. // we could do better than this in Java 5 by looking at the annotations
  79. // on the type...
  80. return false;
  81. }
  82. /*
  83. * (non-Javadoc)
  84. *
  85. * @see org.aspectj.weaver.ReferenceTypeDelegate#isAnnotationStyleAspect()
  86. */
  87. public boolean isAnnotationStyleAspect() {
  88. // we could do better than this in Java 5 by looking at the annotations
  89. // on the type...
  90. return false;
  91. }
  92. public boolean isInterface() {
  93. return this.myClass.isInterface();
  94. }
  95. public boolean isEnum() {
  96. // cant be an enum in Java 1.4 or prior
  97. return false;
  98. }
  99. /*
  100. * (non-Javadoc)
  101. *
  102. * @see org.aspectj.weaver.ReferenceTypeDelegate#isAnnotationWithRuntimeRetention ()
  103. */
  104. public boolean isAnnotationWithRuntimeRetention() {
  105. // cant be an annotation in Java 1.4 or prior
  106. return false;
  107. }
  108. public boolean isAnnotation() {
  109. // cant be an annotation in Java 1.4 or prior
  110. return false;
  111. }
  112. public String getRetentionPolicy() {
  113. // cant be an annotation in Java 1.4 or prior
  114. return null;
  115. }
  116. public boolean canAnnotationTargetType() {
  117. return false;
  118. }
  119. public AnnotationTargetKind[] getAnnotationTargetKinds() {
  120. return null;
  121. }
  122. public boolean isClass() {
  123. return !this.myClass.isInterface() && !this.myClass.isPrimitive() && !this.myClass.isArray();
  124. }
  125. /*
  126. * (non-Javadoc)
  127. *
  128. * @see org.aspectj.weaver.ReferenceTypeDelegate#isGeneric()
  129. */
  130. public boolean isGeneric() {
  131. // cant be generic in 1.4
  132. return false;
  133. }
  134. public boolean isAnonymous() {
  135. // this isn't in < Java 1.5 but I think we are moving beyond the need to support those levels
  136. return this.myClass.isAnonymousClass();
  137. }
  138. public boolean isNested() {
  139. // this isn't in < Java 1.5 but I think we are moving beyond the need to support those levels
  140. return this.myClass.isMemberClass();
  141. }
  142. public ResolvedType getOuterClass() {
  143. // this isn't in < Java 1.5 but I think we are moving beyond the need to support those levels
  144. return ReflectionBasedReferenceTypeDelegateFactory.resolveTypeInWorld(
  145. myClass.getEnclosingClass(),world);
  146. }
  147. /*
  148. * (non-Javadoc)
  149. *
  150. * @see org.aspectj.weaver.ReferenceTypeDelegate#isExposedToWeaver()
  151. */
  152. public boolean isExposedToWeaver() {
  153. // reflection based types are never exposed to the weaver
  154. return false;
  155. }
  156. /*
  157. * (non-Javadoc)
  158. *
  159. * @see org.aspectj.weaver.ReferenceTypeDelegate#hasAnnotation(org.aspectj.weaver .UnresolvedType)
  160. */
  161. public boolean hasAnnotation(UnresolvedType ofType) {
  162. // in Java 1.4 we cant have an annotation
  163. return false;
  164. }
  165. /*
  166. * (non-Javadoc)
  167. *
  168. * @see org.aspectj.weaver.ReferenceTypeDelegate#getAnnotations()
  169. */
  170. public AnnotationAJ[] getAnnotations() {
  171. // no annotations in Java 1.4
  172. return AnnotationAJ.EMPTY_ARRAY;
  173. }
  174. public boolean hasAnnotations() {
  175. return false;
  176. }
  177. /*
  178. * (non-Javadoc)
  179. *
  180. * @see org.aspectj.weaver.ReferenceTypeDelegate#getAnnotationTypes()
  181. */
  182. public ResolvedType[] getAnnotationTypes() {
  183. // no annotations in Java 1.4
  184. return ResolvedType.EMPTY_RESOLVED_TYPE_ARRAY;
  185. }
  186. /*
  187. * (non-Javadoc)
  188. *
  189. * @see org.aspectj.weaver.ReferenceTypeDelegate#getDeclaredFields()
  190. */
  191. public ResolvedMember[] getDeclaredFields() {
  192. if (fields == null) {
  193. Field[] reflectFields = this.myClass.getDeclaredFields();
  194. ResolvedMember[] rFields = new ResolvedMember[reflectFields.length];
  195. for (int i = 0; i < reflectFields.length; i++) {
  196. rFields[i] = ReflectionBasedReferenceTypeDelegateFactory.createResolvedMember(reflectFields[i], world);
  197. }
  198. this.fields = rFields;
  199. }
  200. return fields;
  201. }
  202. /*
  203. * (non-Javadoc)
  204. *
  205. * @see org.aspectj.weaver.ReferenceTypeDelegate#getDeclaredInterfaces()
  206. */
  207. public ResolvedType[] getDeclaredInterfaces() {
  208. if (interfaces == null) {
  209. Class[] reflectInterfaces = this.myClass.getInterfaces();
  210. ResolvedType[] rInterfaces = new ResolvedType[reflectInterfaces.length];
  211. for (int i = 0; i < reflectInterfaces.length; i++) {
  212. rInterfaces[i] = ReflectionBasedReferenceTypeDelegateFactory.resolveTypeInWorld(reflectInterfaces[i], world);
  213. }
  214. this.interfaces = rInterfaces;
  215. }
  216. return interfaces;
  217. }
  218. public boolean isCacheable() {
  219. return true;
  220. }
  221. /*
  222. * (non-Javadoc)
  223. *
  224. * @see org.aspectj.weaver.ReferenceTypeDelegate#getDeclaredMethods()
  225. */
  226. public ResolvedMember[] getDeclaredMethods() {
  227. if (methods == null) {
  228. Method[] reflectMethods = this.myClass.getDeclaredMethods();
  229. Constructor[] reflectCons = this.myClass.getDeclaredConstructors();
  230. ResolvedMember[] rMethods = new ResolvedMember[reflectMethods.length + reflectCons.length];
  231. for (int i = 0; i < reflectMethods.length; i++) {
  232. rMethods[i] = ReflectionBasedReferenceTypeDelegateFactory.createResolvedMember(reflectMethods[i], world);
  233. }
  234. for (int i = 0; i < reflectCons.length; i++) {
  235. rMethods[i + reflectMethods.length] = ReflectionBasedReferenceTypeDelegateFactory.createResolvedMember(
  236. reflectCons[i], world);
  237. }
  238. this.methods = rMethods;
  239. }
  240. return methods;
  241. }
  242. /*
  243. * (non-Javadoc)
  244. *
  245. * @see org.aspectj.weaver.ReferenceTypeDelegate#getDeclaredPointcuts()
  246. */
  247. public ResolvedMember[] getDeclaredPointcuts() {
  248. return ResolvedMember.NONE;
  249. }
  250. /*
  251. * (non-Javadoc)
  252. *
  253. * @see org.aspectj.weaver.ReferenceTypeDelegate#getTypeVariables()
  254. */
  255. public TypeVariable[] getTypeVariables() {
  256. // no type variables in Java 1.4
  257. return TypeVariable.NONE;
  258. }
  259. /*
  260. * (non-Javadoc)
  261. *
  262. * @see org.aspectj.weaver.ReferenceTypeDelegate#getPerClause()
  263. */
  264. public PerClause getPerClause() {
  265. // no per clause...
  266. return null;
  267. }
  268. public Collection<Declare> getDeclares() {
  269. return Collections.emptySet();
  270. }
  271. public Collection<ConcreteTypeMunger> getTypeMungers() {
  272. return Collections.emptySet();
  273. }
  274. /*
  275. * (non-Javadoc)
  276. *
  277. * @see org.aspectj.weaver.ReferenceTypeDelegate#getPrivilegedAccesses()
  278. */
  279. public Collection getPrivilegedAccesses() {
  280. // no aspect members..., not used for weaving
  281. return Collections.EMPTY_SET;
  282. }
  283. /*
  284. * (non-Javadoc)
  285. *
  286. * @see org.aspectj.weaver.ReferenceTypeDelegate#getModifiers()
  287. */
  288. public int getModifiers() {
  289. return this.myClass.getModifiers();
  290. }
  291. /*
  292. * (non-Javadoc)
  293. *
  294. * @see org.aspectj.weaver.ReferenceTypeDelegate#getSuperclass()
  295. */
  296. public ResolvedType getSuperclass() {
  297. if (this.myClass.getSuperclass() == null) {
  298. if (myClass == Object.class) {
  299. return null;
  300. }
  301. return world.resolve(UnresolvedType.OBJECT);
  302. }
  303. return ReflectionBasedReferenceTypeDelegateFactory.resolveTypeInWorld(this.myClass.getSuperclass(), world);
  304. }
  305. /*
  306. * (non-Javadoc)
  307. *
  308. * @see org.aspectj.weaver.ReferenceTypeDelegate#getWeaverState()
  309. */
  310. public WeaverStateInfo getWeaverState() {
  311. return null;
  312. }
  313. public ReferenceType getResolvedTypeX() {
  314. return this.resolvedType;
  315. }
  316. public boolean doesNotExposeShadowMungers() {
  317. return false;
  318. }
  319. public String getDeclaredGenericSignature() {
  320. // no generic sig in 1.4
  321. return null;
  322. }
  323. public ReflectionBasedResolvedMemberImpl createResolvedMemberFor(Member aMember) {
  324. return null;
  325. }
  326. public String getSourcefilename() {
  327. // crappy guess..
  328. return resolvedType.getName() + ".class";
  329. }
  330. public ISourceContext getSourceContext() {
  331. return SourceContextImpl.UNKNOWN_SOURCE_CONTEXT;
  332. }
  333. public boolean copySourceContext() {
  334. return true;
  335. }
  336. public int getCompilerVersion() {
  337. return WeaverVersionInfo.getCurrentWeaverMajorVersion();
  338. }
  339. public void ensureConsistent() {
  340. }
  341. public boolean isWeavable() {
  342. return false;
  343. }
  344. public boolean hasBeenWoven() {
  345. return false;
  346. }
  347. }