Skip to content

Commit 52c7b04

Browse files
Not all deprecation warnings reported for types defined in one java file (#4570)
Check for common enclosing type not CU Further improve isInsideDeprecatedCode() as it is now needed for org.eclipse.jdt.core.tests.compiler.regression.AnnotationTest.test093() Fixes #4563
1 parent 397154a commit 52c7b04

File tree

10 files changed

+174
-18
lines changed

10 files changed

+174
-18
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/ASTNode.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,8 @@ public final boolean isFieldUseDeprecated(FieldBinding field, Scope scope, int f
494494

495495
if (!field.isViewedAsDeprecated()) return false;
496496

497-
// inside same unit - no report
498-
if (scope.isDefinedInSameUnit(field.declaringClass)) return false;
497+
// inside same outermost enclosing type - no report
498+
if (scope.isDefinedInSameEnclosingType(field.declaringClass.outermostEnclosingType())) return false;
499499

500500
// if context is deprecated, may avoid reporting
501501
if (!scope.compilerOptions().reportDeprecationInsideDeprecatedCode && scope.isInsideDeprecatedCode()) return false;
@@ -544,8 +544,8 @@ public final boolean isMethodUseDeprecated(MethodBinding method, Scope scope,
544544

545545
if (!method.isViewedAsDeprecated()) return false;
546546

547-
// inside same unit - no report
548-
if (scope.isDefinedInSameUnit(method.declaringClass)) return false;
547+
// inside same outermost enclosing type - no report
548+
if (scope.isDefinedInSameEnclosingType(method.declaringClass.outermostEnclosingType())) return false;
549549

550550
// non explicit use and non explicitly deprecated - no report
551551
if (!isExplicitUse &&
@@ -617,8 +617,8 @@ public final boolean isTypeUseDeprecated(TypeBinding type, Scope scope) {
617617

618618
if (!refType.isViewedAsDeprecated()) return false;
619619

620-
// inside same unit - no report
621-
if (scope.isDefinedInSameUnit(refType)) return false;
620+
// inside same outermost enclosing type - no report
621+
if (scope.isDefinedInSameEnclosingType(refType.outermostEnclosingType())) return false;
622622

623623
// if context is deprecated, may avoid reporting
624624
if (!scope.compilerOptions().reportDeprecationInsideDeprecatedCode && scope.isInsideDeprecatedCode()) return false;

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/Scope.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3847,6 +3847,15 @@ public final boolean isDefinedInSameUnit(ReferenceBinding type) {
38473847
return false;
38483848
}
38493849

3850+
/* Answer true if this scope and the given type share a outermost enclosing type
3851+
*/
3852+
public final boolean isDefinedInSameEnclosingType(ReferenceBinding type) {
3853+
ClassScope outerMostClassScope = outerMostClassScope();
3854+
if (outerMostClassScope != null && outerMostClassScope.referenceContext != null)
3855+
return TypeBinding.equalsEquals(outerMostClassScope.referenceContext.binding, type.outermostEnclosingType());
3856+
return false;
3857+
}
3858+
38503859
/* Answer true if the scope is nested inside a given type declaration
38513860
*/
38523861
public final boolean isDefinedInType(ReferenceBinding type) {
@@ -3925,7 +3934,7 @@ public boolean isInsideDeprecatedCode(){
39253934
}
39263935
}
39273936
}
3928-
if (this.parent != null)
3937+
if (this.parent != null && !(this.parent instanceof CompilationUnitScope))
39293938
return this.parent.isInsideDeprecatedCode();
39303939
return false;
39313940
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2788,6 +2788,11 @@ public void test093() {
27882788
" @Inherited\n" +
27892789
" ^^^^^^^^^^\n" +
27902790
"The annotation @Inherited is disallowed for this location\n" +
2791+
"----------\n" +
2792+
"2. WARNING in X.java (at line 8)\n" +
2793+
" class B extends A {\n" +
2794+
" ^\n" +
2795+
"The type A is deprecated\n" +
27912796
"----------\n");
27922797
}
27932798
// check handling of empty array initializer (binary check)

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

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,103 @@ public void run() {
388388
}
389389
}
390390
"""};
391+
}
392+
public void testGH4563_cu() {
393+
Runner runner = new Runner();
394+
runner.testFiles = new String[] {
395+
"X.java",
396+
"""
397+
class A {
398+
@Deprecated void m() {}
399+
@Deprecated public String f;
400+
@Deprecated class Inner {}
401+
}
402+
public class X {
403+
void test(A a) {
404+
a.m();
405+
a.f = "";
406+
A.Inner o1 = a.new Inner();
407+
Runnable r = () -> {
408+
a.m();
409+
a.f = "";
410+
A.Inner o2 = a.new Inner();
411+
};
412+
}
413+
}
414+
"""
415+
};
416+
runner.expectedCompilerLog =
417+
"""
418+
----------
419+
1. WARNING in X.java (at line 8)
420+
a.m();
421+
^
422+
The method m() from the type A is deprecated
423+
----------
424+
2. WARNING in X.java (at line 9)
425+
a.f = "";
426+
^
427+
The field A.f is deprecated
428+
----------
429+
3. WARNING in X.java (at line 10)
430+
A.Inner o1 = a.new Inner();
431+
^^^^^
432+
The type A.Inner is deprecated
433+
----------
434+
4. WARNING in X.java (at line 10)
435+
A.Inner o1 = a.new Inner();
436+
^^^^^
437+
The type A.Inner is deprecated
438+
----------
439+
5. WARNING in X.java (at line 12)
440+
a.m();
441+
^
442+
The method m() from the type A is deprecated
443+
----------
444+
6. WARNING in X.java (at line 13)
445+
a.f = "";
446+
^
447+
The field A.f is deprecated
448+
----------
449+
7. WARNING in X.java (at line 14)
450+
A.Inner o2 = a.new Inner();
451+
^^^^^
452+
The type A.Inner is deprecated
453+
----------
454+
8. WARNING in X.java (at line 14)
455+
A.Inner o2 = a.new Inner();
456+
^^^^^
457+
The type A.Inner is deprecated
458+
----------
459+
""";
460+
runner.runWarningTest();
461+
}
462+
public void testGH4563_class() {
463+
Runner runner = new Runner();
464+
runner.testFiles = new String[] {
465+
"X.java",
466+
"""
467+
public class X {
468+
class A {
469+
@Deprecated void m() {}
470+
@Deprecated public String f;
471+
@Deprecated class Inner {}
472+
}
473+
class B {
474+
void test(A a) {
475+
a.m();
476+
a.f = "";
477+
A.Inner o1 = a. new Inner();
478+
Runnable r = () -> {
479+
a.m();
480+
a.f = "";
481+
A.Inner o2 = a.new Inner();
482+
};
483+
}
484+
}
485+
}
486+
"""
487+
};
391488
runner.runConformTest();
392489
}
393490
public static Class testClass() {

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,23 +1445,24 @@ public void testDeprecation() {
14451445
" ^^\n" +
14461446
"The type TC is deprecated\n");
14471447
}
1448-
public void testDeprecation2() { // verify that deprecation warning does not show up when the deprecated element is used in the same file defining it.
1448+
public void testDeprecation2() { // verify that deprecation warning does not show up when the deprecated element is used in the same class defining it.
14491449
this.runNegativeTest(
14501450
new String[] {
1451-
"T.java",
1452-
"@Deprecated\n" +
1453-
"@interface TC {\n" +
1451+
"X.java",
1452+
"public class X {\n" +
1453+
"@Deprecated @interface TC {\n" +
14541454
" public T[] value();\n" +
14551455
"}\n" +
14561456
"@java.lang.annotation.Repeatable(TC.class)\n" +
14571457
"@interface T {\n" +
14581458
" public int value() default -1;\n" +
14591459
"}\n" +
14601460
"interface I extends @T(1) Runnable {\n" +
1461+
"}\n" +
14611462
"}\n"
14621463
},
14631464
"----------\n" +
1464-
"1. ERROR in T.java (at line 9)\n" +
1465+
"1. ERROR in X.java (at line 9)\n" +
14651466
" interface I extends @T(1) Runnable {\n" +
14661467
" ^^\n" +
14671468
"Annotation types that do not specify explicit target element types cannot be applied here\n" +

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6375,7 +6375,12 @@ public void testForwardReference() {
63756375
" Class<? extends Annotation> value();\n" +
63766376
"}\n",
63776377
},
6378-
"");
6378+
"----------\n" +
6379+
"1. WARNING in T.java (at line 5)\n" +
6380+
" @R(TC.class)\n" +
6381+
" ^^\n" +
6382+
"The type TC is deprecated\n" +
6383+
"----------\n");
63796384
}
63806385
public void testHybridTargets() throws Exception {
63816386
this.runConformTest(

org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15JLS4Test.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8485,7 +8485,20 @@ public void test0259() throws JavaModelException {
84858485
0);
84868486
assertEquals("Not a compilation unit", ASTNode.COMPILATION_UNIT, node.getNodeType());
84878487
CompilationUnit unit = (CompilationUnit) node;
8488-
assertProblemsSize(unit, 0);
8488+
assertProblems("unexpected problems",
8489+
"""
8490+
1. WARNING in /Converter15/src/X.java (at line 5)
8491+
@Annot(id=4)
8492+
^^^^^
8493+
The type Annot is deprecated
8494+
----------
8495+
2. WARNING in /Converter15/src/X.java (at line 8)
8496+
@Annot(id=4) class Y {
8497+
^^^^^
8498+
The type Annot is deprecated
8499+
----------
8500+
""",
8501+
unit.getProblems(), contents.toCharArray());
84898502
node = getASTNode(unit, 1);
84908503
assertEquals("Not a type declaration unit", ASTNode.TYPE_DECLARATION, node.getNodeType());
84918504
TypeDeclaration typeDeclaration = (TypeDeclaration) node;

org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15JLS8Test.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8482,7 +8482,20 @@ public void test0259() throws JavaModelException {
84828482
0);
84838483
assertEquals("Not a compilation unit", ASTNode.COMPILATION_UNIT, node.getNodeType());
84848484
CompilationUnit unit = (CompilationUnit) node;
8485-
assertProblemsSize(unit, 0);
8485+
assertProblems("unexpected problems",
8486+
"""
8487+
1. WARNING in /Converter15/src/X.java (at line 5)
8488+
@Annot(id=4)
8489+
^^^^^
8490+
The type Annot is deprecated
8491+
----------
8492+
2. WARNING in /Converter15/src/X.java (at line 8)
8493+
@Annot(id=4) class Y {
8494+
^^^^^
8495+
The type Annot is deprecated
8496+
----------
8497+
""",
8498+
unit.getProblems(), contents.toCharArray());
84868499
node = getASTNode(unit, 1);
84878500
assertEquals("Not a type declaration unit", ASTNode.TYPE_DECLARATION, node.getNodeType());
84888501
TypeDeclaration typeDeclaration = (TypeDeclaration) node;

org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15Test.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8521,7 +8521,20 @@ public void test0259() throws JavaModelException {
85218521
0);
85228522
assertEquals("Not a compilation unit", ASTNode.COMPILATION_UNIT, node.getNodeType());
85238523
CompilationUnit unit = (CompilationUnit) node;
8524-
assertProblemsSize(unit, 0);
8524+
assertProblems("unexpected problems",
8525+
"""
8526+
1. WARNING in /Converter15/src/X.java (at line 5)
8527+
@Annot(id=4)
8528+
^^^^^
8529+
The type Annot is deprecated
8530+
----------
8531+
2. WARNING in /Converter15/src/X.java (at line 8)
8532+
@Annot(id=4) class Y {
8533+
^^^^^
8534+
The type Annot is deprecated
8535+
----------
8536+
""",
8537+
unit.getProblems(), contents.toCharArray());
85258538
node = getASTNode(unit, 1);
85268539
assertEquals("Not a type declaration unit", ASTNode.TYPE_DECLARATION, node.getNodeType());
85278540
TypeDeclaration typeDeclaration = (TypeDeclaration) node;

org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter18Test.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4891,7 +4891,7 @@ public void testBug425601_001() throws JavaModelException {
48914891
" Outer<String>.Middle<String> m;\n"+
48924892
" Outer<String>.Middle<String>.Inner<Object> i;\n"+
48934893
"}\n";
4894-
CompilationUnit cu = (CompilationUnit) buildAST(contents, this.workingCopy);
4894+
CompilationUnit cu = (CompilationUnit) buildAST(contents, this.workingCopy, false/*don't report*/);
48954895
TypeDeclaration typedeclaration = (TypeDeclaration) getASTNode(cu, 0);
48964896
FieldDeclaration[] fields = typedeclaration.getFields();
48974897
ITypeBinding binding = fields[0].getType().resolveBinding();
@@ -4934,7 +4934,7 @@ public void testBug425601_002() throws JavaModelException {
49344934
" Outer<String>.Middle<String> m;\n"+
49354935
" Outer<String>.Middle<String>.Inner<Object> i;\n"+
49364936
"}\n";
4937-
CompilationUnit cu = (CompilationUnit) buildAST(contents, this.workingCopy);
4937+
CompilationUnit cu = (CompilationUnit) buildAST(contents, this.workingCopy, false/*don't report*/);
49384938
TypeDeclaration typedeclaration = (TypeDeclaration) getASTNode(cu, 0);
49394939
FieldDeclaration[] fields = typedeclaration.getFields();
49404940
ITypeBinding binding = fields[0].getType().resolveBinding();

0 commit comments

Comments
 (0)