Thank you for looking into this.

adviceexecution() is what I was trying to remember. And of course,
!within(RAj) would work too.

aj5.bat does almost same as what you wrote below, except that it adds
aspectjweaver.jar to classpath besides setting it as java agent.

Regards
Hemal

On Fri, Apr 6, 2012 at 4:45 PM, Andy Clement <andrew.clem...@gmail.com>wrote:

> I'll need to try out the code myself, but just a quick initial reply:
>
> > 2. I am pretty sure the ThreadLocal I am using is not the correct way to
> > avoid recursion. What is the correct way?
>
> Couple of options.  If you want to continue advising execution() then
> you want something like:
>
> execution(R.new()) && !cflow(adviceexecution())
> "execution of the R constructor except when we are in the control flow
> of some advice"  (aspectj effectively manages the threadlocal for you
> in implementing this)
>
> Or if you can switch to advising call() you can use:
>
> call(R.new()) && !within(RAj)
>
> "call to the R constructor except when it is made from the aspect RAj"
> - this is better than cflow as using cflow injects a test that is made
> at runtime (to check if you are in the control flow).  Straightforward
> within() can be determined statically at compile time and there is no
> runtime test required.
>
> I'll try your program and look at that exception when I get a minute.
>
> One thing aj5 is kind of deprecated - I don't use it myself (actually
> I don't really recall what it does!).  I typically just use:
>
> java -javaagent:<pathtoaj>/lib/aspectjweaver.jar R
>
> as long as araj.jar is on the classpath the loadtime weaver (activated
> via the agent) will find it and weave the aspect as R runs.
>
> cheers,
> Andy
>
> On 6 April 2012 13:39, Hemal Pandya <hemal.pan...@gmail.com> wrote:
> > 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
> >
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@eclipse.org
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>
_______________________________________________
aspectj-users mailing list
aspectj-users@eclipse.org
https://dev.eclipse.org/mailman/listinfo/aspectj-users

Reply via email to