Skip to content

fix: Make lambda type implicit to avoid auto-importing type targeted in lambda #4093

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

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
9 changes: 9 additions & 0 deletions src/main/java/spoon/support/reflect/code/CtLambdaImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import spoon.reflect.declaration.CtParameter;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.CtExecutable;
import spoon.reflect.declaration.CtTypedElement;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.reference.CtIntersectionTypeReference;
Expand Down Expand Up @@ -284,4 +285,12 @@ public <C extends CtLambda<T>> C setExpression(CtExpression<T> expression) {
public CtLambda<T> clone() {
return (CtLambda<T>) super.clone();
}

@Override
public <C extends CtTypedElement> C setType(CtTypeReference<T> type) {
if (type != null) {
type.setImplicit(true);
}
return super.setType(type);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package spoon.reflect.visitor;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import spoon.Launcher;
import spoon.reflect.CtModel;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtStatement;
import spoon.reflect.declaration.CtCompilationUnit;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;

public class DefaultJavaPrettyPrinterTest {
Expand Down Expand Up @@ -60,4 +65,21 @@ private static Launcher createLauncherWithOptimizeParenthesesPrinter() {
});
return launcher;
}


@Test
void testAutoImportPrinterDoesNotImportFunctionalInterfaceTargetedInLambda() {
// contract: The auto-import printer should not import functional interfaces that are
// targeted in lambdas, but are not explicitly referenced anywhere
Launcher launcher = new Launcher();
launcher.addInputResource("src/test/resources/target-functional-interface-in-lambda");
launcher.buildModel();
CtCompilationUnit cu = launcher.getFactory().Type().get("TargetsFunctionalInterface")
.getPosition().getCompilationUnit();

PrettyPrinter autoImportPrettyPrinter = launcher.getEnvironment().createPrettyPrinterAutoImport();
String output = autoImportPrettyPrinter.prettyprint(cu);

assertThat(output, not(containsString("import java.util.function.IntFunction;")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import java.util.function.IntFunction;

public class HasFunctionalInterface {
public static void supplyOne(IntFunction<String> intFunction) {
intFunction.apply(1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class TargetsFunctionalInterface {
public static void main(String[] args) {
HasFunctionalInterface.supplyOne(i -> String.valueOf(i));
}
}