From 63573239996ffca8564a237d12ebe07b038e9507 Mon Sep 17 00:00:00 2001 From: Stephan Herrmann Date: Sat, 3 Feb 2024 20:23:52 +0100 Subject: [PATCH] Cannot invoke "org.eclipse.jdt.internal.compiler.lookup.TypeBinding.hasNullTypeAnnotations()" because "providedType" is null fixes #1954 --- .../internal/compiler/ast/StringTemplate.java | 8 ++++ .../jdt/internal/compiler/lookup/Scope.java | 7 ++++ .../regression/NullAnnotationTests21.java | 38 +++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/StringTemplate.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/StringTemplate.java index eaa2709bdae..53ad0728c37 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/StringTemplate.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/StringTemplate.java @@ -17,6 +17,7 @@ import org.eclipse.jdt.internal.compiler.codegen.ConstantPool; import org.eclipse.jdt.internal.compiler.flow.FlowContext; import org.eclipse.jdt.internal.compiler.flow.FlowInfo; +import org.eclipse.jdt.internal.compiler.impl.Constant; import org.eclipse.jdt.internal.compiler.lookup.BlockScope; import org.eclipse.jdt.internal.compiler.lookup.TypeBinding; import org.eclipse.jdt.internal.compiler.lookup.TypeIds; @@ -52,6 +53,13 @@ public void resolve(BlockScope scope) { exp.resolveType(scope); } } + + @Override + public TypeBinding resolveType(BlockScope scope) { + this.constant = Constant.NotAConstant; + return this.resolvedType = scope.getJavaLangStringTemplate(); + } + private void generateNewTemplateBootstrap(CodeStream codeStream) { int index = codeStream.classFile.recordBootstrapMethod(this); // Kludge, see if this can be moved to CodeStream diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/Scope.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/Scope.java index da70fb7ff3d..ed8e6a36616 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/Scope.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/Scope.java @@ -3051,6 +3051,13 @@ public final ReferenceBinding getJavaLangStringBuilder() { unitScope.recordQualifiedReference(TypeConstants.JAVA_LANG_STRINGBUILDER); return unitScope.environment.getResolvedJavaBaseType(TypeConstants.JAVA_LANG_STRINGBUILDER, this); } + + public TypeBinding getJavaLangStringTemplate() { + CompilationUnitScope unitScope = compilationUnitScope(); + unitScope.recordQualifiedReference(TypeConstants.JAVA_LANG_STRINGTEMPLATE); + return unitScope.environment.getResolvedJavaBaseType(TypeConstants.JAVA_LANG_STRINGTEMPLATE, this); + } + public final ReferenceBinding getJavaLangStringTemplateProcessor() { CompilationUnitScope unitScope = compilationUnitScope(); unitScope.recordQualifiedReference(TypeConstants.JAVA_LANG_STRINGTEMPLATE_PROCESSOR); diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullAnnotationTests21.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullAnnotationTests21.java index 23ebd9beccc..8fb80752496 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullAnnotationTests21.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullAnnotationTests21.java @@ -997,4 +997,42 @@ public class X { runner.classLibraries = this.LIBS; runner.runConformTest(); } + + public void testGH1964() { + Runner runner = new Runner(); + runner.customOptions = getCompilerOptions(); + runner.customOptions.put(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.ENABLED); + runner.customOptions.put(CompilerOptions.OPTION_ReportPreviewFeatures, CompilerOptions.IGNORE); + runner.vmArguments = new String[] {"--enable-preview"}; + runner.testFiles = new String[] { + "JDK21TestingMain.java", + """ + import static java.util.FormatProcessor.FMT; + import static java.lang.StringTemplate.RAW; + + public final class JDK21TestingMain + { + public static void main(final String[] args) + { + final int fourtyTwo = 42; + final String str = FMT."\\{fourtyTwo}"; + + final int x=1; + final int y=2; + final StringTemplate st = RAW."\\{x} + \\{y} = \\{x + y}"; + + final var x1 = STR."Hello World"; + final var x2 = FMT."Hello World"; + final var x3 = RAW."Hello World"; + + System.out.println(STR."Hello World"); + + System.out.println(); + } + } + """ + }; + runner.classLibraries = this.LIBS; + runner.runConformTest(); + } }