Search in sources :

Example 1 with Constructor

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

the class PojoUtils method findConstructor.

public static Constructor findConstructor(PojoDeclaration pojoDeclaration) {
    IBindingManager bindings = BindingManagerFactory.instance.createArguments();
    Expression findParameterByTemplate = (Expression) PojoTemplateManager.instance().create("findParameterByTemplate");
    List<Constructor> constructors = getConstructors(pojoDeclaration);
    int[] supportedFields = new int[constructors.size()];
    Expression findAllReadOnlyFields = (Expression) PojoTemplateManager.instance().create("findAllReadOnlyFields");
    IEntityIterator<Name> iterator = BehaviorUtils.<Name>compileAndLazyEvaluate(findAllReadOnlyFields, pojoDeclaration, bindings);
    int readOnlyFieldCount = 0;
    while (iterator.hasNext()) {
        iterator.next();
        for (int i = 0; i < supportedFields.length; i++) if (BehaviorUtils.evaluateFirstResult(findParameterByTemplate, constructors.get(i), bindings) != null)
            supportedFields[i]++;
        readOnlyFieldCount++;
    }
    for (int i = 0; i < supportedFields.length; i++) if (supportedFields[i] >= readOnlyFieldCount)
        return constructors.get(i);
    return constructors.get(supportedFields.length - 1);
}
Also used : Expression(org.whole.lang.queries.model.Expression) PathExpression(org.whole.lang.queries.model.PathExpression) Constructor(org.whole.lang.pojo.model.Constructor) IBindingManager(org.whole.lang.bindings.IBindingManager) Name(org.whole.lang.pojo.model.Name)

Example 2 with Constructor

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

the class ConstructorPart method getModelSpecificChildren.

protected List<IEntity> getModelSpecificChildren() {
    List<IEntity> list = new ArrayList<IEntity>(1);
    Constructor entity = getModelEntity();
    list.add(entity.getParameters());
    return list;
}
Also used : IEntity(org.whole.lang.model.IEntity) Constructor(org.whole.lang.pojo.model.Constructor) ArrayList(java.util.ArrayList)

Example 3 with Constructor

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

the class PojoUtils method getConstructors.

public static List<Constructor> getConstructors(PojoDeclaration pojoDeclaration) {
    Constructors constructors = pojoDeclaration.getConstructors();
    List<Constructor> constructorsList = new ArrayList<Constructor>(constructors.wSize());
    IEntityIterator<Constructor> i = IteratorFactory.<Constructor>childIterator();
    i.reset(constructors);
    for (Constructor constructor : i) constructorsList.add(constructor);
    Collections.sort(constructorsList, new Comparator<Constructor>() {

        public int compare(Constructor c1, Constructor c2) {
            return c1.getParameters().wSize() - c2.getParameters().wSize();
        }
    });
    return constructorsList;
}
Also used : Constructor(org.whole.lang.pojo.model.Constructor) ArrayList(java.util.ArrayList) Constructors(org.whole.lang.pojo.model.Constructors)

Example 4 with Constructor

use of org.whole.lang.pojo.model.Constructor 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)

Aggregations

Constructor (org.whole.lang.pojo.model.Constructor)4 ArrayList (java.util.ArrayList)3 IBindingManager (org.whole.lang.bindings.IBindingManager)2 IEntity (org.whole.lang.model.IEntity)2 Name (org.whole.lang.pojo.model.Name)2 Expression (org.whole.lang.queries.model.Expression)2 PathExpression (org.whole.lang.queries.model.PathExpression)2 ArrayType (org.whole.lang.pojo.model.ArrayType)1 CollectionType (org.whole.lang.pojo.model.CollectionType)1 Constructors (org.whole.lang.pojo.model.Constructors)1 MapType (org.whole.lang.pojo.model.MapType)1 Parameter (org.whole.lang.pojo.model.Parameter)1 PrimitiveType (org.whole.lang.pojo.model.PrimitiveType)1 Property (org.whole.lang.pojo.model.Property)1 ReferenceType (org.whole.lang.pojo.model.ReferenceType)1 Type (org.whole.lang.pojo.model.Type)1 PrimitiveType (org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.PrimitiveType)1 ReferenceType (org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.ReferenceType)1 FeatureDescriptor (org.whole.lang.reflect.FeatureDescriptor)1