From fe6c8e51b4d0a3241801d14397edff8a03d76e39 Mon Sep 17 00:00:00 2001 From: Esko Luontola Date: Tue, 14 Apr 2015 21:39:09 +0300 Subject: [PATCH] Failing test for lambda impl methods being treated the same way as default methods Reproduces #48 --- .../retrolambda/test/DefaultMethodsTest.java | 23 ++++++++++++++++--- .../UsesLambdasInAnotherPackage.java | 18 +++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 end-to-end-tests/src/test/java/net/orfjackal/retrolambda/test/anotherpackage/UsesLambdasInAnotherPackage.java diff --git a/end-to-end-tests/src/test/java/net/orfjackal/retrolambda/test/DefaultMethodsTest.java b/end-to-end-tests/src/test/java/net/orfjackal/retrolambda/test/DefaultMethodsTest.java index 32d35fb7..695bbe9e 100644 --- a/end-to-end-tests/src/test/java/net/orfjackal/retrolambda/test/DefaultMethodsTest.java +++ b/end-to-end-tests/src/test/java/net/orfjackal/retrolambda/test/DefaultMethodsTest.java @@ -4,6 +4,7 @@ package net.orfjackal.retrolambda.test; +import net.orfjackal.retrolambda.test.anotherpackage.UsesLambdasInAnotherPackage; import org.apache.commons.lang.SystemUtils; import org.hamcrest.*; import org.junit.*; @@ -444,7 +445,7 @@ public void lambdas_with_default_methods() { public void default_methods_with_lambdas() throws Exception { UsesLambdas obj = new UsesLambdas() { }; - assertThat(obj.foo().call(), is("foo")); + assertThat(obj.stateless().call(), is("foo")); } @Test @@ -455,15 +456,31 @@ public void default_methods_with_lambdas_that_capture_this() throws Exception { } private interface UsesLambdas { - default Callable foo() { + default Callable stateless() { return () -> "foo"; } default Callable captureThis() { - return () -> foo().call(); + return () -> stateless().call(); } } + /** + * Lambdas which capture this in default methods will generate the lambda implementation + * method as a private instance method. We must avoid copying those methods to + * the interface implementers as if they were default methods. + */ + @Test + public void default_methods_with_lambdas_in_another_package() throws Exception { + System.out.println("DefaultMethodsTest.default_methods_with_lambdas_in_another_package"); + UsesLambdasInAnotherPackage obj = new UsesLambdasInAnotherPackage() { + }; + assertThat(obj.stateless().call(), is("foo")); + assertThat(obj.captureThis().call(), is("foo")); + assertThat("should contain only delegates to the two default methods", + obj.getClass().getDeclaredMethods(), arrayWithSize(2)); + } + /** * We're unable to backport default methods if we cannot modify the interface, diff --git a/end-to-end-tests/src/test/java/net/orfjackal/retrolambda/test/anotherpackage/UsesLambdasInAnotherPackage.java b/end-to-end-tests/src/test/java/net/orfjackal/retrolambda/test/anotherpackage/UsesLambdasInAnotherPackage.java new file mode 100644 index 00000000..01b96cd7 --- /dev/null +++ b/end-to-end-tests/src/test/java/net/orfjackal/retrolambda/test/anotherpackage/UsesLambdasInAnotherPackage.java @@ -0,0 +1,18 @@ +// Copyright © 2013-2015 Esko Luontola +// This software is released under the Apache License 2.0. +// The license text is at http://www.apache.org/licenses/LICENSE-2.0 + +package net.orfjackal.retrolambda.test.anotherpackage; + +import java.util.concurrent.Callable; + +public interface UsesLambdasInAnotherPackage { + + default Callable stateless() { + return () -> "foo"; + } + + default Callable captureThis() { + return () -> stateless().call(); + } +}