@@ -135,6 +135,11 @@ public void method_references_to_private_methods() throws Exception {
135
135
136
136
Callable <String > ref2 = this ::privateInstanceMethod ;
137
137
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" ));
138
143
}
139
144
140
145
private String privateInstanceMethod () {
@@ -146,7 +151,7 @@ private static String privateClassMethod() {
146
151
}
147
152
148
153
/**
149
- * We must make private lambda implementation methods package-private,
154
+ * We could make private lambda implementation methods package-private,
150
155
* so that the lambda class may call them, but we should not make any
151
156
* more methods non-private than is absolutely necessary.
152
157
*/
@@ -164,10 +169,44 @@ public void will_not_change_the_visibility_of_unrelated_methods() throws Excepti
164
169
private String unrelatedPrivateMethod () {
165
170
return "foo" ;
166
171
}
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
+ }
167
198
}
168
199
169
200
class SuperClass {
170
201
String inheritedMethod () {
171
202
return "superclass version" ;
172
203
}
204
+
205
+ Callable <String > privateMethodInSuperAsMethodReference () {
206
+ return this ::privateMethod ;
207
+ }
208
+
209
+ private String privateMethod () {
210
+ return "superclass version" ;
211
+ }
173
212
}
0 commit comments