mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-14 12:16:49 +00:00
Merge branch 'bulgakovalexander-feature/typeInferenceImprovements'
This commit is contained in:
11 files changed
+97
-34
No files matched your search
@@ -3,6 +3,7 @@ Lombok Changelog
|
||||
|
||||
### v1.18.9 "Edgy Guinea Pig"
|
||||
* ENHANCEMENT: Thanks to Mark Haynes, the `staticConstructor` will now also be generated if a (private) constructor already exists. [Issue #2100](https://github.com/rzwitserloot/lombok/issues/2100)
|
||||
* ENHANCEMENT: `val` is now capable of decoding the type of convoluted expressions (particularly if the right hand side involves lambdas and conditional (ternary) expressions). [Pull Request #2109](https://github.com/rzwitserloot/lombok/pull/2109) with thanks to Alexander Bulgakov.
|
||||
|
||||
### v1.18.8 (May 7th, 2019)
|
||||
* FEATURE: You can now configure `@FieldNameConstants` to `CONSTANT_CASE` the generated constants, using a `lombok.config` option. See the [FieldNameConstants documentation](https://projectlombok.org/features/experimental/FieldNameConstants). [Issue #2092](https://github.com/rzwitserloot/lombok/issues/2092).
|
||||
|
||||
@@ -114,6 +114,7 @@ import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
|
||||
import org.eclipse.jdt.internal.compiler.lookup.Binding;
|
||||
import org.eclipse.jdt.internal.compiler.lookup.CaptureBinding;
|
||||
import org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding;
|
||||
import org.eclipse.jdt.internal.compiler.lookup.RawTypeBinding;
|
||||
import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
|
||||
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
|
||||
import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
|
||||
@@ -844,7 +845,6 @@ public class EclipseHandlerUtil {
|
||||
}
|
||||
|
||||
public static TypeReference makeType(TypeBinding binding, ASTNode pos, boolean allowCompound) {
|
||||
|
||||
if (binding.getClass() == EclipseReflectiveMembers.INTERSECTION_BINDING) {
|
||||
Object[] arr = (Object[]) EclipseReflectiveMembers.reflect(EclipseReflectiveMembers.INTERSECTION_BINDING_TYPES, binding);
|
||||
binding = (TypeBinding) arr[0];
|
||||
@@ -933,7 +933,12 @@ public class EclipseHandlerUtil {
|
||||
WildcardBinding wildcard = (WildcardBinding) binding;
|
||||
if (wildcard.boundKind == Wildcard.EXTENDS) {
|
||||
if (!allowCompound) {
|
||||
return makeType(wildcard.bound, pos, false);
|
||||
TypeBinding bound = wildcard.bound;
|
||||
boolean isObject = bound.id == TypeIds.T_JavaLangObject;
|
||||
TypeBinding[] otherBounds = wildcard.otherBounds;
|
||||
if (isObject && otherBounds != null && otherBounds.length > 0) {
|
||||
return makeType(otherBounds[0], pos, false);
|
||||
} else return makeType(bound, pos, false);
|
||||
} else {
|
||||
Wildcard out = new Wildcard(Wildcard.EXTENDS);
|
||||
setGeneratedBy(out, pos);
|
||||
@@ -960,7 +965,8 @@ public class EclipseHandlerUtil {
|
||||
// Finally, add however many nullTypeArgument[] arrays as that are missing, inverse the list, toArray it, and use that as PTR's typeArgument argument.
|
||||
|
||||
List<TypeReference[]> params = new ArrayList<TypeReference[]>();
|
||||
/* Calculate generics */ {
|
||||
/* Calculate generics */
|
||||
if (!(binding instanceof RawTypeBinding)) {
|
||||
TypeBinding b = binding;
|
||||
while (true) {
|
||||
boolean isFinalStop = b.isLocalType() || !b.isMemberType() || b.enclosingType() == null;
|
||||
|
||||
@@ -336,7 +336,7 @@ public class JavacResolution {
|
||||
if (type instanceof ClassType) {
|
||||
List<Type> ifaces = ((ClassType) type).interfaces_field;
|
||||
Type supertype = ((ClassType) type).supertype_field;
|
||||
if (ifaces != null && ifaces.length() == 1) {
|
||||
if (isObject(supertype) && ifaces != null && ifaces.length() > 0) {
|
||||
return typeToJCTree(ifaces.get(0), ast, allowCompound, allowVoid);
|
||||
}
|
||||
if (supertype != null) return typeToJCTree(supertype, ast, allowCompound, allowVoid);
|
||||
@@ -402,6 +402,10 @@ public class JavacResolution {
|
||||
return genericsToJCTreeNodes(generics, ast, replacement);
|
||||
}
|
||||
|
||||
private static boolean isObject(Type supertype) {
|
||||
return supertype.tsym.toString().equals("java.lang.Object");
|
||||
}
|
||||
|
||||
private static JCExpression genericsToJCTreeNodes(List<Type> generics, JavacAST ast, JCExpression rawTypeNode) throws TypeNotConvertibleException {
|
||||
if (generics != null && !generics.isEmpty()) {
|
||||
ListBuffer<JCExpression> args = new ListBuffer<JCExpression>();
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -263,15 +268,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.
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -362,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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,17 +5,17 @@ class ValLambda {
|
||||
};
|
||||
}
|
||||
public void easyIntersectionLambda() {
|
||||
final java.lang.Object foo = (Runnable & java.io.Serializable) () -> {
|
||||
final java.lang.Runnable foo = (Runnable & java.io.Serializable) () -> {
|
||||
};
|
||||
final java.lang.Object bar = (java.io.Serializable & Runnable) () -> {
|
||||
final java.io.Serializable bar = (java.io.Serializable & Runnable) () -> {
|
||||
};
|
||||
}
|
||||
public void easyLubLambda() {
|
||||
final java.lang.Runnable foo = (System.currentTimeMillis() > 0) ? (Runnable) () -> {
|
||||
} : (Runnable) System.out::println;
|
||||
} : System.out::println;
|
||||
final java.lang.Runnable foo1 = (System.currentTimeMillis() > 0) ? (Runnable) System.out::println : System.out::println;
|
||||
final java.util.function.Function foo2 = (System.currentTimeMillis() < 0) ? (java.util.function.Function) r -> "" : r -> System.currentTimeMillis();
|
||||
java.util.function.Function foo3 = (System.currentTimeMillis() < 0) ? (java.util.function.Function) r -> "" : r -> System.currentTimeMillis();
|
||||
final java.util.function.Function<java.lang.String, java.lang.String> foo4 = (System.currentTimeMillis() < 0) ? (java.util.function.Function<String, String>) r -> "" : r -> String.valueOf(System.currentTimeMillis());
|
||||
}
|
||||
// public void castLubLambda() {
|
||||
// Runnable foo = (Runnable) ((System.currentTimeMillis() > 0) ? () -> {} : System.out::println);
|
||||
// lombok.val foo = (Runnable) ((System.currentTimeMillis() > 0) ? () -> {} : System.out::println);
|
||||
// }
|
||||
}
|
||||
}
|
||||
@@ -56,8 +56,14 @@ public class ValWeirdTypes<Z> {
|
||||
final int singleInt = copy[0];
|
||||
}
|
||||
public void arraysAsList() {
|
||||
final java.util.List<java.lang.Class<? extends java.lang.Object>> x = Arrays.asList(String.class, BigDecimal.class);
|
||||
for (final java.lang.Class<?> y : x) {
|
||||
final java.util.List<java.lang.Class<? extends java.io.Serializable>> x = Arrays.asList(String.class, BigDecimal.class);
|
||||
for (final java.lang.Class<? extends java.io.Serializable> y : x) {
|
||||
}
|
||||
}
|
||||
@SuppressWarnings("all")
|
||||
public void arraysAsList2() {
|
||||
final java.util.List<java.lang.Class<? extends java.lang.Comparable>> x = Arrays.asList(String.class, BigDecimal.class, Comparable.class);
|
||||
for (final java.lang.Class<? extends java.lang.Comparable> y : x) {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,13 +7,17 @@ class ValLambda {
|
||||
};
|
||||
}
|
||||
public void easyIntersectionLambda() {
|
||||
final @lombok.val java.lang.Object foo = (Runnable & java.io.Serializable) () -> {
|
||||
final @lombok.val java.lang.Runnable foo = (Runnable & java.io.Serializable) () -> {
|
||||
};
|
||||
final @lombok.val java.lang.Object bar = (java.io.Serializable & Runnable) () -> {
|
||||
final @lombok.val java.io.Serializable bar = (java.io.Serializable & Runnable) () -> {
|
||||
};
|
||||
}
|
||||
public void easyLubLambda() {
|
||||
final @lombok.val java.lang.Runnable foo = ((System.currentTimeMillis() > 0) ? (Runnable) () -> {
|
||||
} : (Runnable) System.out::println);
|
||||
} : System.out::println);
|
||||
final @lombok.val java.lang.Runnable foo1 = ((System.currentTimeMillis() > 0) ? (Runnable) System.out::println : System.out::println);
|
||||
final @lombok.val java.util.function.Function foo2 = ((System.currentTimeMillis() < 0) ? (java.util.function.Function) (<no type> r) -> "" : (<no type> r) -> System.currentTimeMillis());
|
||||
java.util.function.Function foo3 = ((System.currentTimeMillis() < 0) ? (java.util.function.Function) (<no type> r) -> "" : (<no type> r) -> System.currentTimeMillis());
|
||||
final @lombok.val java.util.function.Function<java.lang.String, java.lang.String> foo4 = ((System.currentTimeMillis() < 0) ? (java.util.function.Function<String, String>) (<no type> r) -> "" : (<no type> r) -> String.valueOf(System.currentTimeMillis()));
|
||||
}
|
||||
}
|
||||
@@ -63,8 +63,14 @@ public class ValWeirdTypes<Z> {
|
||||
final @val int singleInt = copy[0];
|
||||
}
|
||||
public void arraysAsList() {
|
||||
final @val java.util.List<java.lang.Class<? extends java.lang.Object>> x = Arrays.asList(String.class, BigDecimal.class);
|
||||
for (final @val java.lang.Class<? extends java.lang.Object> y : x)
|
||||
final @val java.util.List<java.lang.Class<? extends java.io.Serializable>> x = Arrays.asList(String.class, BigDecimal.class);
|
||||
for (final @val java.lang.Class<? extends java.io.Serializable> y : x)
|
||||
{
|
||||
}
|
||||
}
|
||||
public @SuppressWarnings("all") void arraysAsList2() {
|
||||
final @val java.util.List<java.lang.Class<? extends java.lang.Comparable>> x = Arrays.asList(String.class, BigDecimal.class, Comparable.class);
|
||||
for (final @val java.lang.Class<? extends java.lang.Comparable> y : x)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,11 +10,10 @@ 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;
|
||||
lombok.val foo1 = (System.currentTimeMillis() > 0) ? (Runnable)System.out::println : System.out::println;
|
||||
lombok.val foo2 = (System.currentTimeMillis() < 0) ? (java.util.function.Function) r -> "" : r -> System.currentTimeMillis();
|
||||
java.util.function.Function foo3 = (System.currentTimeMillis() < 0) ? (java.util.function.Function) r -> "" : r -> System.currentTimeMillis();
|
||||
lombok.val foo4 = (System.currentTimeMillis() < 0) ? (java.util.function.Function<String, String>) r -> "" : r -> String.valueOf(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
// public void castLubLambda() {
|
||||
// Runnable foo = (Runnable) ((System.currentTimeMillis() > 0) ? () -> {} : System.out::println);
|
||||
// lombok.val foo = (Runnable) ((System.currentTimeMillis() > 0) ? () -> {} : System.out::println);
|
||||
// }
|
||||
}
|
||||
@@ -72,4 +72,12 @@ public class ValWeirdTypes<Z> {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("all")
|
||||
public void arraysAsList2() {
|
||||
val x = Arrays.asList(String.class, BigDecimal.class, Comparable.class);
|
||||
for (val y : x) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
14 Function is a raw type. References to generic type Function<T,R> should be parameterized
|
||||
15 Function is a raw type. References to generic type Function<T,R> should be parameterized
|
||||
Reference in new issue
Block a user