Skip to content

Instantly share code, notes, and snippets.

@carlosdlf
Created May 24, 2019 20:26
Show Gist options
  • Save carlosdlf/5d1d8363164dc0518fae1b429e9ef021 to your computer and use it in GitHub Desktop.
Save carlosdlf/5d1d8363164dc0518fae1b429e9ef021 to your computer and use it in GitHub Desktop.
package reflection;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MAin {
public static void main(String[] args) {
DemoGis objeto = new DemoGis();
objeto.setNombre("Carlos");
objeto.setEdad(12);
objeto.setSaldo(12.2);
List<String> lst = new ArrayList<>();
lst.add("apodo 1");
lst.add("apodo 2");
lst.add("apodo 3");
objeto.setApodos(lst);
List<Log> logInformation = getLogInformation(DemoGis.class, objeto);
logInformation.stream().forEach(x -> System.out.println(x));
System.out.println("*******************************************************************************************");
System.out.println("Despues del cambio ");
System.out.println("*******************************************************************************************");
// update DemoGis
objeto.setNombre("Carlos Daniel Larico Flores");
List<Log> logInformationafter = getLogInformation(DemoGis.class, objeto);
logInformationafter.stream().forEach(x -> System.out.println(x));
}
public static List<Log> getLogInformation(Class c, Object objeto){
List<Log> result = new ArrayList<>();
Method[] declaredMethods = c.getDeclaredMethods();
Arrays.stream(declaredMethods).forEach(
x -> {
try {
if(x.getName().startsWith("get")){
Object obj = x.invoke(objeto, null);
Log l = new Log();
l.setAttributo(x.getName());
l.setTipoData(obj.getClass().getName());
l.setValor(obj);
result.add(l);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
);
return result;
}
public static class Log {
private String attributo;
private String tipoData;
private Object valor;
@Override
public String toString() {
return "Log{" +
"attributo='" + attributo + '\'' +
", tipoData='" + tipoData + '\'' +
", valor='" + valor + '\'' +
'}';
}
public String getAttributo() {
return attributo;
}
public void setAttributo(String attributo) {
this.attributo = attributo;
}
public String getTipoData() {
return tipoData;
}
public void setTipoData(String tipoData) {
this.tipoData = tipoData;
}
public Object getValor() {
return valor;
}
public void setValor(Object valor) {
this.valor = valor;
}
}
}
package reflection;
import java.util.List;
public class DemoGis {
private String nombre;
private int edad;
private Double saldo;
private List<String> apodos;
public DemoGis() {
}
public DemoGis(String nombre, int edad, Double saldo, List<String> apodos) {
this.nombre = nombre;
this.edad = edad;
this.saldo = saldo;
this.apodos = apodos;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public int getEdad() {
return edad;
}
public void setEdad(int edad) {
this.edad = edad;
}
public Double getSaldo() {
return saldo;
}
public void setSaldo(Double saldo) {
this.saldo = saldo;
}
public List<String> getApodos() {
return apodos;
}
public void setApodos(List<String> apodos) {
this.apodos = apodos;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment