Last active
August 15, 2016 22:04
-
-
Save Host32/3afbe9fcd7f5e75d5b2a50f1ef62486d to your computer and use it in GitHub Desktop.
Java Annotations, que bruxaria é essa?
This file contains 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 @interface Printable { | |
} |
This file contains 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.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 Printable { | |
} |
This file contains 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.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 Printable { | |
String label(); | |
} |
This file contains 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.Method; | |
import java.util.List; | |
public class Printer { | |
public void print(List<User> users) { | |
System.out.println("Listagem de usuários:"); | |
try { | |
for (User user : users) { | |
StringBuilder userData = new StringBuilder(); | |
for (Method method : user.getClass().getDeclaredMethods()) { | |
if (method.isAnnotationPresent(Printable.class)) { | |
Printable printableMeta = method.getAnnotation(Printable.class); | |
userData.append(printableMeta.label()).append(": ").append(method.invoke(user)).append(", "); | |
} | |
} | |
System.out.println(userData.toString()); | |
} | |
} catch (Exception e) { | |
System.err.println("Error!"); | |
} | |
} | |
} |
This file contains 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 User { | |
// atributos e construtores omitidos | |
public String getName(){ | |
return this.name; | |
} | |
public int getAge(){ | |
return this.age; | |
} | |
public String getEmail(){ | |
return this.email; | |
} | |
public String getPassword(){ | |
return this.password; | |
} | |
} |
This file contains 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 User { | |
// atributos e construtores omitidos | |
@Printable(label = "Nome") | |
public String getName(){ | |
return this.name; | |
} | |
@Printable(label = "Idade") | |
public int getAge(){ | |
return this.age; | |
} | |
@Printable(label = "Email") | |
public String getEmail(){ | |
return this.email; | |
} | |
public String getPassword(){ | |
return this.password; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment