Is this code valid?
public abstract class A {
protected static final String c = "my const";
}
@myAnnotation(value=A.c)
public class B extends A {
}
Eclipse
with JDK 1.6.0.23 accepts this, but Maven
2.2.1 with JDK 1.6.0.23 shows me the following compile error:
c has protected access in A
I think I see what is happening here. An instance of an annotations is effectively an interface with a unique static initializer. The only things the annotation spec adds on top are syntactic sugar and a link to the method, class or field. So when you type value=c.A
that is almost like adding a static initilizer to the annotation. The annotation is not a subclass of A, so access is denied. Protected access includes package access, so when you move A into the same package as B the annotation is also in the same package as A. It gets access. Very good question and I think the behavior should be the same for both compilers. I think Eclipse will let you customize what it treats as an error so you might be able to make them agree to both use the undesirable, more restrictive behavior.