I'm certainly confused when trying to clone an object.
On the one hand and tried to use the interface Cloneable
@Override
protected Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
Consultas consultas=new Consultas();
consultas.id = id;
consultas.idOrg = idOrg;
consultas.idUsu = idUsu;
consultas.consulta = consulta;
consultas.estado = estado;
consultas.fecha = fecha;
consultas.leidoAnt = leidoAnt;
consultas.idPerfil = idPerfil;
consultas.fechaUltima = fechaUltima;
consultas.idTema = idTema;
consultas.ficheroAdjunto = ficheroAdjunto;
consultas.leidoConc = leidoConc;
consultas.marca = marca;
consultas.solicitadoTeamviewer=solicitadoTeamviewer;
return consultas;
}
public Object clonar(){
try {
return this.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
}
Well, this does not help me, because although I copy the object well, when I try to modify some field, I modify it in both objects, copying and copying, so I understand that with clone()
is also copied the reference in memory.
Well, seeing that it does not work, I have ventured to create a small method to clone objects.
public static Object clonar(Object acopiar) {
// TODO Auto-generated method stub
Class<?> clazz;
Object obj=null;
try {
clazz = Class.forName(acopiar.getClass().getName());
obj=clazz.newInstance();
Map<String,Method> mapa=new HashMap<String,Method>();
Method[] metodos=clazz.getDeclaredMethods();
for(Method method2:metodos){
mapa.put(method2.getName(), method2);
}
for(Method method:metodos){
if(method.getName().startsWith("get")){
try{
Method setter;
String nombre=method.getName();
Object value=method.invoke(acopiar);
setter = obj.getClass().getMethod(nombre.replace("get","set"),mapa.get(nombre.replace("get","set")).getParameterTypes());
setter.invoke(obj, value);
}catch(Exception e){
System.out.println("No se ha podido settear el campo: "+method.getName()+" : "+e.getClass()+" --> "+e.getMessage());
}
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e1) {
System.out.println("Exception: "+e1.getClass()+" --> "+e1.getMessage());
}
return obj;
}
Well, even with this method, which creates the object well with all its properties, when I modify the source object, the copy is modified too!
Consultas aux=itConsultas.next();
Consultas copia=(Consultas)Utilidades.clonar(aux);
aux.getId().setIdConsulta(Integer.parseInt(idsSplited[0]));
aux.getId().setIdRespuesta(++lastValue);
copias.add(copia);
This is where I try to copy an object.
If someone told me that I could be doing wrong, or that I was missing, I would appreciate it.
EDIT: I have tried to create a copy-constructor which does not work for me, I still share an instance, I have also overwritten the method clone
as @Fernando told me and they continue to share an instance (although, as Luiggi said, this did not work). Any more suggestions? The truth is that I do not know what else can be ...
Here the code of the constructor-copy to see if something happened to me
public Consultas(Consultas a_copiar) {
this.id = a_copiar.id;
this.idOrg = a_copiar.idOrg;
this.idUsu = a_copiar.idUsu;
this.consulta = a_copiar.consulta;
this.estado = a_copiar.estado;
this.fecha = a_copiar.fecha;
this.leidoAnt = a_copiar.leidoAnt;
this.idPerfil = a_copiar.idPerfil;
this.fechaUltima = a_copiar.fechaUltima;
this.idTema = a_copiar.idTema;
this.ficheroAdjunto = a_copiar.ficheroAdjunto;
this.leidoConc = a_copiar.leidoConc;
this.marca = a_copiar.marca;
this.solicitadoTeamviewer=a_copiar.solicitadoTeamviewer;
}