Reflection

getClass()

Here is the jdk1.7 doc.

  [java]
1
public Class<?>[] getClasses()

The actual result type is Class<? extends |X|> where |X| is the erasure of the static type of the expression on which getClass is called.

toString()

  • Here is a ReflectionUtil.java helps you say goodbye to toString();
  [java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.ryo.util; import java.beans.IntrospectionException; import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * Created by houbinbin on 16/5/21. */ public class ReflectionUtil { /** * base toString() method; * @param thisObj * @return */ public static String toString(Object thisObj) { Class clazz = thisObj.getClass(); String entityName = clazz.getSimpleName(); Field fields[] = clazz.getDeclaredFields(); StringBuilder stringBuilder = new StringBuilder(String.format("%s{", entityName)); for(int i = 0; i < fields.length-1; i++) { Field field = fields[i]; stringBuilder.append(buildFieldValue(thisObj, field)+","); } stringBuilder.append(buildFieldValue(thisObj, fields[fields.length-1])); stringBuilder.append("}"); return stringBuilder.toString(); } /** * build "field=fieldValue" * @param object * @param field * @return */ private static String buildFieldValue(Object object, Field field) { final String format = isType(field, String.class) ? "%s='%s'" : "%s=%s"; StringBuilder stringBuilder = new StringBuilder(); Method getMethod = getGetMethod(object.getClass(), field); try { Object fieldValue = getMethod.invoke(object); stringBuilder = new StringBuilder(String.format(format, field.getName(), fieldValue)); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return stringBuilder.toString(); } /** * get the Get() of current field; * @param clazz * @param field * @return */ private static Method getGetMethod(Class clazz, Field field) { PropertyDescriptor propertyDescriptor = null; try { propertyDescriptor = new PropertyDescriptor(field.getName(), clazz); } catch (IntrospectionException e) { e.printStackTrace(); } return propertyDescriptor.getReadMethod(); } /** * adjust just field is the type of * @param field * @param classType * @return */ private static Boolean isType(Field field, Class classType) { return field.getType().equals(classType); } }
  • Student.java
  [java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/** * Created by houbinbin on 16/5/20. */ public class Student extends Base { private Long id; private String name; private int score; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public Student() { } public Student(String name, int score) { this.name = name; this.score = score; } @Override public String toString() { return ReflectionUtil.toString(this); } }

or you can toString() in the base model in your project.

  • StudentTest.java
  [java]
1
2
3
4
5
6
7
8
9
public class StudentTest { @Test public void testToString() { Student student = new Student("hello", 30); System.out.println(student); } }
  • result is
  [plaintext]
1
2
3
Student{id=null,name='hello',score=30} Process finished with exit code 0

lombok

By integrating into the IDE, Project Lombok is able to inject code that is immediately available to the developer.

lombok doc

  • add jar
  [xml]
1
2
3
4
5
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.8</version> </dependency>
  • install plugin in idea.
  [plaintext]
1
IntelliJ IDEA->Prefercence->plugins

search for , install and restart.