1
2
3
4
5
6
7
8
9
10
11 package psiprobe.tools.logging;
12
13 import java.lang.reflect.InvocationTargetException;
14
15 import org.apache.commons.beanutils.PropertyUtils;
16 import org.apache.commons.lang3.reflect.FieldUtils;
17 import org.apache.commons.lang3.reflect.MethodUtils;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import psiprobe.model.Application;
22
23
24
25
26 public class DefaultAccessor {
27
28
29 protected static final Logger logger = LoggerFactory.getLogger(DefaultAccessor.class);
30
31
32 private Application application;
33
34
35 private Object target;
36
37
38
39
40
41
42 public Application getApplication() {
43 return application;
44 }
45
46
47
48
49
50
51 public void setApplication(Application application) {
52 this.application = application;
53 }
54
55
56
57
58
59
60 public Object getTarget() {
61 return target;
62 }
63
64
65
66
67
68
69 public void setTarget(Object target) {
70 this.target = target;
71 }
72
73
74
75
76
77
78 public String getTargetClass() {
79 return getTarget().getClass().getName();
80 }
81
82
83
84
85
86
87
88
89
90
91
92 protected Object getProperty(Object obj, String name, Object defaultValue, boolean forced) {
93 try {
94 if (forced) {
95 return FieldUtils.readField(obj, name, forced);
96 }
97 return PropertyUtils.isReadable(obj, name) ? PropertyUtils.getProperty(obj, name)
98 : defaultValue;
99 } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException
100 | NoSuchMethodException e) {
101 logger.error("", e);
102 }
103 logger.debug("Could not access property '{}' of object '{}'", name, obj);
104 return defaultValue;
105 }
106
107
108
109
110
111
112
113
114
115
116 protected Object getProperty(Object obj, String name, Object defaultValue) {
117 return getProperty(obj, name, defaultValue, false);
118 }
119
120
121
122
123
124
125
126
127
128
129
130 protected Object invokeMethod(Object object, String name, Object param, Object defaultValue) {
131 try {
132 if (param == null) {
133 return MethodUtils.invokeMethod(object, name);
134 }
135 return MethodUtils.invokeMethod(object, name, param);
136 } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
137 logger.error("", e);
138 }
139 return defaultValue;
140 }
141
142 }