Community
Participate
Working Groups
BETA_JAVA8: The following program elicits two different diagnostics: this is needlessly divergent. Also as one of the messages is more explicit we should opt for it. // --- import java.lang.annotation.*; import static java.lang.annotation.ElementType.*; @Target(TYPE_USE) @interface IllegalSyntax {} @Target(TYPE_USE) @interface Legal {} class X { static int staticField; static class StaticNestedClass {} void foo() { // Syntax error, type annotations are illegal here int x = @IllegalSyntax X.staticField; // Type annotations are not allowed on type names used to access static members StaticNestedClass snc = (@IllegalSyntax X.StaticNestedClass) null; } }
Shankha, Thanks for following up.
Working on it.
The two errors received are: 1) Syntax error, type annotations are illegal here 2) Type annotations are not allowed on type names used to access static members I assume (2) is more concise and therefore the chosen one. The issue is the (1) error happens at the parser stage and (2) happens at the annotation stage. There is no information as per as I see to recognize a variable as static at parser stage. Thanks
I have no idea how to go about it. One way could be to report (1) at the annotation processing stage. Consider the test case: import java.lang.annotation.ElementType; import java.lang.annotation.Target; @Target(ElementType.TYPE_USE) @interface IllegalSyntax {} @Target(ElementType.TYPE_USE) @interface Legal {} class Y { public static int staticField; public static class StaticNestedClass {} public int nonStatic = 10; } class X { void foo() { // Syntax error, type annotations are illegal here int x = @IllegalSyntax Y.staticField; // Type annotations are not allowed on type names used to access static members Y.StaticNestedClass snc = (@IllegalSyntax Y.StaticNestedClass) null; Y y = new Y(); int z = @Legal y.nonStatic; } } At the parsing stage while you are processing: int x = @IllegalSyntax Y.staticField; How will you know If Y.staticField is access to a static field? Thanks
(In reply to shankha banerjee from comment #4) > At the parsing stage while you are processing: > int x = @IllegalSyntax Y.staticField; > > How will you know If Y.staticField is access to a static field? I agree, this is not knowable at parsing stage. We should also not defer this to resolve stage because there could be clients that don't request bindings and the erroneous program should continue to elicit the error message already. Thanks for the clear and correct analysis Shankha, I think we can live with this behavior.