Skip to content

Commit

Permalink
Test to reproduce #19
Browse files Browse the repository at this point in the history
  • Loading branch information
luontola committed May 18, 2014
1 parent d213752 commit 9bc1def
Showing 1 changed file with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ public void method_references_to_private_methods() throws Exception {

Callable<String> ref2 = this::privateInstanceMethod;
assertThat(ref2.call(), is("foo"));

// Normal method calls should still work after our magic
// of making them them accessible from the lambda classes.
assertThat(privateClassMethod(), is("foo"));
assertThat(privateInstanceMethod(), is("foo"));
}

private String privateInstanceMethod() {
Expand All @@ -146,7 +151,7 @@ private static String privateClassMethod() {
}

/**
* We must make private lambda implementation methods package-private,
* We could make private lambda implementation methods package-private,
* so that the lambda class may call them, but we should not make any
* more methods non-private than is absolutely necessary.
*/
Expand All @@ -164,10 +169,44 @@ public void will_not_change_the_visibility_of_unrelated_methods() throws Excepti
private String unrelatedPrivateMethod() {
return "foo";
}

/**
* We cannot just make the private methods package-private for the
* lambda class to call them, because that may cause a method in subclass
* to override them.
*/
@Test
public void will_not_cause_private_methods_to_be_overridable() throws Exception {
// Our test assumes that there exists a private method with
// the same name and signature in super and sub classes.
String name = "privateMethod";
assertThat(getClass().getDeclaredMethod(name), is(notNullValue()));
assertThat(getClass().getSuperclass().getDeclaredMethod(name), is(notNullValue()));

assertThat(privateMethod(), is("subclass version"));

Callable<String> ref1 = this::privateMethod;
assertThat(ref1.call(), is("subclass version"));

Callable<String> ref2 = privateMethodInSuperAsMethodReference();
assertThat(ref2.call(), is("superclass version"));
}

private String privateMethod() {
return "subclass version";
}
}

class SuperClass {
String inheritedMethod() {
return "superclass version";
}

Callable<String> privateMethodInSuperAsMethodReference() {
return this::privateMethod;
}

private String privateMethod() {
return "superclass version";
}
}

0 comments on commit 9bc1def

Please sign in to comment.