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