0

I have a java file as follows

package sample;
    public class Profile

    {


    public static String myName(String name)
    {
        myhobby("Football");
        return name;
    }
        public static String myhobby(String hobby)
    {


        return hobby;
    }

    }

I build this file and added the jar file into the below code...

import sample.Profile;

  public class Hello

    {

        public static String sayHello(String name)
        {

            String enter=Test.myName("Ganguly");
            return name;
        }

        public static void main(String[] args)
        {
        String next =   sayHello("Company");

        }
    }

And I wrote aspect as follows...

pointcut printMessage(String name) : call(public static String myhobby(..)) && args (name));
     before(String name) : printMessage(name) {
            System.out.println("value is: "+ name);

     }

But when I run the program...it doesn't printed the parameter value of the function hobby... can any one correct me if I am wrong... Thanks in advance...

user3797438
  • 405
  • 3
  • 6
  • 24

1 Answers1

2

By default, AspectJ IDE only weave current project with aspects of same project, we need add In-Path or Aspect-Path for the project for other scenarios.

From Properties dialog of the second project (your testing project) > 'AspectJ Build' page > InPath , add your jar to the list (the jar is added to Java Build Path library automatically at same time).

Daniel Yang
  • 276
  • 2
  • 11
  • Thank u...it worked..well..I have another one doubt...regarding aspectj..can I ask u? – user3797438 Oct 10 '14 at 06:05
  • Thank u... I installed aspectj plugin in eclipse and then I created a new Aspectj project in which I created an java class and .aj file. I compiled it by importing the jar in in-path of Aspectj build and Runas->Java application. Then it compiles and specified advice runs successfully. Then I exported the project as a jar. Then I created a regular java project and then I imported the exported jar and then I called one of the function (for which advice is written) in the regular java project. But advice never runs when the function is called. can u tell me why... – user3797438 Oct 14 '14 at 08:40
  • not export as normal jar, we should try our best use exact option, 'Run As > Java Application can work' possibly comes from AJDT plugin enables JDT weaving, it seems 'Export as' gives us another option 'Export to jar with AspectJ support', please try 'Jar with AspectJ support'. – Daniel Yang Oct 14 '14 at 08:53
  • yes...I exported jar as export->Jar with Aspectj support...But advice didnt worked – user3797438 Oct 14 '14 at 08:55
  • Say, your project A is class of normal project A, class B is AJ project B, C class of normal project C, when C calls A, it don't work, if C calls B (B calls A) then it works, because B is already woven during B compilation, but C is not AspectJ project, it is not woven. If we want weaving works during C directly calls A, project C should be AspectJ project and A.jar is added to 'In-Path' of project C and B.jar is added to 'ApsectJ Path' of project C, or we change to use runtime weaving other than compilation time weaving. – Daniel Yang Oct 14 '14 at 09:29
  • Sorry, it is Load-Time weaving, Open "Run As ..." dialog, create a launching profile with type 'AspectJ Load-Time Weaving Application', and add B.jar to its 'LTW AspectPath', but I don't know how to run AspectJ runtime weaving by command line. Runtime weaving requires special Class Loader. http://stackoverflow.com/questions/10733247/aspectj-weaving-with-custom-classloader-at-runtime – Daniel Yang Oct 14 '14 at 10:08