Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(DefaultJavaPrettyPrinter): handle implicit type reference to intersection type #3006

Merged
merged 4 commits into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1800,6 +1800,9 @@ private boolean printQualified(CtTypeReference<?> ref) {

@Override
public <T> void visitCtIntersectionTypeReference(CtIntersectionTypeReference<T> reference) {
if (reference.isImplicit()) {
return;
}
elementPrinterHelper.printList(reference.getBounds(),
null, false, null, false, true, "&", true, false, null,
bound -> scan(bound));
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/spoon/test/lambda/LambdaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -71,6 +73,7 @@ public class LambdaTest {
private CtType<Object> panini;
private CtType<Object> tacos;
private CtType<LambdaRxJava> lambdaRxJava;
private CtType<Intersection> intersection;
private SpoonModelBuilder compiler;

@Before
Expand All @@ -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
Expand Down Expand Up @@ -432,6 +436,22 @@ public void testInterfaceWithObjectMethods() {
assertEquals("test", method.getSimpleName());
}

@Test
public void testLambdaWithGenericExtendingMultipleInterfaces() {
final CtLambda<?> lambda = intersection.getElements(new TypeFilter<CtLambda<?>>(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<String> methodNames, String... strs) {
for (String str : strs) {
assertTrue("List should contain "+str+" but it is missing.", methodNames.remove(str));
Expand Down
1 change: 1 addition & 0 deletions src/test/java/spoon/test/lambda/testclasses/Foo.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/spoon/test/lambda/testclasses/Intersection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package spoon.test.lambda.testclasses;

import java.util.Collections;
import java.util.List;

public class Intersection<T> {

public void m(){
multiInheritedGenericsList().stream().filter(elt -> elt.test());
}

public static <T extends A & B> List<T> multiInheritedGenericsList() {
return Collections.emptyList();
}

public interface A {
boolean test() throws Exception;
}

public interface B {
boolean test();
}
}
19 changes: 19 additions & 0 deletions src/test/java/spoon/test/prettyprinter/PrinterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,25 @@ 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/Intersection.java");
spoon.setSourceOutputDirectory(output);
spoon.run();

CtType element = spoon.getFactory().Class().getAll().get(0);
List<CtType<?>> toPrint = new ArrayList<>();
toPrint.add(element);
printer.calculate(element.getPosition().getCompilationUnit(), toPrint);

canBeBuilt(output, 8);
}

@Test
public void testJDTBatchCompilerCanBeBuild() {
Launcher spoon = new Launcher();
Expand Down