I'm trying to load classes at runtime and weave them with some AspectJ aspects at this point. I have load-time weaving enabled, and it works when I use it more conventionally.
I have the following in my @Aspect class:
@Before("call(* mypackage.MyInterface.*())")
public void myInterfaceExecuteCall(JoinPoint thisJoinPoint,
JoinPoint.StaticPart thisJoinPointStaticPart,
JoinPoint.EnclosingStaticPart thisEnclosingJoinPointStaticPart) {
System.out.println(thisJoinPoint.getSignature().getDeclaringType());
System.out.println(thisJoinPoint.getSignature().getName());
}
Then I'm scanning the jars and finding classes that are implementations of MyInterface
:
URLClassLoader classLoader = new URLClassLoader(new URL[] { urlOfJar },
ClassLoader.getSystemClassLoader());
WeavingURLClassLoader weaver = new WeavingURLClassLoader(
classLoader);
HashSet<Class<?>> executableClasses = new HashSet<Class<?>>();
for (String name : classNamesInJar) {
try {
Class<?> myImplementation = weaver.loadClass(name);
if (MyInterface.class.isAssignableFrom(myImplementation)) {
executableClasses.add(myImplementation);
}
} catch (Exception e) {
e.printStackTrace();
} catch (NoClassDefFoundError e) {
e.printStackTrace();
}
}
... and then I'm executing a specific method in the loaded classes at some point:
try {
Method execute = myImplementation.getMethod("execute");
execute.invoke(myImplementation.newInstance());
} catch (Exception e) {
e.printStackTrace();
}
However, the @Before method that I gave you above is never executed when I call execute.invoke(...)
(although the execute
method itself is obviously executed, since I see its output).
Does someone know what I'm doing wrong? What's the way to make myInterfaceExecuteCall
get called before the loaded class's methods get called?