Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-dev] problems using LTW and WeavingClassLoader

Hi all,  
         I think this question has already been asked on this mailing list but I could not find satisfactory answer.. So here is my problem...

I am trying to use LTW using command line aj script as follows :

I have
 Aspect file : TraceAspect.aj  

public aspect TraceAspect {

pointcut trace()
: call (* *.printH*(..));
before() : trace() {
System.out.println("Printing from aspects :  hello...................");
}
}


 java file : Hello.java 

public class Hello{
int i;
String s;
Hello()
{
this.i=0;
this.s="";
}
public void printHello()
{
System.out.println("hello  from printHello" );
}
}


Main.java :

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 *
 * @author lnvidyad
 */
public class Main {

    public static void main(String[] args) {

 ClassLoader classLoader = ClassLoader.getSystemClassLoader();
 System.out.println("my loader " + classLoader.toString() );
        try {
            //load class...
            
Class myClass = classLoader.loadClass("hello");
            System.out.println("Class loaded:" + myClass.getName());

            //get the array of methods...
            Method m[] = myClass.getMethods();

            //make object...
            Object p = null;
            try {
                //actually, make object here...
                p = myClass.newInstance();

                try {
                    //invoke the first method...
                    m[0].invoke(p,null);
                } catch (IllegalAccessException ex) {
                    ex.printStackTrace();
                } catch (IllegalArgumentException ex) {
                    ex.printStackTrace();
                } catch (InvocationTargetException ex) {
                    ex.printStackTrace();
                }

            } catch (InstantiationException ex) {
                ex.printStackTrace();
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}


basically i want to use WeavingURLClassLoader at runtime to load my classes just like URLCLassLoader

So , I did following steps: (all files in same directory)

1.  ajc TraceAspect.java -outjar tracelib.jar
2.  ajc Hello.java
3.  ajc Main.java

4. set ASPECTPATH=tracelib.jar
5. aj Hello                         


Since aj replaces system classloader with WeavingURLCLassLoader, when I load Hello class  it should also perform weaving of TraceAspect
However, TraceAspect is not getting woven and hence I am not able to print message from TraceAspect...  

1. Can someone please explain me what is the problem ?
2 .What is the alternate way of using  WeavingURLClassLoader at runtime ? 

Thanks 

Regards,
Atharva


Back to the top