Skip to content

Detect and omit the JVM's lambda factory method. #82

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
merged 1 commit into from
Jan 10, 2016
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 @@ -6,7 +6,11 @@

import org.junit.Test;

import java.lang.reflect.Method;
import java.util.*;

import static net.orfjackal.retrolambda.test.TestUtil.assertClassExists;
import static org.junit.Assert.assertTrue;

public class LambdaClassesTest {

Expand All @@ -18,10 +22,31 @@ public void the_sequence_number_starts_from_1_for_each_enclosing_class() {
assertClassExists(Dummy2.class.getName() + "$$Lambda$2");
}

@Test
public void capturing_lambda_contain_no_unexpected_methods() throws ClassNotFoundException {
List<String> expected = new ArrayList<>(Arrays.asList("lambdaFactory$", "run"));
Class<?> cls = Class.forName(Dummy1.class.getName() + "$$Lambda$1");
for (Method method : cls.getDeclaredMethods()) {
assertTrue(method.getName() + " not expected", expected.remove(method.getName()));
}
assertTrue("Missing methods: " + expected, expected.isEmpty());
}

@Test
public void non_capturing_lambda_contain_no_unexpected_methods() throws ClassNotFoundException {
List<String> expected = new ArrayList<>(Arrays.asList("lambdaFactory$", "run"));
Class<?> cls = Class.forName(Dummy2.class.getName() + "$$Lambda$1");
for (Method method : cls.getDeclaredMethods()) {
assertTrue(method.getName() + " not expected", expected.remove(method.getName()));
}
assertTrue("Missing methods: " + expected, expected.isEmpty());
}


@SuppressWarnings("UnusedDeclaration")
private class Dummy1 {
private Dummy1() {
// Non-capturing lambdas
Runnable lambda1 = () -> {
};
Runnable lambda2 = () -> {
Expand All @@ -32,10 +57,9 @@ private Dummy1() {
@SuppressWarnings("UnusedDeclaration")
private class Dummy2 {
private Dummy2() {
Runnable lambda1 = () -> {
};
Runnable lambda2 = () -> {
};
// Capturing lambdas
Runnable lambda1 = () -> System.out.println(hashCode());
Runnable lambda2 = () -> System.out.println(hashCode());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public MethodVisitor visitMethod(int access, String name, String desc, String si
if (LambdaNaming.isSerializationHook(access, name, desc)) {
return null; // remove serialization hooks; we serialize lambda instances as-is
}
if (LambdaNaming.isPlatformFactoryMethod(access, name, desc, factoryMethod.getDesc())) {
return null; // remove the JVM's factory method which will not be unused
}
MethodVisitor next = super.visitMethod(access, name, desc, signature, exceptions);
next = new RemoveMagicLambdaConstructorCall(next);
next = new CallPrivateImplMethodsViaAccessMethods(next);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ public static boolean isDeserializationHook(int access, String name, String desc
&& desc.equals("(Ljava/lang/invoke/SerializedLambda;)Ljava/lang/Object;")
&& Flags.hasFlag(access, ACC_PRIVATE | ACC_STATIC | ACC_SYNTHETIC);
}

public static boolean isPlatformFactoryMethod(int access, String name, String desc, String targetDesc) {
return name.equals("get$Lambda")
&& desc.equals(targetDesc)
&& Flags.hasFlag(access, ACC_PRIVATE | ACC_STATIC);
}
}