Hi All, Prompted by a question on stackoverflow, I was trying to see if AspectJ can change the class that is being instantiated. I wouldn't have been surprised if it cannot be done, but the error I am getting is pretty strange.
I have two classes R and R1 (extends R) and using an aspect I am trying to create an R1 instead of R. I stumbled on infinite recursion but when I tried to work around it by using a thread local, I got error as follows. My questions: 1. Is it possible to replace object that is (to be) constructed like I am doing? 2. I am pretty sure the ThreadLocal I am using is not the correct way to avoid recursion. What is the correct way? ***R.java*** public class R{ public static void main(String[] args) {System.out.println(new R().getClass().getName());} } ***R1.java*** public class R1 extends R {} ***RAj.aj*** public aspect RAj { private ThreadLocal<Object> inAspect = new ThreadLocal<Object>(); pointcut createR() : execution(R.new()); Object around() : createR() { System.out.println("aspect:" + inAspect.get() + ":" + this); if (inAspect.get() != null) { return proceed(); } else { inAspect.set(this); return new R1(); } } } compile command: /cygdrive/c/Program\ Files/Java/aspectj-1.6.12/bin/ajc.bat -source 1.7 -outxml -outjar araj.jar -classpath "aspectjrt.jar;." RAj.aj run: /cygdrive/c/Program\ Files/Java/aspectj-1.6.12/bin/aj5.bat -classpath ".;./araj.jar" R errors: Apr 06, 2012 1:37:40 PM org.aspectj.weaver.tools.Jdk14Trace error SEVERE: register definition failed java.lang.RuntimeException: Problem processing attributes in RAj at org.aspectj.weaver.bcel.BcelObjectType.ensureAspectJAttributesUnpacked(BcelObjectType.java:385) at org.aspectj.weaver.bcel.BcelObjectType.<init>(BcelObjectType.java:162) at org.aspectj.weaver.bcel.BcelWorld.buildBcelDelegate(BcelWorld.java:394) at org.aspectj.weaver.bcel.BcelWorld.resolveDelegate(BcelWorld.java:389) at org.aspectj.weaver.ltw.LTWWorld.resolveDelegate(LTWWorld.java:107) at org.aspectj.weaver.World.resolveToReferenceType(World.java:485) at org.aspectj.weaver.World.resolve(World.java:326) at org.aspectj.weaver.bcel.BcelWeaver.addLibraryAspect(BcelWeaver.java:159) at org.aspectj.weaver.loadtime.ClassLoaderWeavingAdaptor.registerAspects(ClassLoaderWeavingAdaptor.java:470) at org.aspectj.weaver.loadtime.ClassLoaderWeavingAdaptor.registerDefinitions(ClassLoaderWeavingAdaptor.java:295) at org.aspectj.weaver.loadtime.ClassLoaderWeavingAdaptor.initialize(ClassLoaderWeavingAdaptor.java:181) at org.aspectj.weaver.loadtime.Aj$ExplicitlyInitializedClassLoaderWeavingAdaptor.initialize(Aj.java:277) at org.aspectj.weaver.loadtime.Aj$ExplicitlyInitializedClassLoaderWeavingAdaptor.getWeavingAdaptor(Aj.java:282) at org.aspectj.weaver.loadtime.Aj$WeaverContainer.getWeaver(Aj.java:260) at org.aspectj.weaver.loadtime.Aj.preProcess(Aj.java:91) at org.aspectj.weaver.loadtime.ClassPreProcessorAgentAdapter.transform(ClassPreProcessorAgentAdapter.java:54) at sun.instrument.TransformerManager.transform(TransformerManager.java:188) at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:424) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:791) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) at java.net.URLClassLoader.access$100(URLClassLoader.java:71) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:423) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:356) at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:476) Caused by: org.aspectj.weaver.BCException: malformed org.aspectj.weaver.PointcutDeclaration attribute (length:73)org.aspectj.weaver.BCException: Bad type signature org.aspectj.weaver.WeaverState at org.aspectj.weaver.AjAttribute.read(AjAttribute.java:137) at org.aspectj.weaver.bcel.Utility.readAjAttributes(Utility.java:101) at org.aspectj.weaver.bcel.BcelObjectType.ensureAspectJAttributesUnpacked(BcelObjectType.java:381) ... 30 more [AppClassLoader@1284903] warning register definition failed -- (RuntimeException) Problem processing attributes in RAj Problem processing attributes in RAj java.lang.RuntimeException: Problem processing attributes in RAj at org.aspectj.weaver.bcel.BcelObjectType.ensureAspectJAttributesUnpacked(BcelObjectType.java:385) at org.aspectj.weaver.bcel.BcelObjectType.<init>(BcelObjectType.java:162) at org.aspectj.weaver.bcel.BcelWorld.buildBcelDelegate(BcelWorld.java:394) at org.aspectj.weaver.bcel.BcelWorld.resolveDelegate(BcelWorld.java:389) at org.aspectj.weaver.ltw.LTWWorld.resolveDelegate(LTWWorld.java:107) at org.aspectj.weaver.World.resolveToReferenceType(World.java:485) at org.aspectj.weaver.World.resolve(World.java:326) at org.aspectj.weaver.bcel.BcelWeaver.addLibraryAspect(BcelWeaver.java:159) at org.aspectj.weaver.loadtime.ClassLoaderWeavingAdaptor.registerAspects(ClassLoaderWeavingAdaptor.java:470) at org.aspectj.weaver.loadtime.ClassLoaderWeavingAdaptor.registerDefinitions(ClassLoaderWeavingAdaptor.java:295) at org.aspectj.weaver.loadtime.ClassLoaderWeavingAdaptor.initialize(ClassLoaderWeavingAdaptor.java:181) at org.aspectj.weaver.loadtime.Aj$ExplicitlyInitializedClassLoaderWeavingAdaptor.initialize(Aj.java:277) at org.aspectj.weaver.loadtime.Aj$ExplicitlyInitializedClassLoaderWeavingAdaptor.getWeavingAdaptor(Aj.java:282) at org.aspectj.weaver.loadtime.Aj$WeaverContainer.getWeaver(Aj.java:260) at org.aspectj.weaver.loadtime.Aj.preProcess(Aj.java:91) at org.aspectj.weaver.loadtime.ClassPreProcessorAgentAdapter.transform(ClassPreProcessorAgentAdapter.java:54) at sun.instrument.TransformerManager.transform(TransformerManager.java:188) at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:424) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:791) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) at java.net.URLClassLoader.access$100(URLClassLoader.java:71) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:423) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:356) at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:476) Caused by: org.aspectj.weaver.BCException: malformed org.aspectj.weaver.PointcutDeclaration attribute (length:73)org.aspectj.weaver.BCException: Bad type signature org.aspectj.weaver.WeaverState at org.aspectj.weaver.AjAttribute.read(AjAttribute.java:137) at org.aspectj.weaver.bcel.Utility.readAjAttributes(Utility.java:101) at org.aspectj.weaver.bcel.BcelObjectType.ensureAspectJAttributesUnpacked(BcelObjectType.java:381) ... 30 more
_______________________________________________ aspectj-users mailing list aspectj-users@eclipse.org https://dev.eclipse.org/mailman/listinfo/aspectj-users