Skip to content

Commit cfe39b0

Browse files
Missing warning for use of deprecated annotation elements (#4573)
+ new check in MemberValuePair.resolveTypeExpecting + ensure that ContainerAnnotation has reasonable source positions * adjust apt & model tests * Version bump(s) for 4.38 stream Fixes #4571 --------- Co-authored-by: Eclipse JDT Bot <[email protected]>
1 parent 43cfd91 commit cfe39b0

File tree

9 files changed

+138
-14
lines changed

9 files changed

+138
-14
lines changed

org.eclipse.jdt.apt.tests/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %pluginName
44
Bundle-SymbolicName: org.eclipse.jdt.apt.tests; singleton:=true
5-
Bundle-Version: 3.6.800.qualifier
5+
Bundle-Version: 3.6.900.qualifier
66
Bundle-ClassPath: apt.jar,
77
aptext.jar,
88
.

org.eclipse.jdt.apt.tests/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<relativePath>../tests-pom/</relativePath>
2020
</parent>
2121
<artifactId>org.eclipse.jdt.apt.tests</artifactId>
22-
<version>3.6.800-SNAPSHOT</version>
22+
<version>3.6.900-SNAPSHOT</version>
2323
<packaging>eclipse-test-plugin</packaging>
2424

2525
<build>

org.eclipse.jdt.apt.tests/src/org/eclipse/jdt/apt/tests/ReadAnnotationTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.eclipse.jdt.apt.tests.annotations.readannotation.CodeExample;
2424
import org.eclipse.jdt.apt.tests.plugin.AptTestsPlugin;
2525
import org.eclipse.jdt.core.IJavaProject;
26+
import org.eclipse.jdt.core.JavaCore;
2627

2728
public class ReadAnnotationTests extends APTTestBase
2829
{
@@ -118,6 +119,7 @@ private IProject setupTest() throws Exception
118119
// This should not be necessary, but see https://bugs.eclipse.org/bugs/show_bug.cgi?id=99638
119120
IJavaProject jproj = getCurrentJavaProject();
120121
jproj.setOption("org.eclipse.jdt.core.compiler.problem.deprecation", "ignore");
122+
jproj.setOption(JavaCore.COMPILER_PB_MEMBER_OF_DEPRECATED_TYPE, JavaCore.IGNORE);
121123
return jproj.getProject();
122124
}
123125

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2013 Jesper S Moller and others.
2+
* Copyright (c) 2013, 2025 Jesper S Moller and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -41,15 +41,29 @@ public ContainerAnnotation(Annotation repeatingAnnotation, ReferenceBinding cont
4141
this.resolvedType = containerAnnotationType;
4242
this.recipient = repeatingAnnotation.recipient;
4343
this.containees = new Annotation[0];
44-
this.memberValue = this.memberValues = new ArrayInitializer();
44+
this.memberValue = this.memberValues = createInitializer(repeatingAnnotation);
4545
addContainee(repeatingAnnotation);
4646
}
4747

48+
private ArrayInitializer createInitializer(Annotation repeatingAnnotation) {
49+
ArrayInitializer arrayInitializer = new ArrayInitializer();
50+
// initialize positions suitable for problem reporting (e.g., deprecation)
51+
MemberValuePair[] pairs = repeatingAnnotation.memberValuePairs();
52+
arrayInitializer.sourceStart = this.sourceStart;
53+
if (pairs != null && pairs.length > 0) {
54+
arrayInitializer.sourceEnd = pairs[pairs.length-1].sourceEnd;
55+
} else {
56+
arrayInitializer.sourceEnd = repeatingAnnotation.declarationSourceEnd;
57+
}
58+
return arrayInitializer;
59+
}
60+
4861
public void addContainee(Annotation repeatingAnnotation) {
4962
final int length = this.containees.length;
5063
System.arraycopy(this.containees, 0, this.containees = new Annotation[length + 1], 0, length);
5164
this.containees[length] = repeatingAnnotation;
5265
this.memberValues.expressions = this.containees;
66+
this.memberValues.sourceEnd = repeatingAnnotation.declarationSourceEnd;
5367
repeatingAnnotation.setPersistibleAnnotation(length == 0 ? this : null);
5468
}
5569

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2017 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -65,6 +65,9 @@ public void resolveTypeExpecting(BlockScope scope, TypeBinding requiredType) {
6565
return;
6666
}
6767

68+
if (this.binding != null && isMethodUseDeprecated(this.binding, scope, false, null))
69+
scope.problemReporter().deprecatedMethod(this.binding, this);
70+
6871
if (this.value == null) {
6972
this.compilerElementPair = new ElementValuePair(this.name, this.value, this.binding);
7073
return;

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

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,111 @@ public Y() {}
619619
};
620620
runner.runConformTest();
621621
}
622+
public void testAnnotationElement() {
623+
Runner runner = new Runner();
624+
runner.testFiles = new String[] {
625+
"X.java",
626+
"""
627+
@interface A1 {
628+
@Deprecated int value() default 0;
629+
}
630+
@interface A2 {
631+
@Deprecated int oldVal() default 0;
632+
int newVal() default 0;
633+
}
634+
public class X {
635+
@A1
636+
public String f1;
637+
@A1(1)
638+
public void m1() {}
639+
@A1(value=2)
640+
public class Inner1 {}
641+
@A2
642+
public String f2;
643+
@A2(oldVal=2)
644+
public void m2() {}
645+
@A2(newVal=4)
646+
public class Inner2 {}
647+
}
648+
"""
649+
};
650+
runner.expectedCompilerLog =
651+
"""
652+
----------
653+
1. WARNING in X.java (at line 11)
654+
@A1(1)
655+
^
656+
The method value() from the type A1 is deprecated
657+
----------
658+
2. WARNING in X.java (at line 13)
659+
@A1(value=2)
660+
^^^^^
661+
The method value() from the type A1 is deprecated
662+
----------
663+
3. WARNING in X.java (at line 17)
664+
@A2(oldVal=2)
665+
^^^^^^
666+
The method oldVal() from the type A2 is deprecated
667+
----------
668+
""";
669+
runner.runWarningTest();
670+
}
671+
public void testAnnotationElement_repeatable() {
672+
Runner runner = new Runner();
673+
runner.testFiles = new String[] {
674+
"X.java",
675+
"""
676+
@interface TC1 {
677+
@Deprecated public T1[] value();
678+
}
679+
@java.lang.annotation.Repeatable(TC1.class)
680+
@interface T1 {
681+
public int value() default -1;
682+
}
683+
@interface TC2 {
684+
public T2[] value();
685+
}
686+
@java.lang.annotation.Repeatable(TC2.class)
687+
@interface T2 {
688+
@Deprecated public int value() default -1;
689+
public int alt() default 3;
690+
}
691+
public class X {
692+
@T1(1) @T1(value=2) @T1
693+
String f1;
694+
@T1 @T1(3) @T1(value=4)
695+
String f2;
696+
@T2 @T2(5) @T2(value=6) @T2(alt=7)
697+
String f3;
698+
}
699+
"""
700+
};
701+
runner.expectedCompilerLog =
702+
"""
703+
----------
704+
1. WARNING in X.java (at line 17)
705+
@T1(1) @T1(value=2) @T1
706+
^^^^^^^^^^^^^^^^^^^^^^^
707+
The method value() from the type TC1 is deprecated
708+
----------
709+
2. WARNING in X.java (at line 19)
710+
@T1 @T1(3) @T1(value=4)
711+
^^^^^^^^^^^^^^^^^^^^^^^
712+
The method value() from the type TC1 is deprecated
713+
----------
714+
3. WARNING in X.java (at line 21)
715+
@T2 @T2(5) @T2(value=6) @T2(alt=7)
716+
^
717+
The method value() from the type T2 is deprecated
718+
----------
719+
4. WARNING in X.java (at line 21)
720+
@T2 @T2(5) @T2(value=6) @T2(alt=7)
721+
^^^^^
722+
The method value() from the type T2 is deprecated
723+
----------
724+
""";
725+
runner.runWarningTest();
726+
}
622727
public static Class testClass() {
623728
return Deprecated15Test.class;
624729
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8667,8 +8667,8 @@ public void test0261() throws JavaModelException {
86678667
0);
86688668
assertEquals("Not a compilation unit", ASTNode.COMPILATION_UNIT, node.getNodeType());
86698669
CompilationUnit unit = (CompilationUnit) node;
8670-
String expectedProblems = "";
8671-
assertProblemsSize(unit, 0, expectedProblems);
8670+
String expectedProblems = "The method annotationValue() from the type Annot is deprecated";
8671+
assertProblemsSize(unit, 1, expectedProblems);
86728672
node = getASTNode(unit, 3);
86738673
assertEquals("Not a type declaration unit", ASTNode.TYPE_DECLARATION, node.getNodeType());
86748674
TypeDeclaration typeDeclaration = (TypeDeclaration) node;
@@ -10971,7 +10971,7 @@ public void test342() throws JavaModelException {
1097110971
CompilationUnit unit= (CompilationUnit) buildAST(
1097210972
contents,
1097310973
this.workingCopy,
10974-
true,
10974+
false,
1097510975
true,
1097610976
true);
1097710977
TypeDeclaration typeDeclaration = (TypeDeclaration) getASTNode(unit, 0);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8664,8 +8664,8 @@ public void test0261() throws JavaModelException {
86648664
0);
86658665
assertEquals("Not a compilation unit", ASTNode.COMPILATION_UNIT, node.getNodeType());
86668666
CompilationUnit unit = (CompilationUnit) node;
8667-
String expectedProblems = "";
8668-
assertProblemsSize(unit, 0, expectedProblems);
8667+
String expectedProblems = "The method annotationValue() from the type Annot is deprecated";
8668+
assertProblemsSize(unit, 1, expectedProblems);
86698669
node = getASTNode(unit, 3);
86708670
assertEquals("Not a type declaration unit", ASTNode.TYPE_DECLARATION, node.getNodeType());
86718671
TypeDeclaration typeDeclaration = (TypeDeclaration) node;
@@ -10968,7 +10968,7 @@ public void test342() throws JavaModelException {
1096810968
CompilationUnit unit= (CompilationUnit) buildAST(
1096910969
contents,
1097010970
this.workingCopy,
10971-
true,
10971+
false,
1097210972
true,
1097310973
true);
1097410974
TypeDeclaration typeDeclaration = (TypeDeclaration) getASTNode(unit, 0);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8703,8 +8703,8 @@ public void test0261() throws JavaModelException {
87038703
0);
87048704
assertEquals("Not a compilation unit", ASTNode.COMPILATION_UNIT, node.getNodeType());
87058705
CompilationUnit unit = (CompilationUnit) node;
8706-
String expectedProblems = "";
8707-
assertProblemsSize(unit, 0, expectedProblems);
8706+
String expectedProblems = "The method annotationValue() from the type Annot is deprecated";
8707+
assertProblemsSize(unit, 1, expectedProblems);
87088708
node = getASTNode(unit, 3);
87098709
assertEquals("Not a type declaration unit", ASTNode.TYPE_DECLARATION, node.getNodeType());
87108710
TypeDeclaration typeDeclaration = (TypeDeclaration) node;
@@ -11007,7 +11007,7 @@ public void test342() throws JavaModelException {
1100711007
CompilationUnit unit= (CompilationUnit) buildAST(
1100811008
contents,
1100911009
this.workingCopy,
11010-
true,
11010+
false,
1101111011
true,
1101211012
true);
1101311013
TypeDeclaration typeDeclaration = (TypeDeclaration) getASTNode(unit, 0);

0 commit comments

Comments
 (0)