use of org.whole.lang.pojo.model.PrimitiveType in project whole by wholeplatform.
the class PojoUtils method createInstanceUsingConstructor.
public static Object createInstanceUsingConstructor(IEntity fromEntity, PojoDeclaration pojoDeclaration, Library library) throws Exception {
ReferenceType referenceType = pojoDeclaration.getName();
Class<?> clazz = Class.forName(referenceType.getValue(), true, ReflectionFactory.getPlatformClassLoader());
Constructor constructor = findConstructor(pojoDeclaration);
int params = constructor.getParameters().wSize();
List<Class<?>> parameterTypes = new ArrayList<Class<?>>(params);
List<Object> initargs = new ArrayList<Object>(params);
IBindingManager bindings = BindingManagerFactory.instance.createArguments();
Expression findPropertyByTemplate = (Expression) PojoTemplateManager.instance().create("findPropertyByTemplate");
Expression findParameterByTemplate = (Expression) PojoTemplateManager.instance().create("findParameterByTemplate");
IEntityIterator<Parameter> iterator = BehaviorUtils.<Parameter>compileAndLazyEvaluate(findParameterByTemplate, constructor, bindings);
while (iterator.hasNext()) {
iterator.next();
Property property = BehaviorUtils.<Property>evaluateFirstResult(findPropertyByTemplate, pojoDeclaration, bindings);
Type type = property.getType();
Name template = property.getTemplate();
FeatureDescriptor fd = fromEntity.wGetEntityDescriptor().getFeatureDescriptorEnum().valueOf(template.wStringValue());
IEntity fieldEntity = fromEntity.wGet(fd);
parameterTypes.add(getClass(type));
initargs.add(Matcher.match(PrimitiveType, type) ? fieldEntity.wGetValue() : create(fieldEntity, library));
}
return clazz.getConstructor(parameterTypes.toArray(new Class<?>[0])).newInstance(initargs.toArray());
}
use of org.whole.lang.pojo.model.PrimitiveType in project whole by wholeplatform.
the class PojoUtils method translate.
public static void translate(IEntity fromEntity, Object toObject, PojoDeclaration pojoDeclaration, Library library) {
// translate inherited properties
IEntityIterator<ReferenceType> superPojosIterator = IteratorFactory.<ReferenceType>childIterator();
superPojosIterator.reset(pojoDeclaration.getTypes());
for (ReferenceType superType : superPojosIterator) {
PojoDeclaration superDeclaration = (PojoDeclaration) findProductDeclaration(superType, library);
translate(fromEntity, toObject, superDeclaration, library);
}
// translate declared properties
IEntityIterator<Property> iterator = IteratorFactory.<Property>childIterator();
iterator.reset(pojoDeclaration.getProperties());
EntityDescriptor<?> ed = fromEntity.wGetEntityDescriptor();
Property property = null;
try {
while (iterator.hasNext()) {
property = iterator.next();
if (isReadOnly(property))
continue;
Type type = property.getType();
Name template = property.getTemplate();
FeatureDescriptor fd = ed.getFeatureDescriptorEnum().valueOf(template.wStringValue());
IEntity fieldEntity = fromEntity.wGet(fd);
if (!EntityUtils.isNotResolver(fieldEntity))
continue;
if (Matcher.match(PrimitiveType, type))
setPropertyValue(property, toObject, fieldEntity.wGetValue());
else
setPropertyValue(property, toObject, create(fieldEntity, library));
}
} catch (Exception e) {
throw new IllegalStateException("Cannot translate property: " + property, e);
}
}
Aggregations