Created
June 1, 2023 16:17
-
-
Save alexandreaquiles/20b4790d17f6a75de7d9ebf286c71a69 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + | |
'}'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CarroController { | |
public void salva(Carro carro) { | |
System.out.println("Salvando o carro: " + carro); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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