diff options
author | acolyer <acolyer> | 2005-12-06 01:11:13 +0000 |
---|---|---|
committer | acolyer <acolyer> | 2005-12-06 01:11:13 +0000 |
commit | 5611db4f27a752ef5b65f61b614eb3d5ca30bb01 (patch) | |
tree | 9b5d55879d5d1f1f22e9e786e057513fa59da0a5 /weaver5/java5-src | |
parent | 278718a6d3bd0c15ebdb9fcf149811575fc99454 (diff) | |
download | aspectj-5611db4f27a752ef5b65f61b614eb3d5ca30bb01.tar.gz aspectj-5611db4f27a752ef5b65f61b614eb3d5ca30bb01.zip |
fixes getGenericXXX methods in Reflection-based resolved member impl, GenericSignatureInformationProvider allows this to work across both 1.4 and 1.5
Diffstat (limited to 'weaver5/java5-src')
4 files changed, 254 insertions, 93 deletions
diff --git a/weaver5/java5-src/org/aspectj/weaver/reflect/Java15AnnotationFinder.java b/weaver5/java5-src/org/aspectj/weaver/reflect/Java15AnnotationFinder.java index 40985bd26..1453e05ff 100644 --- a/weaver5/java5-src/org/aspectj/weaver/reflect/Java15AnnotationFinder.java +++ b/weaver5/java5-src/org/aspectj/weaver/reflect/Java15AnnotationFinder.java @@ -36,6 +36,7 @@ public class Java15AnnotationFinder implements AnnotationFinder { private Repository bcelRepository; private ClassLoader classLoader; + private World world; // must have no-arg constructor for reflective construction public Java15AnnotationFinder() { @@ -46,6 +47,10 @@ public class Java15AnnotationFinder implements AnnotationFinder { this.classLoader = aLoader; } + public void setWorld(World aWorld) { + this.world = aWorld; + } + /* (non-Javadoc) * @see org.aspectj.weaver.reflect.AnnotationFinder#getAnnotation(org.aspectj.weaver.ResolvedType, java.lang.Object) */ @@ -109,9 +114,9 @@ public class Java15AnnotationFinder implements AnnotationFinder { bcelRepository.clear(); if (anns == null) anns = new org.aspectj.apache.bcel.classfile.annotation.Annotation[0]; // convert to our Annotation type - Set<UnresolvedType> annSet = new HashSet<UnresolvedType>(); + Set<ResolvedType> annSet = new HashSet<ResolvedType>(); for (int i = 0; i < anns.length; i++) { - annSet.add(UnresolvedType.forName(anns[i].getTypeName())); + annSet.add(UnresolvedType.forName(anns[i].getTypeName()).resolve(world)); } return annSet; } catch (ClassNotFoundException cnfEx) { @@ -123,7 +128,7 @@ public class Java15AnnotationFinder implements AnnotationFinder { 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())); + annSet.add(UnresolvedType.forName(anns[i].annotationType().getName()).resolve(world)); } return annSet; } diff --git a/weaver5/java5-src/org/aspectj/weaver/reflect/Java15GenericSignatureInformationProvider.java b/weaver5/java5-src/org/aspectj/weaver/reflect/Java15GenericSignatureInformationProvider.java new file mode 100644 index 000000000..c9de9517e --- /dev/null +++ b/weaver5/java5-src/org/aspectj/weaver/reflect/Java15GenericSignatureInformationProvider.java @@ -0,0 +1,103 @@ +/* ******************************************************************* + * 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.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Member; +import java.lang.reflect.Method; +import java.lang.reflect.Type; + +import org.aspectj.weaver.UnresolvedType; +import org.aspectj.weaver.World; + +/** + * Uses Java 1.5 reflection APIs to determine generic signatures + */ +public class Java15GenericSignatureInformationProvider implements + GenericSignatureInformationProvider { + + private final World world; + + public Java15GenericSignatureInformationProvider(World forWorld) { + this.world = forWorld; + } + + /* (non-Javadoc) + * @see org.aspectj.weaver.reflect.GenericSignatureInformationProvider#getGenericParameterTypes(org.aspectj.weaver.reflect.ReflectionBasedResolvedMemberImpl) + */ + public UnresolvedType[] getGenericParameterTypes( + ReflectionBasedResolvedMemberImpl resolvedMember) { + JavaLangTypeToResolvedTypeConverter typeConverter = new JavaLangTypeToResolvedTypeConverter(world); + Type[] pTypes = new Type[0]; + Member member = resolvedMember.getMember(); + if (member instanceof Method) { + pTypes = ((Method)member).getGenericParameterTypes(); + } else if (member instanceof Constructor) { + pTypes = ((Constructor)member).getGenericParameterTypes(); + } + return typeConverter.fromTypes(pTypes); + } + + /* (non-Javadoc) + * @see org.aspectj.weaver.reflect.GenericSignatureInformationProvider#getGenericReturnType(org.aspectj.weaver.reflect.ReflectionBasedResolvedMemberImpl) + */ + public UnresolvedType getGenericReturnType( + ReflectionBasedResolvedMemberImpl resolvedMember) { + JavaLangTypeToResolvedTypeConverter typeConverter = new JavaLangTypeToResolvedTypeConverter(world); + Member member = resolvedMember.getMember(); + if (member instanceof Field) { + return typeConverter.fromType(((Field)member).getGenericType()); + } else if (member instanceof Method) { + return typeConverter.fromType(((Method)member).getGenericReturnType()); + } else if (member instanceof Constructor) { + return typeConverter.fromType(((Constructor)member).getDeclaringClass()); + } else { + throw new IllegalStateException("unexpected member type: " + member); + } + } + + /* (non-Javadoc) + * @see org.aspectj.weaver.reflect.GenericSignatureInformationProvider#isBridge() + */ + public boolean isBridge(ReflectionBasedResolvedMemberImpl resolvedMember) { + Member member = resolvedMember.getMember(); + if (member instanceof Method) { + return ((Method)member).isBridge(); + } else { + return false; + } + } + + /* (non-Javadoc) + * @see org.aspectj.weaver.reflect.GenericSignatureInformationProvider#isVarArgs() + */ + public boolean isVarArgs(ReflectionBasedResolvedMemberImpl resolvedMember) { + Member member = resolvedMember.getMember(); + if (member instanceof Method) { + return ((Method)member).isVarArgs(); + } else if (member instanceof Constructor) { + return ((Constructor)member).isVarArgs(); + } else { + return false; + } + } + + /* (non-Javadoc) + * @see org.aspectj.weaver.reflect.GenericSignatureInformationProvider#isSynthetic() + */ + public boolean isSynthetic(ReflectionBasedResolvedMemberImpl resolvedMember) { + Member member = resolvedMember.getMember(); + return member.isSynthetic(); + } + +} diff --git a/weaver5/java5-src/org/aspectj/weaver/reflect/Java15ReflectionBasedReferenceTypeDelegate.java b/weaver5/java5-src/org/aspectj/weaver/reflect/Java15ReflectionBasedReferenceTypeDelegate.java index 57909f6df..502dd80fb 100644 --- a/weaver5/java5-src/org/aspectj/weaver/reflect/Java15ReflectionBasedReferenceTypeDelegate.java +++ b/weaver5/java5-src/org/aspectj/weaver/reflect/Java15ReflectionBasedReferenceTypeDelegate.java @@ -59,6 +59,7 @@ public class Java15ReflectionBasedReferenceTypeDelegate extends private ResolvedType superclass; private ResolvedType[] superInterfaces; private String genericSignature = null; + private JavaLangTypeToResolvedTypeConverter typeConverter; private Java15AnnotationFinder annotationFinder = null; @@ -70,6 +71,7 @@ public class Java15ReflectionBasedReferenceTypeDelegate extends myType = AjTypeSystem.getAjType(aClass); annotationFinder = new Java15AnnotationFinder(); annotationFinder.setClassLoader(classLoader); + this.typeConverter = new JavaLangTypeToResolvedTypeConverter(aWorld); } @@ -124,7 +126,7 @@ public class Java15ReflectionBasedReferenceTypeDelegate extends public ResolvedType[] getDeclaredInterfaces() { if (superInterfaces == null) { Type[] genericInterfaces = getBaseClass().getGenericInterfaces(); - this.superInterfaces = fromTypes(genericInterfaces); + this.superInterfaces = typeConverter.fromTypes(genericInterfaces); } return superInterfaces; } @@ -133,7 +135,7 @@ public class Java15ReflectionBasedReferenceTypeDelegate extends public ResolvedType getSuperclass() { if (superclass == null && getBaseClass()!=Object.class) {// superclass of Object is null Type t = this.getBaseClass().getGenericSuperclass(); - if (t!=null) superclass = fromType(t); + if (t!=null) superclass = typeConverter.fromType(t); if (t==null) superclass = getWorld().resolve(UnresolvedType.OBJECT); } return superclass; @@ -156,7 +158,7 @@ public class Java15ReflectionBasedReferenceTypeDelegate extends this.getResolvedTypeX().getWorld().recordTypeVariablesCurrentlyBeingProcessed(getBaseClass(),typeVariables); // now fill in the details... for (int i = 0; i < tVars.length; i++) { - TypeVariableReferenceType tvrt = ((TypeVariableReferenceType) fromType(tVars[i])); + TypeVariableReferenceType tvrt = ((TypeVariableReferenceType) typeConverter.fromType(tVars[i])); TypeVariable tv = tvrt.getTypeVariable(); typeVariables[i].setUpperBound(tv.getUpperBound()); typeVariables[i].setAdditionalInterfaceBounds(tv.getAdditionalInterfaceBounds()); @@ -202,13 +204,14 @@ public class Java15ReflectionBasedReferenceTypeDelegate extends new ReflectionBasedResolvedMemberImpl(org.aspectj.weaver.Member.METHOD, getGenericResolvedType(), forMethod.getModifiers(), - fromType(forMethod.getGenericReturnType()), + typeConverter.fromType(forMethod.getReturnType()), forMethod.getName(), - fromTypes(forMethod.getGenericParameterTypes()), - fromTypes(forMethod.getGenericExceptionTypes()), + typeConverter.fromTypes(forMethod.getParameterTypes()), + typeConverter.fromTypes(forMethod.getExceptionTypes()), forMethod ); ret.setAnnotationFinder(this.annotationFinder); + ret.setGenericSignatureInformationProvider(new Java15GenericSignatureInformationProvider(this.getWorld())); return ret; } @@ -219,11 +222,12 @@ public class Java15ReflectionBasedReferenceTypeDelegate extends forConstructor.getModifiers(), getGenericResolvedType(), "init", - fromTypes(forConstructor.getGenericParameterTypes()), - fromTypes(forConstructor.getGenericExceptionTypes()), + typeConverter.fromTypes(forConstructor.getParameterTypes()), + typeConverter.fromTypes(forConstructor.getExceptionTypes()), forConstructor ); ret.setAnnotationFinder(this.annotationFinder); + ret.setGenericSignatureInformationProvider(new Java15GenericSignatureInformationProvider(this.getWorld())); return ret; } @@ -233,11 +237,12 @@ public class Java15ReflectionBasedReferenceTypeDelegate extends org.aspectj.weaver.Member.FIELD, getGenericResolvedType(), forField.getModifiers(), - fromType(forField.getGenericType()), + typeConverter.fromType(forField.getType()), forField.getName(), new UnresolvedType[0], forField); ret.setAnnotationFinder(this.annotationFinder); + ret.setGenericSignatureInformationProvider(new Java15GenericSignatureInformationProvider(this.getWorld())); return ret; } @@ -300,87 +305,6 @@ public class Java15ReflectionBasedReferenceTypeDelegate extends //return false; // for now return getBaseClass().getTypeParameters().length > 0; } - - // Used to prevent recursion - we record what we are working on and return it if asked again *whilst* working on it - private Map /*java.lang.reflect.TypeVariable > TypeVariableReferenceType */typeVariablesInProgress = new HashMap(); - - private ResolvedType fromType(Type aType) { - if (aType instanceof Class) { - Class clazz = (Class)aType; - String name = clazz.getName(); - /** - * getName() can return: - * - * 1. If this class object represents a reference type that is not an array type - * then the binary name of the class is returned - * 2. If this class object represents a primitive type or void, then the - * name returned is a String equal to the Java language keyword corresponding to the primitive type or void. - * 3. If this class object represents a class of arrays, then the internal form - * of the name consists of the name of the element type preceded by one or more '[' characters representing the depth of the array nesting. - */ - if (clazz.isArray()) { - UnresolvedType ut = UnresolvedType.forSignature(name); - return getWorld().resolve(ut); - } else { - return getWorld().resolve(name); - } - } else if (aType instanceof ParameterizedType) { - ParameterizedType pt = (ParameterizedType) aType; - ResolvedType baseType = fromType(pt.getRawType()); - Type[] args = pt.getActualTypeArguments(); - ResolvedType[] resolvedArgs = fromTypes(args); - return TypeFactory.createParameterizedType(baseType, resolvedArgs, getWorld()); - } else if (aType instanceof java.lang.reflect.TypeVariable) { - if (typeVariablesInProgress.get(aType)!=null) // check if we are already working on this type - return (TypeVariableReferenceType)typeVariablesInProgress.get(aType); - - java.lang.reflect.TypeVariable tv = (java.lang.reflect.TypeVariable) aType; - TypeVariable rt_tv = new TypeVariable(tv.getName()); - TypeVariableReferenceType tvrt = new TypeVariableReferenceType(rt_tv,getWorld()); - - typeVariablesInProgress.put(aType,tvrt); // record what we are working on, for recursion case - - Type[] bounds = tv.getBounds(); - ResolvedType[] resBounds = fromTypes(bounds); - ResolvedType upperBound = resBounds[0]; - ResolvedType[] additionalBounds = new ResolvedType[0]; - if (resBounds.length > 1) { - additionalBounds = new ResolvedType[resBounds.length - 1]; - System.arraycopy(resBounds,1,additionalBounds,0,additionalBounds.length); - } - rt_tv.setUpperBound(upperBound); - rt_tv.setAdditionalInterfaceBounds(additionalBounds); - - typeVariablesInProgress.remove(aType); // we have finished working on it - - return tvrt; - } else if (aType instanceof WildcardType) { - WildcardType wildType = (WildcardType) aType; - Type[] lowerBounds = wildType.getLowerBounds(); - Type[] upperBounds = wildType.getUpperBounds(); - ResolvedType bound = null; - boolean isExtends = lowerBounds.length == 0; - if (isExtends) { - bound = fromType(upperBounds[0]); - } else { - bound = fromType(lowerBounds[0]); - } - return new BoundedReferenceType((ReferenceType)bound,isExtends,getWorld()); - } else if (aType instanceof GenericArrayType) { - GenericArrayType gt = (GenericArrayType) aType; - Type componentType = gt.getGenericComponentType(); - return UnresolvedType.makeArray(fromType(componentType),1).resolve(getWorld()); - } - return ResolvedType.MISSING; - } - - private ResolvedType[] fromTypes(Type[] types) { - ResolvedType[] ret = new ResolvedType[types.length]; - for (int i = 0; i < ret.length; i++) { - ret[i] = fromType(types[i]); - } - return ret; - } @Override public boolean isAnonymous() { diff --git a/weaver5/java5-src/org/aspectj/weaver/reflect/JavaLangTypeToResolvedTypeConverter.java b/weaver5/java5-src/org/aspectj/weaver/reflect/JavaLangTypeToResolvedTypeConverter.java new file mode 100644 index 000000000..d4f352d6a --- /dev/null +++ b/weaver5/java5-src/org/aspectj/weaver/reflect/JavaLangTypeToResolvedTypeConverter.java @@ -0,0 +1,129 @@ +/* ******************************************************************* + * 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.reflect.GenericArrayType; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.lang.reflect.WildcardType; +import java.util.HashMap; +import java.util.Map; + +import org.aspectj.weaver.BoundedReferenceType; +import org.aspectj.weaver.ReferenceType; +import org.aspectj.weaver.ResolvedType; +import org.aspectj.weaver.TypeFactory; +import org.aspectj.weaver.TypeVariable; +import org.aspectj.weaver.TypeVariableReferenceType; +import org.aspectj.weaver.UnresolvedType; +import org.aspectj.weaver.World; + +/** + * Handles the translation of java.lang.reflect.Type objects into + * AspectJ UnresolvedTypes. + * + */ +public class JavaLangTypeToResolvedTypeConverter { + + // Used to prevent recursion - we record what we are working on and return it if asked again *whilst* working on it + private Map<Type,TypeVariableReferenceType> typeVariablesInProgress + = new HashMap<Type,TypeVariableReferenceType>(); + private final World world; + + public JavaLangTypeToResolvedTypeConverter(World aWorld) { + this.world = aWorld; + } + + private World getWorld() { + return this.world; + } + + public ResolvedType fromType(Type aType) { + if (aType instanceof Class) { + Class clazz = (Class)aType; + String name = clazz.getName(); + /** + * getName() can return: + * + * 1. If this class object represents a reference type that is not an array type + * then the binary name of the class is returned + * 2. If this class object represents a primitive type or void, then the + * name returned is a String equal to the Java language keyword corresponding to the primitive type or void. + * 3. If this class object represents a class of arrays, then the internal form + * of the name consists of the name of the element type preceded by one or more '[' characters representing the depth of the array nesting. + */ + if (clazz.isArray()) { + UnresolvedType ut = UnresolvedType.forSignature(name.replace('.','/')); + return getWorld().resolve(ut); + } else { + return getWorld().resolve(name); + } + } else if (aType instanceof ParameterizedType) { + ParameterizedType pt = (ParameterizedType) aType; + ResolvedType baseType = fromType(pt.getRawType()); + Type[] args = pt.getActualTypeArguments(); + ResolvedType[] resolvedArgs = fromTypes(args); + return TypeFactory.createParameterizedType(baseType, resolvedArgs, getWorld()); + } else if (aType instanceof java.lang.reflect.TypeVariable) { + if (typeVariablesInProgress.get(aType)!=null) // check if we are already working on this type + return typeVariablesInProgress.get(aType); + + java.lang.reflect.TypeVariable tv = (java.lang.reflect.TypeVariable) aType; + TypeVariable rt_tv = new TypeVariable(tv.getName()); + TypeVariableReferenceType tvrt = new TypeVariableReferenceType(rt_tv,getWorld()); + + typeVariablesInProgress.put(aType,tvrt); // record what we are working on, for recursion case + + Type[] bounds = tv.getBounds(); + ResolvedType[] resBounds = fromTypes(bounds); + ResolvedType upperBound = resBounds[0]; + ResolvedType[] additionalBounds = new ResolvedType[0]; + if (resBounds.length > 1) { + additionalBounds = new ResolvedType[resBounds.length - 1]; + System.arraycopy(resBounds,1,additionalBounds,0,additionalBounds.length); + } + rt_tv.setUpperBound(upperBound); + rt_tv.setAdditionalInterfaceBounds(additionalBounds); + + typeVariablesInProgress.remove(aType); // we have finished working on it + + return tvrt; + } else if (aType instanceof WildcardType) { + WildcardType wildType = (WildcardType) aType; + Type[] lowerBounds = wildType.getLowerBounds(); + Type[] upperBounds = wildType.getUpperBounds(); + ResolvedType bound = null; + boolean isExtends = lowerBounds.length == 0; + if (isExtends) { + bound = fromType(upperBounds[0]); + } else { + bound = fromType(lowerBounds[0]); + } + return new BoundedReferenceType((ReferenceType)bound,isExtends,getWorld()); + } else if (aType instanceof GenericArrayType) { + GenericArrayType gt = (GenericArrayType) aType; + Type componentType = gt.getGenericComponentType(); + return UnresolvedType.makeArray(fromType(componentType),1).resolve(getWorld()); + } + return ResolvedType.MISSING; + } + + public ResolvedType[] fromTypes(Type[] types) { + ResolvedType[] ret = new ResolvedType[types.length]; + for (int i = 0; i < ret.length; i++) { + ret[i] = fromType(types[i]); + } + return ret; + } + + +} |