Browse Source

246125: c16

tags/V1_6_3rc1
aclement 15 years ago
parent
commit
c6d17d523c

+ 37
- 69
weaver5/java5-src/org/aspectj/weaver/reflect/Java15AnnotationFinder.java View File

import org.aspectj.weaver.AnnotationAJ; import org.aspectj.weaver.AnnotationAJ;
import org.aspectj.weaver.ResolvedType; import org.aspectj.weaver.ResolvedType;
import org.aspectj.weaver.UnresolvedType; import org.aspectj.weaver.UnresolvedType;
import org.aspectj.weaver.WeakClassLoaderReference;
import org.aspectj.weaver.World; import org.aspectj.weaver.World;
import org.aspectj.weaver.bcel.BcelAnnotation; import org.aspectj.weaver.bcel.BcelAnnotation;
import org.aspectj.weaver.bcel.BcelWeakClassLoaderReference;


/** /**
* Find the given annotation (if present) on the given object * Find the given annotation (if present) on the given object
public class Java15AnnotationFinder implements AnnotationFinder, ArgNameFinder { public class Java15AnnotationFinder implements AnnotationFinder, ArgNameFinder {


private Repository bcelRepository; private Repository bcelRepository;
private WeakClassLoaderReference classLoaderRef;
private BcelWeakClassLoaderReference classLoaderRef;
private World world; private World world;


// must have no-arg constructor for reflective construction // must have no-arg constructor for reflective construction
// TODO: No easy way to ask the world factory for the right kind of // TODO: No easy way to ask the world factory for the right kind of
// repository so // repository so
// default to the safe one! (pr160674) // default to the safe one! (pr160674)
this.classLoaderRef = new WeakClassLoaderReference(aLoader);
this.bcelRepository = new NonCachingClassLoaderRepository(
classLoaderRef);
this.classLoaderRef = new BcelWeakClassLoaderReference(aLoader);
this.bcelRepository = new NonCachingClassLoaderRepository(classLoaderRef);
} }


public void setWorld(World aWorld) { public void setWorld(World aWorld) {
/* /*
* (non-Javadoc) * (non-Javadoc)
* *
* @see
* org.aspectj.weaver.reflect.AnnotationFinder#getAnnotation(org.aspectj
* .weaver.ResolvedType, java.lang.Object)
* @see org.aspectj.weaver.reflect.AnnotationFinder#getAnnotation(org.aspectj .weaver.ResolvedType, java.lang.Object)
*/ */
public Object getAnnotation(ResolvedType annotationType, Object onObject) { public Object getAnnotation(ResolvedType annotationType, Object onObject) {
try { try {
Class<? extends Annotation> annotationClass = (Class<? extends Annotation>) Class
.forName(annotationType.getName(), false, getClassLoader());
Class<? extends Annotation> annotationClass = (Class<? extends Annotation>) Class.forName(annotationType.getName(),
false, getClassLoader());
if (onObject.getClass().isAnnotationPresent(annotationClass)) { if (onObject.getClass().isAnnotationPresent(annotationClass)) {
return onObject.getClass().getAnnotation(annotationClass); return onObject.getClass().getAnnotation(annotationClass);
} }
return null; return null;
} }


public Object getAnnotationFromClass(ResolvedType annotationType,
Class aClass) {
public Object getAnnotationFromClass(ResolvedType annotationType, Class aClass) {
try { try {
Class<? extends Annotation> annotationClass = (Class<? extends Annotation>) Class
.forName(annotationType.getName(), false, getClassLoader());
Class<? extends Annotation> annotationClass = (Class<? extends Annotation>) Class.forName(annotationType.getName(),
false, getClassLoader());
if (aClass.isAnnotationPresent(annotationClass)) { if (aClass.isAnnotationPresent(annotationClass)) {
return aClass.getAnnotation(annotationClass); return aClass.getAnnotation(annotationClass);
} }
return null; return null;
} }


public Object getAnnotationFromMember(ResolvedType annotationType,
Member aMember) {
public Object getAnnotationFromMember(ResolvedType annotationType, Member aMember) {
if (!(aMember instanceof AccessibleObject)) if (!(aMember instanceof AccessibleObject))
return null; return null;
AccessibleObject ao = (AccessibleObject) aMember; AccessibleObject ao = (AccessibleObject) aMember;
try { try {
Class annotationClass = Class.forName(annotationType.getName(),
false, getClassLoader());
Class annotationClass = Class.forName(annotationType.getName(), false, getClassLoader());
if (ao.isAnnotationPresent(annotationClass)) { if (ao.isAnnotationPresent(annotationClass)) {
return ao.getAnnotation(annotationClass); return ao.getAnnotation(annotationClass);
} }
return classLoaderRef.getClassLoader(); return classLoaderRef.getClassLoader();
} }


public AnnotationAJ getAnnotationOfType(UnresolvedType ofType,
Member onMember) {
public AnnotationAJ getAnnotationOfType(UnresolvedType ofType, Member onMember) {
if (!(onMember instanceof AccessibleObject)) if (!(onMember instanceof AccessibleObject))
return null; return null;
// here we really want both the runtime visible AND the class visible // here we really want both the runtime visible AND the class visible
// don't hog // don't hog
// memory. // memory.
try { try {
JavaClass jc = bcelRepository.loadClass(onMember
.getDeclaringClass());
JavaClass jc = bcelRepository.loadClass(onMember.getDeclaringClass());
org.aspectj.apache.bcel.classfile.annotation.AnnotationGen[] anns = new org.aspectj.apache.bcel.classfile.annotation.AnnotationGen[0]; org.aspectj.apache.bcel.classfile.annotation.AnnotationGen[] anns = new org.aspectj.apache.bcel.classfile.annotation.AnnotationGen[0];
if (onMember instanceof Method) { if (onMember instanceof Method) {
org.aspectj.apache.bcel.classfile.Method bcelMethod = jc
.getMethod((Method) onMember);
org.aspectj.apache.bcel.classfile.Method bcelMethod = jc.getMethod((Method) onMember);
if (bcelMethod == null) { if (bcelMethod == null) {
// pr220430 // pr220430
// System.err.println( // System.err.println(
anns = bcelMethod.getAnnotations(); anns = bcelMethod.getAnnotations();
} }
} else if (onMember instanceof Constructor) { } else if (onMember instanceof Constructor) {
org.aspectj.apache.bcel.classfile.Method bcelCons = jc
.getMethod((Constructor) onMember);
org.aspectj.apache.bcel.classfile.Method bcelCons = jc.getMethod((Constructor) onMember);
anns = bcelCons.getAnnotations(); anns = bcelCons.getAnnotations();
} else if (onMember instanceof Field) { } else if (onMember instanceof Field) {
org.aspectj.apache.bcel.classfile.Field bcelField = jc
.getField((Field) onMember);
org.aspectj.apache.bcel.classfile.Field bcelField = jc.getField((Field) onMember);
anns = bcelField.getAnnotations(); anns = bcelField.getAnnotations();
} }
// the answer is cached and we don't want to hold on to memory // the answer is cached and we don't want to hold on to memory


public String getAnnotationDefaultValue(Member onMember) { public String getAnnotationDefaultValue(Member onMember) {
try { try {
JavaClass jc = bcelRepository.loadClass(onMember
.getDeclaringClass());
JavaClass jc = bcelRepository.loadClass(onMember.getDeclaringClass());
if (onMember instanceof Method) { if (onMember instanceof Method) {
org.aspectj.apache.bcel.classfile.Method bcelMethod = jc
.getMethod((Method) onMember);
org.aspectj.apache.bcel.classfile.Method bcelMethod = jc.getMethod((Method) onMember);


if (bcelMethod == null) { if (bcelMethod == null) {
// pr220430 // pr220430
// don't hog // don't hog
// memory. // memory.
try { try {
JavaClass jc = bcelRepository.loadClass(onMember
.getDeclaringClass());
JavaClass jc = bcelRepository.loadClass(onMember.getDeclaringClass());
org.aspectj.apache.bcel.classfile.annotation.AnnotationGen[] anns = new org.aspectj.apache.bcel.classfile.annotation.AnnotationGen[0]; org.aspectj.apache.bcel.classfile.annotation.AnnotationGen[] anns = new org.aspectj.apache.bcel.classfile.annotation.AnnotationGen[0];
if (onMember instanceof Method) { if (onMember instanceof Method) {
org.aspectj.apache.bcel.classfile.Method bcelMethod = jc
.getMethod((Method) onMember);
org.aspectj.apache.bcel.classfile.Method bcelMethod = jc.getMethod((Method) onMember);
if (bcelMethod == null) { if (bcelMethod == null) {
// fallback on reflection - see pr220430 // fallback on reflection - see pr220430
// System.err.println( // System.err.println(
anns = bcelMethod.getAnnotations(); anns = bcelMethod.getAnnotations();
} }
} else if (onMember instanceof Constructor) { } else if (onMember instanceof Constructor) {
org.aspectj.apache.bcel.classfile.Method bcelCons = jc
.getMethod((Constructor) onMember);
org.aspectj.apache.bcel.classfile.Method bcelCons = jc.getMethod((Constructor) onMember);
anns = bcelCons.getAnnotations(); anns = bcelCons.getAnnotations();
} else if (onMember instanceof Field) { } else if (onMember instanceof Field) {
org.aspectj.apache.bcel.classfile.Field bcelField = jc
.getField((Field) onMember);
org.aspectj.apache.bcel.classfile.Field bcelField = jc.getField((Field) onMember);
anns = bcelField.getAnnotations(); anns = bcelField.getAnnotations();
} }
// the answer is cached and we don't want to hold on to memory // the answer is cached and we don't want to hold on to memory
// convert to our Annotation type // convert to our Annotation type
Set<ResolvedType> annSet = new HashSet<ResolvedType>(); Set<ResolvedType> annSet = new HashSet<ResolvedType>();
for (int i = 0; i < anns.length; i++) { for (int i = 0; i < anns.length; i++) {
annSet.add(world.resolve(UnresolvedType.forSignature(anns[i]
.getTypeSignature())));
annSet.add(world.resolve(UnresolvedType.forSignature(anns[i].getTypeSignature())));
} }
return annSet; return annSet;
} catch (ClassNotFoundException cnfEx) { } catch (ClassNotFoundException cnfEx) {
Annotation[] anns = ao.getDeclaredAnnotations(); Annotation[] anns = ao.getDeclaredAnnotations();
Set<UnresolvedType> annSet = new HashSet<UnresolvedType>(); Set<UnresolvedType> annSet = new HashSet<UnresolvedType>();
for (int i = 0; i < anns.length; i++) { for (int i = 0; i < anns.length; i++) {
annSet.add(UnresolvedType.forName(
anns[i].annotationType().getName()).resolve(world));
annSet.add(UnresolvedType.forName(anns[i].annotationType().getName()).resolve(world));
} }
return annSet; return annSet;
} }
// memory. // memory.
try { try {
JavaClass jc = bcelRepository.loadClass(forClass); JavaClass jc = bcelRepository.loadClass(forClass);
org.aspectj.apache.bcel.classfile.annotation.AnnotationGen[] anns = jc
.getAnnotations();
org.aspectj.apache.bcel.classfile.annotation.AnnotationGen[] anns = jc.getAnnotations();
bcelRepository.clear(); bcelRepository.clear();
if (anns == null) if (anns == null)
return new ResolvedType[0]; return new ResolvedType[0];
ResolvedType[] ret = new ResolvedType[anns.length]; ResolvedType[] ret = new ResolvedType[anns.length];
for (int i = 0; i < ret.length; i++) { for (int i = 0; i < ret.length; i++) {
ret[i] = inWorld.resolve(UnresolvedType.forSignature(anns[i]
.getTypeSignature()));
ret[i] = inWorld.resolve(UnresolvedType.forSignature(anns[i].getTypeSignature()));
} }
return ret; return ret;
} catch (ClassNotFoundException cnfEx) { } catch (ClassNotFoundException cnfEx) {
Annotation[] classAnnotations = forClass.getAnnotations(); Annotation[] classAnnotations = forClass.getAnnotations();
ResolvedType[] ret = new ResolvedType[classAnnotations.length]; ResolvedType[] ret = new ResolvedType[classAnnotations.length];
for (int i = 0; i < classAnnotations.length; i++) { for (int i = 0; i < classAnnotations.length; i++) {
ret[i] = inWorld.resolve(classAnnotations[i].annotationType()
.getName());
ret[i] = inWorld.resolve(classAnnotations[i].annotationType().getName());
} }


return ret; return ret;
return null; return null;


try { try {
JavaClass jc = bcelRepository.loadClass(forMember
.getDeclaringClass());
JavaClass jc = bcelRepository.loadClass(forMember.getDeclaringClass());
LocalVariableTable lvt = null; LocalVariableTable lvt = null;
int numVars = 0; int numVars = 0;
if (forMember instanceof Method) { if (forMember instanceof Method) {
org.aspectj.apache.bcel.classfile.Method bcelMethod = jc
.getMethod((Method) forMember);
org.aspectj.apache.bcel.classfile.Method bcelMethod = jc.getMethod((Method) forMember);
lvt = bcelMethod.getLocalVariableTable(); lvt = bcelMethod.getLocalVariableTable();
numVars = bcelMethod.getArgumentTypes().length; numVars = bcelMethod.getArgumentTypes().length;
} else if (forMember instanceof Constructor) { } else if (forMember instanceof Constructor) {
org.aspectj.apache.bcel.classfile.Method bcelCons = jc
.getMethod((Constructor) forMember);
org.aspectj.apache.bcel.classfile.Method bcelCons = jc.getMethod((Constructor) forMember);
lvt = bcelCons.getLocalVariableTable(); lvt = bcelCons.getLocalVariableTable();
numVars = bcelCons.getArgumentTypes().length; numVars = bcelCons.getArgumentTypes().length;
} }
return null; return null;
} }


private String[] getParameterNamesFromLVT(LocalVariableTable lvt,
int numVars) {
private String[] getParameterNamesFromLVT(LocalVariableTable lvt, int numVars) {
if (lvt == null) if (lvt == null)
return null;// pr222987 - prevent NPE return null;// pr222987 - prevent NPE
LocalVariable[] vars = lvt.getLocalVariableTable(); LocalVariable[] vars = lvt.getLocalVariableTable();
// don't hog // don't hog
// memory. // memory.
try { try {
JavaClass jc = bcelRepository.loadClass(onMember
.getDeclaringClass());
JavaClass jc = bcelRepository.loadClass(onMember.getDeclaringClass());
org.aspectj.apache.bcel.classfile.annotation.AnnotationGen[][] anns = null; org.aspectj.apache.bcel.classfile.annotation.AnnotationGen[][] anns = null;
if (onMember instanceof Method) { if (onMember instanceof Method) {
org.aspectj.apache.bcel.classfile.Method bcelMethod = jc
.getMethod((Method) onMember);
org.aspectj.apache.bcel.classfile.Method bcelMethod = jc.getMethod((Method) onMember);
if (bcelMethod == null) { if (bcelMethod == null) {
// pr220430 // pr220430
// System.err.println( // System.err.println(
anns = bcelMethod.getParameterAnnotations(); anns = bcelMethod.getParameterAnnotations();
} }
} else if (onMember instanceof Constructor) { } else if (onMember instanceof Constructor) {
org.aspectj.apache.bcel.classfile.Method bcelCons = jc
.getMethod((Constructor) onMember);
org.aspectj.apache.bcel.classfile.Method bcelCons = jc.getMethod((Constructor) onMember);
anns = bcelCons.getParameterAnnotations(); anns = bcelCons.getParameterAnnotations();
} else if (onMember instanceof Field) { } else if (onMember instanceof Field) {
// anns = null; // anns = null;
if (anns[i] != null) { if (anns[i] != null) {
result[i] = new ResolvedType[anns[i].length]; result[i] = new ResolvedType[anns[i].length];
for (int j = 0; j < anns[i].length; j++) { for (int j = 0; j < anns[i].length; j++) {
result[i][j] = world.resolve(UnresolvedType
.forSignature(anns[i][j].getTypeSignature()));
result[i][j] = world.resolve(UnresolvedType.forSignature(anns[i][j].getTypeSignature()));
} }
} }
} }
if (anns[i] != null) { if (anns[i] != null) {
result[i] = new ResolvedType[anns[i].length]; result[i] = new ResolvedType[anns[i].length];
for (int j = 0; j < anns[i].length; j++) { for (int j = 0; j < anns[i].length; j++) {
result[i][j] = UnresolvedType.forName(
anns[i][j].annotationType().getName()).resolve(
world);
result[i][j] = UnresolvedType.forName(anns[i][j].annotationType().getName()).resolve(world);
} }
} }
} }

Loading…
Cancel
Save