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

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