Skip to content

Instantly share code, notes, and snippets.

@rodrigovilar
Created April 8, 2019 16:21
Show Gist options
  • Save rodrigovilar/aa36137c65e5ce1fbc66d00157d9fe52 to your computer and use it in GitHub Desktop.
Save rodrigovilar/aa36137c65e5ce1fbc66d00157d9fe52 to your computer and use it in GitHub Desktop.
Reflection - Embelezator
public class Cliente {
private long cpf;
private long cnpj;
private String nome;
private String slogan;
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
@Gritar
public String getSlogan() {
return slogan;
}
public void setSlogan(String slogan) {
this.slogan = slogan;
}
@Mask("###.###.###-##")
public long getCpf() {
return cpf;
}
public void setCpf(long cpf) {
this.cpf = cpf;
}
@Mask("##.###.###/####-##")
public long getCnpj() {
return cnpj;
}
public void setCnpj(long cnpj) {
this.cnpj = cnpj;
}
}
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.ParseException;
import javax.swing.text.MaskFormatter;
public class Embelezator {
public Object get(Object object, String propriedade) throws Exception {
Method method =
object.getClass().getMethod(getNomeMetodo(propriedade));
Object original = method.invoke(object); //object.nomeDoMetodo()
if (method.isAnnotationPresent(Gritar.class)) {
return aplicarGritar(original);
}
if (method.isAnnotationPresent(Mask.class)) {
return aplicarMask(method, original);
}
return original;
}
protected Object aplicarMask(Method method, Object original) throws ParseException {
Mask maskAnnotation = method.getAnnotation(Mask.class);
String mascara = maskAnnotation.value();
String s = original.toString();
MaskFormatter mf = new MaskFormatter(mascara);
mf.setValueContainsLiteralCharacters(false);
mf.setPlaceholderCharacter('0');
return mf.valueToString(s);
}
protected String aplicarGritar(Object original) {
return ((String) original).toUpperCase();
}
protected String getNomeMetodo(String propriedade) {
String nomeDoMetodo = "get" +
propriedade.substring(0, 1).toUpperCase() +
propriedade.substring(1, propriedade.length());
return nomeDoMetodo;
}
public String toString(Object object) throws Exception {
String resultado = "";
Field[] campos = object.getClass().getDeclaredFields();
for (Field field : campos) {
resultado += get(object, field.getName()) + ", ";
}
return resultado;
}
}
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class EmbezatorTest {
private Embelezator embelezator;
private Cliente cliente;
private Fornecedor fornecedor;
@Before
public void init() {
embelezator = new Embelezator();
cliente = new Cliente();
cliente.setNome("Ruan");
cliente.setSlogan("Malakoi");
cliente.setCpf(12345678901L); //123.456.789-01
cliente.setCnpj(12345678901234L);//12.345.678/9012-34
fornecedor = new Fornecedor();
fornecedor.setCEP(12345678);
fornecedor.setNomeFantasia("Tekfin");
}
@Test
public void semAnotacao() throws Exception {
assertEquals("Ruan",
embelezator.get(cliente, "nome"));
}
@Test
public void gritar() throws Exception {
assertEquals("MALAKOI",
embelezator.get(cliente, "slogan"));
}
@Test
public void formataMask() throws Exception {
assertEquals("123.456.789-01",
embelezator.get(cliente, "cpf"));
assertEquals("12.345.678/9012-34",
embelezator.get(cliente, "cnpj"));
}
@Test
public void formataMaskMenor() throws Exception {
cliente.setCpf(12345L); //123.450.000-00
assertEquals("123.450.000-00",
embelezator.get(cliente, "cpf"));
}
@Test
public void fornecedor() throws Exception {
assertEquals("12345-678",
embelezator.get(fornecedor, "CEP"));
assertEquals("TEKFIN",
embelezator.get(fornecedor, "nomeFantasia"));
}
@Test
public void testToString() throws Exception {
assertEquals("123.456.789-01, 12.345.678/9012-34, Ruan, MALAKOI, ",
embelezator.toString(cliente));
assertEquals("TEKFIN, 12345-678, ", embelezator.toString(fornecedor));
}
}
public class Fornecedor {
private String nomeFantasia;
private int CEP;
@Gritar
public String getNomeFantasia() {
return nomeFantasia;
}
public void setNomeFantasia(String nomeFantasia) {
this.nomeFantasia = nomeFantasia;
}
@Mask("#####-###")
public int getCEP() {
return CEP;
}
public void setCEP(int cEP) {
CEP = cEP;
}
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Gritar {
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Mask {
String value();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment