mardi 25 juin 2019

Access Attribute of an Object with an Attribute from another Object with Java Reflection

I have a class with attributes such as

class A {
 public Integer att1;
 public Integer att2;
,...
}

Further, I have an instance of it:

A a1=new A();

Now, I want to write a method which sets the attribute of a1, something like that:

setAttribute((new A()).att1,false);

public void setAttribute(Boolean b, boolean newValue){ 
 //has reference to object a1
 //magic: this method sets the attribute of the object a1 (!) but gets the Boolean attribute from another object
}

The setAttribute method gets an attribute of an A instance and sets the newValue to the same attribute of ANOTHER object. How can I realize such a method? I am looking for something like an attribute Id....





Aucun commentaire:

Enregistrer un commentaire