From 3da5136074128b20db7c679c9afdd8ba5a714be7 Mon Sep 17 00:00:00 2001 From: Romain Lebouc Date: Wed, 29 May 2019 14:19:22 +0200 Subject: [PATCH 1/3] Added a failing test case for Multi-inherited generic lambda --- src/test/java/spoon/test/lambda/LambdaTest.java | 10 ++++++++++ src/test/java/spoon/test/lambda/testclasses/Foo.java | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/src/test/java/spoon/test/lambda/LambdaTest.java b/src/test/java/spoon/test/lambda/LambdaTest.java index 06b84ba783f..8a58005d52a 100644 --- a/src/test/java/spoon/test/lambda/LambdaTest.java +++ b/src/test/java/spoon/test/lambda/LambdaTest.java @@ -432,6 +432,16 @@ public void testInterfaceWithObjectMethods() { assertEquals("test", method.getSimpleName()); } + @Test + public void testLambdaWithGenericExtendingMultipleInterfaces() { + final CtLambda lambda1 = foo.getElements(new TypeFilter>(CtLambda.class)).get(9); + assertEquals(1, lambda1.getParameters().size()); + final CtParameter ctParameterFirstLambda = lambda1.getParameters().get(0); + assertEquals("elt", ctParameterFirstLambda.getSimpleName()); + assertTrue(ctParameterFirstLambda.getType().isImplicit()); + assertEquals("", ctParameterFirstLambda.getType().toString()); + } + private void assertHasStrings(List methodNames, String... strs) { for (String str : strs) { assertTrue("List should contain "+str+" but it is missing.", methodNames.remove(str)); diff --git a/src/test/java/spoon/test/lambda/testclasses/Foo.java b/src/test/java/spoon/test/lambda/testclasses/Foo.java index 92be0b07af9..93a0b0503f5 100644 --- a/src/test/java/spoon/test/lambda/testclasses/Foo.java +++ b/src/test/java/spoon/test/lambda/testclasses/Foo.java @@ -1,6 +1,7 @@ package spoon.test.lambda.testclasses; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.function.Consumer; import java.util.function.Predicate; @@ -55,6 +56,14 @@ public void m9() { }; } + public void m10(){ + multiInheritedGenericsList().stream().filter(elt -> elt.test()); + } + + public static List multiInheritedGenericsList() { + return Collections.emptyList(); + } + public static void printPersonsWithPredicate(List roster, Predicate tester) { for (Person p : roster) { if (tester.test(p)) { From b0d50f18f06e24e24595c7b2d49a0766616777d6 Mon Sep 17 00:00:00 2001 From: nharrand Date: Mon, 3 Jun 2019 19:40:01 +0200 Subject: [PATCH 2/3] fix(DefaultJavaPrettyPrinter): implicit type reference to intersection type --- .../visitor/DefaultJavaPrettyPrinter.java | 3 +++ .../spoon/test/prettyprinter/PrinterTest.java | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java b/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java index 75a495caf82..6917849e92e 100644 --- a/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java +++ b/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java @@ -1800,6 +1800,9 @@ private boolean printQualified(CtTypeReference ref) { @Override public void visitCtIntersectionTypeReference(CtIntersectionTypeReference reference) { + if (reference.isImplicit()) { + return; + } elementPrinterHelper.printList(reference.getBounds(), null, false, null, false, true, "&", true, false, null, bound -> scan(bound)); diff --git a/src/test/java/spoon/test/prettyprinter/PrinterTest.java b/src/test/java/spoon/test/prettyprinter/PrinterTest.java index 8d34e3d88fb..6125f3751c1 100644 --- a/src/test/java/spoon/test/prettyprinter/PrinterTest.java +++ b/src/test/java/spoon/test/prettyprinter/PrinterTest.java @@ -209,6 +209,27 @@ public void testRuleCanBeBuild() { canBeBuilt(output, 7); } + + @Test + public void testLambdaCanBeBuild() { + Launcher spoon = new Launcher(); + PrettyPrinter printer = spoon.createPrettyPrinter(); + spoon.getEnvironment().setAutoImports(true); + String output = "./target/spoon-lambda/"; + spoon.addInputResource("./src/test/java/spoon/test/lambda/testclasses/Foo.java"); + spoon.setSourceOutputDirectory(output); + spoon.run(); + + CtType element = spoon.getFactory().Class().getAll().get(0); + List> toPrint = new ArrayList<>(); + toPrint.add(element); + printer.calculate(element.getPosition().getCompilationUnit(), toPrint); + String result = printer.getResult(); + + //assertTrue("The result should contain direct this accessor for field: "+result, !result.contains("Rule.Phoneme.this.phonemeText")); + canBeBuilt(output, 8); + } + @Test public void testJDTBatchCompilerCanBeBuild() { Launcher spoon = new Launcher(); From f06c6ef49cddfc713d3b19e326802694d1edeb01 Mon Sep 17 00:00:00 2001 From: nharrand Date: Mon, 3 Jun 2019 20:29:55 +0200 Subject: [PATCH 3/3] up --- .../java/spoon/test/lambda/LambdaTest.java | 22 +++++++++++++----- .../spoon/test/lambda/testclasses/Foo.java | 8 ------- .../test/lambda/testclasses/Intersection.java | 23 +++++++++++++++++++ .../spoon/test/prettyprinter/PrinterTest.java | 4 +--- 4 files changed, 40 insertions(+), 17 deletions(-) create mode 100644 src/test/java/spoon/test/lambda/testclasses/Intersection.java diff --git a/src/test/java/spoon/test/lambda/LambdaTest.java b/src/test/java/spoon/test/lambda/LambdaTest.java index 8a58005d52a..e6c682cd280 100644 --- a/src/test/java/spoon/test/lambda/LambdaTest.java +++ b/src/test/java/spoon/test/lambda/LambdaTest.java @@ -32,6 +32,7 @@ import spoon.reflect.declaration.CtType; import spoon.reflect.factory.Factory; import spoon.reflect.reference.CtArrayTypeReference; +import spoon.reflect.reference.CtIntersectionTypeReference; import spoon.reflect.reference.CtParameterReference; import spoon.reflect.reference.CtTypeParameterReference; import spoon.reflect.reference.CtTypeReference; @@ -43,6 +44,7 @@ import spoon.reflect.visitor.filter.TypeFilter; import spoon.test.lambda.testclasses.Bar; import spoon.test.lambda.testclasses.Foo; +import spoon.test.lambda.testclasses.Intersection; import spoon.test.lambda.testclasses.Kuu; import spoon.test.lambda.testclasses.LambdaRxJava; import spoon.test.lambda.testclasses.Panini; @@ -71,6 +73,7 @@ public class LambdaTest { private CtType panini; private CtType tacos; private CtType lambdaRxJava; + private CtType intersection; private SpoonModelBuilder compiler; @Before @@ -89,6 +92,7 @@ public void setUp() { panini = factory.Type().get(Panini.class); tacos = factory.Type().get(Tacos.class); lambdaRxJava = factory.Type().get(LambdaRxJava.class); + intersection = factory.Type().get(Intersection.class); } @Test @@ -434,12 +438,18 @@ public void testInterfaceWithObjectMethods() { @Test public void testLambdaWithGenericExtendingMultipleInterfaces() { - final CtLambda lambda1 = foo.getElements(new TypeFilter>(CtLambda.class)).get(9); - assertEquals(1, lambda1.getParameters().size()); - final CtParameter ctParameterFirstLambda = lambda1.getParameters().get(0); - assertEquals("elt", ctParameterFirstLambda.getSimpleName()); - assertTrue(ctParameterFirstLambda.getType().isImplicit()); - assertEquals("", ctParameterFirstLambda.getType().toString()); + final CtLambda lambda = intersection.getElements(new TypeFilter>(CtLambda.class)).get(0); + assertEquals(1, lambda.getParameters().size()); + final CtParameter parameter = lambda.getParameters().get(0); + assertEquals("elt", parameter.getSimpleName()); + assertTrue(parameter.getType().isImplicit()); + assertEquals("", parameter.getType().toString()); + CtIntersectionTypeReference typeReference = (CtIntersectionTypeReference) parameter.getType(); + + assertParameterIsNamedBy("elt", parameter); + assertTrue(typeReference.getBounds().size() == 2); + assertHasExpressionBody(lambda); + assertIsWellPrinted("( elt) -> elt.test()", lambda); } private void assertHasStrings(List methodNames, String... strs) { diff --git a/src/test/java/spoon/test/lambda/testclasses/Foo.java b/src/test/java/spoon/test/lambda/testclasses/Foo.java index 93a0b0503f5..042f9927739 100644 --- a/src/test/java/spoon/test/lambda/testclasses/Foo.java +++ b/src/test/java/spoon/test/lambda/testclasses/Foo.java @@ -56,14 +56,6 @@ public void m9() { }; } - public void m10(){ - multiInheritedGenericsList().stream().filter(elt -> elt.test()); - } - - public static List multiInheritedGenericsList() { - return Collections.emptyList(); - } - public static void printPersonsWithPredicate(List roster, Predicate tester) { for (Person p : roster) { if (tester.test(p)) { diff --git a/src/test/java/spoon/test/lambda/testclasses/Intersection.java b/src/test/java/spoon/test/lambda/testclasses/Intersection.java new file mode 100644 index 00000000000..cf22fe13d08 --- /dev/null +++ b/src/test/java/spoon/test/lambda/testclasses/Intersection.java @@ -0,0 +1,23 @@ +package spoon.test.lambda.testclasses; + +import java.util.Collections; +import java.util.List; + +public class Intersection { + + public void m(){ + multiInheritedGenericsList().stream().filter(elt -> elt.test()); + } + + public static List multiInheritedGenericsList() { + return Collections.emptyList(); + } + + public interface A { + boolean test() throws Exception; + } + + public interface B { + boolean test(); + } +} diff --git a/src/test/java/spoon/test/prettyprinter/PrinterTest.java b/src/test/java/spoon/test/prettyprinter/PrinterTest.java index 6125f3751c1..cdb0c8820b1 100644 --- a/src/test/java/spoon/test/prettyprinter/PrinterTest.java +++ b/src/test/java/spoon/test/prettyprinter/PrinterTest.java @@ -216,7 +216,7 @@ public void testLambdaCanBeBuild() { PrettyPrinter printer = spoon.createPrettyPrinter(); spoon.getEnvironment().setAutoImports(true); String output = "./target/spoon-lambda/"; - spoon.addInputResource("./src/test/java/spoon/test/lambda/testclasses/Foo.java"); + spoon.addInputResource("./src/test/java/spoon/test/lambda/testclasses/Intersection.java"); spoon.setSourceOutputDirectory(output); spoon.run(); @@ -224,9 +224,7 @@ public void testLambdaCanBeBuild() { List> toPrint = new ArrayList<>(); toPrint.add(element); printer.calculate(element.getPosition().getCompilationUnit(), toPrint); - String result = printer.getResult(); - //assertTrue("The result should contain direct this accessor for field: "+result, !result.contains("Rule.Phoneme.this.phonemeText")); canBeBuilt(output, 8); }