From da0a6d8d5088b92dbc3602cdccff8d667b6d5e8b Mon Sep 17 00:00:00 2001 From: Srikanth Sankaran <131454720+srikanth-sankaran@users.noreply.github.com> Date: Tue, 23 May 2023 18:03:00 +0530 Subject: [PATCH] Revert https://github.com/eclipse-jdt/eclipse.jdt.core/pull/630 (#1079) * Revert https://github.com/eclipse-jdt/eclipse.jdt.core/pull/630 * Point-fix for NullTypeAnnotationTest.testBug522142_bogusError() in absence of #630. Disable testGH854 to enable running of more test suites --------- Co-authored-by: Stephan Herrmann --- .../internal/compiler/lookup/ClassScope.java | 3 - .../compiler/lookup/ReferenceBinding.java | 22 ---- .../compiler/lookup/TypeVariableBinding.java | 11 ++ .../regression/NullAnnotationTests18.java | 4 +- .../regression/NullTypeAnnotationTest.java | 115 ++++++++++++++++++ 5 files changed, 128 insertions(+), 27 deletions(-) diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/ClassScope.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/ClassScope.java index 95b92835abb..34c9a2476f9 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/ClassScope.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/ClassScope.java @@ -1667,9 +1667,6 @@ private ReferenceBinding findSupertype(TypeReference typeReference) { typeReference.aboutToResolve(this); // allows us to trap completion & selection nodes unitScope.recordQualifiedReference(typeReference.getTypeName()); this.superTypeReference = typeReference; - if (this.compilerOptions().isAnnotationBasedNullAnalysisEnabled) { - this.hasDefaultNullnessFor(0 /*location*/, typeReference.sourceStart); - } ReferenceBinding superType = (ReferenceBinding) typeReference.resolveSuperType(this); return superType; } catch (AbortCompilation e) { diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/ReferenceBinding.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/ReferenceBinding.java index 5fe198bd8b4..0e8c8d8ddea 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/ReferenceBinding.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/ReferenceBinding.java @@ -60,7 +60,6 @@ import org.eclipse.jdt.internal.compiler.ast.LambdaExpression; import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration; import org.eclipse.jdt.internal.compiler.ast.NullAnnotationMatching; -import org.eclipse.jdt.internal.compiler.ast.TypeReference; import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; import org.eclipse.jdt.internal.compiler.impl.JavaFeature; @@ -1356,27 +1355,6 @@ public boolean implementsInterface(ReferenceBinding anInterface, boolean searchH } } } - // see https://github.com/eclipse-jdt/eclipse.jdt.core/issues/629 - if (nextPosition == 0 && this instanceof SourceTypeBinding) { - SourceTypeBinding sourceType = (SourceTypeBinding) this; - if (sourceType.scope != null && sourceType.scope.referenceContext != null && sourceType.scope.compilerOptions().isAnnotationBasedNullAnalysisEnabled) { - TypeReference[] references = sourceType.scope.referenceContext.superInterfaces; - if (references == null || references.length == 0) { - return false; - } - for (TypeReference reference: references) { - if (!(reference.resolvedType instanceof ReferenceBinding)) { - reference.resolveType(sourceType.scope); - } - if (reference.resolvedType instanceof ReferenceBinding) { - ReferenceBinding binding = (ReferenceBinding) reference.resolvedType; - if (binding.isEquivalentTo(anInterface)) { - return true; - } - } - } - } - } return false; } diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/TypeVariableBinding.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/TypeVariableBinding.java index 9daaf5b8bda..fa5e7441118 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/TypeVariableBinding.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/TypeVariableBinding.java @@ -247,6 +247,17 @@ private TypeConstants.BoundCheckStatus internalBoundCheck(Substitution substitut break; } return BoundCheckStatus.OK; + } else if (checkNullAnnotations && argumentType.kind() == Binding.TYPE_PARAMETER) { + // refresh argumentType in case a nullness default(TYPE_PARAMETER) was applied late: + TypeVariableBinding tvb = (TypeVariableBinding) argumentType; + if (tvb.declaringElement instanceof SourceTypeBinding) { + TypeVariableBinding[] typeVariables = ((SourceTypeBinding) tvb.declaringElement).typeVariables; + if (typeVariables != null && typeVariables.length > tvb.rank) { + TypeVariableBinding refreshed = typeVariables[tvb.rank]; + if (refreshed.id == argumentType.id) + argumentType = refreshed; + } + } } boolean unchecked = false; if (this.superclass.id != TypeIds.T_JavaLangObject) { diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullAnnotationTests18.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullAnnotationTests18.java index f1bc533b306..20de952d62b 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullAnnotationTests18.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullAnnotationTests18.java @@ -631,7 +631,7 @@ public void testNullableVar() { "----------\n"; runner.runNegativeTest(); } - public void testGH629_01() { + public void _testGH629_01() { Map options = getCompilerOptions(); options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_18); options.put(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, "test.NonNull"); @@ -660,7 +660,7 @@ public void testGH629_01() { options, ""); } - public void testGH629_02() { + public void _testGH629_02() { Map options = getCompilerOptions(); options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_18); options.put(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, "test.NonNull"); diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullTypeAnnotationTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullTypeAnnotationTest.java index 37e644b7c42..4e2b9e59d12 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullTypeAnnotationTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullTypeAnnotationTest.java @@ -18748,4 +18748,119 @@ public void testRedundantNonNull_field() { runner.classLibraries = this.LIBS; runner.runWarningTest(); } +public void testGH1007_srikanth() { + Runner runner = new Runner(); + runner.testFiles = + new String[] { + "SubClass.java", + "// ECJ error in next line: Type mismatch: cannot convert from Class to Class[]\n" + + "@AnnotationWithArrayInitializer(annotationArgument = SubClass.class)\n" + + "class AnnotatedClass2 extends AnnotatedSuperClass {}\n" + + "\n" + + "//ECJ error in next line: Type mismatch: cannot convert from Class to Class\n" + + "@AnnotationWithArrayInitializer(annotationArgument = {SubClass.class})\n" + + "class AnnotatedClass extends AnnotatedSuperClass {}\n" + + "\n" + + "\n" + + "class AnnotatedSuperClass {}\n" + + "\n" + + "@interface AnnotationWithArrayInitializer {\n" + + " Class[] annotationArgument();\n" + + "}\n" + + "\n" + + "class SubClass extends SuperClass {}\n" + + "abstract class SuperClass {}" + }; + runner.runConformTest(); +} +public void _testGH854() { + Runner runner = new Runner(); + runner.testFiles = + new String[] { + "Annot.java", + "public @interface Annot {\n" + + " Class>[] inits(); \n" + + "}\n", + "Configuration.java", + "public interface Configuration {\n" + + "}\n", + "Init.java", + "public interface Init {\n" + + "}\n", + "App.java", + "interface I {}\n" + + "class IImpl implements I, Init {}\n" + + "@Annot(inits = {App.MyInit.class})\n" + + "public class App {\n" + + " static class MyInit extends IImpl {}\n" + + "}\n" + }; + runner.runConformTest(); +} +public void testVSCodeIssue3076() { + Runner runner = new Runner(); + runner.testFiles = + new String[] { + "demo/cache/AbstractCache.java", + "package demo.cache;\n" + + "\n" + + "public abstract class AbstractCache {\n" + + " public enum Expiry {\n" + + " ONE, TWO, THREE\n" + + " }\n" + + "\n" + + " protected abstract void cacheThis(int param1, Expiry param2);\n" + + "}\n", + "demo/Annot.java", + "package demo;\n" + + "public @interface Annot {\n" + + " String defaultProperty();\n" + + "}\n", + "demo/cache/MyCache.java", + "package demo.cache;\n" + + "\n" + + "import demo.Annot;\n" + + "\n" + + "/**\n" + + " * This annotation is what causes the confusion around the nested Expiry type.\n" + + " *\n" + + " * If you comment out this annotation the language server has no problem\n" + + " * figuring it out.\n" + + " *\n" + + " * It can be *any* annotation.\n" + + " * So it would seem that referring to your own class outside of the\n" + + " * class definition is what triggers this particular bug.\n" + + " */\n" + + "@Annot(defaultProperty = MyCache.DEFAULT_PROPERTY_NAME)\n" + + "public class MyCache extends AbstractCache {\n" + + " public static final String DEFAULT_PROPERTY_NAME = \"WHATEVER\";\n" + + "\n" + + " @Override\n" + + " protected void cacheThis(int param1, Expiry param2) {\n" + + " throw new UnsupportedOperationException(\"Unimplemented method 'doSomethingElse'\");\n" + + " }\n" + + "}\n" + }; + runner.runConformTest(); +} +public void testGH969() { + Runner runner = new Runner(); + runner.testFiles = + new String[] { + "mypackage/Example.java", + "package mypackage;\n" + + "\n" + + "import java.io.Serializable;\n" + + "\n" + + "@Deprecated(since = Example.SINCE)\n" + + "public class Example implements Serializable {\n" + + " \n" + + " static final String SINCE = \"...\";\n" + + "\n" + + " private T target;\n" + + "\n" + + "}" + }; + runner.runConformTest(); +} }