Reflexão e Anotações no mundo Java
-
Upload
mateus-parente -
Category
Software
-
view
78 -
download
5
Transcript of Reflexão e Anotações no mundo Java
![Page 1: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/1.jpg)
Reflexão e AnotaçõesMateus Parente
![Page 2: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/2.jpg)
REAPROVEITAR
![Page 3: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/3.jpg)
REUTILIZAR
![Page 4: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/4.jpg)
public class Funcionario {
private String nome;
private String endereco;
private Integer idade;
private BigDecimal salario;
...
}
![Page 5: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/5.jpg)
VALIDADOR
![Page 6: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/6.jpg)
VALIDADOR!= null
![Page 7: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/7.jpg)
![Page 8: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/8.jpg)
TIPOS DE SOLUÇÕESsofisticada
gambiarracriativa
![Page 9: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/9.jpg)
public class Validador {
public List<String> validar(Funcionario funcionario){List<String> invalidos = new ArrayList<>();if(funcionario.getNome() == null)
invalidos.add("nome");
if(funcionario.getEndereco() == null)invalidos.add("endereco");
return invalidos;}
}
![Page 10: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/10.jpg)
PROBLEMAS
![Page 11: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/11.jpg)
PROBLEMASnome de atributo
![Page 12: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/12.jpg)
PROBLEMASnome de atributo
só funciona para Funcionario
![Page 13: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/13.jpg)
PROBLEMASnome de atributo
só funciona para Funcionarioinclusão de atributo
![Page 14: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/14.jpg)
PROBLEMASnome de atributo
só funciona para Funcionarioinclusão de atributo
código extenso
![Page 15: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/15.jpg)
public class Validador {
public List<String> validar(Funcionario funcionario){List<String> invalidos = new ArrayList<>();
if(funcionario.getNome() == null)invalidos.add("nome");
if(funcionario.getEndereco() == null)invalidos.add("endereco");
if(funcionario.getTelefone() == null)invalidos.add("telefone");
if(funcionario.getDataDeNascimento() == null)invalidos.add("dataDeNascimento");
if(funcionario.getIdade() == null)invalidos.add("idade");
return invalidos;
}
}
![Page 16: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/16.jpg)
HORA DE REFATOR
AR
![Page 17: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/17.jpg)
MISSÃOdesconhecer meus atributos
![Page 18: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/18.jpg)
public class Validador {
public List<String> validar(Funcionario funcionario){List<String> invalidos = new ArrayList<>();
Atributo[] atributos = funcionario.getAtributos();for(Atributo atributo : atributos){
if(atributo.getValue() == null)invalidos.add(atributo.getNome());
}return invalidos;
}
}
![Page 19: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/19.jpg)
ProbleminhaAtributos não estão no Objeto, e sim, na Classe.
![Page 20: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/20.jpg)
public class Validador {
public List<String> validar(Funcionario funcionario){List<String> invalidos = new ArrayList<>();
Classe classeDoFuncionario = funcionario.getClasse();Atributo[] atributos = classeDoFuncionario.getAtributos();for(Atributo atributo : atributos){
if(atributo.getValue() == null)invalidos.add(atributo.getNome());
}return invalidos;
}
}
![Page 21: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/21.jpg)
Outro ProbleminhaValor dos Atributos não estão na classe, e sim, no Objeto
![Page 22: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/22.jpg)
public class Validador {
public List<String> validar(Funcionario funcionario){List<String> invalidos = new ArrayList<>();
Classe classeDoFuncionario = funcionario.getClasse();Atributo[] atributos = classeDoFuncionario.getAtributos();for(Atributo atributo : atributos){
Object valor = atributo.buscar(funcionario);if(valor == null)
invalidos.add(atributo.getNome());}return invalidos;
}
}
![Page 23: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/23.jpg)
VAMOS A REALIDADE
![Page 24: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/24.jpg)
Class
java.lang.Class
![Page 25: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/25.jpg)
Classfuncionario.getClass();Funcionario.class;Class.forName(“br.com.javou.Funcionario”)
![Page 26: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/26.jpg)
FIELDjava.lang.reflect.Field
![Page 27: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/27.jpg)
Fieldclazz.getFields();clazz.getDeclaredFields();
![Page 28: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/28.jpg)
Adaptando...
![Page 29: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/29.jpg)
public class Validador {
public List<String> validar(Funcionario funcionario){List<String> invalidos = new ArrayList<>();
Class<?> classeDoFuncionario = funcionario.getClass();Field[] atributos = classeDoFuncionario.getFields();for(Field atributo : atributos){
Object valor = atributo.get(funcionario);if(valor == null)
invalidos.add(atributo.getName());}return invalidos;
}
}
![Page 30: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/30.jpg)
Pra variar... ProbleminhaclasseDoFuncionario.getFields()
ESTÁ VAZIA
![Page 31: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/31.jpg)
getFields()x
getDeclaredFields()
Class
![Page 32: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/32.jpg)
getFields()x
getDeclaredFields()
Classpublicos + superclass
publicos + privados
![Page 33: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/33.jpg)
public class Validador {
public List<String> validar(Funcionario funcionario){List<String> invalidos = new ArrayList<>();
Class<?> classeDoFuncionario = funcionario.getClass();Field[] atributos = classeDoFuncionario.getDeclaredFields();for(Field atributo : atributos){
Object valor = atributo.get(funcionario);if(valor == null)
invalidos.add(atributo.getName());}return invalidos;
}
}
![Page 34: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/34.jpg)
java.lang.IllegalAccessException: Class br.com.reflections.validadores.Validador can not access a member of class br.com.reflections.models.Funcionario with modifiers "private".
![Page 35: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/35.jpg)
…public List<String> validar(Funcionario funcionario){
List<String> invalidos = new ArrayList<>();
Class<?> classeDoFuncionario = funcionario.getClass();Field[] atributos = classeDoFuncionario.getDeclaredFields();for(Field atributo : atributos){
atributo.setAccessible(true);Object valor = atributo.get(funcionario);if(valor == null)
invalidos.add(atributo.getName());}return invalidos;
}
![Page 36: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/36.jpg)
public class Funcionario {
private String nome;
private String endereco;
private Integer idade;
private BigDecimal salario;
...
}
![Page 37: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/37.jpg)
public void test(){
Funcionario funcionario = new Funcionario();funcionario.setNome(“Mateus”);funcionario.setEndereco(“Lagoa Redonda”);
Validador validador = new Validador();List<String> erros = validador.valida(funcionario);
assertTrue(erros.contains(“idade”));assertTrue(erros.contains(“salario”));assertFalse(erros.contains(“nome”));assertFalse(erros.contains(“endereco”));
}
@Test
![Page 38: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/38.jpg)
Tudo funcionando
![Page 39: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/39.jpg)
HORA DE REFATOR
AR
![Page 40: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/40.jpg)
…public List<String> validar(Object entidade){
List<String> invalidos = new ArrayList<>();
Class<?> clazz = entidade.getClass();Field[] atributos = clazz.getDeclaredFields();for(Field atributo : atributos){
atributo.setAccessible(true);Object valor = atributo.get(entidade);if(valor == null)
invalidos.add(atributo.getName());}return invalidos;
}
![Page 41: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/41.jpg)
COMPONENTESREUTILIZÁVEIS
![Page 42: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/42.jpg)
PERSONALIZAR
MENSAGENS
![Page 43: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/43.jpg)
public class Funcionario {
private String Nome;
private String Endereço;
private Integer Idade;
private BigDecimal Salário;
...
}
![Page 44: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/44.jpg)
ANOTAÇÕES
![Page 45: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/45.jpg)
@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)public @interface Validacao {
String descricao();
}
![Page 46: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/46.jpg)
public class Funcionario {
@Validacao(descricao = ‘Nome completo’)
private String nome;
@Validacao(descricao = ‘Endereço’)
private String endereco;
…
}
![Page 47: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/47.jpg)
…Class<?> clazz = entidade.getClass();
Field[] atributos = clazz.getDeclaredFields();for(Field atributo : atributos){
atributo.setAccessible(true);Object valor = atributo.get(entidade);if(valor == null) {
if(atributo.isAnnotationPresent(Validacao.class)){Validacao validacao =
atributo.getAnnotation(Validacao.class);invalidos.add(validacao.descricao());
}}
}
![Page 48: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/48.jpg)
desvantagens
![Page 49: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/49.jpg)
complexidade /
performance
![Page 50: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/50.jpg)
obrigado
![Page 51: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/51.jpg)
Perguntas
![Page 52: Reflexão e Anotações no mundo Java](https://reader034.fdocumentos.tips/reader034/viewer/2022042618/589e27f41a28ab5c128b4cd5/html5/thumbnails/52.jpg)
Referencias:Componentes Reutilizáveis em Java com Reflexão e Anotações - Eduardo Guerra
Laboratório Java com Testes, XML e Design Patterns - Caelum