Skip to content

Instantly share code, notes, and snippets.

@jelinski
Created September 7, 2018 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jelinski/57407dd687bd421655691c50dbe9e4a0 to your computer and use it in GitHub Desktop.
Save jelinski/57407dd687bd421655691c50dbe9e4a0 to your computer and use it in GitHub Desktop.
Draft for code that will be used to demonstrate how changing lambda to method reference can change the program behaviour
package pl.jellysoft;
import org.junit.Test;
import java.util.Arrays;
public class MethodReferenceSemanticChangeTest {
// final will solve it
private BusinessObject businessObject = new BusinessObject();
@Test
public void semanticChangeTest() {
//BusinessObject businessObject = new BusinessObject();
Arrays.asList("One", "Two", "Three")
.forEach(o -> businessObject.print(o));
// .forEach(businessObject::print);
}
private class BusinessObject {
Object getObject() {
return null;
}
void print(Object o) {
System.out.println(o.toString());
businessObject = new BusinessObjectDifferentImpl();
}
}
private class BusinessObjectDifferentImpl extends BusinessObject {
@Override
void print(Object o) {
super.print("Prefix:" + o);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment