自定义注解

 1 @Taget({ElementType.METHOD, ElementType.TYPE})
 2 
 3     CONSTRUCTOR 构造方法
 4     FIELD 字段声明
 5 
 6     LOCAL_VARIABLE 局部变量
 7 
 8     METHOD 方法声明
 9 
10     PACKAGE 包声明
11 
12     PARAMENTER 参数声明
13 
14     TYPE 类或接口
15 
16 @Retention(RetentionPolicy.RUNTIME)
17 
18     SOURCE 只在源码显示,编译时会丢弃
19 
20     CLASS 编译时会记录到class中,运行时忽略
21 
22     RUNTIME 运行时存在,可以通过反射读取
23 
24 @Inherited
25 
26     允许自雷继承
27 
28 @Documented
29 
30     生成javadoc时会包含注解
 1 /**
 2  * Description: ToStringIgnore
 3  * 这个方法表示ToString方法忽略该字段
 4  */
 5 @Retention(RetentionPolicy.RUNTIME)
 6 @Target({ ElementType.FIELD })
 7 @Documented
 8 public @interface ToStringIgnore {
 9 
10 }
 1 public class ToStringUtils {
 2 
 3     private static Joiner joiner = Joiner.on(", \n");
 4 
 5     public static String toString(Object o) {
 6         String str = o.getClass().getSimpleName() + " { \n";
 7         Field[] declaredFields = o.getClass().getDeclaredFields();
 8         List<String> fields = Lists.newArrayList();
 9         if (declaredFields != null && declaredFields.length != 0) {
10             for (Field item : declaredFields) {
11                 if (null != item.getAnnotation(ToStringIgnore.class)) {
12                     continue;
13                 }
14                 item.setAccessible(true);
15                 String field = "";
16                 try {
17                     field = item.getName() + ":" + item.get(o);
18                 } catch (IllegalAccessException ignore) {}
19                 fields.add(field);
20             }
21         }
22         str += joiner.join(fields) + " \n} ";
23         return str;
24     }
25 }
 1 public class User implements Serializable {
 2 
 3     private static final long serialVersionUID = -5334408912188522583L;
 4 
 5     private int id;
 6 
 7     private String username;
 8 
 9     @ToStringIgnore
10     private String password;
11 
12     private String firstName;
13 
14     private int age;
15 
16     private boolean male;
17 
18     public User(int id, String username, String password, String firstName, int age, boolean male) {
19         this.id = id;
20         this.username = username;
21         this.password = password;
22         this.firstName = firstName;
23         this.age = age;
24         this.male = male;
25     }
26 
27     public int getId() {
28         return id;
29     }
30 
31     public void setId(int id) {
32         this.id = id;
33     }
34 
35     public String getUsername() {
36         return username;
37     }
38 
39     public void setUsername(String username) {
40         this.username = username;
41     }
42 
43     public String getPassword() {
44         return password;
45     }
46 
47     public void setPassword(String password) {
48         this.password = password;
49     }
50 
51     public String getFirstName() {
52         return firstName;
53     }
54 
55     public void setFirstName(String firstName) {
56         this.firstName = firstName;
57     }
58 
59     public int getAge() {
60         return age;
61     }
62 
63     public void setAge(int age) {
64         this.age = age;
65     }
66 
67     public boolean isMale() {
68         return male;
69     }
70 
71     public void setMale(boolean male) {
72         this.male = male;
73     }
74 
75     @Override
76     public String toString() {
77         return "User{" +
78                 "id=" + id +
79                 ", username='" + username + '\'' +
80                 ", password='" + password + '\'' +
81                 ", firstName='" + firstName + '\'' +
82                 ", age=" + age +
83                 ", male=" + male +
84                 '}';
85     }
86 }
    @Test
    public void test() {
        String s = ToStringUtils.toString(new User(1, "admin", "admin", "asldas", 1, true));
        logger.info("{}", s);
    }

 

posted @ 2016-08-22 23:02  走在大牛的路上  阅读(262)  评论(0编辑  收藏  举报