From f0343886a331f3cb2175545a062f3736610f9179 Mon Sep 17 00:00:00 2001 From: Bulgakov Alexander Date: Wed, 24 Apr 2019 14:06:15 +0300 Subject: improvements type inference of type intersections (affects ValLambda.java, ValWeirdTypes.java tests) --- src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java | 7 ++++++- src/core/lombok/javac/JavacResolution.java | 6 +++++- 2 files changed, 11 insertions(+), 2 deletions(-) (limited to 'src/core/lombok') diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index 010dc9d8..438314ca 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -893,7 +893,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); diff --git a/src/core/lombok/javac/JavacResolution.java b/src/core/lombok/javac/JavacResolution.java index abbf6726..7f940d2a 100644 --- a/src/core/lombok/javac/JavacResolution.java +++ b/src/core/lombok/javac/JavacResolution.java @@ -336,7 +336,7 @@ public class JavacResolution { if (type instanceof ClassType) { List 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 generics, JavacAST ast, JCExpression rawTypeNode) throws TypeNotConvertibleException { if (generics != null && !generics.isEmpty()) { ListBuffer args = new ListBuffer(); -- cgit From 1fec035aa546b7a033acb67cf4f7c1afb8a79f52 Mon Sep 17 00:00:00 2001 From: Bulgakov Alexander Date: Wed, 8 May 2019 23:34:37 +0300 Subject: #1976. [@val] improved raw generic type inference by eclipse compiler. --- src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java | 3 ++- test/transform/resource/after-delombok/ValLambda.java | 5 ++++- test/transform/resource/after-ecj/ValLambda.java | 3 +++ test/transform/resource/before/ValLambda.java | 3 +++ test/transform/resource/messages-ecj/ValLambda.java.messages | 2 ++ 5 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 test/transform/resource/messages-ecj/ValLambda.java.messages (limited to 'src/core/lombok') diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index 1e791341..257f5cec 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -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; @@ -965,7 +966,7 @@ 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 params = new ArrayList(); - /* Calculate generics */ { + /* Calculate generics */ if(!(binding instanceof RawTypeBinding)) { TypeBinding b = binding; while (true) { boolean isFinalStop = b.isLocalType() || !b.isMemberType() || b.enclosingType() == null; diff --git a/test/transform/resource/after-delombok/ValLambda.java b/test/transform/resource/after-delombok/ValLambda.java index 772db9a5..7e745e81 100644 --- a/test/transform/resource/after-delombok/ValLambda.java +++ b/test/transform/resource/after-delombok/ValLambda.java @@ -13,9 +13,12 @@ class ValLambda { public void easyLubLambda() { final java.lang.Runnable foo = (System.currentTimeMillis() > 0) ? (Runnable) () -> { } : 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 foo4 = (System.currentTimeMillis() < 0) ? (java.util.function.Function) 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); // } -} +} \ No newline at end of file diff --git a/test/transform/resource/after-ecj/ValLambda.java b/test/transform/resource/after-ecj/ValLambda.java index 53258b3e..e7cbb28e 100644 --- a/test/transform/resource/after-ecj/ValLambda.java +++ b/test/transform/resource/after-ecj/ValLambda.java @@ -15,5 +15,8 @@ class ValLambda { public void easyLubLambda() { final @lombok.val java.lang.Runnable foo = ((System.currentTimeMillis() > 0) ? (Runnable) () -> { } : System.out::println); + final @lombok.val 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 @lombok.val java.util.function.Function foo4 = ((System.currentTimeMillis() < 0) ? (java.util.function.Function) ( r) -> "" : ( r) -> String.valueOf(System.currentTimeMillis())); } } \ No newline at end of file diff --git a/test/transform/resource/before/ValLambda.java b/test/transform/resource/before/ValLambda.java index 4ec73b82..d13221fd 100644 --- a/test/transform/resource/before/ValLambda.java +++ b/test/transform/resource/before/ValLambda.java @@ -11,6 +11,9 @@ class ValLambda { public void easyLubLambda() { lombok.val foo = (System.currentTimeMillis() > 0) ? (Runnable)()-> {} : 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) r -> "" : r -> String.valueOf(System.currentTimeMillis()); } // public void castLubLambda() { diff --git a/test/transform/resource/messages-ecj/ValLambda.java.messages b/test/transform/resource/messages-ecj/ValLambda.java.messages new file mode 100644 index 00000000..21c831c3 --- /dev/null +++ b/test/transform/resource/messages-ecj/ValLambda.java.messages @@ -0,0 +1,2 @@ +14 Function is a raw type. References to generic type Function should be parameterized +15 Function is a raw type. References to generic type Function should be parameterized \ No newline at end of file -- cgit From 2704f073aad6deb362707e0311b6275cde7c5e1a Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Tue, 21 May 2019 23:20:00 +0200 Subject: Code review + mention in changelog for the improvement to val handling vs. lambdas and conditional (ternary) expressions. --- doc/changelog.markdown | 2 +- src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java | 4 ++-- src/eclipseAgent/lombok/eclipse/agent/PatchVal.java | 6 +++--- test/transform/resource/after-delombok/ValLambda.java | 4 ---- test/transform/resource/before/ValLambda.java | 5 ----- 5 files changed, 6 insertions(+), 15 deletions(-) (limited to 'src/core/lombok') diff --git a/doc/changelog.markdown b/doc/changelog.markdown index e0e3d49a..1d927e62 100644 --- a/doc/changelog.markdown +++ b/doc/changelog.markdown @@ -2,7 +2,7 @@ Lombok Changelog ---------------- ### v1.18.9 "Edgy Guinea Pig" -* Nothing yet. +* 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). diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index 257f5cec..463990d1 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -845,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]; @@ -966,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 params = new ArrayList(); - /* Calculate generics */ if(!(binding instanceof RawTypeBinding)) { + /* Calculate generics */ + if (!(binding instanceof RawTypeBinding)) { TypeBinding b = binding; while (true) { boolean isFinalStop = b.isLocalType() || !b.isMemberType() || b.enclosingType() == null; diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchVal.java b/src/eclipseAgent/lombok/eclipse/agent/PatchVal.java index 832a25e3..c0c2cea6 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchVal.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchVal.java @@ -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 diff --git a/test/transform/resource/after-delombok/ValLambda.java b/test/transform/resource/after-delombok/ValLambda.java index b993028e..00ff27ad 100644 --- a/test/transform/resource/after-delombok/ValLambda.java +++ b/test/transform/resource/after-delombok/ValLambda.java @@ -18,8 +18,4 @@ class ValLambda { java.util.function.Function foo3 = (System.currentTimeMillis() < 0) ? (java.util.function.Function) r -> "" : r -> System.currentTimeMillis(); final java.util.function.Function foo4 = (System.currentTimeMillis() < 0) ? (java.util.function.Function) 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); -// } } \ No newline at end of file diff --git a/test/transform/resource/before/ValLambda.java b/test/transform/resource/before/ValLambda.java index 5d40f220..35f83c3c 100644 --- a/test/transform/resource/before/ValLambda.java +++ b/test/transform/resource/before/ValLambda.java @@ -16,9 +16,4 @@ class ValLambda { 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) 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); -// } } -- cgit