Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-users] AW: Pointcut on a constructorwithacustom@Annotation

> the object may belong to several types only some of which 
> have
> the annotation...

In normal Java parlance, you say an object has a class, obtained using 
getClass(), regardless of how many superinterfaces or superclasses 
the class has.  This tracks what the Java programmer expects when 
they write the Java equivalent (below).

Also, since in Java you have to specify that annotations are inherited,
it's a distinction that makes a difference in Java, and should probably
be respected, instead of being erased if we made @target(Annotation) return
true if the object class or any of its supertypes or superinterfaces
have that annotation (leaving aside the question of runtime retention).
The annotation writer, by making the annotation class not inherited, 
has said that derived classes should not be treated as annotated, 
so any developer (aspect or otherwise) using the annotation should 
track that.

It's an understandable mistake, given target(type) semantics.
But in Java that instanceof(type) is true even for supertypes of the 
object, so target(type) follows suit.

In any case, you're right that we should qualify the documentation
right there to avoid misconceptions.  We might also want other PCD's
with the semantics you propose, since that would be very hard to
write at present.  Compelling use-cases would motivate new PCD's.

Wes

 $ cat Annot.java
import java.lang.annotation.*;
@Retention(value= RetentionPolicy.RUNTIME)
@interface Ann{}
public class Annot {
  public static void main(String[] a) {
    boolean hasAnnotation = null !=
    new C().getClass().getAnnotation(Ann.class);
    System.out.println("C hasAnnotation: " + hasAnnotation);
    hasAnnotation = null !=
    new B().getClass().getAnnotation(Ann.class);
    System.out.println("B hasAnnotation: " + hasAnnotation);
  }
}
@Ann
class B {}
class C extends B {}

 $ aspectj-1.5 -1.5 Annot.java && j5 Annot
C hasAnnotation: false
B hasAnnotation: true

> ------------Original Message------------
> From: "Ron Bodkin" <rbodkin@xxxxxxxxxxxxxx>
> To: aspectj-users@xxxxxxxxxxx
> Date: Fri, Apr-21-2006 8:11 AM
> Subject: RE: [aspectj-users] AW: Pointcut on a constructorwithacustom@Annotation
>
> >Annotations are inherited if you put @Inherited on them.
> >Making the annotation inherited ensures @target matches:
> 
> So @target(Annotation) matches if and only if
> thisJoinPoint.getTarget().getClass() has the annotation. The guide says
> "where the object bound to this (or target, respectively) has an 
> annotation
> of the specified type" 
> 
> However, it wasn't obvious to me from reading this text that it refers 
> only
> to the most-derived class ... since annotations aren't inherited by 
> default,
> I think it's not right to refer to an object as having an annotation: 
> only
> types do, and the object may belong to several types only some of which 
> have
> the annotation...
> 
> >Also Ramnivas raised a bug a while ago on some problems with '+' when
> >annotations are in the mix:
> >https://bugs.eclipse.org/bugs/show_bug.cgi?id=128664
> 
> Sounds like I rediscovered this.
> 
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
> 



Back to the top