Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] LTW: Are there any posibilities to avoid using ajc and overcome NoSuchMethodError

Actually the aspects do not need to be compiled with AspectJ. The aspects can be compiled with javac. *But* before they are usable they must be 'finished off' by ajc, but that can be done at loadtime. Here is my example:

// Code.java
public class Code {
  public static void main(String []argv) {
    new Code().m();
  }

  public void m() {}
}

// Doit.java
import org.aspectj.lang.annotation.*;
@Aspect
public class Doit {
  @Around("execution(* m(..))")
  public void advice() {
    System.out.println("advice running");
  }
}

// META-INF/aop-ajc.xml
<aspectj>
    <aspects>
        <!-- declare existing aspects to the weaver -->
        <aspect name="Doit"/>
    </aspects>
    <weaver options="-showWeaveInfo">
        <include within="*"/>
    </weaver>
</aspectj>

====
> javac Doit.java Code.java

> java -javaagent:../lib/aspectjweaver.jar Code

[AppClassLoader@1ef6a746] weaveinfo Join point 'method-execution(void Code.m())' in Type 'Code' (Code.java:6) advised by around advice from 'Doit' (Doit.java)

advice running



The aspect should get 'finished off' (and the necessary aspectOf() and hasAspect() added) by the weaver just before it gets used. That doesn't seem to be happening with your setup - does it work for you with the agent?

On a quick look I think the problem might be that the WeavingURLClassLoader path into the system doesn't have a concept of unfinished aspects. You could try additionally including the aspect library in the classURLs list? That might be enough to remind it that it needs to finish it off.

cheers
Andy



On 16 April 2013 05:55, Alexander Kriegisch <Alexander@xxxxxxxxxxxxxx> wrote:
Theoretically you can use ajc from aspectjtools.jar and compile on the fly, even thought this does not sound so nice.

See my answer at http://stackoverflow.com/a/15837624/1082681 for a code sample.

Alexander Kriegisch

Am 16.04.2013 um 14:16 schrieb Ron DiFrango <rdifrango@xxxxxxxxxxxxxxxxxxxxx>:

Gokul,

Even if you are using load time weaving, the aspects themselves have to be compiled with the aspect compiler.  I don't think there is any way around this.

Thanks,


Ron DiFrango

From: aspectj-users-bounces@xxxxxxxxxxx [aspectj-users-bounces@xxxxxxxxxxx] on behalf of Gokul [mail_gokulin@xxxxxxxxxxx]
Sent: Tuesday, April 16, 2013 12:37 AM
To: aspectj-users@xxxxxxxxxxx
Subject: [aspectj-users] LTW: Are there any posibilities to avoid using ajc and overcome NoSuchMethodError

Hi,

My project uses a custom plugin framework, where plugin's are JAR files loaded at run-time by the framework using custom class loader. We have several plugin JAR files that are to be loaded and AOP looks to be a perfect solution to add a particular functionality across plugins.

Following are my Aspect definition and other implementation details:

package net.XXX.pub.plugin;

@Aspect
public class FatalWarningAdvice
{
    @Around("target(net.XXX.pub.plugin.XXX.XXModelSerializable) && call(@ReportFatalError * *(..))")
    public void handleFatalWarning(ProceedingJoinPoint joinPoint) throws Throwable {

        XXModelSerializable target = (XXModelSerializable)joinPoint.getTarget();

        try {           
            joinPoint.proceed();
        } catch(Throwable ex) {
            throw new XXPluginException(target.getDetailedErrorMessage(ex));
        }

    }

    /* Adding below method solves my problem, but are there any better way to solve this problem ?
    public static FatalWarningAdvice aspectOf() {
        return new FatalWarningAdvice();
    }*/
}

The above aspect is part of public.jar file. I have also defined aop.xml file under META-INF folder as follows:

<aspectj>
    <aspects>
        <!-- declare existing aspects to the weaver -->
        <aspect name="net.XXX.pub.plugin.FatalWarningAdvice"/>
    </aspects>
    <weaver options="-showWeaveInfo">
        <include within="com.XXX.plugin..*"/>
        <include within="net.XXX.pub.plugin..*"/>
    </weaver>
</aspectj>

I want FatalWarningAdvice aspect to be weaved on runtime when plugin's are loaded by the plugin framework. Following code demonstrated the same:

private synchronized ClassLoader getClassLoader(String jarName) throws IOException {
        ClassLoader classLoader = classLoaders.get(jarName);
        if(null == classLoader) {
            ClassLoader startupClassLoader = PluginServiceImpl.class.getClassLoader();
            File jarFile = new File(runtimeJarDir, jarName);
            File aspectJarFile = new File("public.jar");
            //classLoader = new NestedJarClassLoader(jarFile, tmpJarDir, startupClassLoader);
            //Below call provides list of URLs that will be input to AspectJ weaver.
            List<URL> classURLs = NestedJarClassLoader.getURLList(jarFile, tmpJarDir);
            classURLs.add(aspectJarFile.toURI().toURL());
            classLoader = new WeavingURLClassLoader(classURLs.toArray(new URL[classURLs.size()]), new URL[]{aspectJarFile.toURI().toURL()}, startupClassLoader);
            classLoaders.put(jarName, classLoader);
        }
        return classLoader;
    }

Upon executing the above code I get the following exception:
java.lang.NoSuchMethodError: net.XXX.pub.plugin.FatalWarningAdvice.aspectOf()Lnet/XXX/pub/plugin/FatalWarningAdvice;

I could temporarily over come this problem by adding below lines of code in my Aspect:
   
    public static FatalWarningAdvice aspectOf() {
        return new FatalWarningAdvice();
    }

Are there any work-around to this problem ? One solution is to use ajc or iajc but then I requires change to my build environment. As part of LTW are there any possibility to avoid using ajc ?
Introducing ajc in the build process is a pain as it will affect the team, each developer has to include the aspectj-tools.jar to ant library (to my knowledge). Are there any possibilities for AspectJ to include these methods at runtime ?

Thanks in advance
Gokul



_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users

_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users



Back to the top