mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-13 22:16:11 +00:00
#1976. naive fix of the eclipse compiler's error 'The target type of this expression must be a functional interface' in code like 'lombok.val foo = (System.currentTimeMillis() > 0) ? (Runnable)()-> {} : System.out::println;'
This commit is contained in:
1 parent
8276dee255
commit
06fb1034ea
4 files changed
+37
-6
No files matched your search
@@ -1,16 +1,16 @@
|
||||
/*
|
||||
* Copyright (C) 2010-2019 The Project Lombok Authors.
|
||||
*
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
@@ -21,8 +21,11 @@
|
||||
*/
|
||||
package lombok.eclipse.agent;
|
||||
|
||||
import org.eclipse.jdt.core.compiler.CategorizedProblem;
|
||||
import org.eclipse.jdt.internal.compiler.CompilationResult;
|
||||
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
|
||||
import org.eclipse.jdt.internal.compiler.ast.Annotation;
|
||||
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.ImportReference;
|
||||
@@ -44,11 +47,13 @@ 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 java.lang.reflect.Field;
|
||||
|
||||
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 {
|
||||
|
||||
@@ -358,6 +363,32 @@ public class PatchVal {
|
||||
} 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);
|
||||
|
||||
return ifTrueResolvedType;
|
||||
}
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ class ValLambda {
|
||||
}
|
||||
public void easyLubLambda() {
|
||||
final java.lang.Runnable foo = (System.currentTimeMillis() > 0) ? (Runnable) () -> {
|
||||
} : (Runnable) System.out::println;
|
||||
} : System.out::println;
|
||||
}
|
||||
// public void castLubLambda() {
|
||||
// Runnable foo = (Runnable) ((System.currentTimeMillis() > 0) ? () -> {} : System.out::println);
|
||||
|
||||
@@ -14,6 +14,6 @@ class ValLambda {
|
||||
}
|
||||
public void easyLubLambda() {
|
||||
final @lombok.val java.lang.Runnable foo = ((System.currentTimeMillis() > 0) ? (Runnable) () -> {
|
||||
} : (Runnable) System.out::println);
|
||||
} : System.out::println);
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ class ValLambda {
|
||||
}
|
||||
|
||||
public void easyLubLambda() {
|
||||
lombok.val foo = (System.currentTimeMillis() > 0) ? (Runnable)()-> {} : (Runnable)System.out::println;
|
||||
lombok.val foo = (System.currentTimeMillis() > 0) ? (Runnable)()-> {} : System.out::println;
|
||||
}
|
||||
|
||||
// public void castLubLambda() {
|
||||
|
||||
Reference in new issue
Block a user