@@ -27,183 +27,173 @@ import org.aspectj.weaver.UnresolvedType; | |||
import org.aspectj.weaver.World; | |||
/** | |||
* @author colyer | |||
* Creates the appropriate ReflectionBasedReferenceTypeDelegate according to | |||
* the VM level we are running at. Uses reflection to avoid 1.5 dependencies in | |||
* 1.4 and 1.3 code base. | |||
* @author colyer Creates the appropriate ReflectionBasedReferenceTypeDelegate according to the VM level we are running at. Uses | |||
* reflection to avoid 1.5 dependencies in 1.4 and 1.3 code base. | |||
*/ | |||
public class ReflectionBasedReferenceTypeDelegateFactory { | |||
public static ReflectionBasedReferenceTypeDelegate | |||
createDelegate(ReferenceType forReferenceType, World inWorld, ClassLoader usingClassLoader) { | |||
public static ReflectionBasedReferenceTypeDelegate createDelegate(ReferenceType forReferenceType, World inWorld, | |||
ClassLoader usingClassLoader) { | |||
try { | |||
Class c = Class.forName(forReferenceType.getName(),false,usingClassLoader); | |||
Class c = Class.forName(forReferenceType.getName(), false, usingClassLoader); | |||
if (LangUtil.is15VMOrGreater()) { | |||
ReflectionBasedReferenceTypeDelegate rbrtd = create15Delegate(forReferenceType,c,usingClassLoader,inWorld); | |||
if (rbrtd!=null) return rbrtd; // can be null if we didn't find the class the delegate logic loads | |||
ReflectionBasedReferenceTypeDelegate rbrtd = create15Delegate(forReferenceType, c, usingClassLoader, inWorld); | |||
if (rbrtd != null) { | |||
return rbrtd; // can be null if we didn't find the class the delegate logic loads | |||
} | |||
} | |||
return new ReflectionBasedReferenceTypeDelegate(c,usingClassLoader,inWorld,forReferenceType); | |||
return new ReflectionBasedReferenceTypeDelegate(c, usingClassLoader, inWorld, forReferenceType); | |||
} catch (ClassNotFoundException cnfEx) { | |||
return null; | |||
} | |||
} | |||
public static ReflectionBasedReferenceTypeDelegate create14Delegate(ReferenceType forReferenceType, World inWorld, | |||
ClassLoader usingClassLoader) { | |||
try { | |||
Class c = Class.forName(forReferenceType.getName(), false, usingClassLoader); | |||
return new ReflectionBasedReferenceTypeDelegate(c, usingClassLoader, inWorld, forReferenceType); | |||
} catch (ClassNotFoundException cnfEx) { | |||
return null; | |||
} | |||
} | |||
// can return 'null' if we can't find the class | |||
private static ReflectionBasedReferenceTypeDelegate create15Delegate(ReferenceType forReferenceType, Class forClass, ClassLoader usingClassLoader, World inWorld) { | |||
private static ReflectionBasedReferenceTypeDelegate create15Delegate(ReferenceType forReferenceType, Class forClass, | |||
ClassLoader usingClassLoader, World inWorld) { | |||
try { | |||
Class delegateClass = Class.forName("org.aspectj.weaver.reflect.Java15ReflectionBasedReferenceTypeDelegate"); | |||
ReflectionBasedReferenceTypeDelegate ret = (ReflectionBasedReferenceTypeDelegate) delegateClass.newInstance(); | |||
ret.initialize(forReferenceType,forClass,usingClassLoader,inWorld); | |||
ret.initialize(forReferenceType, forClass, usingClassLoader, inWorld); | |||
return ret; | |||
} catch (ClassNotFoundException cnfEx) { | |||
throw new IllegalStateException("Attempted to create Java 1.5 reflection based delegate but org.aspectj.weaver.reflect.Java15ReflectionBasedReferenceTypeDelegate was not found on classpath"); | |||
throw new IllegalStateException( | |||
"Attempted to create Java 1.5 reflection based delegate but org.aspectj.weaver.reflect.Java15ReflectionBasedReferenceTypeDelegate was not found on classpath"); | |||
} catch (InstantiationException insEx) { | |||
throw new IllegalStateException("Attempted to create Java 1.5 reflection based delegate but InstantiationException: " + insEx + " occured"); | |||
throw new IllegalStateException("Attempted to create Java 1.5 reflection based delegate but InstantiationException: " | |||
+ insEx + " occured"); | |||
} catch (IllegalAccessException illAccEx) { | |||
throw new IllegalStateException("Attempted to create Java 1.5 reflection based delegate but IllegalAccessException: " + illAccEx + " occured"); | |||
throw new IllegalStateException("Attempted to create Java 1.5 reflection based delegate but IllegalAccessException: " | |||
+ illAccEx + " occured"); | |||
} | |||
} | |||
private static GenericSignatureInformationProvider createGenericSignatureProvider(World inWorld) { | |||
if (LangUtil.is15VMOrGreater()) { | |||
try { | |||
Class providerClass = Class.forName("org.aspectj.weaver.reflect.Java15GenericSignatureInformationProvider"); | |||
Constructor cons = providerClass.getConstructor(new Class[] {World.class}); | |||
GenericSignatureInformationProvider ret = (GenericSignatureInformationProvider) cons.newInstance(new Object[] {inWorld}); | |||
return ret; | |||
Constructor cons = providerClass.getConstructor(new Class[] { World.class }); | |||
GenericSignatureInformationProvider ret = (GenericSignatureInformationProvider) cons | |||
.newInstance(new Object[] { inWorld }); | |||
return ret; | |||
} catch (ClassNotFoundException cnfEx) { | |||
// drop through and create a 14 provider... | |||
// throw new IllegalStateException("Attempted to create Java 1.5 generic signature provider but org.aspectj.weaver.reflect.Java15GenericSignatureInformationProvider was not found on classpath"); | |||
// throw new | |||
// IllegalStateException("Attempted to create Java 1.5 generic signature provider but org.aspectj.weaver.reflect.Java15GenericSignatureInformationProvider was not found on classpath"); | |||
} catch (NoSuchMethodException nsmEx) { | |||
throw new IllegalStateException("Attempted to create Java 1.5 generic signature provider but: " + nsmEx + " occured"); | |||
throw new IllegalStateException("Attempted to create Java 1.5 generic signature provider but: " + nsmEx | |||
+ " occured"); | |||
} catch (InstantiationException insEx) { | |||
throw new IllegalStateException("Attempted to create Java 1.5 generic signature provider but: " + insEx + " occured"); | |||
throw new IllegalStateException("Attempted to create Java 1.5 generic signature provider but: " + insEx | |||
+ " occured"); | |||
} catch (InvocationTargetException invEx) { | |||
throw new IllegalStateException("Attempted to create Java 1.5 generic signature provider but: " + invEx + " occured"); | |||
throw new IllegalStateException("Attempted to create Java 1.5 generic signature provider but: " + invEx | |||
+ " occured"); | |||
} catch (IllegalAccessException illAcc) { | |||
throw new IllegalStateException("Attempted to create Java 1.5 generic signature provider but: " + illAcc + " occured"); | |||
throw new IllegalStateException("Attempted to create Java 1.5 generic signature provider but: " + illAcc | |||
+ " occured"); | |||
} | |||
} | |||
return new Java14GenericSignatureInformationProvider(); | |||
} | |||
/** | |||
* convert a java.lang.reflect.Member into a resolved member in the world | |||
* | |||
* @param reflectMember | |||
* @param inWorld | |||
* @return | |||
*/ | |||
public static ResolvedMember createResolvedMember(Member reflectMember, World inWorld) { | |||
if (reflectMember instanceof Method) { | |||
return createResolvedMethod((Method)reflectMember,inWorld); | |||
return createResolvedMethod((Method) reflectMember, inWorld); | |||
} else if (reflectMember instanceof Constructor) { | |||
return createResolvedConstructor((Constructor)reflectMember,inWorld); | |||
return createResolvedConstructor((Constructor) reflectMember, inWorld); | |||
} else { | |||
return createResolvedField((Field)reflectMember,inWorld); | |||
return createResolvedField((Field) reflectMember, inWorld); | |||
} | |||
} | |||
public static ResolvedMember createResolvedMethod(Method aMethod, World inWorld) { | |||
ReflectionBasedResolvedMemberImpl ret = new ReflectionBasedResolvedMemberImpl(org.aspectj.weaver.Member.METHOD, | |||
toResolvedType(aMethod.getDeclaringClass(),(IReflectionWorld)inWorld), | |||
aMethod.getModifiers(), | |||
toResolvedType(aMethod.getReturnType(),(IReflectionWorld)inWorld), | |||
aMethod.getName(), | |||
toResolvedTypeArray(aMethod.getParameterTypes(),inWorld), | |||
toResolvedTypeArray(aMethod.getExceptionTypes(),inWorld), | |||
aMethod | |||
); | |||
toResolvedType(aMethod.getDeclaringClass(), (IReflectionWorld) inWorld), aMethod.getModifiers(), toResolvedType( | |||
aMethod.getReturnType(), (IReflectionWorld) inWorld), aMethod.getName(), toResolvedTypeArray(aMethod | |||
.getParameterTypes(), inWorld), toResolvedTypeArray(aMethod.getExceptionTypes(), inWorld), aMethod); | |||
if (inWorld instanceof IReflectionWorld) { | |||
ret.setAnnotationFinder(((IReflectionWorld)inWorld).getAnnotationFinder()); | |||
ret.setAnnotationFinder(((IReflectionWorld) inWorld).getAnnotationFinder()); | |||
} | |||
ret.setGenericSignatureInformationProvider(createGenericSignatureProvider(inWorld)); | |||
return ret; | |||
} | |||
public static ResolvedMember createResolvedAdviceMember(Method aMethod, World inWorld) { | |||
ReflectionBasedResolvedMemberImpl ret = | |||
new ReflectionBasedResolvedMemberImpl(org.aspectj.weaver.Member.ADVICE, | |||
toResolvedType(aMethod.getDeclaringClass(),(IReflectionWorld)inWorld), | |||
aMethod.getModifiers(), | |||
toResolvedType(aMethod.getReturnType(),(IReflectionWorld)inWorld), | |||
aMethod.getName(), | |||
toResolvedTypeArray(aMethod.getParameterTypes(),inWorld), | |||
toResolvedTypeArray(aMethod.getExceptionTypes(),inWorld), | |||
aMethod | |||
); | |||
ReflectionBasedResolvedMemberImpl ret = new ReflectionBasedResolvedMemberImpl(org.aspectj.weaver.Member.ADVICE, | |||
toResolvedType(aMethod.getDeclaringClass(), (IReflectionWorld) inWorld), aMethod.getModifiers(), toResolvedType( | |||
aMethod.getReturnType(), (IReflectionWorld) inWorld), aMethod.getName(), toResolvedTypeArray(aMethod | |||
.getParameterTypes(), inWorld), toResolvedTypeArray(aMethod.getExceptionTypes(), inWorld), aMethod); | |||
if (inWorld instanceof IReflectionWorld) { | |||
ret.setAnnotationFinder(((IReflectionWorld)inWorld).getAnnotationFinder()); | |||
ret.setAnnotationFinder(((IReflectionWorld) inWorld).getAnnotationFinder()); | |||
} | |||
ret.setGenericSignatureInformationProvider(createGenericSignatureProvider(inWorld)); | |||
return ret; | |||
} | |||
public static ResolvedMember createStaticInitMember(Class forType, World inWorld) { | |||
return new ResolvedMemberImpl(org.aspectj.weaver.Member.STATIC_INITIALIZATION, | |||
toResolvedType(forType,(IReflectionWorld)inWorld), | |||
Modifier.STATIC, | |||
ResolvedType.VOID, | |||
"<clinit>", | |||
new UnresolvedType[0], | |||
new UnresolvedType[0] | |||
); | |||
return new ResolvedMemberImpl(org.aspectj.weaver.Member.STATIC_INITIALIZATION, toResolvedType(forType, | |||
(IReflectionWorld) inWorld), Modifier.STATIC, ResolvedType.VOID, "<clinit>", new UnresolvedType[0], | |||
new UnresolvedType[0]); | |||
} | |||
public static ResolvedMember createResolvedConstructor(Constructor aConstructor, World inWorld) { | |||
ReflectionBasedResolvedMemberImpl ret = | |||
new ReflectionBasedResolvedMemberImpl(org.aspectj.weaver.Member.CONSTRUCTOR, | |||
toResolvedType(aConstructor.getDeclaringClass(),(IReflectionWorld)inWorld), | |||
aConstructor.getModifiers(), | |||
ReflectionBasedResolvedMemberImpl ret = new ReflectionBasedResolvedMemberImpl(org.aspectj.weaver.Member.CONSTRUCTOR, | |||
toResolvedType(aConstructor.getDeclaringClass(), (IReflectionWorld) inWorld), aConstructor.getModifiers(), | |||
// to return what BCEL returns, the return type for ctor is void | |||
ResolvedType.VOID,//toResolvedType(aConstructor.getDeclaringClass(),(IReflectionWorld)inWorld), | |||
"<init>", | |||
toResolvedTypeArray(aConstructor.getParameterTypes(),inWorld), | |||
toResolvedTypeArray(aConstructor.getExceptionTypes(),inWorld), | |||
aConstructor | |||
); | |||
ResolvedType.VOID,// toResolvedType(aConstructor.getDeclaringClass(),(IReflectionWorld)inWorld), | |||
"<init>", toResolvedTypeArray(aConstructor.getParameterTypes(), inWorld), toResolvedTypeArray(aConstructor | |||
.getExceptionTypes(), inWorld), aConstructor); | |||
if (inWorld instanceof IReflectionWorld) { | |||
ret.setAnnotationFinder(((IReflectionWorld)inWorld).getAnnotationFinder()); | |||
ret.setAnnotationFinder(((IReflectionWorld) inWorld).getAnnotationFinder()); | |||
} | |||
ret.setGenericSignatureInformationProvider(createGenericSignatureProvider(inWorld)); | |||
return ret; | |||
} | |||
public static ResolvedMember createResolvedField(Field aField, World inWorld) { | |||
ReflectionBasedResolvedMemberImpl ret = | |||
new ReflectionBasedResolvedMemberImpl(org.aspectj.weaver.Member.FIELD, | |||
toResolvedType(aField.getDeclaringClass(),(IReflectionWorld)inWorld), | |||
aField.getModifiers(), | |||
toResolvedType(aField.getType(),(IReflectionWorld)inWorld), | |||
aField.getName(), | |||
new UnresolvedType[0], | |||
aField); | |||
ReflectionBasedResolvedMemberImpl ret = new ReflectionBasedResolvedMemberImpl(org.aspectj.weaver.Member.FIELD, | |||
toResolvedType(aField.getDeclaringClass(), (IReflectionWorld) inWorld), aField.getModifiers(), toResolvedType( | |||
aField.getType(), (IReflectionWorld) inWorld), aField.getName(), new UnresolvedType[0], aField); | |||
if (inWorld instanceof IReflectionWorld) { | |||
ret.setAnnotationFinder(((IReflectionWorld)inWorld).getAnnotationFinder()); | |||
ret.setAnnotationFinder(((IReflectionWorld) inWorld).getAnnotationFinder()); | |||
} | |||
ret.setGenericSignatureInformationProvider(createGenericSignatureProvider(inWorld)); | |||
return ret; | |||
} | |||
public static ResolvedMember createHandlerMember(Class exceptionType, Class inType,World inWorld) { | |||
return new ResolvedMemberImpl( | |||
org.aspectj.weaver.Member.HANDLER, | |||
toResolvedType(inType,(IReflectionWorld)inWorld), | |||
Modifier.STATIC, | |||
"<catch>", | |||
"(" + inWorld.resolve(exceptionType.getName()).getSignature() + ")V"); | |||
public static ResolvedMember createHandlerMember(Class exceptionType, Class inType, World inWorld) { | |||
return new ResolvedMemberImpl(org.aspectj.weaver.Member.HANDLER, toResolvedType(inType, (IReflectionWorld) inWorld), | |||
Modifier.STATIC, "<catch>", "(" + inWorld.resolve(exceptionType.getName()).getSignature() + ")V"); | |||
} | |||
public static ResolvedType resolveTypeInWorld(Class aClass, World aWorld) { | |||
// classes that represent arrays return a class name that is the signature of the array type, ho-hum... | |||
// classes that represent arrays return a class name that is the signature of the array type, ho-hum... | |||
String className = aClass.getName(); | |||
if (aClass.isArray()) { | |||
return aWorld.resolve(UnresolvedType.forSignature(className.replace('.','/'))); | |||
} | |||
else{ | |||
return aWorld.resolve(UnresolvedType.forSignature(className.replace('.', '/'))); | |||
} else { | |||
return aWorld.resolve(className); | |||
} | |||
} | |||
} | |||
private static ResolvedType toResolvedType(Class aClass, IReflectionWorld aWorld) { | |||
return aWorld.resolve(aClass); | |||
} | |||
@@ -211,7 +201,7 @@ public class ReflectionBasedReferenceTypeDelegateFactory { | |||
private static ResolvedType[] toResolvedTypeArray(Class[] classes, World inWorld) { | |||
ResolvedType[] ret = new ResolvedType[classes.length]; | |||
for (int i = 0; i < ret.length; i++) { | |||
ret[i] = ((IReflectionWorld)inWorld).resolve(classes[i]); | |||
ret[i] = ((IReflectionWorld) inWorld).resolve(classes[i]); | |||
} | |||
return ret; | |||
} |
@@ -33,6 +33,7 @@ public class ReflectionWorld extends World implements IReflectionWorld { | |||
private WeakClassLoaderReference classLoaderReference; | |||
private AnnotationFinder annotationFinder; | |||
private boolean mustUseOneFourDelegates = false; // for testing | |||
private ReflectionWorld() { | |||
// super(); | |||
@@ -53,6 +54,15 @@ public class ReflectionWorld extends World implements IReflectionWorld { | |||
annotationFinder = makeAnnotationFinderIfAny(classLoaderReference.getClassLoader(), this); | |||
} | |||
public ReflectionWorld(boolean forceUseOf14Delegates, ClassLoader aClassLoader) { | |||
this(aClassLoader); | |||
this.mustUseOneFourDelegates = forceUseOf14Delegates; | |||
if (forceUseOf14Delegates) { | |||
// Dont use 1.4 delegates and yet allow autoboxing | |||
this.setBehaveInJava5Way(false); | |||
} | |||
} | |||
public static AnnotationFinder makeAnnotationFinderIfAny(ClassLoader loader, World world) { | |||
AnnotationFinder annotationFinder = null; | |||
try { | |||
@@ -97,7 +107,11 @@ public class ReflectionWorld extends World implements IReflectionWorld { | |||
} | |||
protected ReferenceTypeDelegate resolveDelegate(ReferenceType ty) { | |||
return ReflectionBasedReferenceTypeDelegateFactory.createDelegate(ty, this, classLoaderReference.getClassLoader()); | |||
if (mustUseOneFourDelegates) { | |||
return ReflectionBasedReferenceTypeDelegateFactory.create14Delegate(ty, this, classLoaderReference.getClassLoader()); | |||
} else { | |||
return ReflectionBasedReferenceTypeDelegateFactory.createDelegate(ty, this, classLoaderReference.getClassLoader()); | |||
} | |||
} | |||
public static class ReflectionWorldException extends RuntimeException { |
@@ -0,0 +1,69 @@ | |||
/* ******************************************************************* | |||
* 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; | |||
import junit.framework.TestCase; | |||
// test cases for Adrian's additions to ReferenceType | |||
// XXX - couldn't find any unit test cases for the rest of the ReferenceType class | |||
public abstract class CommonReferenceTypeTests extends TestCase { | |||
private World world; | |||
public abstract World getWorld(); | |||
public void setUp() { | |||
world = getWorld(); | |||
} | |||
public void testUnresolvedTypeSignatureProcessing() { | |||
world.setBehaveInJava5Way(true); | |||
UnresolvedType ut = null; | |||
ut = UnresolvedType.forName("java.util.List<java.util.List<java.lang.String>>[]").resolve(world); | |||
ut = UnresolvedType.forSignature("[Pjava/util/List<Pjava/util/List<Ljava/lang/String;>;>;").resolve(world); | |||
assertEquals("Signatures not equal ", "[Pjava/util/List<Pjava/util/List<Ljava/lang/String;>;>;", ut.getSignature()); | |||
assertEquals("Names not equal ", "java.util.List<java.util.List<java.lang.String>>[]", ut.getName()); | |||
} | |||
public void testIsRawTrue() { | |||
world.setBehaveInJava5Way(true); | |||
UnresolvedType javaLangClass = UnresolvedType.forName("java.lang.Class"); | |||
ResolvedType rtx = world.resolve(javaLangClass); | |||
assertTrue("Resolves to reference type", (rtx instanceof ReferenceType)); | |||
ReferenceType rt = (ReferenceType) rtx; | |||
assertTrue("java.lang.Class is raw", rt.isRawType()); | |||
} | |||
public void testIsRawFalse() { | |||
world.setBehaveInJava5Way(true); | |||
UnresolvedType javaLangObject = UnresolvedType.forName("java.lang.Object"); | |||
ResolvedType rtx = world.resolve(javaLangObject); | |||
assertTrue("Resolves to reference type", (rtx instanceof ReferenceType)); | |||
ReferenceType rt = (ReferenceType) rtx; | |||
assertFalse("java.lang.Object is not raw", rt.isRawType()); | |||
} | |||
public void testIsGenericTrue() { | |||
world.setBehaveInJava5Way(true); | |||
UnresolvedType javaLangClass = UnresolvedType.forName("java.lang.Class"); | |||
ResolvedType rtx = world.resolve(javaLangClass); | |||
assertTrue("java.lang.Class has underpinning generic type", rtx.getGenericType().isGenericType()); | |||
} | |||
public void testIsGenericFalse() { | |||
world.setBehaveInJava5Way(true); | |||
UnresolvedType javaLangObject = UnresolvedType.forName("java.lang.Object"); | |||
ResolvedType rtx = world.resolve(javaLangObject); | |||
assertFalse(rtx.isGenericType()); | |||
} | |||
} |
@@ -0,0 +1,225 @@ | |||
/* ******************************************************************* | |||
* Copyright (c) 2002-2008 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://www.eclipse.org/legal/epl-v10.html | |||
* | |||
* Contributors: | |||
* PARC initial implementation | |||
* Andy Clement | |||
* ******************************************************************/ | |||
package org.aspectj.weaver; | |||
import java.lang.reflect.Modifier; | |||
import java.util.ArrayList; | |||
import java.util.Arrays; | |||
import java.util.HashSet; | |||
import java.util.Iterator; | |||
import java.util.List; | |||
import java.util.Set; | |||
import junit.framework.TestCase; | |||
import org.aspectj.testing.util.TestUtil; | |||
/** | |||
* An abstract set of tests that any World implementation should be able to pass. To run it against your World, subclass it and | |||
* implement getWorld(). | |||
* | |||
* @author Andy Clement | |||
*/ | |||
public abstract class CommonWorldTests extends TestCase { | |||
/** | |||
* @return an instance of the World to be tested | |||
*/ | |||
protected abstract World getWorld(); | |||
private World world; | |||
public void setUp() { | |||
world = getWorld(); | |||
} | |||
private final UnresolvedType[] primitiveTypeXs = UnresolvedType.forSignatures(new String[] { "B", "S", "C", "I", "J", "F", "D", | |||
"V" }); | |||
public void testPrimitiveTypes() { | |||
ResolvedType[] primitives = world.resolve(primitiveTypeXs); | |||
for (int i = 0, len = primitives.length; i < len; i++) { | |||
ResolvedType ty = primitives[i]; | |||
modifiersTest(ty, Modifier.PUBLIC | Modifier.FINAL); | |||
fieldsTest(ty, ResolvedMember.NONE); | |||
methodsTest(ty, ResolvedMember.NONE); | |||
interfacesTest(ty, ResolvedType.NONE); | |||
superclassTest(ty, null); | |||
pointcutsTest(ty, ResolvedMember.NONE); | |||
isInterfaceTest(ty, false); | |||
isClassTest(ty, false); | |||
isAspectTest(ty, false); | |||
for (int j = 0; j < len; j++) { | |||
ResolvedType ty1 = primitives[j]; | |||
if (ty.equals(ty1)) { | |||
isCoerceableFromTest(ty, ty1, true); | |||
} else if (ty == ResolvedType.BOOLEAN || ty1 == ResolvedType.BOOLEAN || ty == ResolvedType.VOID | |||
|| ty1 == ResolvedType.VOID) { | |||
isCoerceableFromTest(ty, ty1, false); | |||
} else { | |||
isCoerceableFromTest(ty, ty1, true); | |||
} | |||
} | |||
// Result of this depends on whether autoboxing is supported | |||
// isCoerceableFromTest(ty, UnresolvedType.OBJECT, getSupportsAutoboxing()); | |||
primAssignTest("B", new String[] {}); | |||
primAssignTest("S", new String[] { "B" }); | |||
primAssignTest("C", new String[] { "B" }); | |||
primAssignTest("I", new String[] { "B", "S", "C" }); | |||
primAssignTest("J", new String[] { "B", "S", "C", "I" }); | |||
primAssignTest("F", new String[] { "B", "S", "C", "I", "J" }); | |||
primAssignTest("D", new String[] { "B", "S", "C", "I", "J", "F" }); | |||
primAssignTest("Z", new String[] {}); | |||
primAssignTest("V", new String[] {}); | |||
} | |||
} | |||
private void primAssignTest(String sig, String[] lowers) { | |||
ResolvedType[] primitives = getWorld().resolve(primitiveTypeXs); | |||
UnresolvedType tx = UnresolvedType.forSignature(sig); | |||
ResolvedType ty = getWorld().resolve(tx, true); | |||
assertTrue("Couldnt find type " + tx, !ty.isMissing()); | |||
ResolvedType[] lowerTyArray = getWorld().resolve(UnresolvedType.forSignatures(lowers)); | |||
List lowerTys = new ArrayList(Arrays.asList(lowerTyArray)); | |||
lowerTys.add(ty); | |||
Set allLowerTys = new HashSet(lowerTys); | |||
Set allUpperTys = new HashSet(Arrays.asList(primitives)); | |||
allUpperTys.removeAll(allLowerTys); | |||
for (Iterator i = allLowerTys.iterator(); i.hasNext();) { | |||
ResolvedType other = (ResolvedType) i.next(); | |||
isAssignableFromTest(ty, other, true); | |||
} | |||
for (Iterator i = allUpperTys.iterator(); i.hasNext();) { | |||
ResolvedType other = (ResolvedType) i.next(); | |||
isAssignableFromTest(ty, other, false); | |||
} | |||
} | |||
public void testPrimitiveArrays() { | |||
ResolvedType[] primitives = world.resolve(primitiveTypeXs); | |||
for (int i = 0, len = primitives.length; i < len; i++) { | |||
ResolvedType ty = primitives[i]; | |||
UnresolvedType tx = UnresolvedType.forSignature("[" + ty.getSignature()); | |||
ResolvedType aty = getWorld().resolve(tx, true); | |||
assertTrue("Couldnt find type " + tx, !aty.isMissing()); | |||
modifiersTest(aty, Modifier.PUBLIC | Modifier.FINAL); | |||
fieldsTest(aty, ResolvedMember.NONE); | |||
methodsTest(aty, ResolvedMember.NONE); | |||
interfaceTest(aty, new ResolvedType[] { world.getCoreType(UnresolvedType.CLONEABLE), | |||
world.getCoreType(UnresolvedType.SERIALIZABLE) }); | |||
superclassTest(aty, UnresolvedType.OBJECT); | |||
pointcutsTest(aty, ResolvedMember.NONE); | |||
isInterfaceTest(aty, false); | |||
isClassTest(aty, false); | |||
isAspectTest(aty, false); | |||
for (int j = 0; j < len; j++) { | |||
ResolvedType ty1 = primitives[j]; | |||
isCoerceableFromTest(aty, ty1, false); | |||
tx = UnresolvedType.forSignature("[" + ty1.getSignature()); | |||
ResolvedType aty1 = getWorld().resolve(tx, true); | |||
assertTrue("Couldnt find type " + tx, !aty1.isMissing()); | |||
if (ty.equals(ty1)) { | |||
isCoerceableFromTest(aty, aty1, true); | |||
isAssignableFromTest(aty, aty1, true); | |||
} else { | |||
isCoerceableFromTest(aty, aty1, false); | |||
isAssignableFromTest(aty, aty1, false); | |||
} | |||
} | |||
} | |||
} | |||
// ---- tests for parts of ResolvedType objects | |||
protected void modifiersTest(ResolvedType ty, int mods) { | |||
assertEquals(ty + " modifiers:", Modifier.toString(mods), Modifier.toString(ty.getModifiers())); | |||
} | |||
protected void fieldsTest(ResolvedType ty, Member[] x) { | |||
TestUtil.assertSetEquals(ty + " fields:", x, ty.getDeclaredJavaFields()); | |||
} | |||
protected void methodsTest(ResolvedType ty, Member[] x) { | |||
TestUtil.assertSetEquals(ty + " methods:", x, ty.getDeclaredJavaMethods()); | |||
} | |||
protected void mungersTest(ResolvedType ty, ShadowMunger[] x) { | |||
TestUtil.assertSetEquals(ty + " mungers:", x, ty.getDeclaredShadowMungersArray()); | |||
} | |||
protected void interfaceTest(ResolvedType type, ResolvedType[] expectedInterfaces) { | |||
ResolvedType[] interfaces = type.getDeclaredInterfaces(); | |||
for (int i = 0; i < expectedInterfaces.length; i++) { | |||
boolean wasMissing = true; | |||
for (int j = 0; j < interfaces.length; j++) { | |||
if (interfaces[j].getSignature().equals(expectedInterfaces[i].getSignature())) { | |||
wasMissing = false; | |||
} | |||
} | |||
if (wasMissing) { | |||
fail("Expected declared interface " + expectedInterfaces[i] + " but it wasn't found in " | |||
+ Arrays.asList(interfaces)); | |||
} | |||
} | |||
} | |||
protected void interfacesTest(ResolvedType ty, ResolvedType[] x) { | |||
TestUtil.assertArrayEquals(ty + " interfaces:", x, ty.getDeclaredInterfaces()); | |||
} | |||
protected void superclassTest(ResolvedType ty, UnresolvedType x) { | |||
assertEquals(ty + " superclass:", x, ty.getSuperclass()); | |||
} | |||
protected void pointcutsTest(ResolvedType ty, Member[] x) { | |||
TestUtil.assertSetEquals(ty + " pointcuts:", x, ty.getDeclaredPointcuts()); | |||
} | |||
protected void isInterfaceTest(ResolvedType ty, boolean x) { | |||
assertEquals(ty + " is interface:", x, ty.isInterface()); | |||
} | |||
protected void isAspectTest(ResolvedType ty, boolean x) { | |||
assertEquals(ty + " is aspect:", x, ty.isAspect()); | |||
} | |||
protected void isClassTest(ResolvedType ty, boolean x) { | |||
assertEquals(ty + " is class:", x, ty.isClass()); | |||
} | |||
protected void isCoerceableFromTest(UnresolvedType ty0, UnresolvedType ty1, boolean x) { | |||
assertEquals(ty0 + " is coerceable from " + ty1, x, ty0.resolve(world).isCoerceableFrom(ty1.resolve(world))); | |||
assertEquals(ty1 + " is coerceable from " + ty0, x, ty1.resolve(world).isCoerceableFrom(ty0.resolve(world))); | |||
} | |||
protected void isAssignableFromTest(UnresolvedType ty0, UnresolvedType ty1, boolean x) { | |||
assertEquals(ty0 + " is assignable from " + ty1, x, ty0.resolve(world).isAssignableFrom(ty1.resolve(world))); | |||
} | |||
// ---- tests for parts of ResolvedMethod objects | |||
protected void modifiersTest(ResolvedMember m, int mods) { | |||
assertEquals(m + " modifiers:", Modifier.toString(mods), Modifier.toString(m.getModifiers())); | |||
} | |||
protected void exceptionsTest(ResolvedMember m, UnresolvedType[] exns) { | |||
TestUtil.assertSetEquals(m + " exceptions:", exns, m.getExceptions()); | |||
} | |||
} |
@@ -0,0 +1,38 @@ | |||
/* ******************************************************************* | |||
* Copyright (c) 2002-2008 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://www.eclipse.org/legal/epl-v10.html | |||
* | |||
* Contributors: | |||
* PARC initial implementation | |||
* ******************************************************************/ | |||
// default package | |||
package org.aspectj.weaver; | |||
import junit.framework.Test; | |||
import junit.framework.TestCase; | |||
import junit.framework.TestSuite; | |||
import org.aspectj.weaver.reflect.ReflectionWorldBasicTest; | |||
import org.aspectj.weaver.reflect.ReflectionWorldReferenceTypeTest; | |||
import org.aspectj.weaver.reflect.ReflectionWorldSpecificTest; | |||
public class MatcherModuleTests extends TestCase { | |||
public static Test suite() { | |||
TestSuite suite = new TestSuite(MatcherModuleTests.class.getName()); | |||
suite.addTestSuite(ReflectionWorldSpecificTest.class); | |||
suite.addTestSuite(ReflectionWorldReferenceTypeTest.class); | |||
suite.addTestSuite(ReflectionWorldBasicTest.class); | |||
return suite; | |||
} | |||
public MatcherModuleTests(String name) { | |||
super(name); | |||
} | |||
} |
@@ -0,0 +1,27 @@ | |||
/* ******************************************************************* | |||
* Copyright (c) 2002-2008 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://www.eclipse.org/legal/epl-v10.html | |||
* | |||
* Contributors: | |||
* Andy Clement | |||
* ******************************************************************/ | |||
package org.aspectj.weaver.reflect; | |||
import org.aspectj.weaver.CommonWorldTests; | |||
import org.aspectj.weaver.World; | |||
public class ReflectionWorldBasicTest extends CommonWorldTests { | |||
protected boolean getSupportsAutoboxing() { | |||
return false; | |||
} | |||
protected World getWorld() { | |||
return new ReflectionWorld(true, getClass().getClassLoader()); | |||
} | |||
} |
@@ -0,0 +1,27 @@ | |||
/* ******************************************************************* | |||
* Copyright (c) 2002-2008 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://www.eclipse.org/legal/epl-v10.html | |||
* | |||
* Contributors: | |||
* Andy Clement | |||
* ******************************************************************/ | |||
package org.aspectj.weaver.reflect; | |||
import org.aspectj.weaver.CommonReferenceTypeTests; | |||
import org.aspectj.weaver.World; | |||
public class ReflectionWorldReferenceTypeTest extends CommonReferenceTypeTests { | |||
protected boolean getSupportsAutoboxing() { | |||
return false; | |||
} | |||
public World getWorld() { | |||
return new ReflectionWorld(false, getClass().getClassLoader()); | |||
} | |||
} |
@@ -0,0 +1,41 @@ | |||
/* ******************************************************************* | |||
* 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 junit.framework.TestCase; | |||
import org.aspectj.weaver.ResolvedType; | |||
import org.aspectj.weaver.World; | |||
public class ReflectionWorldSpecificTest extends TestCase { | |||
public void testDelegateCreation() { | |||
World world = new ReflectionWorld(true, getClass().getClassLoader()); | |||
ResolvedType rt = world.resolve("java.lang.Object"); | |||
assertNotNull(rt); | |||
assertEquals("Ljava/lang/Object;", rt.getSignature()); | |||
} | |||
public void testArrayTypes() { | |||
IReflectionWorld world = new ReflectionWorld(true, getClass().getClassLoader()); | |||
String[] strArray = new String[1]; | |||
ResolvedType rt = world.resolve(strArray.getClass()); | |||
assertTrue(rt.isArray()); | |||
} | |||
public void testPrimitiveTypes() { | |||
IReflectionWorld world = new ReflectionWorld(true, getClass().getClassLoader()); | |||
assertEquals("int", ResolvedType.INT, world.resolve(int.class)); | |||
assertEquals("void", ResolvedType.VOID, world.resolve(void.class)); | |||
} | |||
} |