diff options
Diffstat (limited to 'src/eclipseAgent')
-rw-r--r-- | src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java | 21 | ||||
-rw-r--r-- | src/eclipseAgent/lombok/eclipse/agent/PatchVal.java | 126 | ||||
-rwxr-xr-x | src/eclipseAgent/lombok/launch/PatchFixesHider.java | 12 |
3 files changed, 147 insertions, 12 deletions
diff --git a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java index 0e74dfaf..24c1216e 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java +++ b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java @@ -122,6 +122,7 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable { patchEcjTransformers(sm, ecjOnly); patchExtensionMethod(sm, ecjOnly); patchRenameField(sm); + patchNullCheck(sm); if (reloadExistingClasses) sm.reloadClasses(instrumentation); } @@ -774,4 +775,24 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable { .build()); } } + + private static void patchNullCheck(ScriptManager sm) { + /* Avoid warnings caused by the null check generated for lombok.NonNull if NonNullByDefault is used. */ + + /* Avoid "Redundant null check: comparing '@NonNull String' against null" */ + sm.addScript(ScriptBuilder.exitEarly() + .target(new MethodTarget("org.eclipse.jdt.internal.compiler.problem.ProblemReporter", "expressionNonNullComparison", "boolean", "org.eclipse.jdt.internal.compiler.ast.Expression", "boolean")) + .decisionMethod(new Hook("lombok.launch.PatchFixesHider$PatchFixes", "isGenerated", "boolean", "org.eclipse.jdt.internal.compiler.ast.ASTNode")) + .valueMethod(new Hook("lombok.launch.PatchFixesHider$PatchFixes", "returnTrue", "boolean", "java.lang.Object")) + .request(StackRequest.PARAM1) + .transplant().build()); + + /* Avoid "Dead code" */ + sm.addScript(ScriptBuilder.exitEarly() + .target(new MethodTarget("org.eclipse.jdt.internal.compiler.problem.ProblemReporter", "fakeReachable", "void", "org.eclipse.jdt.internal.compiler.ast.ASTNode")) + .decisionMethod(new Hook("lombok.launch.PatchFixesHider$PatchFixes", "isGenerated", "boolean", "org.eclipse.jdt.internal.compiler.ast.ASTNode")) + .request(StackRequest.PARAM1) + .transplant().build()); + } + } diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchVal.java b/src/eclipseAgent/lombok/eclipse/agent/PatchVal.java index b32c99cd..0c66bb31 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchVal.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchVal.java @@ -21,16 +21,27 @@ */ package lombok.eclipse.agent; +import lombok.permit.Permit; +import org.eclipse.jdt.core.compiler.CategorizedProblem; +import org.eclipse.jdt.internal.compiler.CompilationResult; +import org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies; import org.eclipse.jdt.internal.compiler.ast.ASTNode; +import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration; import org.eclipse.jdt.internal.compiler.ast.Annotation; +import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; +import org.eclipse.jdt.internal.compiler.ast.ConditionalExpression; import org.eclipse.jdt.internal.compiler.ast.Expression; import org.eclipse.jdt.internal.compiler.ast.ForeachStatement; +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.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.ReferenceContext; import org.eclipse.jdt.internal.compiler.lookup.ArrayBinding; import org.eclipse.jdt.internal.compiler.lookup.Binding; import org.eclipse.jdt.internal.compiler.lookup.BlockScope; @@ -42,13 +53,15 @@ import org.eclipse.jdt.internal.compiler.lookup.Scope; import org.eclipse.jdt.internal.compiler.lookup.TypeBinding; import org.eclipse.jdt.internal.compiler.lookup.TypeConstants; import org.eclipse.jdt.internal.compiler.lookup.TypeIds; - -import lombok.permit.Permit; +import org.eclipse.jdt.internal.compiler.problem.AbortCompilation; +import org.eclipse.jdt.internal.compiler.problem.ProblemReporter; import java.lang.reflect.Field; +import static lombok.Lombok.sneakyThrow; import static lombok.eclipse.Eclipse.poss; import static lombok.eclipse.handlers.EclipseHandlerUtil.makeType; +import static org.eclipse.jdt.core.compiler.CategorizedProblem.CAT_TYPE; public class PatchVal { @@ -263,15 +276,11 @@ public class PatchVal { resolved = null; } if (resolved != null) { - if (resolved.getClass().getSimpleName().startsWith("IntersectionTypeBinding")) { - // We intentionally deconstruct these into simply 'Object', because picking an arbitrary type amongst the intersection feels worse. - } else { - try { - replacement = makeType(resolved, local.type, false); - if (!decomponent) init.resolvedType = replacement.resolveType(scope); - } catch (Exception e) { - // Some type thing failed. - } + try { + replacement = makeType(resolved, local.type, false); + if (!decomponent) init.resolvedType = replacement.resolveType(scope); + } catch (Exception e) { + // Some type thing failed. } } } @@ -357,11 +366,106 @@ public class PatchVal { } private static TypeBinding resolveForExpression(Expression collection, BlockScope scope) { + CompilationUnitDeclaration referenceContext = scope.compilationUnitScope().referenceContext; + ProblemReporter oldProblemReporter = referenceContext.problemReporter; + referenceContext.problemReporter = new ProblemReporter(DefaultErrorHandlingPolicies.exitOnFirstError(), + oldProblemReporter.options, oldProblemReporter.problemFactory); try { return collection.resolveType(scope); } catch (ArrayIndexOutOfBoundsException e) { // 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; + } finally { + referenceContext.problemReporter = oldProblemReporter; + } + } + + private static void clearIgnoreFurtherInvestigationField(ReferenceContext currentContext) { + if (currentContext instanceof AbstractMethodDeclaration) { + AbstractMethodDeclaration methodDeclaration = (AbstractMethodDeclaration) currentContext; + methodDeclaration.ignoreFurtherInvestigation = false; + } else if (currentContext instanceof LambdaExpression) { + LambdaExpression lambdaExpression = (LambdaExpression) currentContext; + setValue(getField(LambdaExpression.class, "ignoreFurtherInvestigation"), lambdaExpression, false); + + Scope parent = lambdaExpression.enclosingScope.parent; + while (parent != null) { + switch(parent.kind) { + case Scope.CLASS_SCOPE: + case Scope.METHOD_SCOPE: + ReferenceContext parentAST = parent.referenceContext(); + if (parentAST != lambdaExpression) { + clearIgnoreFurtherInvestigationField(parentAST); + return; + } + default: + parent = parent.parent; + break; + } + } + + } else if (currentContext instanceof TypeDeclaration) { + TypeDeclaration typeDeclaration = (TypeDeclaration) currentContext; + typeDeclaration.ignoreFurtherInvestigation = false; + } else if (currentContext instanceof CompilationUnitDeclaration) { + CompilationUnitDeclaration typeDeclaration = (CompilationUnitDeclaration) currentContext; + typeDeclaration.ignoreFurtherInvestigation = false; + } else { + throw new UnsupportedOperationException("clearIgnoreFurtherInvestigationField for " + currentContext.getClass()); + } + } + + private static void setValue(Field field, Object object, Object value) { + try { + field.set(object, value); + } catch (IllegalAccessException e) { + throw sneakyThrow(e); + } + } + + private static Field getField(Class clazz, String name) { + try { + return Permit.getField(clazz, name); + } catch (NoSuchFieldException e) { + throw sneakyThrow(e); } } } diff --git a/src/eclipseAgent/lombok/launch/PatchFixesHider.java b/src/eclipseAgent/lombok/launch/PatchFixesHider.java index 3741aba8..563beab1 100755 --- a/src/eclipseAgent/lombok/launch/PatchFixesHider.java +++ b/src/eclipseAgent/lombok/launch/PatchFixesHider.java @@ -315,7 +315,17 @@ final class PatchFixesHider { } return result; } - + + public static boolean isGenerated(org.eclipse.jdt.internal.compiler.ast.ASTNode node) { + boolean result = false; + try { + result = node.getClass().getField("$generatedBy").get(node) != null; + } catch (Exception e) { + // better to assume it isn't generated + } + return result; + } + public static boolean isListRewriteOnGeneratedNode(org.eclipse.jdt.core.dom.rewrite.ListRewrite rewrite) { return isGenerated(rewrite.getParent()); } |