-
Notifications
You must be signed in to change notification settings - Fork 3
/
Deserialiser.java
158 lines (138 loc) · 4.81 KB
/
Deserialiser.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package org.epics.pvmarshaller.marshaller.deserialisers;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import org.epics.pvdata.pv.PVStructure;
import org.epics.pvmarshaller.marshaller.api.IPVStructureDeserialiser;
/**
* Top level class for the deserialisation of PVStrucures into an Object
* @author Matt Taylor
*
*/
public class Deserialiser {
MapDeserialiser mapDeserialiser = new MapDeserialiser(this);
MapScalarArrayDeserialiser mapScalarArrayDeserialiser = new MapScalarArrayDeserialiser();
ScalarArrayDeserialiser scalarArrayDeserialiser = new ScalarArrayDeserialiser(this);
ScalarDeserialiser scalarDeserialiser = new ScalarDeserialiser(this);
StructureArrayDeserialiser structureArrayDeserialiser = new StructureArrayDeserialiser(this);
StructureDeserialiser structureDeserialiser = new StructureDeserialiser(this);
UnionDeserialiser unionDeserialiser = new UnionDeserialiser(this);
UnionArrayDeserialiser unionArrayDeserialiser = new UnionArrayDeserialiser(this);
boolean ignoreUnknownFields = false;
/**
* Converts the specified PVStructure into an object of the type specified
* @param pvStructure The PVStructure to convert
* @param targetClass The class of the expected output object
* @return An object of type T
* @throws Exception
*/
public <T> T fromPVStructure(PVStructure pvStructure, Class<T> targetClass) throws Exception {
T deserialisedObject = (T)structureDeserialiser.createObjectFromPVStructure(pvStructure, targetClass);
return deserialisedObject;
}
/**
* Deserialises a single field within a PVStructure
* @param pvStructure The object to deserialise the field from
* @param fieldName The name of the field to deserialise
* @return The deserialised object
* @throws Exception
*/
public Object getObjectFromField(PVStructure pvStructure, String fieldName) throws Exception {
Map<?, ?> newMap = mapDeserialiser.createMapFromPVStructure(pvStructure, Map.class, Object.class);
return newMap.get(fieldName);
}
/**
* Gets the MapDeserialiser used for converting maps
* @return
*/
public MapDeserialiser getMapDeserialiser() {
return mapDeserialiser;
}
/**
* Gets the MapScalarArrayDeserialiser used for converting maps of scalar arrays
* @return
*/
public MapScalarArrayDeserialiser getMapScalarArrayDeserialiser() {
return mapScalarArrayDeserialiser;
}
/**
* Gets the ScalarArrayDeserialiser used for converting scalar arrays
* @return
*/
public ScalarArrayDeserialiser getScalarArrayDeserialiser() {
return scalarArrayDeserialiser;
}
/**
* Gets the ScalarDeserialiser used for converting Scalar fields
* @return
*/
public ScalarDeserialiser getScalarDeserialiser() {
return scalarDeserialiser;
}
/**
* Gets the StructureArrayDeserialiser used for converting Arrays of Structures
* @return
*/
public StructureArrayDeserialiser getStructureArrayDeserialiser() {
return structureArrayDeserialiser;
}
/**
* Gets the StructureDeserialiser used for converting Structures
* @return
*/
public StructureDeserialiser getStructureDeserialiser() {
return structureDeserialiser;
}
/**
* Gets the UnionDeserialiser used for converting unions
* @return
*/
public UnionDeserialiser getUnionDeserialiser() {
return unionDeserialiser;
}
/**
* Gets the UnionArrayDeserialiser used for converting union arrays
* @return
*/
public UnionArrayDeserialiser getUnionArrayDeserialiser() {
return unionArrayDeserialiser;
}
/**
* Gets whether unknown fields should be ignored
* @return
*/
public boolean getIgnoreUnknownFields() {
return ignoreUnknownFields;
}
/**
* Sets whether unknown fields should be ignored
* @param ignoreUnknownMembers
*/
public void setIgnoreUnknownFields(boolean ignoreUnknownMembers) {
this.ignoreUnknownFields = ignoreUnknownMembers;
}
/**
* Finds the setter method for a given member in the specified Object
* @param object The object to find the setter in
* @param variableName The name of the member to find the setter for
* @return the Setter method, or null if no setter is found and IgnoreUnknownFields has been set to true
* @throws IllegalArgumentException
*/
public Method findSetter(Object object, String variableName) throws IllegalArgumentException {
Class<?> clazz = object.getClass();
while (clazz != Object.class) {
Method[] allMethods = clazz.getDeclaredMethods();
for (Method m : allMethods) {
if (m.getName().toLowerCase().equals("set" + variableName.toLowerCase()) && m.getParameters().length == 1) {
m.setAccessible(true);
return m;
}
}
clazz = clazz.getSuperclass();
}
if (ignoreUnknownFields == false) {
throw new IllegalArgumentException("Unable to find setter for " + variableName + " in class " + object.getClass());
}
return null;
}
}