Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deprecated check routines in ASTNode to also check since value #2874

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@
import org.eclipse.jdt.internal.compiler.ast.TypeReference.AnnotationPosition;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.env.AccessRestriction;
import org.eclipse.jdt.internal.compiler.impl.StringConstant;
import org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding;
import org.eclipse.jdt.internal.compiler.lookup.ArrayBinding;
import org.eclipse.jdt.internal.compiler.lookup.Binding;
import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
import org.eclipse.jdt.internal.compiler.lookup.ClassScope;
import org.eclipse.jdt.internal.compiler.lookup.ElementValuePair;
import org.eclipse.jdt.internal.compiler.lookup.ExtraCompilerModifiers;
import org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
import org.eclipse.jdt.internal.compiler.lookup.InferenceContext18;
Expand Down Expand Up @@ -528,6 +530,8 @@ public final boolean isFieldUseDeprecated(FieldBinding field, Scope scope, int f
// inside same unit - no report
if (scope.isDefinedInSameUnit(field.declaringClass)) return false;

if (sinceValueUnreached(field, scope)) return false;

// if context is deprecated, may avoid reporting
if (!scope.compilerOptions().reportDeprecationInsideDeprecatedCode && scope.isInsideDeprecatedCode()) return false;
return true;
Expand Down Expand Up @@ -578,6 +582,8 @@ public final boolean isMethodUseDeprecated(MethodBinding method, Scope scope,
// inside same unit - no report
if (scope.isDefinedInSameUnit(method.declaringClass)) return false;

if (sinceValueUnreached(method, scope)) return false;

// non explicit use and non explicitly deprecated - no report
if (!isExplicitUse &&
(method.modifiers & ClassFileConstants.AccDeprecated) == 0) {
Expand All @@ -589,6 +595,40 @@ public final boolean isMethodUseDeprecated(MethodBinding method, Scope scope,
return true;
}

private boolean sinceValueUnreached(Binding binding, Scope scope) {
if (binding instanceof TypeBinding typeBinding) {
if (!typeBinding.isReadyForAnnotations()) {
return false;
}
}
AnnotationBinding[] annotations = binding.getAnnotations();
for (AnnotationBinding annotation : annotations) {
if (annotation != null && String.valueOf(annotation.getAnnotationType().readableName()).equals("java.lang.Deprecated")) { //$NON-NLS-1$
ElementValuePair[] pairs = annotation.getElementValuePairs();
for (ElementValuePair pair : pairs) {
if (String.valueOf(pair.getName()).equals("since")) { //$NON-NLS-1$
if (pair.getValue() instanceof StringConstant strConstant) {
try {
String value = strConstant.stringValue();
int sinceValue = Integer.parseInt(value);
// As long as the AST levels and ClassFileConstants.MAJOR_VERSION grow simultaneously,
// we can use the offset of +44 to compute the Major version from the given AST Level
long sinceLevel = ClassFileConstants.getComplianceLevelForJavaVersion(sinceValue + 44);
long complianceLevel = scope.compilerOptions().complianceLevel;
if (complianceLevel < sinceLevel) {
return true;
}
} catch (NumberFormatException e) {
// do nothing and fall through
}
}
}
}
}
}
return false;
}

public boolean isSuper() {

return false;
Expand Down Expand Up @@ -649,6 +689,8 @@ public final boolean isTypeUseDeprecated(TypeBinding type, Scope scope) {
// inside same unit - no report
if (scope.isDefinedInSameUnit(refType)) return false;

if (sinceValueUnreached(refType, scope)) return false;

// if context is deprecated, may avoid reporting
if (!scope.compilerOptions().reportDeprecationInsideDeprecatedCode && scope.isInsideDeprecatedCode()) return false;
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,15 @@ public void testBug533063_2() throws Exception {
"----------\n";
runner.runWarningTest();
}
public void testBug534304() throws Exception {
public void testBug534304_1() throws Exception {
Map<String, String> options= getCompilerOptions();
String compliance= options.get(CompilerOptions.OPTION_Compliance);
if (compliance.equals(CompilerOptions.VERSION_9) ||
compliance.equals(CompilerOptions.VERSION_10) ||
compliance.equals(CompilerOptions.VERSION_11) ||
compliance.equals(CompilerOptions.VERSION_12)) {
return;
}
runNegativeTest(
new String[] {
"p1/C1.java",
Expand Down Expand Up @@ -955,6 +963,48 @@ public void testBug534304() throws Exception {
"CMissing cannot be resolved to a type\n" +
"----------\n");
}
public void testBug534304_2() throws Exception {
Map<String, String> options= getCompilerOptions();
String compliance= options.get(CompilerOptions.OPTION_Compliance);
if (compliance.equals(CompilerOptions.VERSION_9) ||
compliance.equals(CompilerOptions.VERSION_10) ||
compliance.equals(CompilerOptions.VERSION_11) ||
compliance.equals(CompilerOptions.VERSION_12)) {
runNegativeTest(
new String[] {
"p1/C1.java",
"package p1;\n" +
"\n" +
"import pdep.Dep1;\n" +
"\n" +
"public class C1 {\n" +
" Dep1 f;\n" +
"}\n",
"pdep/Dep1.java",
"package pdep;\n" +
"\n" +
"import pmissing.CMissing;\n" +
"\n" +
"@Deprecated(since=\"13\")\n" +
"@CMissing\n" +
"public class Dep1 {\n" +
"\n" +
"}\n"
},
"----------\n" +
"----------\n" +
"1. ERROR in pdep\\Dep1.java (at line 3)\n" +
" import pmissing.CMissing;\n" +
" ^^^^^^^^\n" +
"The import pmissing cannot be resolved\n" +
"----------\n" +
"2. ERROR in pdep\\Dep1.java (at line 6)\n" +
" @CMissing\n" +
" ^^^^^^^^\n" +
"CMissing cannot be resolved to a type\n" +
"----------\n");
}
}
public void testBug542795() throws Exception {
Runner runner = new Runner();
runner.customOptions = new HashMap<>();
Expand Down