Skip to content

Commit 9bc1def

Browse files
committed
Test to reproduce #19
1 parent d213752 commit 9bc1def

File tree

1 file changed

+40
-1
lines changed
  • end-to-end-tests/src/test/java/net/orfjackal/retrolambda/test

1 file changed

+40
-1
lines changed

end-to-end-tests/src/test/java/net/orfjackal/retrolambda/test/LambdaTest.java

+40-1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ public void method_references_to_private_methods() throws Exception {
135135

136136
Callable<String> ref2 = this::privateInstanceMethod;
137137
assertThat(ref2.call(), is("foo"));
138+
139+
// Normal method calls should still work after our magic
140+
// of making them them accessible from the lambda classes.
141+
assertThat(privateClassMethod(), is("foo"));
142+
assertThat(privateInstanceMethod(), is("foo"));
138143
}
139144

140145
private String privateInstanceMethod() {
@@ -146,7 +151,7 @@ private static String privateClassMethod() {
146151
}
147152

148153
/**
149-
* We must make private lambda implementation methods package-private,
154+
* We could make private lambda implementation methods package-private,
150155
* so that the lambda class may call them, but we should not make any
151156
* more methods non-private than is absolutely necessary.
152157
*/
@@ -164,10 +169,44 @@ public void will_not_change_the_visibility_of_unrelated_methods() throws Excepti
164169
private String unrelatedPrivateMethod() {
165170
return "foo";
166171
}
172+
173+
/**
174+
* We cannot just make the private methods package-private for the
175+
* lambda class to call them, because that may cause a method in subclass
176+
* to override them.
177+
*/
178+
@Test
179+
public void will_not_cause_private_methods_to_be_overridable() throws Exception {
180+
// Our test assumes that there exists a private method with
181+
// the same name and signature in super and sub classes.
182+
String name = "privateMethod";
183+
assertThat(getClass().getDeclaredMethod(name), is(notNullValue()));
184+
assertThat(getClass().getSuperclass().getDeclaredMethod(name), is(notNullValue()));
185+
186+
assertThat(privateMethod(), is("subclass version"));
187+
188+
Callable<String> ref1 = this::privateMethod;
189+
assertThat(ref1.call(), is("subclass version"));
190+
191+
Callable<String> ref2 = privateMethodInSuperAsMethodReference();
192+
assertThat(ref2.call(), is("superclass version"));
193+
}
194+
195+
private String privateMethod() {
196+
return "subclass version";
197+
}
167198
}
168199

169200
class SuperClass {
170201
String inheritedMethod() {
171202
return "superclass version";
172203
}
204+
205+
Callable<String> privateMethodInSuperAsMethodReference() {
206+
return this::privateMethod;
207+
}
208+
209+
private String privateMethod() {
210+
return "superclass version";
211+
}
173212
}

0 commit comments

Comments
 (0)