From 553b25addde4fab136258f7718e274a98bfbe34a Mon Sep 17 00:00:00 2001 From: Rawi01 Date: Thu, 21 Oct 2021 10:38:37 +0200 Subject: [fixes #2985] Resolve var/val only once in eclipse --- buildScripts/tests.ant.xml | 16 +++- .../lombok/eclipse/agent/EclipsePatcher.java | 1 + .../lombok/eclipse/agent/PatchVal.java | 85 +++++++++++----------- test/core/src/lombok/DirectoryRunner.java | 3 +- test/core/src/lombok/RunTestsViaEcj.java | 26 +++++-- .../after-delombok/ValInvalidParameter.java | 29 ++++++++ test/transform/resource/after-ecj/ValErrors.java | 4 +- .../resource/after-ecj/ValInvalidParameter.java | 29 ++++++++ .../resource/before/ValInvalidParameter.java | 32 ++++++++ .../ValInvalidParameter.java.messages | 1 + .../resource/messages-ecj/ValErrors.java.messages | 6 +- .../messages-ecj/ValInBasicFor.java.messages | 6 +- .../messages-ecj/ValInvalidParameter.java.messages | 9 +++ .../ValInvalidParameter.java.messages | 9 +++ 14 files changed, 194 insertions(+), 62 deletions(-) create mode 100644 test/transform/resource/after-delombok/ValInvalidParameter.java create mode 100644 test/transform/resource/after-ecj/ValInvalidParameter.java create mode 100644 test/transform/resource/before/ValInvalidParameter.java create mode 100644 test/transform/resource/messages-delombok/ValInvalidParameter.java.messages create mode 100644 test/transform/resource/messages-ecj/ValInvalidParameter.java.messages create mode 100644 test/transform/resource/messages-idempotent/ValInvalidParameter.java.messages diff --git a/buildScripts/tests.ant.xml b/buildScripts/tests.ant.xml index 9d9e9541..838ac353 100644 --- a/buildScripts/tests.ant.xml +++ b/buildScripts/tests.ant.xml @@ -152,13 +152,18 @@ This buildfile is part of projectlombok.org. It takes care of compiling and runn + - Running TestEclipse on eclipse-@{version} on JVM${ant.java.version}. + + + + Running TestEclipse on eclipse-@{version} on JVM${ant.java.version} using. Compiler compliance level: @{compiler.compliance.level} + @@ -175,11 +180,16 @@ This buildfile is part of projectlombok.org. It takes care of compiling and runn - + + + + + + @@ -217,5 +227,5 @@ This buildfile is part of projectlombok.org. It takes care of compiling and runn - + diff --git a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java index 328860e3..33dad64e 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java +++ b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java @@ -764,6 +764,7 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable { sm.addScript(ScriptBuilder.replaceMethodCall() .target(new MethodTarget(LOCALDECLARATION_SIG, "resolve", "void", BLOCKSCOPE_SIG)) + .target(new MethodTarget(LOCALDECLARATION_SIG, "resolve", "void", BLOCKSCOPE_SIG, "boolean")) .methodToReplace(new Hook(EXPRESSION_SIG, "resolveType", TYPEBINDING_SIG, BLOCKSCOPE_SIG)) .requestExtra(StackRequest.THIS) .replacementMethod(new Hook("lombok.launch.PatchFixesHider$Val", "skipResolveInitializerIfAlreadyCalled2", TYPEBINDING_SIG, EXPRESSION_SIG, BLOCKSCOPE_SIG, LOCALDECLARATION_SIG)) diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchVal.java b/src/eclipseAgent/lombok/eclipse/agent/PatchVal.java index 3e96e75d..e758979d 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchVal.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchVal.java @@ -35,13 +35,11 @@ import org.eclipse.jdt.internal.compiler.ast.FunctionalExpression; import org.eclipse.jdt.internal.compiler.ast.ImportReference; import org.eclipse.jdt.internal.compiler.ast.LambdaExpression; import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration; -import org.eclipse.jdt.internal.compiler.ast.MessageSend; import org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference; import org.eclipse.jdt.internal.compiler.ast.SingleTypeReference; import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; import org.eclipse.jdt.internal.compiler.ast.TypeReference; import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; -import org.eclipse.jdt.internal.compiler.impl.Constant; import org.eclipse.jdt.internal.compiler.impl.ReferenceContext; import org.eclipse.jdt.internal.compiler.lookup.ArrayBinding; import org.eclipse.jdt.internal.compiler.lookup.Binding; @@ -250,7 +248,6 @@ public class PatchVal { } TypeBinding resolved = null; - Constant oldConstant = init.constant; try { resolved = decomponent ? getForEachComponentType(init, scope) : resolveForExpression(init, scope); } catch (NullPointerException e) { @@ -260,6 +257,46 @@ public class PatchVal { // just go with 'Object' and let the IDE print the appropriate errors. resolved = null; } + + if (resolved == null) { + if (init instanceof ConditionalExpression) { + ConditionalExpression cexp = (ConditionalExpression) init; + Expression ifTrue = cexp.valueIfTrue; + Expression ifFalse = cexp.valueIfFalse; + TypeBinding ifTrueResolvedType = ifTrue.resolvedType; + CompilationResult compilationResult = scope.referenceCompilationUnit().compilationResult; + CategorizedProblem[] problems = compilationResult.problems; + CategorizedProblem lastProblem = problems[compilationResult.problemCount - 1]; + if (ifTrueResolvedType != null && ifFalse.resolvedType == null && lastProblem.getCategoryID() == CAT_TYPE) { + int problemCount = compilationResult.problemCount; + for (int i = 0; i < problemCount; ++i) { + if (problems[i] == lastProblem) { + problems[i] = null; + if (i + 1 < problemCount) { + System.arraycopy(problems, i + 1, problems, i, problemCount - i + 1); + } + break; + } + } + compilationResult.removeProblem(lastProblem); + if (!compilationResult.hasErrors()) { + clearIgnoreFurtherInvestigationField(scope.referenceContext()); + setValue(getField(CompilationResult.class, "hasMandatoryErrors"), compilationResult, false); + } + + if (ifFalse instanceof FunctionalExpression) { + FunctionalExpression functionalExpression = (FunctionalExpression) ifFalse; + functionalExpression.setExpectedType(ifTrueResolvedType); + } + if (ifFalse.resolvedType == null) { + resolveForExpression(ifFalse, scope); + } + + resolved = ifTrueResolvedType; + } + } + } + if (resolved != null) { try { replacement = makeType(resolved, local.type, false); @@ -267,10 +304,6 @@ public class PatchVal { } catch (Exception e) { // Some type thing failed. } - } else { - if (init instanceof MessageSend && ((MessageSend) init).actualReceiverType == null) { - init.constant = oldConstant; - } } } @@ -370,43 +403,7 @@ public class PatchVal { // Known cause of issues; for example: val e = mth("X"), where mth takes 2 arguments. return null; } catch (AbortCompilation e) { - if (collection instanceof ConditionalExpression) { - ConditionalExpression cexp = (ConditionalExpression) collection; - Expression ifTrue = cexp.valueIfTrue; - Expression ifFalse = cexp.valueIfFalse; - TypeBinding ifTrueResolvedType = ifTrue.resolvedType; - CategorizedProblem problem = e.problem; - if (ifTrueResolvedType != null && ifFalse.resolvedType == null && problem.getCategoryID() == CAT_TYPE) { - CompilationResult compilationResult = e.compilationResult; - CategorizedProblem[] problems = compilationResult.problems; - int problemCount = compilationResult.problemCount; - for (int i = 0; i < problemCount; ++i) { - if (problems[i] == problem) { - problems[i] = null; - if (i + 1 < problemCount) { - System.arraycopy(problems, i + 1, problems, i, problemCount - i + 1); - } - break; - } - } - compilationResult.removeProblem(problem); - if (!compilationResult.hasErrors()) { - clearIgnoreFurtherInvestigationField(scope.referenceContext()); - setValue(getField(CompilationResult.class, "hasMandatoryErrors"), compilationResult, false); - } - - if (ifFalse instanceof FunctionalExpression) { - FunctionalExpression functionalExpression = (FunctionalExpression) ifFalse; - functionalExpression.setExpectedType(ifTrueResolvedType); - } - if (ifFalse.resolvedType == null) { - ifFalse.resolve(scope); - } - - return ifTrueResolvedType; - } - } - throw e; + return null; } } diff --git a/test/core/src/lombok/DirectoryRunner.java b/test/core/src/lombok/DirectoryRunner.java index b041c42e..53347e24 100644 --- a/test/core/src/lombok/DirectoryRunner.java +++ b/test/core/src/lombok/DirectoryRunner.java @@ -55,7 +55,8 @@ public class DirectoryRunner extends Runner { }, ECJ { @Override public int getVersion() { - return Eclipse.getEcjCompilerVersion(); + String javaVersionString = System.getProperty("compiler.compliance.level"); + return javaVersionString != null ? Integer.parseInt(javaVersionString) : Eclipse.getEcjCompilerVersion(); } }; diff --git a/test/core/src/lombok/RunTestsViaEcj.java b/test/core/src/lombok/RunTestsViaEcj.java index afba8c7f..6137de49 100644 --- a/test/core/src/lombok/RunTestsViaEcj.java +++ b/test/core/src/lombok/RunTestsViaEcj.java @@ -64,9 +64,22 @@ import org.osgi.framework.BundleContext; public class RunTestsViaEcj extends AbstractRunTests { protected CompilerOptions ecjCompilerOptions() { CompilerOptions options = new CompilerOptions(); - options.complianceLevel = Eclipse.getLatestEcjCompilerVersionConstant(); - options.sourceLevel = Eclipse.getLatestEcjCompilerVersionConstant(); - options.targetJDK = Eclipse.getLatestEcjCompilerVersionConstant(); + Map warnings = new HashMap(); + + String javaVersionString = System.getProperty("compiler.compliance.level"); + long ecjCompilerVersionConstant = Eclipse.getLatestEcjCompilerVersionConstant(); + long ecjCompilerVersion = Eclipse.getEcjCompilerVersion(); + if (javaVersionString != null) { + long javaVersion = Long.parseLong(javaVersionString); + ecjCompilerVersionConstant = (javaVersion + 44) << 16; + ecjCompilerVersion = javaVersion; + } else { + // Preview features are only allowed if the maximum compiler version is equal to the source version + warnings.put("org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures", "enabled"); + } + options.complianceLevel = ecjCompilerVersionConstant; + options.sourceLevel = ecjCompilerVersionConstant; + options.targetJDK = ecjCompilerVersionConstant; options.docCommentSupport = false; options.parseLiteralExpressionsAsConstants = true; options.inlineJsrBytecode = true; @@ -78,17 +91,14 @@ public class RunTestsViaEcj extends AbstractRunTests { options.reportUnusedParameterWhenOverridingConcrete = false; options.reportDeadCodeInTrivialIfStatement = false; options.generateClassFiles = false; - Map warnings = new HashMap(); warnings.put(CompilerOptions.OPTION_ReportUnusedLocal, "ignore"); warnings.put(CompilerOptions.OPTION_ReportUnusedLabel, "ignore"); warnings.put(CompilerOptions.OPTION_ReportUnusedImport, "ignore"); warnings.put(CompilerOptions.OPTION_ReportUnusedPrivateMember, "ignore"); warnings.put(CompilerOptions.OPTION_ReportIndirectStaticAccess, "warning"); warnings.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, "warning"); - warnings.put("org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures", "enabled"); warnings.put("org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures", "ignore"); - int ecjVersion = Eclipse.getEcjCompilerVersion(); - warnings.put(CompilerOptions.OPTION_Source, (ecjVersion < 9 ? "1." : "") + ecjVersion); + warnings.put(CompilerOptions.OPTION_Source, (ecjCompilerVersion < 9 ? "1." : "") + ecjCompilerVersion); options.set(warnings); return options; } @@ -96,7 +106,7 @@ public class RunTestsViaEcj extends AbstractRunTests { protected IErrorHandlingPolicy ecjErrorHandlingPolicy() { return new IErrorHandlingPolicy() { public boolean stopOnFirstError() { - return true; + return false; } public boolean proceedOnErrors() { diff --git a/test/transform/resource/after-delombok/ValInvalidParameter.java b/test/transform/resource/after-delombok/ValInvalidParameter.java new file mode 100644 index 00000000..f3d4229c --- /dev/null +++ b/test/transform/resource/after-delombok/ValInvalidParameter.java @@ -0,0 +1,29 @@ +public class ValInvalidParameter { + public void val() { + final java.lang.Object a = a(new NonExistingClass()); + final java.lang.Object b = a(a(new NonExistingClass())); + final java.lang.Object c = nonExisitingMethod(b(1)); + final java.lang.Object d = nonExistingObject.nonExistingMethod(); + final java.lang.Object e = b(1).nonExistingMethod(); + final java.lang.Object f = 1 > 2 ? a(new NonExistingClass()) : a(new NonExistingClass()); + final java.lang.Object g = b2(1); + final java.lang.Integer h = b2(a("a"), a(null)); + final int i = a(a(null)); + } + + public int a(String param) { + return 0; + } + + public int a(Integer param) { + return 0; + } + + public Integer b(int i) { + return i; + } + + public Integer b2(int i, int j) { + return i; + } +} \ No newline at end of file diff --git a/test/transform/resource/after-ecj/ValErrors.java b/test/transform/resource/after-ecj/ValErrors.java index 1bd61f87..19c2facd 100644 --- a/test/transform/resource/after-ecj/ValErrors.java +++ b/test/transform/resource/after-ecj/ValErrors.java @@ -4,9 +4,9 @@ public class ValErrors { super(); } public void unresolvableExpression() { - val c = d; + final @val java.lang.Object c = d; } public void arrayInitializer() { - val e = {"foo", "bar"}; + final @val java.lang.Object e = {"foo", "bar"}; } } \ No newline at end of file diff --git a/test/transform/resource/after-ecj/ValInvalidParameter.java b/test/transform/resource/after-ecj/ValInvalidParameter.java new file mode 100644 index 00000000..14549aa7 --- /dev/null +++ b/test/transform/resource/after-ecj/ValInvalidParameter.java @@ -0,0 +1,29 @@ +import lombok.val; +public class ValInvalidParameter { + public ValInvalidParameter() { + super(); + } + public void val() { + final @val java.lang.Object a = a(new NonExistingClass()); + final @val java.lang.Object b = a(a(new NonExistingClass())); + final @val java.lang.Object c = nonExisitingMethod(b(1)); + final @val java.lang.Object d = nonExistingObject.nonExistingMethod(); + final @val java.lang.Object e = b(1).nonExistingMethod(); + final @val java.lang.Object f = ((1 > 2) ? a(new NonExistingClass()) : a(new NonExistingClass())); + final @val java.lang.Object g = b2(1); + final @val java.lang.Object h = b2(a("a"), a(null)); + final @val java.lang.Object i = a(a(null)); + } + public int a(String param) { + return 0; + } + public int a(Integer param) { + return 0; + } + public Integer b(int i) { + return i; + } + public Integer b2(int i, int j) { + return i; + } +} \ No newline at end of file diff --git a/test/transform/resource/before/ValInvalidParameter.java b/test/transform/resource/before/ValInvalidParameter.java new file mode 100644 index 00000000..f4961c4e --- /dev/null +++ b/test/transform/resource/before/ValInvalidParameter.java @@ -0,0 +1,32 @@ +//version :9 +import lombok.val; + +public class ValInvalidParameter { + public void val() { + val a = a(new NonExistingClass()); + val b = a(a(new NonExistingClass())); + val c = nonExisitingMethod(b(1)); + val d = nonExistingObject.nonExistingMethod(); + val e = b(1).nonExistingMethod(); + val f = 1 > 2 ? a(new NonExistingClass()) : a(new NonExistingClass()); + val g = b2(1); + val h = b2(a("a"), a(null)); + val i = a(a(null)); + } + + public int a(String param) { + return 0; + } + + public int a(Integer param) { + return 0; + } + + public Integer b(int i) { + return i; + } + + public Integer b2(int i, int j) { + return i; + } +} \ No newline at end of file diff --git a/test/transform/resource/messages-delombok/ValInvalidParameter.java.messages b/test/transform/resource/messages-delombok/ValInvalidParameter.java.messages new file mode 100644 index 00000000..da0df315 --- /dev/null +++ b/test/transform/resource/messages-delombok/ValInvalidParameter.java.messages @@ -0,0 +1 @@ +12 Cannot use 'val' here because initializer expression does not have a representable type: Type cannot be resolved \ No newline at end of file diff --git a/test/transform/resource/messages-ecj/ValErrors.java.messages b/test/transform/resource/messages-ecj/ValErrors.java.messages index c4c76901..9fcec493 100644 --- a/test/transform/resource/messages-ecj/ValErrors.java.messages +++ b/test/transform/resource/messages-ecj/ValErrors.java.messages @@ -1,2 +1,4 @@ -6 d cannot be resolved to a variable -10 'val' is not compatible with array initializer expressions. Use the full form (new int[] { ... } instead of just { ... }) +7 d cannot be resolved to a variable +7 d cannot be resolved or is not a field +11 'val' is not compatible with array initializer expressions. Use the full form (new int[] { ... } instead of just { ... }) +11 Type mismatch: cannot convert from String[] to Object \ No newline at end of file diff --git a/test/transform/resource/messages-ecj/ValInBasicFor.java.messages b/test/transform/resource/messages-ecj/ValInBasicFor.java.messages index b32eabe4..00bc643f 100644 --- a/test/transform/resource/messages-ecj/ValInBasicFor.java.messages +++ b/test/transform/resource/messages-ecj/ValInBasicFor.java.messages @@ -1,2 +1,4 @@ -7 'val' is not allowed in old-style for loops -7 Type mismatch: cannot convert from int to val +8 'val' is not allowed in old-style for loops +8 Type mismatch: cannot convert from int to val +8 Type mismatch: cannot convert from String to val +8 Type mismatch: cannot convert from double to val \ No newline at end of file diff --git a/test/transform/resource/messages-ecj/ValInvalidParameter.java.messages b/test/transform/resource/messages-ecj/ValInvalidParameter.java.messages new file mode 100644 index 00000000..cbf2d7c4 --- /dev/null +++ b/test/transform/resource/messages-ecj/ValInvalidParameter.java.messages @@ -0,0 +1,9 @@ +5 NonExistingClass cannot be resolved to a type +6 NonExistingClass cannot be resolved to a type +7 The method nonExisitingMethod(Integer) is undefined for the type ValInvalidParameter +8 nonExistingObject cannot be resolved +9 The method nonExistingMethod() is undefined for the type Integer +10 NonExistingClass cannot be resolved to a type +11 The method b2(int, int) in the type ValInvalidParameter is not applicable for the arguments (int) +12 The method a(String) is ambiguous for the type ValInvalidParameter +13 The method a(String) is ambiguous for the type ValInvalidParameter \ No newline at end of file diff --git a/test/transform/resource/messages-idempotent/ValInvalidParameter.java.messages b/test/transform/resource/messages-idempotent/ValInvalidParameter.java.messages new file mode 100644 index 00000000..539d29cd --- /dev/null +++ b/test/transform/resource/messages-idempotent/ValInvalidParameter.java.messages @@ -0,0 +1,9 @@ +3 cannot find symbol +4 cannot find symbol +5 cannot find symbol +6 cannot find symbol +7 cannot find symbol +8 cannot find symbol +9 method b2 in class ValInvalidParameter cannot be applied to given types; +10 reference to a is ambiguous +11 reference to a is ambiguous \ No newline at end of file -- cgit