1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
|
/* *******************************************************************
* Copyright (c) 2005 Contributors.
* All rights reserved.
* This program and the accompanying materials are made available
* under the terms of the Eclipse Public License v1.0
* which accompanies this distribution and is available at
* http://eclipse.org/legal/epl-v10.html
*
* Contributors:
* Adrian Colyer Initial implementation
* ******************************************************************/
package org.aspectj.weaver.reflect;
import java.lang.annotation.Annotation;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.aspectj.apache.bcel.classfile.AnnotationDefault;
import org.aspectj.apache.bcel.classfile.Attribute;
import org.aspectj.apache.bcel.classfile.JavaClass;
import org.aspectj.apache.bcel.classfile.LocalVariable;
import org.aspectj.apache.bcel.classfile.LocalVariableTable;
import org.aspectj.apache.bcel.classfile.annotation.AnnotationGen;
import org.aspectj.apache.bcel.util.NonCachingClassLoaderRepository;
import org.aspectj.apache.bcel.util.Repository;
import org.aspectj.weaver.AnnotationAJ;
import org.aspectj.weaver.ResolvedType;
import org.aspectj.weaver.UnresolvedType;
import org.aspectj.weaver.World;
import org.aspectj.weaver.bcel.BcelAnnotation;
import org.aspectj.weaver.bcel.BcelWeakClassLoaderReference;
/**
* Find the given annotation (if present) on the given object
*
*/
public class Java15AnnotationFinder implements AnnotationFinder, ArgNameFinder {
private Repository bcelRepository;
private BcelWeakClassLoaderReference classLoaderRef;
private World world;
// must have no-arg constructor for reflective construction
public Java15AnnotationFinder() {
}
public void setClassLoader(ClassLoader aLoader) {
// TODO: No easy way to ask the world factory for the right kind of
// repository so
// default to the safe one! (pr160674)
this.classLoaderRef = new BcelWeakClassLoaderReference(aLoader);
this.bcelRepository = new NonCachingClassLoaderRepository(classLoaderRef);
}
public void setWorld(World aWorld) {
this.world = aWorld;
}
/*
* (non-Javadoc)
*
* @see org.aspectj.weaver.reflect.AnnotationFinder#getAnnotation(org.aspectj .weaver.ResolvedType, java.lang.Object)
*/
public Object getAnnotation(ResolvedType annotationType, Object onObject) {
try {
Class<? extends Annotation> annotationClass = (Class<? extends Annotation>) Class.forName(annotationType.getName(),
false, getClassLoader());
if (onObject.getClass().isAnnotationPresent(annotationClass)) {
return onObject.getClass().getAnnotation(annotationClass);
}
} catch (ClassNotFoundException ex) {
// just return null
}
return null;
}
public Object getAnnotationFromClass(ResolvedType annotationType, Class aClass) {
try {
Class<? extends Annotation> annotationClass = (Class<? extends Annotation>) Class.forName(annotationType.getName(),
false, getClassLoader());
if (aClass.isAnnotationPresent(annotationClass)) {
return aClass.getAnnotation(annotationClass);
}
} catch (ClassNotFoundException ex) {
// just return null
}
return null;
}
public Object getAnnotationFromMember(ResolvedType annotationType, Member aMember) {
if (!(aMember instanceof AccessibleObject))
return null;
AccessibleObject ao = (AccessibleObject) aMember;
try {
Class annotationClass = Class.forName(annotationType.getName(), false, getClassLoader());
if (ao.isAnnotationPresent(annotationClass)) {
return ao.getAnnotation(annotationClass);
}
} catch (ClassNotFoundException ex) {
// just return null
}
return null;
}
private ClassLoader getClassLoader() {
return classLoaderRef.getClassLoader();
}
public AnnotationAJ getAnnotationOfType(UnresolvedType ofType, Member onMember) {
if (!(onMember instanceof AccessibleObject))
return null;
// here we really want both the runtime visible AND the class visible
// annotations
// so we bail out to Bcel and then chuck away the JavaClass so that we
// don't hog
// memory.
try {
JavaClass jc = bcelRepository.loadClass(onMember.getDeclaringClass());
org.aspectj.apache.bcel.classfile.annotation.AnnotationGen[] anns = new org.aspectj.apache.bcel.classfile.annotation.AnnotationGen[0];
if (onMember instanceof Method) {
org.aspectj.apache.bcel.classfile.Method bcelMethod = jc.getMethod((Method) onMember);
if (bcelMethod == null) {
// pr220430
// System.err.println(
// "Unexpected problem in Java15AnnotationFinder: cannot retrieve annotations on method '"
// +
// onMember.getName()+"' in class '"+jc.getClassName()+"'");
} else {
anns = bcelMethod.getAnnotations();
}
} else if (onMember instanceof Constructor) {
org.aspectj.apache.bcel.classfile.Method bcelCons = jc.getMethod((Constructor) onMember);
anns = bcelCons.getAnnotations();
} else if (onMember instanceof Field) {
org.aspectj.apache.bcel.classfile.Field bcelField = jc.getField((Field) onMember);
anns = bcelField.getAnnotations();
}
// the answer is cached and we don't want to hold on to memory
bcelRepository.clear();
// OPTIMIZE make constant 0 size array for sharing
if (anns == null)
anns = new org.aspectj.apache.bcel.classfile.annotation.AnnotationGen[0];
// convert to our Annotation type
for (int i = 0; i < anns.length; i++) {
if (anns[i].getTypeSignature().equals(ofType.getSignature())) {
return new BcelAnnotation(anns[i], world);
}
}
return null;
} catch (ClassNotFoundException cnfEx) {
// just use reflection then
}
return null;
}
public String getAnnotationDefaultValue(Member onMember) {
try {
JavaClass jc = bcelRepository.loadClass(onMember.getDeclaringClass());
if (onMember instanceof Method) {
org.aspectj.apache.bcel.classfile.Method bcelMethod = jc.getMethod((Method) onMember);
if (bcelMethod == null) {
// pr220430
// System.err.println(
// "Unexpected problem in Java15AnnotationFinder: cannot retrieve annotations on method '"
// +
// onMember.getName()+"' in class '"+jc.getClassName()+"'");
} else {
Attribute[] attrs = bcelMethod.getAttributes();
for (int i = 0; i < attrs.length; i++) {
Attribute attribute = attrs[i];
if (attribute.getName().equals("AnnotationDefault")) {
AnnotationDefault def = (AnnotationDefault) attribute;
return def.getElementValue().stringifyValue();
}
}
return null;
}
}
} catch (ClassNotFoundException cnfEx) {
// just use reflection then
}
return null;
}
public Set getAnnotations(Member onMember) {
if (!(onMember instanceof AccessibleObject))
return Collections.EMPTY_SET;
// here we really want both the runtime visible AND the class visible
// annotations
// so we bail out to Bcel and then chuck away the JavaClass so that we
// don't hog
// memory.
try {
JavaClass jc = bcelRepository.loadClass(onMember.getDeclaringClass());
org.aspectj.apache.bcel.classfile.annotation.AnnotationGen[] anns = new org.aspectj.apache.bcel.classfile.annotation.AnnotationGen[0];
if (onMember instanceof Method) {
org.aspectj.apache.bcel.classfile.Method bcelMethod = jc.getMethod((Method) onMember);
if (bcelMethod == null) {
// fallback on reflection - see pr220430
// System.err.println(
// "Unexpected problem in Java15AnnotationFinder: cannot retrieve annotations on method '"
// +
// onMember.getName()+"' in class '"+jc.getClassName()+"'");
} else {
anns = bcelMethod.getAnnotations();
}
} else if (onMember instanceof Constructor) {
org.aspectj.apache.bcel.classfile.Method bcelCons = jc.getMethod((Constructor) onMember);
anns = bcelCons.getAnnotations();
} else if (onMember instanceof Field) {
org.aspectj.apache.bcel.classfile.Field bcelField = jc.getField((Field) onMember);
anns = bcelField.getAnnotations();
}
// the answer is cached and we don't want to hold on to memory
bcelRepository.clear();
// OPTIMIZE make this a constant 0 size array
if (anns == null)
anns = new org.aspectj.apache.bcel.classfile.annotation.AnnotationGen[0];
// convert to our Annotation type
Set<ResolvedType> annSet = new HashSet<ResolvedType>();
for (int i = 0; i < anns.length; i++) {
annSet.add(world.resolve(UnresolvedType.forSignature(anns[i].getTypeSignature())));
}
return annSet;
} catch (ClassNotFoundException cnfEx) {
// just use reflection then
}
AccessibleObject ao = (AccessibleObject) onMember;
Annotation[] anns = ao.getDeclaredAnnotations();
Set<UnresolvedType> annSet = new HashSet<UnresolvedType>();
for (int i = 0; i < anns.length; i++) {
annSet.add(UnresolvedType.forName(anns[i].annotationType().getName()).resolve(world));
}
return annSet;
}
public ResolvedType[] getAnnotations(Class forClass, World inWorld) {
// here we really want both the runtime visible AND the class visible
// annotations
// so we bail out to Bcel and then chuck away the JavaClass so that we
// don't hog
// memory.
try {
JavaClass jc = bcelRepository.loadClass(forClass);
org.aspectj.apache.bcel.classfile.annotation.AnnotationGen[] anns = jc.getAnnotations();
bcelRepository.clear();
if (anns == null)
return ResolvedType.NONE;
ResolvedType[] ret = new ResolvedType[anns.length];
for (int i = 0; i < ret.length; i++) {
ret[i] = inWorld.resolve(UnresolvedType.forSignature(anns[i].getTypeSignature()));
}
return ret;
} catch (ClassNotFoundException cnfEx) {
// just use reflection then
}
Annotation[] classAnnotations = forClass.getAnnotations();
ResolvedType[] ret = new ResolvedType[classAnnotations.length];
for (int i = 0; i < classAnnotations.length; i++) {
ret[i] = inWorld.resolve(classAnnotations[i].annotationType().getName());
}
return ret;
}
public String[] getParameterNames(Member forMember) {
if (!(forMember instanceof AccessibleObject))
return null;
try {
JavaClass jc = bcelRepository.loadClass(forMember.getDeclaringClass());
LocalVariableTable lvt = null;
int numVars = 0;
if (forMember instanceof Method) {
org.aspectj.apache.bcel.classfile.Method bcelMethod = jc.getMethod((Method) forMember);
lvt = bcelMethod.getLocalVariableTable();
numVars = bcelMethod.getArgumentTypes().length;
} else if (forMember instanceof Constructor) {
org.aspectj.apache.bcel.classfile.Method bcelCons = jc.getMethod((Constructor) forMember);
lvt = bcelCons.getLocalVariableTable();
numVars = bcelCons.getArgumentTypes().length;
}
return getParameterNamesFromLVT(lvt, numVars);
} catch (ClassNotFoundException cnfEx) {
; // no luck
}
return null;
}
private String[] getParameterNamesFromLVT(LocalVariableTable lvt, int numVars) {
if (lvt == null)
return null;// pr222987 - prevent NPE
LocalVariable[] vars = lvt.getLocalVariableTable();
if (vars.length < numVars) {
// basic error, we can't get the names...
return null;
}
String[] ret = new String[numVars];
for (int i = 0; i < numVars; i++) {
ret[i] = vars[i + 1].getName();
}
return ret;
}
public static final ResolvedType[][] NO_PARAMETER_ANNOTATIONS = new ResolvedType[][] {};
public ResolvedType[][] getParameterAnnotationTypes(Member onMember) {
if (!(onMember instanceof AccessibleObject))
return NO_PARAMETER_ANNOTATIONS;
// here we really want both the runtime visible AND the class visible
// annotations
// so we bail out to Bcel and then chuck away the JavaClass so that we
// don't hog memory.
try {
JavaClass jc = bcelRepository.loadClass(onMember.getDeclaringClass());
org.aspectj.apache.bcel.classfile.annotation.AnnotationGen[][] anns = null;
if (onMember instanceof Method) {
org.aspectj.apache.bcel.classfile.Method bcelMethod = jc.getMethod((Method) onMember);
if (bcelMethod == null) {
// pr220430
// System.err.println(
// "Unexpected problem in Java15AnnotationFinder: cannot retrieve annotations on method '"
// +
// onMember.getName()+"' in class '"+jc.getClassName()+"'");
} else {
anns = bcelMethod.getParameterAnnotations();
}
} else if (onMember instanceof Constructor) {
org.aspectj.apache.bcel.classfile.Method bcelCons = jc.getMethod((Constructor) onMember);
anns = bcelCons.getParameterAnnotations();
} else if (onMember instanceof Field) {
// anns = null;
}
// the answer is cached and we don't want to hold on to memory
bcelRepository.clear();
if (anns == null)
return NO_PARAMETER_ANNOTATIONS;
ResolvedType[][] result = new ResolvedType[anns.length][];
// CACHING??
for (int i = 0; i < anns.length; i++) {
if (anns[i] != null) {
result[i] = new ResolvedType[anns[i].length];
for (int j = 0; j < anns[i].length; j++) {
result[i][j] = world.resolve(UnresolvedType.forSignature(anns[i][j].getTypeSignature()));
}
}
}
return result;
} catch (ClassNotFoundException cnfEx) {
// just use reflection then
}
// reflection...
AccessibleObject ao = (AccessibleObject) onMember;
Annotation[][] anns = null;
if (onMember instanceof Method) {
anns = ((Method) ao).getParameterAnnotations();
} else if (onMember instanceof Constructor) {
anns = ((Constructor) ao).getParameterAnnotations();
} else if (onMember instanceof Field) {
// anns = null;
}
if (anns == null)
return NO_PARAMETER_ANNOTATIONS;
ResolvedType[][] result = new ResolvedType[anns.length][];
// CACHING??
for (int i = 0; i < anns.length; i++) {
if (anns[i] != null) {
result[i] = new ResolvedType[anns[i].length];
for (int j = 0; j < anns[i].length; j++) {
result[i][j] = UnresolvedType.forName(anns[i][j].annotationType().getName()).resolve(world);
}
}
}
return result;
}
public Object getParamAnnotation(Member onMember, int argsIndex, int paramAnnoIndex) {
if (!(onMember instanceof AccessibleObject))
return null;
// here we really want both the runtime visible AND the class visible
// annotations so we bail out to Bcel and then chuck away the JavaClass so that we
// don't hog memory.
// try {
// JavaClass jc = bcelRepository.loadClass(onMember.getDeclaringClass());
// org.aspectj.apache.bcel.classfile.annotation.AnnotationGen[][] anns = null;
// if (onMember instanceof Method) {
// org.aspectj.apache.bcel.classfile.Method bcelMethod = jc.getMethod((Method) onMember);
// if (bcelMethod == null) {
// // pr220430
// // System.err.println(
// // "Unexpected problem in Java15AnnotationFinder: cannot retrieve annotations on method '"
// // + onMember.getName()+"' in class '"+jc.getClassName()+"'");
// } else {
// anns = bcelMethod.getParameterAnnotations();
// }
// } else if (onMember instanceof Constructor) {
// org.aspectj.apache.bcel.classfile.Method bcelCons = jc.getMethod((Constructor) onMember);
// anns = bcelCons.getParameterAnnotations();
// } else if (onMember instanceof Field) {
// // anns = null;
// }
// // the answer is cached and we don't want to hold on to memory
// bcelRepository.clear();
// if (anns == null)
// return null;
//
// if (argsIndex>=anns.length) {
// return null;
// }
// AnnotationGen[] parameterAnnotationsOnParticularArg = anns[argsIndex];
// if (parameterAnnotationsOnParticularArg==null || paramAnnoIndex>=parameterAnnotationsOnParticularArg.length) {
// return null;
// }
// AnnotationGen parameterAnnotation = parameterAnnotationsOnParticularArg[paramAnnoIndex];
//// if (parameterAnnotation.getTypeSignature().equals(ofType.getSignature())) {
// return new BcelAnnotation(parameterAnnotation, world);
//// } else {
//// return null;
//// }
// } catch (ClassNotFoundException cnfEx) {
// // just use reflection then
// }
// reflection...
AccessibleObject ao = (AccessibleObject) onMember;
Annotation[][] anns = null;
if (onMember instanceof Method) {
anns = ((Method) ao).getParameterAnnotations();
} else if (onMember instanceof Constructor) {
anns = ((Constructor) ao).getParameterAnnotations();
} else if (onMember instanceof Field) {
// anns = null;
}
if (anns == null || argsIndex>=anns.length) {
return null;
}
Annotation[] parameterAnnotationsOnParticularArg = anns[argsIndex];
if (parameterAnnotationsOnParticularArg==null || paramAnnoIndex>=parameterAnnotationsOnParticularArg.length) {
return null;
}
return parameterAnnotationsOnParticularArg[paramAnnoIndex];
}
}
|