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

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