Skip to content
Open
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 @@ -487,4 +487,98 @@ public void testRemoveConditions() throws Exception {
Assert.assertFalse(cu.getImports().isEmpty());

}

@Test
public void testDontRemoveImportsUsedByLambdas() throws Exception {
String externalClass = "package blah;" +
"import java.util.function.Supplier;" +
"public class MyClassThatNeedsToBeImported {" +
"public static <T> Supplier<T> memoize(Supplier<T> delegate) {" +
"return null;" + //Implementation not relevant to testcase
"}" +
"}";
String code = "package example;" +
"import blah.MyClassThatNeedsToBeImported;" +
"import java.util.function.Supplier;" +
"class MyClass {" +
"Supplier o;" +
"public MyClass() {" +
" this.o = MyClassThatNeedsToBeImported.memoize(this::load)::get;" +

"load();" +

"}" +
"private Object load() {" +
"return null;"+ //Implementation not relevant to testcase
"}}";

CompilationUnit cu = compile(code, externalClass);
String codeBefore = cu.toString();

CleanDeadDeclarationsVisitor<?> visitor = new CleanDeadDeclarationsVisitor<Object>();
cu.accept(visitor, null);
String codeAfter = cu.toString();

Assert.assertEquals(codeBefore, codeAfter);
}

@Test
public void testDontRemoveImportsUsedByLambdasExample2() throws Exception {
String code = "package example;" +
"import java.util.Collection;" +
"import java.util.Objects;" +
"import static java.util.stream.Collectors.toSet;" +
" public class ExampleGroupPickerSearcher {" +
" public ExampleGroupPickerSearcher() {" +
" final Resolver<String> nameResolver = rawValues ->" +
" rawValues.stream()" +
" .map(this::convertToIndexValue)" +
" .filter(Objects::nonNull)" +
" .collect(toSet());" +
" }" +
" public String convertToIndexValue(final Object rawValue) {" +
" return \"\";" +
" }" +
" public interface Resolver<T> {" +
" Collection<T> resolveNames(Collection<Object> rawValues);" +
" }" +
" }";
CompilationUnit cu = compile(code);
String codeBefore = cu.toString();

CleanDeadDeclarationsVisitor<?> visitor = new CleanDeadDeclarationsVisitor<Object>();
cu.accept(visitor, null);
String codeAfter = cu.toString();

Assert.assertEquals(codeBefore, codeAfter);
}

@Test
public void testDontRemoveImportsUsedByLambdasExample3() throws Exception {
String code = "package example;" +
"import java.util.function.Supplier;" +
"public class ExampleDefaultTemplateManager {" +
"public ExampleDefaultTemplateManager() {" +
"ContextFreeReference<Object> holderRef = new ContextFreeReference<>(this::initTemplates);" +
"}" +
"private Object initTemplates() {" +
"return null;" +
"}" +
"public class ContextFreeReference<T> implements Supplier<T> {" +
"public ContextFreeReference(Supplier<T> supplier) {" +
"}" +
"public T get() {" +
"return null;" +
"}" +
"}" +
"}";
CompilationUnit cu = compile(code);
String codeBefore = cu.toString();

CleanDeadDeclarationsVisitor<?> visitor = new CleanDeadDeclarationsVisitor<Object>();
cu.accept(visitor, null);
String codeAfter = cu.toString();

Assert.assertEquals(codeBefore, codeAfter);
}
}