Search in sources :

Example 6 with Name

use of org.whole.lang.pojo.model.Name in project whole by wholeplatform.

the class PojoUtils method setPropertyValue.

public static void setPropertyValue(Property property, Object toObject, Object value) throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException {
    Class<?> fromClass = toObject.getClass();
    Name name = property.getName();
    String fieldName = name.getValue();
    if (isFieldOnly(property)) {
        Field field = fromClass.getField(fieldName);
        field.set(toObject, value);
    } else {
        String setterMethodName = "set" + StringUtils.toUpperCap(fieldName);
        for (Method method : fromClass.getMethods()) {
            Class<?>[] parameterTypes = method.getParameterTypes();
            if (setterMethodName.equals(method.getName()) && parameterTypes.length == 1 && parameterTypes[0].isAssignableFrom(DataTypeUtils.unboxFilter(value.getClass()))) {
                method.invoke(toObject, new Object[] { value });
                return;
            }
        }
        throw new IllegalArgumentException("missing setter method for property");
    }
}
Also used : Field(java.lang.reflect.Field) Method(java.lang.reflect.Method) Name(org.whole.lang.pojo.model.Name)

Example 7 with Name

use of org.whole.lang.pojo.model.Name 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());
}
Also used : IEntity(org.whole.lang.model.IEntity) Constructor(org.whole.lang.pojo.model.Constructor) ArrayList(java.util.ArrayList) ReferenceType(org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.ReferenceType) ReferenceType(org.whole.lang.pojo.model.ReferenceType) Name(org.whole.lang.pojo.model.Name) PrimitiveType(org.whole.lang.pojo.model.PrimitiveType) PrimitiveType(org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.PrimitiveType) Type(org.whole.lang.pojo.model.Type) MapType(org.whole.lang.pojo.model.MapType) ArrayType(org.whole.lang.pojo.model.ArrayType) ReferenceType(org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.ReferenceType) CollectionType(org.whole.lang.pojo.model.CollectionType) ReferenceType(org.whole.lang.pojo.model.ReferenceType) Expression(org.whole.lang.queries.model.Expression) PathExpression(org.whole.lang.queries.model.PathExpression) FeatureDescriptor(org.whole.lang.reflect.FeatureDescriptor) IBindingManager(org.whole.lang.bindings.IBindingManager) Parameter(org.whole.lang.pojo.model.Parameter) Property(org.whole.lang.pojo.model.Property)

Example 8 with Name

use of org.whole.lang.pojo.model.Name in project whole by wholeplatform.

the class PojoUtils method getPropertyValue.

public static Object getPropertyValue(Property property, Object fromObject) throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
    Class<?> fromClass = fromObject.getClass();
    Object fieldValue;
    Name name = property.getName();
    String filedName = name.wStringValue();
    if (isFieldOnly(property)) {
        Field field = fromClass.getField(filedName);
        fieldValue = field.get(fromObject);
    } else {
        Type type = property.getType();
        boolean isBoolean = type.wContainsValue(PrimitiveTypeEnum._boolean);
        String getterMethodName = (isBoolean ? "is" : "get") + StringUtils.toUpperCap(filedName);
        Method getterMethod = fromClass.getMethod(getterMethodName, new Class[0]);
        fieldValue = getterMethod.invoke(fromObject, new Object[0]);
    }
    return fieldValue;
}
Also used : Field(java.lang.reflect.Field) PrimitiveType(org.whole.lang.pojo.model.PrimitiveType) PrimitiveType(org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.PrimitiveType) Type(org.whole.lang.pojo.model.Type) MapType(org.whole.lang.pojo.model.MapType) ArrayType(org.whole.lang.pojo.model.ArrayType) ReferenceType(org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.ReferenceType) CollectionType(org.whole.lang.pojo.model.CollectionType) ReferenceType(org.whole.lang.pojo.model.ReferenceType) Method(java.lang.reflect.Method) Name(org.whole.lang.pojo.model.Name)

Example 9 with Name

use of org.whole.lang.pojo.model.Name 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);
    }
}
Also used : PojoDeclaration(org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.PojoDeclaration) PojoDeclaration(org.whole.lang.pojo.model.PojoDeclaration) PrimitiveType(org.whole.lang.pojo.model.PrimitiveType) PrimitiveType(org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.PrimitiveType) Type(org.whole.lang.pojo.model.Type) MapType(org.whole.lang.pojo.model.MapType) ArrayType(org.whole.lang.pojo.model.ArrayType) ReferenceType(org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.ReferenceType) CollectionType(org.whole.lang.pojo.model.CollectionType) ReferenceType(org.whole.lang.pojo.model.ReferenceType) FeatureDescriptor(org.whole.lang.reflect.FeatureDescriptor) IEntity(org.whole.lang.model.IEntity) Property(org.whole.lang.pojo.model.Property) ReferenceType(org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.ReferenceType) ReferenceType(org.whole.lang.pojo.model.ReferenceType) InvocationTargetException(java.lang.reflect.InvocationTargetException) Name(org.whole.lang.pojo.model.Name)

Example 10 with Name

use of org.whole.lang.pojo.model.Name in project whole by wholeplatform.

the class PojoNormalizerVisitor method createDefaultTemplateInParameters.

public static void createDefaultTemplateInParameters(Library entity, IBindingManager bindings) {
    Path findAllParametersWithoutTemlate = (Path) PojoTemplateManager.instance().create("findAllParametersWithoutTemlate");
    Path findParameterTemplate = (Path) PojoTemplateManager.instance().create("findPropertyTemplate");
    bindings.wEnterScope();
    for (Parameter parameter : BehaviorUtils.<Parameter>compileAndLazyEvaluate(findAllParametersWithoutTemlate, entity, bindings)) {
        PojoDeclaration pojo = (PojoDeclaration) bindings.wGet("pojo");
        Name name = BehaviorUtils.<Name>evaluateFirstResult(findParameterTemplate, pojo, bindings);
        if (name != null)
            parameter.setTemplate(EntityUtils.clone(name));
    }
    bindings.wExitScope();
}
Also used : Path(org.whole.lang.queries.model.Path) PojoDeclaration(org.whole.lang.pojo.model.PojoDeclaration) Parameter(org.whole.lang.pojo.model.Parameter) Name(org.whole.lang.pojo.model.Name)

Aggregations

Name (org.whole.lang.pojo.model.Name)10 IBindingManager (org.whole.lang.bindings.IBindingManager)4 Expression (org.whole.lang.queries.model.Expression)4 PathExpression (org.whole.lang.queries.model.PathExpression)4 IEntity (org.whole.lang.model.IEntity)3 ArrayType (org.whole.lang.pojo.model.ArrayType)3 CollectionType (org.whole.lang.pojo.model.CollectionType)3 MapType (org.whole.lang.pojo.model.MapType)3 PrimitiveType (org.whole.lang.pojo.model.PrimitiveType)3 Property (org.whole.lang.pojo.model.Property)3 ReferenceType (org.whole.lang.pojo.model.ReferenceType)3 Type (org.whole.lang.pojo.model.Type)3 PrimitiveType (org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.PrimitiveType)3 ReferenceType (org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.ReferenceType)3 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 Constructor (org.whole.lang.pojo.model.Constructor)2 Parameter (org.whole.lang.pojo.model.Parameter)2 PojoDeclaration (org.whole.lang.pojo.model.PojoDeclaration)2 Path (org.whole.lang.queries.model.Path)2