[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-users] Exposing state held in method annotations

First time I run the sample code, I get a NoSuchMethodError when trying to
pull back the "m" method, because it's not public and therefore needs the
setAccessible(true) call, or changing "m" to public (which is what I did).
Doing so (and printing the return value from getMethodAnnotation) prints
"1", indicating that method annotations *are* preserved in the .class.

Hate to say it, Wes, but it was the example that was broken this time. :-)

Ted Neward
Author, Instructor, Consultant
Java, .NET, Web services
http://www.neward.net/ted

> -----Original Message-----
> From: aspectj-users-admin@xxxxxxxxxxx [mailto:aspectj-users-
> admin@xxxxxxxxxxx] On Behalf Of Wes Isberg
> Sent: Thursday, January 27, 2005 12:52 PM
> To: aspectj-users@xxxxxxxxxxx
> Subject: Re: [aspectj-users] Exposing state held in method annotations
> 
> Hi Eric -
> 
> > But how can I expose the "value" state "{1}" that I passed in above?
> > The following does *not* work:
> >
> > pointcut constantArgumentMethods(int[] value) :
> > call(@ConstantArguments(value) * *.*(..));
> 
> @withincode() should surface a method annotation, but 1.5.0M1
> doesn't yet support binding (and also fails to compile when a
> literal type is used).
> 
> Java 5 should permit you to reflectively read the annotation on a method.
> Code for that is below, but looks broken: when annotations are added
> to methods, they no longer are returned by Class.getMethod*(..)
> (true of both javac and jdt, so my code could be wrong - corrections?)
> 
> See the developer's notebook section on runtime matching and context
> exposure for annotations:
> http://dev.eclipse.org/viewcvs/indextech.cgi/~checkout~/aspectj-
> home/doc/ajdk15notebook/annotations-pointcuts-and-advice.html#d0e1169
> 
> Wes
> 
> https://bugs.eclipse.org/bugs/show_bug.cgi?id=83875
> 
> ------------ misc/MAI.java
> package misc;
> 
> import java.lang.reflect.Method;
> import java.lang.annotation.*;
> import java.util.Arrays;
> 
> /**
>  * MAI: method annotation instance
>  */
> public class MAI {
> 
>     public static void main(String[] args) {
>     	m();
>     	getMethodAnnotation();
>     }
> 
>     @CConstantArguments(id=1)
>     static void m() {
>         System.out.println("in m()");
>     }
> 
>     static int getMethodAnnotation() {
>     	try {
>         	Class c = MAI.class;
>         	Method[] ms = c.getMethods();
>         	System.out.println("" + Arrays.asList(ms));
>         	Method m = c.getMethod("m", new Class[0]);
>         	CConstantArguments ca =
> m.getAnnotation(CConstantArguments.class);
>     		return (null == ca ? -1 : ca.id());
>     	} catch (Exception e) {
>     		throw new Error(e);
>     	}
>     }
> }
> @Retention(RetentionPolicy.RUNTIME)
> @interface CConstantArguments {
>    int id();
> }
> 
> 
> > ------------Original Message------------
> > From: Eric Bodden <eric@xxxxxxxxx>
> > To: aspectj-users@xxxxxxxxxxx
> > Date: Thu, Jan-27-2005 10:26 AM
> > Subject: [aspectj-users] Exposing state held in method annotations
> >
> >
> > -----BEGIN PGP SIGNED MESSAGE-----
> > Hash: SHA1
> >
> > Hi all.
> >
> > Today I came across an annotation problem which I don't know how to
> > solve:
> >
> > I have a method annotated like this:
> >
> > @ConstantArguments({1}) public int foo(Wrapper a) {
> > ..
> > }
> >
> > Now I am matching on calls to it like this:
> >
> > pointcut constantArgumentMethods() : call(@ConstantArguments *
> > *.*(..));
> >
> > But how can I expose the "value" state "{1}" that I passed in above?
> > The following does *not* work:
> >
> > pointcut constantArgumentMethods(int[] value) :
> > call(@ConstantArguments(value) * *.*(..));
> >
> > I guess @target will not help, since it returns annotations of the
> > target *type*, not *method*, or am I wrong?
> >
> > Cheers,
> > Eric
> >
> > P.S. Definition of the ynnotation type:
> >
> > public @interface ConstantArguments {
> >
> >     int[] value() default {};
> >
> > }
> >
> > - --
> > Eric Bodden
> > Chair I2 for Programming Languages and Program Analysis
> > RWTH Aachen University
> >
> > -----BEGIN PGP SIGNATURE-----
> > Version: PGP 8.0.3
> >
> > iQA/AwUBQfkyNMwiFCm7RlWCEQJUvwCdF6oPnnOj0IOrwgK3oqBFsEG+f+EAn1lW
> > n+BcQ4OfJRo3KlGuMlt7q+3Y
> > =IJhX
> > -----END PGP SIGNATURE-----
> >
> >
> > _______________________________________________
> > aspectj-users mailing list
> > aspectj-users@xxxxxxxxxxx
> > http://dev.eclipse.org/mailman/listinfo/aspectj-users
> >
> 
> 
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> http://dev.eclipse.org/mailman/listinfo/aspectj-users