Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] How can I use 'declare' to verify that a class implements a certain interface?

Hmm. I'm using 1.3.0..... I wrote a little set of test classes, etc.
to figure out what worked. I saw errors on all my classes when I first
tried the expression that I said doesn't work. ;) The PC with two
"withins" gave me the error on just the class with the annotation but
not the interface.

Here is the code I used:

== experiments.parents.RequireInterface.aj ==

package experiments.parents;

public aspect RequireInterface {
	declare error:
		 within (@MyAnnotation experiments.parents.*) &&
		!within (experiments.parents.MyInterface+): "Must implement MyInterface";
	
	/**
	 * Alternative: Force all classes with the annotation to implement
the interface.
	 * Any classes that don't implement the interfaces methods will not compile.
	 */
//	declare parents: (@MyAnnotation *) implements MyInterface;
}

== experiments.parents.MyAnnotation.java ==

package experiments.parents;
public @interface MyAnnotation {}

== experiments.parents.MyInterface.java ==

package experiments.parents;
public interface MyInterface {
  int doit ();
}

== experiments.parents.HasInterfaceImpl.java ==

package experiments.parents;
// Class that implements the required interface
@MyAnnotation
public class HasInterfaceImpl implements MyInterface {
	public int doit() { return 0;	}
}

== experiments.parents.MissingInterfaceImpl.java ==

package experiments.parents;
// Class that doesn't implements the required interface
@MyAnnotation
public class MissingInterfaceImpl {
	//...
}

When I build, the only error is on the last class.

dean


On 1/8/06, Jeppe Cramon <jeppe@xxxxxxxxx> wrote:
>  Thanks, but it doesn't seem to work. It creates multiple error markers in
> Eclipse, which don't seem logical or consistent.
>  Have I stumpled on a bug in the AJDT 1.3?
>
>  /Jeppe
>


Back to the top