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

Method _convert

src/main/java/field/utility/Conversions.java:369–515  ·  view source on GitHub ↗
(Object value, List<Class> fit, Consumer<String> extraInfo)

Source from the content-addressed store, hash-verified

367
368 return convert(value, Collections.singletonList(fit));
369 }
370
371 static protected Object _convert(Object value, List<Class> fit, Consumer<String> extraInfo) {
372 if (value == null) return null;
373 if (fit == null) return value;
374 if (fit.get(0)
375 .isInstance(value)) return value;
376
377 // promote non-arrays to arrays
378 if (List.class.isAssignableFrom(fit.get(0))) {
379
380 if (!(value instanceof List)) {
381 return Collections.singletonList(_convert(value, fit.subList(1, fit.size()), extraInfo));
382 } else {
383 return value;
384 }
385 } else if (Map.class.isAssignableFrom(fit.get(0))) {
386 Object converted = ScriptUtils.convert(value, Map.class);
387 if (converted != null) return converted;
388 }
389
390 if (Map.class.isAssignableFrom(fit.get(0)) && (fit.size() == 1 || String.class.isAssignableFrom(fit.get(1)))) {
391 // promote non-Map<String, V> to Map<String, V>
392 if (!(value instanceof Map)) {
393 return Collections.singletonMap("" + value + ":" + System.identityHashCode(value),
394 _convert(value, fit.subList(2, fit.size()), extraInfo));
395 } else {
396 return value;
397 }
398
399
400 } else if (Collection.class.isAssignableFrom(fit.get(0))) {
401
402 try {
403 Object converted = ScriptUtils.convert(value, Collection.class);
404 if (converted != null) return converted;
405 } catch (ClassCastException e) {
406 }
407
408 if (!(value instanceof Collection) && fit.size() > 1) {
409 return Collections.singletonList(convert(value, fit.subList(1, fit.size())));
410 } else {
411 return value;
412 }
413 }
414
415 if (fit.get(0)
416 .isInterface() && value instanceof InvocationHandler) {
417 return Proxy.newProxyInstance(Thread.currentThread()
418 .getContextClassLoader(), new Class[]{fit.get(0)},
419 (InvocationHandler) value);
420 }
421
422 if (value instanceof ScriptObjectMirror && ((ScriptObjectMirror) value).isArray() && fit.size() > 1 && Supplier.class
423 .isAssignableFrom(
424 fit.get(0))) {
425
426 Object[] a = new Object[((ScriptObjectMirror) value).size()];

Callers 1

convertMethod · 0.95

Calls 9

convertMethod · 0.95
getMethod · 0.95
getMethod · 0.65
getSlotMethod · 0.65
acceptMethod · 0.65
sizeMethod · 0.45
unwrapMethod · 0.45
getNameMethod · 0.45
printlnMethod · 0.45

Tested by

no test coverage detected