eclipse-jdt / eclipse.jdt.core

Eclipse Public License 2.0
191 stars 151 forks source link

Class compiles in Eclipse but not in javac (1.8) (access to a private field from the annotation) #1216

Open lrozenblyum opened 1 year ago

lrozenblyum commented 1 year ago
package testjavac;

@SomeAnnotation(value = AnnotationPrivateMember.SOME_CONST)
public class AnnotationPrivateMember {
    private static final String SOME_CONST = "some value";
}

@interface SomeAnnotation {
    String value();
}

Eclipse successfully compiles it (via JDK1.8). Reproduced in Eclipse 2022-12 and 2023-06

Javac reports:

AnnotationPrivateMember.java:[3,48] SOME_CONST has private access in testjavac.AnnotationPrivateMember

The problem was discussed in the past in https://stackoverflow.com/questions/3369731/private-field-in-java-annotations

iloveeclipse commented 1 year ago

Works with master (I20230705-1800) with both java 8 / 17 target. Please try latest build https://download.eclipse.org/eclipse/downloads/drops4/I20230707-0600/ and if that still doesn't work for you, please attach full project with project settings, may be some specific compiler option is not default one.

lrozenblyum commented 1 year ago

Just to double-check @iloveeclipse - 'works with master' means that Eclipse compiler now produces an error like javac?

iloveeclipse commented 1 year ago

Just to double-check @iloveeclipse - 'works with master' means that Eclipse compiler now produces an error like javac?

Oh, I'm sorry, I misread the bug report, I assumed ecj produces error.

So ecj doesn't report any error here, I assume because it inlines the constant value into annotation. Without reading JLS spec I honestly would say that ecj is right. Why should the value be not accessible here?

But @srikanth-sankaran would for sure know right JLS chapter and would explain which rule applies here and who does it wrong - ecj or javac :-)

lrozenblyum commented 1 year ago

@iloveeclipse @srikanth-sankaran maybe this post https://stackoverflow.com/a/54424804/1429367 and the referred JLS chapter https://docs.oracle.com/javase/specs/jls/se11/html/jls-6.html#jls-6.6.1 could be helpful.

srikanth-sankaran commented 1 year ago

This is hit upon again in https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1861 - I'll take a look.

srikanth-sankaran commented 1 year ago

See Example 6.6-5. Access to private Fields, Methods, and Constructors here: https://docs.oracle.com/javase/specs/jls/se21/html/jls-6.html#d5e11140

srikanth-sankaran commented 1 year ago

See also https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2355 - From a comment there, "When testing with ejc, with version 3.31.0 and earlier there is this error: The field AClass.DISPLAY_NAME is not visible" - this could be useful to track down the change that introduced the regression

iloveeclipse commented 3 months ago

Removed out-of-date 4.34 milestone. Please set valid one if needed.