What happens is that I have classes with different names but with exactly the same attributes and methods. What happens is that one comes from a request class that was generated by itself when consuming a Web Service and the other is from a class that was automatically generated when compiling my screen by other methods.
In itself it is this. I have a class called Class1 and a class called Class2. Both classes have the same attributes and the same getters and setter, for example:
public class Clase1 implements java.io.Serializable
{
private java.lang.String variableId;
private java.lang.String variableNombre;
//Sus getters y setters
}
public class Clase2 implements java.io.Serializable
{
private java.lang.String variableId;
private java.lang.String variableNombre;
//Sus getters y setters
}
Now I need to create an object of Class1 from the values of the attributes of Class2
Plus like this (manually):
Clase1 obj = new Clase1();
Clase2 obj2 = new Clase2();
obj.setVariableId(obj2.getVariableId);
obj.setVariableNombre(obj2.getVariableNombre);
Is there a way to pass the values from one side to another in the form of casting or with some trick and not one by one? (because they are not really 2 attributes, there are many and some with an array of objects inside, etc). I repeat, both classes are exactly the same, the only thing that differentiates them is the package where they are found and the name that these classes have.
You can use an external library with a utility to do what you require.
For example, I recommend BeanUtils from SpringFramework where you would have something like this:
This will try to copy matching properties from obj to obj2.
https://www.logicbig.com/how-to/code-snippets/jcode-spring-framework-beanutils.html
I indicate three options, two of which you will not like and the last one is the ñapa that will serve you
Refactor the code to only use the class that they give you, that way you don't have duplicate classes with an f control and a rename of all the code from one class to another will work if they are both the same. ( I don't know which editor you're using, but they all allow you to refactor classes and rename them by applying it to all code with three clicks )
the second option is to create a method that receives the object of the first class as a parameter and converts it (it is a more tedious way and I like it less because of the code)
the third option has a higher cost of resources and would be transforming it even Json ( conversion documentation ) to later deserialize it in the other object