AspectJ - Weaving with a custom ClassLoader at runtime

I am trying to load classes at runtime and weave them with some aspects of AspectJ at this point. I have an overload function in time, and it works when I use it more conditionally.

My @Aspect class has the following:

@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 go through the banks and find the 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 execute 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 never executes when I call execute.invoke(...) (although the execute method itself is explicitly executed since I can see its output).

Does anyone know what I'm doing wrong? How to call myInterfaceExecuteCall call to call the called methods of the class?

+4
source share
2 answers

OK I found out what it was, and it doesn't work the way I intended, but here is a workaround:

Just do @Before("execution(* mypackage.MyInterface.*())") Instead of call . This works even if the class was loaded manually and at runtime using a special class loader. This is because AspectJ does not care about calls made using Method.invoke(...) . I hope someone else can use this workaround.

Here is a link to the documentation containing memorable information:

For example, pointcutcut does not select reflective calls for a method implemented in java.lang.reflect.Method.invoke (Object, Object []).

http://www.eclipse.org/aspectj/doc/released/progguide/implementation.html

If you have another solution, feel free to respond!

+4
source

If I'm not mistaken, AspectJ cannot weave the JDK classes. This explains this:

For example, pointcutcut does not select reflective calls for a method implemented in java.lang.reflect.Method.invoke (Object, Object []).

0
source

Source: https://habr.com/ru/post/1414093/


All Articles