public static void main(String[] args) throws Exception {
PointcutParser parser = new PointcutParser();
+ parser.setClassLoader(RuntimePointcuts.class.getClassLoader());
PointcutExpression pc1 = parser.parsePointcutExpression("PCLib.anyMethodExecution()");
PointcutParameter param = parser.createPointcutParameter("s",String.class);
PointcutExpression pc2 = parser.parsePointcutExpression("PCLib.joinPointWithStringArg(s)",RuntimePointcuts.class,new PointcutParameter[] {param});
--- /dev/null
+// random collection of pointcuts to check that
+// reflective world and PointcutParser can interpret
+// them correctly.
+
+public aspect PointcutLibrary {
+
+ public pointcut propertyAccess() : get(* *);
+ public pointcut propertyUpdate() : set(* *);
+ public pointcut methodExecution() : execution(* *(..));
+ public pointcut propertyGet() : execution(!void get*(..));
+ public pointcut propertySet(Object newValue)
+ : execution(void set*(..)) && args(newValue);
+
+}
\ No newline at end of file
--- /dev/null
+import org.aspectj.weaver.tools.*;
+
+public class ReflectOnAjcCompiledPointcuts {
+
+ public static void main(String[] args) {
+ PointcutParser p = new PointcutParser();
+ PointcutExpression pe = null;
+ pe = p.parsePointcutExpression("PointcutLibrary.propertyAccess()");
+ pe = p.parsePointcutExpression("PointcutLibrary.propertyUpdate()");
+ pe = p.parsePointcutExpression("PointcutLibrary.methodExecution()");
+ pe = p.parsePointcutExpression("PointcutLibrary.propertyGet()");
+ pe = p.parsePointcutExpression("PointcutLibrary.propertySet(Object)");
+
+ PointcutParameter pp = p.createPointcutParameter("foo",String.class);
+ p.parsePointcutExpression("execution(* *(..)) && PointcutLibrary.propertySet(foo)",
+ Object.class,
+ new PointcutParameter[] {pp});
+
+ }
+
+
+}
\ No newline at end of file
runTest("no StackOverflowError with circular pcd in generic aspect - 2");
}
+
+// public void testPointcutParsingOfCompiledPointcuts() {
+// runTest("pointcut parsing with ajc compiled pointcut references");
+// }
+
/*
* Load-time weaving bugs
*/
<ajc-test dir="bugs150" pr="112756" title="pointcut expression containing 'assert'">
<compile files="Pr112756.aj" options="-warn:assertIdentifier -Xdev:Pinpoint"/>
</ajc-test>
-
+
+ <ajc-test dir="java5/reflection" title="pointcut parsing with ajc compiled pointcut references">
+ <compile files="PointcutLibrary.aj,ReflectOnAjcCompiledPointcuts.java"></compile>
+ <run class="ReflectOnAjcCompiledPointcuts" classpath=".;../lib/aspectj/aspectjtools.jar"/>
+ </ajc-test>
+
<ajc-test dir="bugs150/pr114436" title="ClassFormatError binary weaving perthis">
<compile files="SimpleTrace.aj,ConcreteSimpleTracing.aj" outjar="aspects.jar"/>
<compile files="TestClass.java" aspectpath="aspects.jar"/>
<ajc-test dir="java5/ataspectj/annotationGen" title="runtime pointcut resolution referencing compiled pointcuts">
<compile files="PCLib.aj,RuntimePointcuts.java" options="-1.5">
</compile>
- <run class="RuntimePointcuts" classpath="../lib/bcel/bcel.jar"/>
+ <run class="RuntimePointcuts" classpath=".;../lib/bcel/bcel.jar" ltw=""/>
</ajc-test>
<ajc-test dir="java5/ataspectj/annotationGen" title="ann gen for decp">
import java.util.List;
import java.util.StringTokenizer;
+import org.aspectj.apache.bcel.classfile.Attribute;
import org.aspectj.apache.bcel.classfile.ClassParser;
import org.aspectj.apache.bcel.classfile.JavaClass;
import org.aspectj.apache.bcel.classfile.Method;
import org.aspectj.weaver.ResolvedTypeMunger;
import org.aspectj.weaver.UnresolvedType;
import org.aspectj.weaver.World;
+import org.aspectj.weaver.AjAttribute.Aspect;
import org.aspectj.weaver.patterns.FormalBinding;
import org.aspectj.weaver.patterns.PerClause;
import org.aspectj.weaver.patterns.Pointcut;
if (anns.length == 0) {
return false;
}
+ boolean couldBeAtAspectJStyle = false;
for (int i = 0; i < anns.length; i++) {
Annotation ann = anns[i];
if ("Lorg/aspectj/lang/annotation/Aspect;".equals(ann.getTypeSignature())) {
- return true;
+ couldBeAtAspectJStyle = true;
}
}
- return false;
+
+ if (!couldBeAtAspectJStyle) return false;
+
+ // ok, so it has the annotation, but it could have been put
+ // on a code style aspect by the annotation visitor
+ Attribute[] attributes = jc.getAttributes();
+ for (int i = 0; i < attributes.length; i++) {
+ if (attributes[i].getName().equals(Aspect.AttributeName)) {
+ return false;
+ }
+ }
+ return true;
} catch (IOException e) {
// assume it is one as a best effort
return true;
--- /dev/null
+/* *******************************************************************
+ * Copyright (c) 2004 IBM Corporation.
+ * All rights reserved.
+ * This program and the accompanying materials are made available
+ * under the terms of the Common Public License v1.0
+ * which accompanies this distribution and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ *
+ * ******************************************************************/
+package org.aspectj.weaver.internal.tools;
+
+import org.aspectj.weaver.ResolvedType;
+import org.aspectj.weaver.World;
+import org.aspectj.weaver.patterns.TypePattern;
+import org.aspectj.weaver.reflect.ReflectionBasedReferenceTypeDelegateFactory;
+import org.aspectj.weaver.tools.TypePatternMatcher;
+
+public class TypePatternMatcherImpl implements TypePatternMatcher {
+
+ private final TypePattern pattern;
+ private final World world;
+
+ public TypePatternMatcherImpl(TypePattern pattern, World world) {
+ this.pattern = pattern;
+ this.world = world;
+ }
+
+ public boolean matches(Class aClass) {
+ ResolvedType rt =
+ ReflectionBasedReferenceTypeDelegateFactory.resolveTypeInWorld(aClass,world);
+ return pattern.matchesStatically(rt);
+ }
+
+}
--- /dev/null
+/* *******************************************************************
+ * 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.ltw;
+
+import org.aspectj.weaver.Advice;
+import org.aspectj.weaver.ConcreteTypeMunger;
+import org.aspectj.weaver.Member;
+import org.aspectj.weaver.ReferenceType;
+import org.aspectj.weaver.ReferenceTypeDelegate;
+import org.aspectj.weaver.ResolvedMember;
+import org.aspectj.weaver.ResolvedType;
+import org.aspectj.weaver.ResolvedTypeMunger;
+import org.aspectj.weaver.World;
+import org.aspectj.weaver.AjAttribute.AdviceAttribute;
+import org.aspectj.weaver.patterns.Pointcut;
+import org.aspectj.weaver.patterns.PerClause.Kind;
+
+/**
+ * @author adrian
+ *
+ * For use in LT weaving
+ *
+ * Backed by both a BcelWorld and a ReflectionWorld
+ *
+ * Needs a callback when a woven class is defined
+ * This is the trigger for us to ditch the class from
+ * Bcel and cache it in the reflective world instead.
+ *
+ * Problems with classes that are loaded by delegates
+ * of our classloader
+ *
+ * Create by passing in a classloader, message handler
+ */
+public class LTWWorld extends World {
+
+ protected ReferenceTypeDelegate resolveDelegate(ReferenceType ty) {
+ // TODO Auto-generated method stub
+ return null;
+
+ }
+
+ public Advice createAdviceMunger(AdviceAttribute attribute, Pointcut pointcut, Member signature) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public ConcreteTypeMunger makeCflowStackFieldAdder(ResolvedMember cflowField) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public ConcreteTypeMunger makeCflowCounterFieldAdder(ResolvedMember cflowField) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public ConcreteTypeMunger makePerClauseAspect(ResolvedType aspect, Kind kind) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public ConcreteTypeMunger concreteTypeMunger(ResolvedTypeMunger munger, ResolvedType aspectType) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+}
--- /dev/null
+/* *******************************************************************
+ * 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.ltw;
+
+import org.aspectj.weaver.IWeaver;
+
+/**
+ * @author adrian
+ *
+ */
+public class LTWeaver implements IWeaver {
+
+}
*/
public interface AnnotationFinder {
+ void setClassLoader(ClassLoader annotationLoader);
+
Object getAnnotation(ResolvedType annotationType, Object onObject);
Object getAnnotationFromMember(ResolvedType annotationType, Member aMember);
public class ReflectionBasedReferenceTypeDelegate implements ReferenceTypeDelegate {
protected Class myClass = null;
+ protected ClassLoader classLoader = null;
private World world;
private ReferenceType resolvedType;
private ResolvedMember[] fields = null;
private ResolvedMember[] methods = null;
private ResolvedType[] interfaces = null;
- public ReflectionBasedReferenceTypeDelegate(Class forClass, World inWorld, ReferenceType resolvedType) {
- initialize(resolvedType,forClass,inWorld);
+ public ReflectionBasedReferenceTypeDelegate(Class forClass, ClassLoader aClassLoader, World inWorld, ReferenceType resolvedType) {
+ initialize(resolvedType,forClass, aClassLoader, inWorld);
}
/** for reflective construction only */
public ReflectionBasedReferenceTypeDelegate() {}
- public void initialize(ReferenceType aType, Class aClass, World aWorld) {
+ public void initialize(ReferenceType aType, Class aClass, ClassLoader aClassLoader, World aWorld) {
this.myClass = aClass;
this.resolvedType = aType;
this.world = aWorld;
+ this.classLoader = aClassLoader;
}
protected Class getBaseClass() {
protected World getWorld() {
return this.world;
}
-
- protected ReflectionWorld getReflectionWorld() {
- return (ReflectionWorld) this.world;
- }
-
-
+
public ReferenceType buildGenericType() {
throw new UnsupportedOperationException("Shouldn't be asking for generic type at 1.4 source level or lower");
}
Class[] reflectInterfaces = this.myClass.getInterfaces();
this.interfaces = new ResolvedType[reflectInterfaces.length];
for (int i = 0; i < reflectInterfaces.length; i++) {
- this.interfaces[i] = getReflectionWorld().resolve(reflectInterfaces[i]);
+ this.interfaces[i] = ReflectionBasedReferenceTypeDelegateFactory
+ .resolveTypeInWorld(reflectInterfaces[i],world);
}
}
return interfaces;
*/
public ResolvedType getSuperclass() {
if (this.myClass.getSuperclass() == null) return null;
- return getReflectionWorld().resolve(this.myClass.getSuperclass());
+ return ReflectionBasedReferenceTypeDelegateFactory
+ .resolveTypeInWorld(this.myClass.getSuperclass(),world);
}
/* (non-Javadoc)
public class ReflectionBasedReferenceTypeDelegateFactory {
public static ReflectionBasedReferenceTypeDelegate
- createDelegate(ReferenceType forReferenceType, World inWorld) {
+ createDelegate(ReferenceType forReferenceType, World inWorld, ClassLoader usingClassLoader) {
try {
- Class c = Class.forName(forReferenceType.getName());
+ Class c = Class.forName(forReferenceType.getName(),false,usingClassLoader);
if (LangUtil.is15VMOrGreater()) {
- ReflectionBasedReferenceTypeDelegate rbrtd = create15Delegate(forReferenceType,c,inWorld);
+ 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,inWorld,forReferenceType);
+ return new ReflectionBasedReferenceTypeDelegate(c,usingClassLoader,inWorld,forReferenceType);
} catch (ClassNotFoundException cnfEx) {
return null;
}
}
- private static ReflectionBasedReferenceTypeDelegate create15Delegate(ReferenceType forReferenceType, Class forClass, World inWorld) {
+ private static ReflectionBasedReferenceTypeDelegate create15Delegate(ReferenceType forReferenceType, Class forClass, ClassLoader usingClassLoader, World inWorld) {
try {
- Class delegateClass = Class.forName("org.aspectj.weaver.reflect.Java15ReflectionBasedReferenceTypeDelegate");
+ Class delegateClass = Class.forName("org.aspectj.weaver.reflect.Java15ReflectionBasedReferenceTypeDelegate",false,usingClassLoader);
ReflectionBasedReferenceTypeDelegate ret = (ReflectionBasedReferenceTypeDelegate) delegateClass.newInstance();
- ret.initialize(forReferenceType,forClass,inWorld);
+ ret.initialize(forReferenceType,forClass,usingClassLoader,inWorld);
return ret;
} catch (ClassNotFoundException cnfEx) {
return null;
toResolvedTypeArray(aMethod.getExceptionTypes(),inWorld),
aMethod
);
+ if (inWorld instanceof ReflectionWorld) {
+ ret.setAnnotationFinder(((ReflectionWorld)inWorld).getAnnotationFinder());
+ }
return ret;
}
public static ResolvedMember createResolvedAdviceMember(Method aMethod, World inWorld) {
- return new ReflectionBasedResolvedMemberImpl(org.aspectj.weaver.Member.ADVICE,
+ ReflectionBasedResolvedMemberImpl ret =
+ new ReflectionBasedResolvedMemberImpl(org.aspectj.weaver.Member.ADVICE,
toResolvedType(aMethod.getDeclaringClass(),(ReflectionWorld)inWorld),
aMethod.getModifiers(),
toResolvedType(aMethod.getReturnType(),(ReflectionWorld)inWorld),
toResolvedTypeArray(aMethod.getExceptionTypes(),inWorld),
aMethod
);
+ if (inWorld instanceof ReflectionWorld) {
+ ret.setAnnotationFinder(((ReflectionWorld)inWorld).getAnnotationFinder());
+ }
+ return ret;
}
public static ResolvedMember createStaticInitMember(Class forType, World inWorld) {
}
public static ResolvedMember createResolvedConstructor(Constructor aConstructor, World inWorld) {
- return new ReflectionBasedResolvedMemberImpl(org.aspectj.weaver.Member.CONSTRUCTOR,
+ ReflectionBasedResolvedMemberImpl ret =
+ new ReflectionBasedResolvedMemberImpl(org.aspectj.weaver.Member.CONSTRUCTOR,
toResolvedType(aConstructor.getDeclaringClass(),(ReflectionWorld)inWorld),
aConstructor.getModifiers(),
toResolvedType(aConstructor.getDeclaringClass(),(ReflectionWorld)inWorld),
toResolvedTypeArray(aConstructor.getExceptionTypes(),inWorld),
aConstructor
);
+ if (inWorld instanceof ReflectionWorld) {
+ ret.setAnnotationFinder(((ReflectionWorld)inWorld).getAnnotationFinder());
+ }
+ return ret;
}
public static ResolvedMember createResolvedField(Field aField, World inWorld) {
- return new ReflectionBasedResolvedMemberImpl(org.aspectj.weaver.Member.FIELD,
+ ReflectionBasedResolvedMemberImpl ret =
+ new ReflectionBasedResolvedMemberImpl(org.aspectj.weaver.Member.FIELD,
toResolvedType(aField.getDeclaringClass(),(ReflectionWorld)inWorld),
aField.getModifiers(),
toResolvedType(aField.getType(),(ReflectionWorld)inWorld),
aField.getName(),
new UnresolvedType[0],
aField);
+ if (inWorld instanceof ReflectionWorld) {
+ ret.setAnnotationFinder(((ReflectionWorld)inWorld).getAnnotationFinder());
+ }
+ return ret;
}
public static ResolvedMember createHandlerMember(Class exceptionType, Class inType,World inWorld) {
"(" + 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...
+ String className = aClass.getName();
+ if (aClass.isArray()) {
+ return aWorld.resolve(UnresolvedType.forSignature(className));
+ }
+ else{
+ return aWorld.resolve(className);
+ }
+ }
+
private static ResolvedType toResolvedType(Class aClass, ReflectionWorld aWorld) {
return aWorld.resolve(aClass);
}
*/
public class ReflectionBasedResolvedMemberImpl extends ResolvedMemberImpl {
- private static AnnotationFinder annotationFinder = null;
-
- static {
- try {
- Class java15AnnotationFinder = Class.forName("org.aspectj.weaver.reflect.Java15AnnotationFinder");
- annotationFinder = (AnnotationFinder) java15AnnotationFinder.newInstance();
- } catch(Exception ex) {
- // must be on 1.4 or earlier
- }
- }
+ private AnnotationFinder annotationFinder = null;
private Member reflectMember;
this.reflectMember = reflectMember;
}
+ public void setAnnotationFinder(AnnotationFinder finder) {
+ this.annotationFinder = finder;
+ }
+
public boolean hasAnnotation(UnresolvedType ofType) {
unpackAnnotations();
return super.hasAnnotation(ofType);
private Map withinAnnotationVar = new HashMap();
private Map withinCodeAnnotationVar = new HashMap();
private Map annotationVar = new HashMap();
+ private AnnotationFinder annotationFinder;
public static Shadow makeExecutionShadow(World inWorld, java.lang.reflect.Member forMethod) {
Kind kind = (forMethod instanceof Method) ? Shadow.MethodExecution : Shadow.ConstructorExecution;
this.world = world;
this.enclosingType = enclosingType;
this.enclosingMember = enclosingMember;
+ if (world instanceof ReflectionWorld) {
+ this.annotationFinder = ((ReflectionWorld)world).getAnnotationFinder();
+ }
}
/* (non-Javadoc)
*/
public Var getThisVar() {
if (thisVar == null && hasThis()) {
- thisVar = ReflectionVar.createThisVar(getThisType().resolve(world));
+ thisVar = ReflectionVar.createThisVar(getThisType().resolve(world),this.annotationFinder);
}
return thisVar;
}
*/
public Var getTargetVar() {
if (targetVar == null && hasTarget()) {
- targetVar = ReflectionVar.createTargetVar(getThisType().resolve(world));
+ targetVar = ReflectionVar.createTargetVar(getThisType().resolve(world),this.annotationFinder);
}
return targetVar;
}
if (argsVars == null) {
this.argsVars = new Var[this.getArgCount()];
for (int j = 0; j < this.argsVars.length; j++) {
- this.argsVars[j] = ReflectionVar.createArgsVar(getArgType(j).resolve(world), j);
+ this.argsVars[j] = ReflectionVar.createArgsVar(getArgType(j).resolve(world), j,this.annotationFinder);
}
}
if (i < argsVars.length) {
public Var getKindedAnnotationVar(UnresolvedType forAnnotationType) {
ResolvedType annType = forAnnotationType.resolve(world);
if (annotationVar.get(annType) == null) {
- Var v = ReflectionVar.createAtAnnotationVar(annType);
+ Var v = ReflectionVar.createAtAnnotationVar(annType,this.annotationFinder);
annotationVar.put(annType,v);
}
return (Var) annotationVar.get(annType);
public Var getWithinAnnotationVar(UnresolvedType forAnnotationType) {
ResolvedType annType = forAnnotationType.resolve(world);
if (withinAnnotationVar.get(annType) == null) {
- Var v = ReflectionVar.createWithinAnnotationVar(annType);
+ Var v = ReflectionVar.createWithinAnnotationVar(annType,this.annotationFinder);
withinAnnotationVar.put(annType,v);
}
return (Var) withinAnnotationVar.get(annType);
public Var getWithinCodeAnnotationVar(UnresolvedType forAnnotationType) {
ResolvedType annType = forAnnotationType.resolve(world);
if (withinCodeAnnotationVar.get(annType) == null) {
- Var v = ReflectionVar.createWithinCodeAnnotationVar(annType);
+ Var v = ReflectionVar.createWithinCodeAnnotationVar(annType,this.annotationFinder);
withinCodeAnnotationVar.put(annType,v);
}
return (Var) withinCodeAnnotationVar.get(annType);
*/
public Var getThisAnnotationVar(UnresolvedType forAnnotationType) {
if (atThisVar == null) {
- atThisVar = ReflectionVar.createThisAnnotationVar(forAnnotationType.resolve(world));
+ atThisVar = ReflectionVar.createThisAnnotationVar(forAnnotationType.resolve(world),this.annotationFinder);
}
return atThisVar;
}
*/
public Var getTargetAnnotationVar(UnresolvedType forAnnotationType) {
if (atTargetVar == null) {
- atTargetVar = ReflectionVar.createTargetAnnotationVar(forAnnotationType.resolve(world));
+ atTargetVar = ReflectionVar.createTargetAnnotationVar(forAnnotationType.resolve(world),this.annotationFinder);
}
return atTargetVar;
}
Var[] vars = (Var[]) atArgsVars.get(annType);
if (i > (vars.length - 1) ) return null;
if (vars[i] == null) {
- vars[i] = ReflectionVar.createArgsAnnotationVar(annType, i);
+ vars[i] = ReflectionVar.createArgsAnnotationVar(annType, i,this.annotationFinder);
}
return vars[i];
}
static final int AT_WITHINCODE_VAR = 7;
static final int AT_ANNOTATION_VAR = 8;
- private static AnnotationFinder annotationFinder = null;
+ private AnnotationFinder annotationFinder = null;
- static {
- try {
- Class java15AnnotationFinder = Class.forName("org.aspectj.weaver.reflect.Java15AnnotationFinder");
- annotationFinder = (AnnotationFinder) java15AnnotationFinder.newInstance();
- } catch(Exception ex) {
- // must be on 1.4 or earlier
- }
- }
+// static {
+// try {
+// Class java15AnnotationFinder = Class.forName("org.aspectj.weaver.reflect.Java15AnnotationFinder");
+// annotationFinder = (AnnotationFinder) java15AnnotationFinder.newInstance();
+// } catch(ClassNotFoundException ex) {
+// // must be on 1.4 or earlier
+// } catch(IllegalAccessException ex) {
+// // not so good
+// throw new RuntimeException("AspectJ internal error",ex);
+// } catch(InstantiationException ex) {
+// throw new RuntimeException("AspectJ internal error",ex);
+// }
+// }
private int argsIndex = 0;
private int varType;
- public static ReflectionVar createThisVar(ResolvedType type) {
- ReflectionVar ret = new ReflectionVar(type);
+ public static ReflectionVar createThisVar(ResolvedType type,AnnotationFinder finder) {
+ ReflectionVar ret = new ReflectionVar(type,finder);
ret.varType = THIS_VAR;
return ret;
}
- public static ReflectionVar createTargetVar(ResolvedType type) {
- ReflectionVar ret = new ReflectionVar(type);
+ public static ReflectionVar createTargetVar(ResolvedType type, AnnotationFinder finder) {
+ ReflectionVar ret = new ReflectionVar(type,finder);
ret.varType = TARGET_VAR;
return ret;
}
- public static ReflectionVar createArgsVar(ResolvedType type, int index) {
- ReflectionVar ret = new ReflectionVar(type);
+ public static ReflectionVar createArgsVar(ResolvedType type, int index, AnnotationFinder finder) {
+ ReflectionVar ret = new ReflectionVar(type,finder);
ret.varType = ARGS_VAR;
ret.argsIndex = index;
return ret;
}
- public static ReflectionVar createThisAnnotationVar(ResolvedType type) {
- ReflectionVar ret = new ReflectionVar(type);
+ public static ReflectionVar createThisAnnotationVar(ResolvedType type, AnnotationFinder finder) {
+ ReflectionVar ret = new ReflectionVar(type,finder);
ret.varType = AT_THIS_VAR;
return ret;
}
- public static ReflectionVar createTargetAnnotationVar(ResolvedType type) {
- ReflectionVar ret = new ReflectionVar(type);
+ public static ReflectionVar createTargetAnnotationVar(ResolvedType type, AnnotationFinder finder) {
+ ReflectionVar ret = new ReflectionVar(type,finder);
ret.varType = AT_TARGET_VAR;
return ret;
}
- public static ReflectionVar createArgsAnnotationVar(ResolvedType type, int index) {
- ReflectionVar ret = new ReflectionVar(type);
+ public static ReflectionVar createArgsAnnotationVar(ResolvedType type, int index, AnnotationFinder finder) {
+ ReflectionVar ret = new ReflectionVar(type,finder);
ret.varType = AT_ARGS_VAR;
ret.argsIndex = index;
return ret;
}
- public static ReflectionVar createWithinAnnotationVar(ResolvedType annType) {
- ReflectionVar ret = new ReflectionVar(annType);
+ public static ReflectionVar createWithinAnnotationVar(ResolvedType annType, AnnotationFinder finder) {
+ ReflectionVar ret = new ReflectionVar(annType,finder);
ret.varType = AT_WITHIN_VAR;
return ret;
}
- public static ReflectionVar createWithinCodeAnnotationVar(ResolvedType annType) {
- ReflectionVar ret = new ReflectionVar(annType);
+ public static ReflectionVar createWithinCodeAnnotationVar(ResolvedType annType, AnnotationFinder finder) {
+ ReflectionVar ret = new ReflectionVar(annType,finder);
ret.varType = AT_WITHINCODE_VAR;
return ret;
}
- public static ReflectionVar createAtAnnotationVar(ResolvedType annType) {
- ReflectionVar ret = new ReflectionVar(annType);
+ public static ReflectionVar createAtAnnotationVar(ResolvedType annType, AnnotationFinder finder) {
+ ReflectionVar ret = new ReflectionVar(annType,finder);
ret.varType = AT_ANNOTATION_VAR;
return ret;
}
- private ReflectionVar(ResolvedType type) {
+ private ReflectionVar(ResolvedType type,AnnotationFinder finder) {
super(type);
+ this.annotationFinder = finder;
}
*/
public class ReflectionWorld extends World {
+ private ClassLoader classLoader;
+ private AnnotationFinder annotationFinder;
+
public ReflectionWorld() {
super();
this.setMessageHandler(new ExceptionBasedMessageHandler());
setBehaveInJava5Way(LangUtil.is15VMOrGreater());
+ this.classLoader = ReflectionWorld.class.getClassLoader();
+ if (LangUtil.is15VMOrGreater()) {
+ initializeAnnotationFinder(this.classLoader);
+ }
+ }
+
+ public ReflectionWorld(ClassLoader aClassLoader) {
+ super();
+ this.setMessageHandler(new ExceptionBasedMessageHandler());
+ setBehaveInJava5Way(LangUtil.is15VMOrGreater());
+ this.classLoader = aClassLoader;
+ if (LangUtil.is15VMOrGreater()) {
+ initializeAnnotationFinder(this.classLoader);
+ }
+ }
+
+ private void initializeAnnotationFinder(ClassLoader loader) {
+ try {
+ Class java15AnnotationFinder = Class.forName("org.aspectj.weaver.reflect.Java15AnnotationFinder");
+ this.annotationFinder = (AnnotationFinder) java15AnnotationFinder.newInstance();
+ this.annotationFinder.setClassLoader(loader);
+ } catch(ClassNotFoundException ex) {
+ // must be on 1.4 or earlier
+ } catch(IllegalAccessException ex) {
+ // not so good
+ throw new RuntimeException("AspectJ internal error",ex);
+ } catch(InstantiationException ex) {
+ throw new RuntimeException("AspectJ internal error",ex);
+ }
+ }
+
+ public ClassLoader getClassLoader() {
+ return this.classLoader;
+ }
+
+ public AnnotationFinder getAnnotationFinder() {
+ return this.annotationFinder;
}
public ResolvedType resolve(Class aClass) {
* @see org.aspectj.weaver.World#resolveDelegate(org.aspectj.weaver.ReferenceType)
*/
protected ReferenceTypeDelegate resolveDelegate(ReferenceType ty) {
- return ReflectionBasedReferenceTypeDelegateFactory.createDelegate(ty, this);
+ return ReflectionBasedReferenceTypeDelegateFactory.createDelegate(ty, this, this.classLoader);
}
/* (non-Javadoc)
package org.aspectj.weaver.tools;
import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
import java.util.HashSet;
import java.util.Iterator;
+import java.util.Properties;
import java.util.Set;
import org.aspectj.bridge.IMessageHandler;
import org.aspectj.weaver.World;
import org.aspectj.weaver.bcel.AtAjAttributes;
import org.aspectj.weaver.internal.tools.PointcutExpressionImpl;
+import org.aspectj.weaver.internal.tools.TypePatternMatcherImpl;
import org.aspectj.weaver.patterns.AndPointcut;
import org.aspectj.weaver.patterns.CflowPointcut;
import org.aspectj.weaver.patterns.FormalBinding;
import org.aspectj.weaver.patterns.SimpleScope;
import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;
import org.aspectj.weaver.patterns.ThisOrTargetPointcut;
+import org.aspectj.weaver.patterns.TypePattern;
import org.aspectj.weaver.reflect.PointcutParameterImpl;
import org.aspectj.weaver.reflect.ReflectionWorld;
*/
public class PointcutParser {
- private static World world = new ReflectionWorld();
+ private World world;
+ private ClassLoader classLoader;
private Set supportedPrimitives;
/**
*/
public PointcutParser() {
supportedPrimitives = getAllSupportedPointcutPrimitives();
+ setClassLoader(PointcutParser.class.getClassLoader());
}
/**
throw new UnsupportedOperationException("Cannot handle if, cflow, and cflowbelow primitives");
}
}
+ setClassLoader(PointcutParser.class.getClassLoader());
+ }
+
+ /**
+ * Create a pointcut parser that can parse pointcut expressions built
+ * from a user-defined subset of AspectJ's supported pointcut primitives.
+ * The following restrictions apply:
+ * <ul>
+ * <li>The <code>if, cflow, and cflowbelow</code> pointcut designators are not supported
+ * <li>Pointcut expressions must be self-contained :- they cannot contain references
+ * to other named pointcuts
+ * <li>The pointcut expression must be anonymous with no formals allowed.
+ * </ul>
+ * @param supportedPointcutKinds a set of PointcutPrimitives this parser
+ * should support
+ * @param classLoader the class loader to use for resolving types
+ * @throws UnsupportedOperationException if the set contains if, cflow, or
+ * cflow below
+ */
+ public PointcutParser(Set/*<PointcutPrimitives>*/ supportedPointcutKinds,ClassLoader cl) {
+ this(supportedPointcutKinds);
+ setClassLoader(cl);
+ }
+
+ /**
+ * Set the classloader that this parser should use for
+ * type resolution.
+ * @param aLoader
+ */
+ public void setClassLoader(ClassLoader aLoader) {
+ this.classLoader = aLoader;
+ world = new ReflectionWorld(this.classLoader);
+ }
+
+ /**
+ * Set the lint properties for this parser from the
+ * given resource on the classpath.
+ * @param resourcePath path to a file containing aspectj
+ * lint properties
+ */
+ public void setLintProperties(String resourcePath)throws IOException {
+ URL url = this.classLoader.getResource(resourcePath);
+ InputStream is = url.openStream();
+ Properties p = new Properties();
+ p.load(is);
+ setLintProperties(p);
+ }
+
+ /**
+ * Set the lint properties for this parser from the
+ * given properties set.
+ * @param properties
+ */
+ public void setLintProperties(Properties properties) {
+ getWorld().getLint().setFromProperties(properties);
}
public PointcutParameter createPointcutParameter(String name, Class type) {
pc = pc.resolve(resolutionScope);
ResolvedType declaringTypeForResolution = null;
if (inScope != null) {
- declaringTypeForResolution = world.resolve(inScope.getName());
+ declaringTypeForResolution = getWorld().resolve(inScope.getName());
} else {
- declaringTypeForResolution = ResolvedType.OBJECT.resolve(world);
+ declaringTypeForResolution = ResolvedType.OBJECT.resolve(getWorld());
}
IntMap arity = new IntMap(formalParameters.length);
for (int i = 0; i < formalParameters.length; i++) {
}
pc = pc.concretize(declaringTypeForResolution, declaringTypeForResolution, arity);
validateAgainstSupportedPrimitives(pc,expression); // again, because we have now followed any ref'd pcuts
- pcExpr = new PointcutExpressionImpl(pc,expression,formalParameters,world);
+ pcExpr = new PointcutExpressionImpl(pc,expression,formalParameters,getWorld());
} catch (ParserException pEx) {
throw new IllegalArgumentException(buildUserMessageFromParserException(expression,pEx));
} catch (ReflectionWorld.ReflectionWorldException rwEx) {
return pcExpr;
}
+ /**
+ * Parse the given aspectj type pattern, and return a
+ * matcher that can be used to match types using it.
+ * @param typePattern an aspectj type pattern
+ * @return a type pattern matcher that matches using the given
+ * pattern
+ * @throws IllegalArgumentException if the type pattern cannot
+ * be successfully parsed.
+ */
+ public TypePatternMatcher parseTypePattern(String typePattern)
+ throws IllegalArgumentException {
+ try {
+ TypePattern tp = new PatternParser(typePattern).parseTypePattern();
+ tp.resolve(world);
+ return new TypePatternMatcherImpl(tp,world);
+ } catch (ParserException pEx) {
+ throw new IllegalArgumentException(buildUserMessageFromParserException(typePattern,pEx));
+ } catch (ReflectionWorld.ReflectionWorldException rwEx) {
+ throw new IllegalArgumentException(rwEx.getMessage());
+ }
+ }
+
+ private World getWorld() {
+ return world;
+ }
+
/* for testing */
Set getSupportedPrimitives() {
return supportedPrimitives;
/* for testing */
IMessageHandler setCustomMessageHandler(IMessageHandler aHandler) {
- IMessageHandler current = world.getMessageHandler();
- world.setMessageHandler(aHandler);
+ IMessageHandler current = getWorld().getMessageHandler();
+ getWorld().setMessageHandler(aHandler);
return current;
}
formalBindings[i] = new FormalBinding(UnresolvedType.forName(formalParameters[i].getType().getName()),formalParameters[i].getName(),i);
}
if (inScope == null) {
- return new SimpleScope(world,formalBindings);
+ return new SimpleScope(getWorld(),formalBindings);
} else {
- ResolvedType inType = world.resolve(inScope.getName());
+ ResolvedType inType = getWorld().resolve(inScope.getName());
ISourceContext sourceContext = new ISourceContext() {
public ISourceLocation makeSourceLocation(IHasPosition position) {
return new SourceLocation(new File(""),0);
--- /dev/null
+/* *******************************************************************
+ * Copyright (c) 2004 IBM Corporation.
+ * All rights reserved.
+ * This program and the accompanying materials are made available
+ * under the terms of the Common Public License v1.0
+ * which accompanies this distribution and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ *
+ * ******************************************************************/
+package org.aspectj.weaver.tools;
+
+/**
+ * A compiled AspectJ type pattern that can be used to
+ * match against types at runtime.
+ */
+public interface TypePatternMatcher {
+
+ /**
+ * Does this type pattern matcher match the
+ * given type (Class).
+ */
+ public boolean matches(Class aClass);
+}
package org.aspectj.weaver.tools;
import java.util.HashSet;
+import java.util.Properties;
import java.util.Set;
import org.aspectj.bridge.AbortException;
}
}
+ public void testXLintConfiguration() {
+ PointcutParser p = new PointcutParser();
+ try {
+ p.parsePointcutExpression("this(FooBar)");
+ } catch(IllegalArgumentException ex) {
+ assertTrue("should have xlint:invalidAbsoluteTypeName",ex.getMessage().indexOf("Xlint:invalidAbsoluteTypeName") != -1);
+ }
+ Properties props = new Properties();
+ props.put("invalidAbsoluteTypeName","ignore");
+ p.setLintProperties(props);
+ p.parsePointcutExpression("this(FooBar)");
+ }
+
private static class IgnoreWarningsMessageHandler implements IMessageHandler {
public boolean handleMessage(IMessage message) throws AbortException {
//$JUnit-BEGIN$
suite.addTestSuite(PointcutParserTest.class);
suite.addTestSuite(PointcutExpressionTest.class);
+ suite.addTestSuite(TypePatternMatcherTest.class);
//$JUnit-END$
return suite;
}
--- /dev/null
+/*******************************************************************************
+ * Copyright (c) 2004 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.aspectj.weaver.tools;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+public class TypePatternMatcherTest extends TestCase {
+
+ TypePatternMatcher tpm;
+
+ public void testMatching() {
+ assertTrue("Map+ matches Map",tpm.matches(Map.class));
+ assertTrue("Map+ matches HashMap",tpm.matches(HashMap.class));
+ assertFalse("Map+ does not match String",tpm.matches(String.class));
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ PointcutParser pp = new PointcutParser();
+ tpm = pp.parseTypePattern("java.util.Map+");
+ }
+
+
+
+}
public class Java15AnnotationFinder implements AnnotationFinder {
private Repository bcelRepository;
+ private ClassLoader classLoader;
+ // must have no-arg constructor for reflective construction
public Java15AnnotationFinder() {
- this.bcelRepository = new ClassLoaderRepository(getClass().getClassLoader());
+ }
+
+ public void setClassLoader(ClassLoader aLoader) {
+ this.bcelRepository = new ClassLoaderRepository(aLoader);
+ this.classLoader = aLoader;
}
/* (non-Javadoc)
*/
public Object getAnnotation(ResolvedType annotationType, Object onObject) {
try {
- Class annotationClass = Class.forName(annotationType.getName());
+ Class annotationClass = Class.forName(annotationType.getName(),false,classLoader);
if (onObject.getClass().isAnnotationPresent(annotationClass)) {
return onObject.getClass().getAnnotation(annotationClass);
}
public Object getAnnotationFromClass(ResolvedType annotationType, Class aClass) {
try {
- Class annotationClass = Class.forName(annotationType.getName());
+ Class annotationClass = Class.forName(annotationType.getName(),false,classLoader);
if (aClass.isAnnotationPresent(annotationClass)) {
return aClass.getAnnotation(annotationClass);
}
if (!(aMember instanceof AccessibleObject)) return null;
AccessibleObject ao = (AccessibleObject) aMember;
try {
- Class annotationClass = Class.forName(annotationType.getName());
+ Class annotationClass = Class.forName(annotationType.getName(),false,classLoader);
if (ao.isAnnotationPresent(annotationClass)) {
return ao.getAnnotation(annotationClass);
}
private ResolvedType superclass;
private ResolvedType[] superInterfaces;
private String genericSignature = null;
- private Java15AnnotationFinder annotationFinder = new Java15AnnotationFinder();
+ private Java15AnnotationFinder annotationFinder = null;
public Java15ReflectionBasedReferenceTypeDelegate() {}
- public void initialize(ReferenceType aType, Class aClass, World aWorld) {
- super.initialize(aType, aClass, aWorld);
+ @Override
+ public void initialize(ReferenceType aType, Class aClass, ClassLoader classLoader, World aWorld) {
+ super.initialize(aType, aClass, classLoader, aWorld);
myType = AjTypeSystem.getAjType(aClass);
+ annotationFinder = new Java15AnnotationFinder();
+ annotationFinder.setClassLoader(classLoader);
}
fromTypes(forMethod.getGenericExceptionTypes()),
forMethod
);
+ ret.setAnnotationFinder(this.annotationFinder);
return ret;
}
fromTypes(forConstructor.getGenericExceptionTypes()),
forConstructor
);
+ ret.setAnnotationFinder(this.annotationFinder);
return ret;
}
private ResolvedMember createGenericFieldMember(Field forField) {
- return new ReflectionBasedResolvedMemberImpl(
+ ReflectionBasedResolvedMemberImpl ret =
+ new ReflectionBasedResolvedMemberImpl(
org.aspectj.weaver.Member.FIELD,
getResolvedTypeX(),
forField.getModifiers(),
forField.getName(),
new UnresolvedType[0],
forField);
+ ret.setAnnotationFinder(this.annotationFinder);
+ return ret;
}
public ResolvedMember[] getDeclaredPointcuts() {