Skip to content

Commit 794b394

Browse files
committed
Fixed for Bug4986
1 parent 0b27f84 commit 794b394

3 files changed

Lines changed: 59 additions & 6 deletions

File tree

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3357,8 +3357,27 @@ public void importProblem(ImportReference importRef, Binding expectedImport) {
33573357
case ProblemReasons.NotVisible :
33583358
case ProblemReasons.NotAccessible :
33593359
id = (expectedImport.problemId() == ProblemReasons.NotVisible) ? IProblem.NotVisibleField : IProblem.NotAccessibleField;
3360-
readableArguments = new String[] {CharOperation.toString(importRef.tokens), new String(field.declaringClass.readableName())};
3361-
shortArguments = new String[] {CharOperation.toString(importRef.tokens), new String(field.declaringClass.shortReadableName())};
3360+
if (importRef.isStatic() && field.declaringClass != null) {
3361+
// messages.properties uses:
3362+
// 71 = The field {1}.{0} is not visible
3363+
readableArguments = new String[] {
3364+
new String(field.name), // {0}
3365+
new String(field.declaringClass.readableName()) // {1}
3366+
};
3367+
shortArguments = new String[] {
3368+
new String(field.name), // {0}
3369+
new String(field.declaringClass.shortReadableName()) // {1}
3370+
};
3371+
} else {
3372+
readableArguments = new String[] {
3373+
CharOperation.toString(importRef.tokens),
3374+
new String(field.declaringClass.readableName())
3375+
};
3376+
shortArguments = new String[] {
3377+
CharOperation.toString(importRef.tokens),
3378+
new String(field.declaringClass.shortReadableName())
3379+
};
3380+
}
33623381
break;
33633382
case ProblemReasons.Ambiguous :
33643383
id = IProblem.AmbiguousField;
@@ -5039,6 +5058,7 @@ private boolean isRecoveredName(char[][] qualifiedName) {
50395058
return false;
50405059
}
50415060

5061+
50425062
public void javadocAmbiguousMethodReference(int sourceStart, int sourceEnd, Binding fieldBinding, int modifiers) {
50435063
int severity = computeSeverity(IProblem.JavadocAmbiguousMethodReference);
50445064
if (severity == ProblemSeverities.Ignore) return;

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NonFatalErrorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ public void testImportStaticProblems() {
518518
"2. ERROR in X.java (at line 2)\n" +
519519
" import static p1.Y.f;\n" +
520520
" ^^^^^^\n" +
521-
"The field Y.p1.Y.f is not visible\n" +
521+
"The field Y.f is not visible\n" +
522522
"----------\n",
523523
"OK",
524524
"",

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/StaticImportTest.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public void test005() { // test visibility
254254
"1. ERROR in p\\X.java (at line 3)\n" +
255255
" import static p2.Z.Zint;\n" +
256256
" ^^^^^^^^^\n" +
257-
"The field Z.p2.Z.Zint is not visible\n" +
257+
"The field Z.Zint is not visible\n" +
258258
"----------\n" +
259259
"2. ERROR in p\\X.java (at line 4)\n" +
260260
" import static p2.Z.ZMember;\n" +
@@ -1368,7 +1368,7 @@ public void test036() {
13681368
"1. ERROR in X.java (at line 2)\n" +
13691369
" import static p.A.CONSTANT_B;\n" +
13701370
" ^^^^^^^^^^^^^^\n" +
1371-
"The field B.p.A.CONSTANT_B is not visible\n" +
1371+
"The field B.CONSTANT_B is not visible\n" +
13721372
"----------\n" +
13731373
"2. ERROR in X.java (at line 5)\n" +
13741374
" static int j = p.A.CONSTANT_B;\n" +
@@ -2517,7 +2517,7 @@ public void test073() {
25172517
"1. ERROR in test\\Outer.java (at line 2)\n" +
25182518
" import static test.Outer.Inner.VALUE;\n" +
25192519
" ^^^^^^^^^^^^^^^^^^^^^^\n" +
2520-
"The field Outer.Inner.test.Outer.Inner.VALUE is not visible\n" +
2520+
"The field Outer.Inner.VALUE is not visible\n" +
25212521
"----------\n" +
25222522
"2. ERROR in test\\Outer.java (at line 4)\n" +
25232523
" int i = VALUE;\n" +
@@ -3559,4 +3559,37 @@ public class Sub extends Super {}
35593559
runner.expectedOutputString = "1";
35603560
runner.runConformTest();
35613561
}
3562+
3563+
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4986
3564+
// Test that error message for inaccessible static import field is correctly formatted
3565+
public void testBug4986() {
3566+
this.runNegativeTest(
3567+
new String[] {
3568+
"p/C.java",
3569+
"""
3570+
package p;
3571+
3572+
import static p.C.f;
3573+
3574+
public class C {
3575+
private static int f;
3576+
}
3577+
"""
3578+
},
3579+
"""
3580+
----------
3581+
1. ERROR in p\\C.java (at line 3)
3582+
import static p.C.f;
3583+
^^^^^
3584+
The field C.f is not visible
3585+
----------
3586+
2. WARNING in p\\C.java (at line 6)
3587+
private static int f;
3588+
^
3589+
The value of the field C.f is not used
3590+
----------
3591+
"""
3592+
);
3593+
}
3594+
35623595
}

0 commit comments

Comments
 (0)