Search code examples
javaaopaspectj

The method aspectOf is undefined


I built a mailserver in java and I have been providedwith an ObserverProtocol written with AOP. I am using Eclipse with AspectJ plugin as required

I try to use static methods like aspectOf, who I as understand should be added at the weaving step

Eclipse can't seem to make it work as I always get the following error :

Description Resource Path Location Type The method aspectOf() is undefined for the type ObserverProtocol MailReaderBean.java /emailClent_test/src/emailserver line 86 Java Problem

From what I understood by snooping around various websites, tutorials and documentation, it seems that my .aj files aren't being woven correctly

I tried compiling manually with ajc to no avail, and I tinkered with paths and settings and jars and libs in various ways, nothing seems to work and I can't seem to find a definitive guide or tutorial to setup things correctly

My aspect code :

package protocol;


import java.util.WeakHashMap;
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;


public abstract aspect ObserverProtocol 
{

    protected interface Subject { }


    protected interface Observer { }

    private WeakHashMap perSubjectObservers;        

    protected List getObservers(Subject s) 
    {
        if (perSubjectObservers == null) 
        {
            perSubjectObservers = new WeakHashMap();
        }

        List observers = (List)perSubjectObservers.get(s);

        if ( observers == null ) 
        {
            observers = new LinkedList();
            perSubjectObservers.put(s, observers);
        }

        return observers;
    }       

    public void addObserver(Subject s, Observer o) 
    {
        getObservers(s).add(o);
    }       

    public void removeObserver(Subject s, Observer o) 
    {
        getObservers(s).remove(o);
    }

    protected abstract pointcut subjectChange(Subject s);

    after(Subject s): subjectChange(s) 
    {
        Iterator iter = getObservers(s).iterator();

        while ( iter.hasNext() ) 
        {
            updateObserver(s, ((Observer)iter.next()));
        }
    }       

    protected abstract void updateObserver(Subject s, Observer o);

    public static ObserverProtocol aspectOf() {
        // TODO Auto-generated method stub
        return this;
    }
}

Offending code in my java server

//Add observer Proxy for monitoring the subject MailServer.
ObserverProtocol.aspectOf().addObserver(this, proxy );

Is there any resources that could help me understand how weaving works and how to setup my build without resorting to Spring or Maven?


Solution

  • This method (and hasAspect()) is added during weaving. If your aspect was built with javac rather than ajc, then it will not have these methods when jvm starts because the weaver hasn't yet run.

    I recommend you read these documents: