public static <T> BeanInstance<T> create(Class<T> elementType) { try { return new BeanInstance<T>(newInstance(elementType)); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public Object convert(Bean source, Class<? extends Object> specificType) { Object instance = null; try { instance = newInstance(specificType); } catch (Exception e) { throw new UnsupportedOperationException(e); } Map<String, Object> valuesToInject = new HashMap<String, Object>(); Map<BeanId, Object> instanceCache = new HashMap<BeanId, Object>(); return convert(source, instance, valuesToInject, instanceCache); }
public Object getDefaultValue() { if (defaultDeclaringInstance == null) { try { defaultDeclaringInstance = newInstance(field.getDeclaringClass()); } catch (InstantiationException e) { throw new UnsupportedOperationException("Cannot access default values " + "from fields of class which cannot be constructed.", e); } catch (IllegalAccessException e) { throw new UnsupportedOperationException("Cannot access default values " + "from fields of class which cannot be constructed.", e); } catch (InvocationTargetException e) { throw new UnsupportedOperationException("Cannot access default values " + "from fields of class which cannot be constructed.", e); } catch (NoSuchMethodException e) { throw new UnsupportedOperationException("Cannot access default values " + "from fields of class which cannot be constructed.", e); } } try { return field.get(defaultDeclaringInstance); } catch (IllegalArgumentException e) { throw new UnsupportedOperationException("Cannot access default values " + "from fields of instances which cannot be accessed.", e); } catch (IllegalAccessException e) { throw new UnsupportedOperationException("Cannot access default values " + "from fields of class which cannot be accessed.", e); } }
private void convertPropertyRefList(Bean source, Map<String, Object> values, Map<BeanId, Object> instanceCache) { for (SchemaPropertyRefList prop : source.getSchema().get(SchemaPropertyRefList.class)) { List<BeanId> references = source.getReference(prop.getName()); if (references == null) { continue; } Collection<Object> c = newCollection(forName(prop.getCollectionType())); for (BeanId beanId : references) { Bean b = beanId.getBean(); if (b != null) { Object beanInstance = instanceCache.get(beanId); if (beanInstance == null) { String type = b.getSchema().getType(); try { beanInstance = newInstance(forName(type)); } catch (Exception e) { throw new UnsupportedOperationException(e); } beanInstance = convert(b, beanInstance, new HashMap<String, Object>(), instanceCache); } c.add(beanInstance); } } values.put(prop.getFieldName(), c); } }
private void convertPropertyRefMap(Bean source, Map<String, Object> values, Map<BeanId, Object> instanceCache) { for (SchemaPropertyRefMap prop : source.getSchema().get(SchemaPropertyRefMap.class)) { List<BeanId> beans = source.getReference(prop.getName()); if (beans == null) { continue; } Map<Object, Object> c = newMap(forName(prop.getMapType())); for (BeanId beanId : beans) { Bean b = beanId.getBean(); if (b != null) { Object beanInstance = instanceCache.get(beanId); if (beanInstance == null) { try { beanInstance = newInstance(forName(b.getSchema().getType())); } catch (Exception e) { throw new UnsupportedOperationException(e); } beanInstance = convert(b, beanInstance, new HashMap<String, Object>(), instanceCache); } c.put(beanId.getInstanceId(), beanInstance); } } values.put(prop.getFieldName(), c); } }
private void convertPropertyRef(Bean source, Map<String, Object> values, Map<BeanId, Object> instanceCache) { for (SchemaPropertyRef prop : source.getSchema().get(SchemaPropertyRef.class)) { BeanId id = source.getFirstReference(prop.getName()); if (id == null) { continue; } Bean ref = id.getBean(); if (ref == null) { continue; } Schema refSchema = ref.getSchema(); SchemaPropertyRef schemaRef = source.getSchema().get(SchemaPropertyRef.class, prop.getName()); Object beanInstance = instanceCache.get(id); if (beanInstance == null) { try { beanInstance = newInstance(forName(refSchema.getType())); } catch (Exception e) { throw new UnsupportedOperationException(e); } beanInstance = convert(ref, beanInstance, new HashMap<String, Object>(), instanceCache); } values.put(schemaRef.getFieldName(), beanInstance); } }