Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Why annotations are not exposed through JoinPoint.StaticPart?

> But "one can't new up annotations in an easy way", which I interpret as "one> cannot have a code like the above".>> If I have grokked you correctly, then this is sad. Java annotations are not> like C# attributes after all.
exactly right.  You can mess about and build them, but it isn't as
easy as new, you can't even call newInstance on the type via
reflection as it is an interface - here is the kind of code you can
hack up to build an annotation instance in java...

Annotation annotation = (Annotation) Proxy.newProxyInstance(
  clazz.getClassLoader(),
  new Class[] { Annotation.class },
  new InvocationHandler() {
    @Override public Object invoke(Object proxy, Method method, Object[] args) {
      return clazz; // only getClass() or annotationType() should be called.
    }
  });

I think reflection is cheaper than that.

Andy


On 14 December 2011 14:04, Mark <mark.kharitonov@xxxxxxxxx> wrote:
> OK, let me repeat, because I am slow. There is some internal AspectJ code
> that runs during the compilation. This code can know what the annotations
> are and theoretically could have injected the following code into the static
> constructor:
>
> typeAnnotations = new Annotation[2];
> typeAnnotations[0] = new TypeAnnotation1("yaba daba doo");
> typeAnnotations[1] = new TypeAnnotation2(3);
>
> for a type annotated like so:
>
> @TypeAnnotation1("yaba daba doo")
> @TypeAnnotation2(3)
> public class MyType {}
>
> But "one can't new up annotations in an easy way", which I interpret as "one
> cannot have a code like the above".
>
> If I have grokked you correctly, then this is sad. Java annotations are not
> like C# attributes after all.
>
> --
> View this message in context: http://aspectj.2085585.n4.nabble.com/Why-annotations-are-not-exposed-through-JoinPoint-StaticPart-tp4196637p4197257.html
> Sent from the AspectJ - users mailing list archive at Nabble.com.
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users


Back to the top