Issue
How can I reparse java.util.function.Consumer instance and retrieve arguments and values of its lambda expression (forexample "student.person.surname"). Shortly I want to retrieve lambda expression (Consumer) as a text at runtime.
import lombok.Data;
import java.util.function.Consumer;
public class ConsumerTest {
@Data
public static class Person{
private Integer id;
private String name;
private String surName;
}
@Data
public static class Student{
private Integer id;
private Person person;
}
public static void main(String args[])
{
Student student = new Student();
student.setId(1);
Person person = new Person();
person.setName("Ali");
person.setSurName("Veli");
person.setId(2);
student.setPerson(person);
Consumer<Student> displayLambda = s -> s.getPerson().setSurName("Gulsoy");
displayLambda.accept(student);
//I want to reparse displaylambda instance and print arguments.
//As here I must be able to retrieve "student.person.surname" and "Gulsoy"
}
}
Solution
Thanks a lot for the answers. I have found a way to my problem by using de.danielbechler.diff.ObjectDifferBuilder even though I haven't read lambda expression exactly.
Before running consumer accept method, student object is cloned and after executing displayLambda.accept(student) we can get the difference between changed student object and previous student object. And so we can catch changed parameter and value as seen below.
import de.danielbechler.diff.ObjectDifferBuilder;
import de.danielbechler.diff.node.DiffNode;
import de.danielbechler.diff.node.Visit;
import lombok.Data;
import java.util.function.Consumer;
public class ConsumerTest {
@Data
public static class Person implements Cloneable{
private Integer id;
private String name;
private String surName;
public Person clone() throws CloneNotSupportedException {
Person clonedObj = (Person) super.clone();
clonedObj.name = new String(this.name);
clonedObj.surName = new String(this.surName);
clonedObj.id = new Integer(id);
return clonedObj;
}
}
@Data
public static class Student implements Cloneable{
private Integer id;
private Person person;
public Student clone() throws CloneNotSupportedException {
Student clonedObj = (Student) super.clone();
clonedObj.id = new Integer(id);
clonedObj.person = this.person.clone();
return clonedObj;
}
}
public static void main(String args[])
{
Consumer<Student> displayLambda = s -> s.getPerson().setSurName("Gülsoy");
Student student = new Student();
student.setId(1);
Person person = new Person();
person.setName("Ali");
person.setSurName("Veli");
person.setId(2);
student.setPerson(person);
Student preStudent=null;
// Before running consumer accept method, clone unchanged student object
try {
preStudent = student.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
// executing consumer lambda expression with accept method
displayLambda.accept(student);
// Checking difference with new Student instance and previous Student instance
DiffNode diff = ObjectDifferBuilder.buildDefault().compare(preStudent, student);
if (diff.hasChanges()) {
Student finalPreStudent = preStudent;
diff.visit((node, visit) -> {
if (!node.hasChildren()) { // Only print if the property has no child
final Object oldValue = node.canonicalGet(finalPreStudent);
final Object newValue = node.canonicalGet(student);
// By doing this way we can catch parameter name and changing value
final String message = node.getPropertyName() + " changed from " +
oldValue + " to " + newValue;
System.out.println(message);
}
});
} else {
System.out.println("No differences");
}
}
}
Answered By - zekai Answer Checked By - Terry (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.