Skip to content

Instantly share code, notes, and snippets.

@alexandreaquiles
Created June 1, 2023 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexandreaquiles/20b4790d17f6a75de7d9ebf286c71a69 to your computer and use it in GitHub Desktop.
Save alexandreaquiles/20b4790d17f6a75de7d9ebf286c71a69 to your computer and use it in GitHub Desktop.
public class Carro {
private String modelo;
private String marca;
private String cor;
private String tipo;
private Integer ano;
public String getModelo() {
return modelo;
}
public void setModelo(String modelo) {
this.modelo = modelo;
}
public String getMarca() {
return marca;
}
public void setMarca(String marca) {
this.marca = marca;
}
public String getCor() {
return cor;
}
public void setCor(String cor) {
this.cor = cor;
}
public String getTipo() {
return tipo;
}
public void setTipo(String tipo) {
this.tipo = tipo;
}
public Integer getAno() {
return ano;
}
public void setAno(Integer ano) {
this.ano = ano;
}
@Override
public String toString() {
return "Carro{" +
"modelo='" + modelo + '\'' +
", marca='" + marca + '\'' +
", cor='" + cor + '\'' +
", tipo='" + tipo + '\'' +
", ano=" + ano +
'}';
}
}
public class CarroController {
public void salva(Carro carro) {
System.out.println("Salvando o carro: " + carro);
}
}
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) throws Exception {
Map<String, String> params = new HashMap<>();
params.put("modelo", "Audi v8");
params.put("marca", "Audi");
params.put("cor", "Prata");
params.put("tipo", "Sedã");
params.put("ano", "2022");
var carroController = new CarroController();
// isso não dá
// Carro carro = params;
// Reflection
Class<Carro> carroClass = Carro.class;
Carro carro = carroClass.getDeclaredConstructor().newInstance();
// Field modelo = carroClass.getDeclaredField("modelo");
// modelo.setAccessible(true);
// modelo.set(carro, params.get("modelo"));
//
// Field marca = carroClass.getDeclaredField("marca");
// marca.setAccessible(true);
// marca.set(carro, params.get("marca"));
//
// Field cor = carroClass.getDeclaredField("cor");
// cor.setAccessible(true);
// cor.set(carro, params.get("cor"));
//
// Field tipo = carroClass.getDeclaredField("tipo");
// tipo.setAccessible(true);
// tipo.set(carro, params.get("tipo"));
params.keySet().forEach(key -> {
try {
Field field = carroClass.getDeclaredField(key);
field.setAccessible(true);
String value = params.get(key);
Class<?> fieldType = field.getType();
if (fieldType.equals(String.class)) {
field.set(carro, value);
} else if (fieldType.equals(Integer.class)) {
Integer integerValue = Integer.valueOf(value);
field.set(carro, integerValue);
} else {
throw new RuntimeException("Unsupported type" + fieldType);
}
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
});
carroController.salva(carro);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment