Repository navigation
Expand file tree
/
Copy pathBaseObjectHandler.java
More file actions
112 lines (103 loc) · 3.73 KB
/
Copy pathBaseObjectHandler.java
File metadata and controls
112 lines (103 loc) · 3.73 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.github.mustachejava.reflect;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Locale;
public abstract class BaseObjectHandler extends AbstractObjectHandler {
protected Field getField(Class aClass, String name) throws NoSuchFieldException {
Field member;
try {
member = aClass.getDeclaredField(name);
} catch (NoSuchFieldException nsfe) {
Class superclass = aClass.getSuperclass();
if (superclass != null && superclass != Object.class) {
return getField(superclass, name);
}
throw nsfe;
}
checkField(member);
member.setAccessible(true);
return member;
}
protected Method getMethod(Class<?> aClass, String name, Class<?>... params) throws NoSuchMethodException {
Method member;
try {
member = aClass.getMethod(name, params);
if (member.getDeclaringClass() == Object.class) {
throw new NoSuchMethodException();
}
} catch (NoSuchMethodException nsme) {
try {
member = aClass.getDeclaredMethod(name, params);
} catch (NoSuchMethodException nsme2) {
Class superclass = aClass.getSuperclass();
if (superclass != null && superclass != Object.class) {
return getMethod(superclass, name);
}
throw nsme2;
}
}
checkMethod(member);
member.setAccessible(true);
return member;
}
protected AccessibleObject findMember(Class sClass, String name) {
if (String.class == sClass && "value".equals(name)) { // under java11 it would return a wrapper we don't want
return null;
}
if (checkClass(sClass)) {
// We won't be able to get methods or members on the class directly, so we will look at superclasses and interfaces
for (Class anInterface : sClass.getInterfaces()) {
AccessibleObject member = findMember(anInterface, name);
if (member != null) return member;
}
Class superclass = sClass.getSuperclass();
if (superclass != null && superclass != Object.class) {
AccessibleObject ao = findMember(superclass, name);
if (ao != null) return ao;
}
}
return findMemberOnClass(sClass, name);
}
private AccessibleObject findMemberOnClass(Class sClass, String name) {
if (name.isEmpty()) return null;
AccessibleObject ao;
try {
ao = getMethod(sClass, name);
} catch (NoSuchMethodException e) {
String propertyname = name.substring(0, 1).toUpperCase(Locale.ROOT) +
(name.length() > 1 ? name.substring(1) : "");
try {
ao = getMethod(sClass, "get" + propertyname);
} catch (NoSuchMethodException e2) {
try {
ao = getMethod(sClass, "is" + propertyname);
} catch (NoSuchMethodException e3) {
try {
ao = getField(sClass, name);
} catch (NoSuchFieldException e4) {
ao = null;
}
}
}
}
return ao;
}
// We default to not allowing private methods
protected void checkMethod(Method member) throws NoSuchMethodException {
if ((member.getModifiers() & Modifier.PRIVATE) == Modifier.PRIVATE) {
throw new NoSuchMethodException("Only public, protected and package members allowed");
}
}
// We default to not allowing private fields
protected void checkField(Field member) throws NoSuchFieldException {
if ((member.getModifiers() & Modifier.PRIVATE) == Modifier.PRIVATE) {
throw new NoSuchFieldException("Only public, protected and package members allowed");
}
}
// We default to not allowing private classes
protected boolean checkClass(Class sClass) {
return (sClass.getModifiers() & Modifier.PUBLIC) != Modifier.PUBLIC;
}
}