AspectJ java throwing weird errors

691 views Asked by At

I'm trying to implement the observer pattern into my program using AspectJ (not spring) and I tried to write the adding observer method but the compiler seems to complain

after(Subject employee, Observer handler): execution(com.company.domain.EmployeeController.registerTo(*))&& this(handler)&& args(employee){
        employee.addObserver(handler);
    }

The oart where it complains is this:execution(com.company.domain.EmployeeController.registerTo(*))

The error is:

[ERROR] Failed to execute goal org.codehaus.mojo:aspectj-maven-plugin:1.10:compile (default) on project SpaceX: AJC compiler errors:
[ERROR] error at after(Subject employee, Observer handler): execution(com.company.domain.EmployeeController.registerTo(*))&& this(handler)&& args(conc){
[ERROR] 
[ERROR] D:\Projects2016\SpaceX\src\main\java\aspects\SpacexObserver.aj:12:0::0 Syntax error on token ")", "(" expected
[ERROR] -> [Help 1]

It makes no sense to me what it is expecting. I split that declaration in different lines and that's how I saw that it complains exactly on the 2nd closing of the (com.company.domain.EmployeeController.registerTo(*)--->)<--- That's the paranthesis. I don't know why it is complaining though

I tried changing the aspectj versions but that doesn't do anything. Chnaing the parameter to anything else but a * just makes it throw a different version so I cant figure out why it won't like it.

Removing the method and trying to run the aspect wont work because then the entire thing makes no sense and I also don't know how to debug this.

I have yet to find out what is going on. I will post here the entire Aspect and maybe people can figure out cause after a few days I still cant find out why my program refuses to work considering everything.

public privileged aspect SpacexObserver extends AbstractObserver{
    protected pointcut observed(Subject o):execution(* com.company.domain.EmployeeController.createDesign(..)) && target(o);

    //adding an observer
    after(Subject employee, Observer handler): execution(com.company.domain.EmployeeController.registerTo(*))&& this(handler)&& args(employee){
        employee.addObserver(handler);
        System.out.println("wer");
    }

    //removing an observer
//    after(ViewResultsHandler handler):execution(* ViewResultsHandler.close())&& target(handler){
//        Employee con=handler.concurs;
//        con.removeObserver(handler);
//
//    }

    //observer action
    public void Employee.update(Object o){
        System.out.println("call loadParticipants");
    }
}

And for the AbstractObserver part I have this:

    protected interface Subject{
        void addObserver(Observer o);
//        void removeObserver(Observer o);
        void notifyObservers(Object newData);

    }

    protected interface Observer{
        void update(Object newData);
    }

    private Set<Observer> Subject.observers=new HashSet<Observer>();
    public void Subject.addObserver(Observer o){
        System.out.println("Adding observer");
        observers.add(o);
    }
    public void Subject.removeObserver(Observer o){
        System.out.println("Removing observer");
        observers.remove(o);
    }
    public void Subject.notifyObservers(Object newData){
        for(Observer obs:observers){
            obs.update(newData);
        }
    }

    protected abstract pointcut observed(Subject o);

    after (Subject o) returning:observed(o){
        o.notifyObservers(null);
    }
}

I believe that here after(Subject employee, Observer handler) I'm supposed to use the classes that actually implement the subject/observer but the aspect refuses to see them as such (it doesn't see the methods they implement) so I dont knwo if it makes any difference in findign otu why I have this problem. As I heard, observer pattern can be implemented only using aspectj and that's what Im using. The error doesn't make sense

0

There are 0 answers