MCPcopy Create free account
hub / github.com/OpenEndedGroup/Field2 / simpleDeserializeFromMapSafe

Method simpleDeserializeFromMapSafe

src/main/java/fieldbox/io/EDN.java:192–240  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

190 }
191
192 public TagHandler simpleDeserializeFromMapSafe() {
193 return (tag, o) -> {
194
195 try {
196 Map<?, ?> m = (Map) o;
197 String clazz = (String) m.get(Keyword.newKeyword("$class"));
198
199 Class<?> c = Thread.currentThread()
200 .getContextClassLoader()
201 .loadClass(clazz);
202 Object instance = getUnsafe().allocateInstance(c);
203
204 m.entrySet()
205 .forEach(e -> {
206 String s = "";
207 Object k = e.getKey();
208 if (k instanceof Keyword) s = ((Keyword) k).getName();
209 else s = k.toString();
210
211 if (s.startsWith("$")) return;
212
213 try {
214 Field f = c.getField(s);
215 f.setAccessible(true);
216 if (Modifier.isFinal(f.getModifiers())) {
217 long of = getUnsafe().objectFieldOffset(f);
218 if (f.getType() == Float.TYPE) getUnsafe().putFloat(instance, of, ((Number) e.getValue()).floatValue());
219 else throw new IllegalArgumentException("can't handle " + f.getType() + " / " + f + " / " + s + " " + instance);
220 } else {
221 if (f.getType() == Float.TYPE) f.set(instance, ((Number) e.getValue()).floatValue());
222 else f.set(instance, e.getValue());
223
224 }
225 } catch (NoSuchFieldException e1) {
226 e1.printStackTrace();
227 } catch (IllegalAccessException e1) {
228 e1.printStackTrace();
229 }
230
231 });
232 return instance;
233 } catch (InstantiationException e) {
234 e.printStackTrace();
235 } catch (ClassNotFoundException e) {
236 e.printStackTrace();
237 }
238 return null;
239 };
240 }
241
242
243 public us.bpsm.edn.printer.Printer.Fn<?> simpleSerializeToMap(Tag tag, Class c) {

Callers 1

EDNMethod · 0.95

Calls 9

getUnsafeMethod · 0.95
getTypeMethod · 0.80
getMethod · 0.65
setMethod · 0.65
forEachMethod · 0.45
getNameMethod · 0.45
toStringMethod · 0.45
getFieldMethod · 0.45
getValueMethod · 0.45

Tested by

no test coverage detected