fork(1) download
  1. import java.io.Serializable;
  2. import java.lang.annotation.Annotation;
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.InvocationTargetException;
  5. import java.lang.reflect.Method;
  6. import java.lang.reflect.Modifier;
  7.  
  8. @Deprecated
  9. class ObjetoTesteReflection implements Serializable {
  10.  
  11. private static final long serialVersionUID = 1L;
  12.  
  13. private Long id;
  14. private String nome;
  15.  
  16. public Long getId() {
  17. return id;
  18. }
  19.  
  20. public void setId(Long id) {
  21. this.id = id;
  22. }
  23.  
  24. public String getNome() {
  25. return nome;
  26. }
  27.  
  28. public void setNome(String nome) {
  29. this.nome = nome;
  30. }
  31. }
  32.  
  33. class TestMainClass {
  34.  
  35. public static void main(String[] args) throws IllegalArgumentException,
  36.  
  37. ObjetoTesteReflection obj = new ObjetoTesteReflection();
  38.  
  39. obj.setId(13L);
  40. obj.setNome("Objeto para testes.");
  41.  
  42. metodoDeTeste(obj);
  43.  
  44. }
  45.  
  46. private static void metodoDeTeste(Object obj) throws SecurityException,
  47.  
  48. // Pegando a classe do objeto a manipular
  49. Class<?> clazz = obj.getClass();
  50.  
  51. System.out.println("Classe:");
  52. System.out.println("Nome: " + clazz.getName());
  53. System.out.println("Anotações: ");
  54. for (Annotation a : clazz.getAnnotations()) {
  55. System.out.println("\tNome: " + a.getClass().getName());
  56. }
  57. System.out.println();
  58.  
  59. System.out.println("Atributos:");
  60.  
  61. // Printando os atributos dos atributos da classe na tela
  62. for (Field field : clazz.getDeclaredFields()) {
  63. System.out.print(Modifier.toString(field.getModifiers()));
  64. System.out.print(" " + field.getType().getName());
  65. System.out.println(" " + field.getName());
  66.  
  67. // Liberando acesso ao valor do atributo
  68. // field.setAccessible(true);
  69. // System.out.println(" = " + field.get(obj));
  70. // field.setAccessible(false);
  71. }
  72.  
  73. System.out.println("Métodos:");
  74.  
  75. for (Method m : clazz.getMethods()) {
  76. System.out.print(Modifier.toString(m.getModifiers()));
  77. System.out.print(" " + m.getName());
  78. System.out.print(" " + m.getName());
  79.  
  80. // Buscando o tipo dos parâmetros
  81. Class<?>[] params = m.getParameterTypes();
  82.  
  83. // Verificando se é um método get (não precisamos aumentar a
  84. // complexidade do exemplo)
  85. if (m.getName().startsWith("get")) {
  86. // No nosso caso sabemos que o método não recebe parâmetros
  87. // Também existe m.invoke(Object objeto,Object... args)
  88. System.out.println(" = " + m.invoke(obj));
  89. }else{
  90. System.out.println();
  91. }
  92. }
  93. }
  94. }
stdin
Standard input is empty
compilation info
Note: Main.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
stdout
Classe:
Nome: ObjetoTesteReflection
Anotações: 
	Nome: $Proxy3

Atributos:
private static final long serialVersionUID
private java.lang.Long id
private java.lang.String nome
Métodos:
public setId setId
public setNome setNome
public getNome getNome = Objeto para testes.
public getId getId = 13
public final native wait wait
public final wait wait
public final wait wait
public native hashCode hashCode
public final native getClass getClass = class ObjetoTesteReflection
public equals equals
public toString toString
public final native notify notify
public final native notifyAll notifyAll