How to attach a pointcut to a to a method of class residing inside a .jar file using command line?

StackOverflow https://stackoverflow.com/questions/22298945

  •  12-06-2023
  •  | 
  •  

Question

I am new to AOP . I have created a class in HookShow.java file:

public class HookShow
{
        void show(String msg)
        {
            System.out.println("In show :"+msg);
        }
        public static void main(String[] args)
        {
            HookShow cm=new HookShow();
            cm.show("called from main method");

        }
}

then i compiled it and added the generated .class file into .jar file using :

jar cf HSHOW.jar HookShow.class

Now i have a HSHOW.jar. Now i am creating an aspect that has a pointcut which is executed on the call of show(). But i dont have any idea about how can i reffer the class and method inside that jar file. Following is my Aspect file:

public aspect AspectHookShow
{
   pointcut changemsgPointcut( String str) :execution(void HookShow.show(String)) && args(str);

   before(String str ) : changemsgPointcut( str)
   {
      System.out.println("inside pointcut :"+str);
   }    
}

so please can anyone tell me how can i reffer to the method inside class in my jar file.

Was it helpful?

Solution

After a research of 3 hours i found 3 ways to achive what is mentioned above, good thing was that i had not to change the code much , what i needed to just execute some commands on cmd prompt :

First Way:

I got one way from Oreilly AspectJ Cookbook :

steps: 1:Compile the HookShow class using the traditional javac command:

javac HookShow.java

2:Package the generated HookShow.class file into a .jar file titled MyApp.jar:

jar -cvf MyApp.jar HookShow.class

3:Compile the AspectHookShow.java aspect using the ajc command, specifying the new MyApp.jar on the command line using the -inpath option:

> ajc -inpath MyApp.jar AspectHookShow.java

The -inpath option forces the ajc compiler to extract the Java byte code from the supplied .jar files into the destination directory as specified by the -d option. The ajc compiler then includes that extracted byte code in the aspect weaving process.

4:If no errors occur during compilation with ajc then you will have successfully woven the classes contained within the MyApp.jar file with the AspectHookShow aspect. Because the ajc command extracts the classes from the .jar files supplied to the -inpath option, they are no longer needed to run the application. However, you can optionally re-package your new application in a .jar file of its own using the -outjar option when running the ajc command:

> ajc  -inpath MyApp.jar -outjar MyAspectOrientedApp.jar AspectHookShow.java

This produces a MyAspectOrientedApp.jar that contains your application's aspects and classes that can then be run using the traditional java command:

> java -classpath MyAspectOrientedApp.jar HookSHow

Second Way:

Execute following command:

>ajc -janars aspectjrt.jar ;HSHOW.jar AspectHookShow.java -outjar Final.jar

This will create Final.jar. then i put this jar in my classpath and now i m able to use it in any class.

Hope it will be useful for someone else like me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top