Search in sources :

Example 1 with ModelType

use of com.vaadin.flow.templatemodel.ModelType in project flow by vaadin.

the class ModelDescriptorTest method listInsideList.

@Test
public void listInsideList() {
    ModelDescriptor<?> descriptor = ModelDescriptor.get(ListInsideListBeanModel.class);
    Assert.assertEquals(1, descriptor.getPropertyNames().count());
    ListModelType<?> listPropertyType = (ListModelType<?>) descriptor.getPropertyType("beans");
    Type javaType = listPropertyType.getJavaType();
    Assert.assertTrue("Expected instanceof ParameterizedType for List", javaType instanceof ParameterizedType);
    javaType = ((ParameterizedType) javaType).getActualTypeArguments()[0];
    Assert.assertTrue("Expected instanceof ParameterizedType for List in List", javaType instanceof ParameterizedType);
    Assert.assertEquals(Bean.class, ((ParameterizedType) javaType).getActualTypeArguments()[0]);
    Assert.assertTrue(listPropertyType.getItemType() instanceof ListModelType<?>);
    ListModelType<?> type = (ListModelType<?>) listPropertyType.getItemType();
    Assert.assertTrue(type.getItemType() instanceof BeanModelType<?>);
    BeanModelType<?> modelType = (BeanModelType<?>) type.getItemType();
    Assert.assertSame(Bean.class, modelType.getProxyType());
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) BasicModelType(com.vaadin.flow.templatemodel.BasicModelType) ListModelType(com.vaadin.flow.template.angular.model.ListModelType) ModelType(com.vaadin.flow.templatemodel.ModelType) BeanModelType(com.vaadin.flow.template.angular.model.BeanModelType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ListModelType(com.vaadin.flow.template.angular.model.ListModelType) BeanModelType(com.vaadin.flow.template.angular.model.BeanModelType) Test(org.junit.Test)

Example 2 with ModelType

use of com.vaadin.flow.templatemodel.ModelType in project flow by vaadin.

the class BeanModelType method getListModelType.

private static ModelType getListModelType(Type propertyType, PropertyFilter propertyFilter, String propertyName, Class<?> declaringClass) {
    assert ListModelType.isList(propertyType);
    ParameterizedType pt = (ParameterizedType) propertyType;
    Type itemType = pt.getActualTypeArguments()[0];
    if (itemType instanceof ParameterizedType) {
        return new ListModelType<>((ComplexModelType<?>) getModelType(itemType, propertyFilter, propertyName, declaringClass));
    } else {
        if (!(itemType instanceof Class<?>)) {
            throw new InvalidTemplateModelException("Element type " + itemType.getTypeName() + " is not a valid Bean type. Used in class " + declaringClass.getSimpleName() + " with property named " + propertyName + " with list type " + propertyType.getTypeName());
        }
        Class<?> beansListItemType = (Class<?>) itemType;
        return new ListModelType<>(new BeanModelType<>(beansListItemType, propertyFilter));
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) BasicModelType(com.vaadin.flow.templatemodel.BasicModelType) ModelType(com.vaadin.flow.templatemodel.ModelType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ComplexModelType(com.vaadin.flow.templatemodel.ComplexModelType) InvalidTemplateModelException(com.vaadin.flow.templatemodel.InvalidTemplateModelException)

Example 3 with ModelType

use of com.vaadin.flow.templatemodel.ModelType in project flow by vaadin.

the class TemplateModelProxyHandler method intercept.

/**
 * Processes a method invocation on a Byte buddy proxy instance and returns
 * the result. This method will be invoked on an invocation handler when a
 * method is invoked on a proxy instance that it is associated with.
 *
 * @param target
 *            the proxy instance
 * @param method
 *            the {@code Method} instance corresponding to the proxied
 *            method invoked on the proxy instance.
 *
 * @param args
 *            an array of objects containing the values of the arguments
 *            passed in the method invocation on the proxy instance.
 * @return the value to return from the method invocation on the proxy
 *         instance.
 */
@RuntimeType
@SuppressWarnings("static-method")
public Object intercept(@This Object target, @Origin Method method, @AllArguments Object[] args) {
    String propertyName = ReflectTools.getPropertyName(method);
    BeanModelType<?> modelType = getModelTypeForProxy(target);
    if (!modelType.hasProperty(propertyName)) {
        throw new InvalidTemplateModelException(modelType.getProxyType().getName() + " has no property named " + propertyName + " (or it has been excluded)");
    }
    ModelType propertyType = modelType.getPropertyType(propertyName);
    ModelMap modelMap = ModelMap.get(getStateNodeForProxy(target));
    if (ReflectTools.isGetter(method)) {
        return handleGetter(modelMap, propertyName, propertyType);
    } else if (ReflectTools.isSetter(method)) {
        Object value = args[0];
        handleSetter(modelMap, propertyName, propertyType, value);
        return null;
    }
    throw new InvalidTemplateModelException(getUnsupportedMethodMessage(method, args));
}
Also used : InvalidTemplateModelException(com.vaadin.flow.templatemodel.InvalidTemplateModelException) ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) ModelType(com.vaadin.flow.templatemodel.ModelType) RuntimeType(net.bytebuddy.implementation.bind.annotation.RuntimeType)

Example 4 with ModelType

use of com.vaadin.flow.templatemodel.ModelType in project flow by vaadin.

the class TemplateModelUtil method resolveListAndRun.

/**
 * Resolves a list model type and a model list based on a model instance and
 * passes those to the provided callback.
 *
 * @param <R>
 *            the return value type
 * @param model
 *            the model instance for which to resolve a type and a list, not
 *            <code>null</code>
 * @param modelPath
 *            the model path to resolve, not <code>null</code>
 * @param callback
 *            the callback to run with the resolved list type and model
 *            list, not <code>null</code>
 * @return the value returned by the callback
 */
public static <R> R resolveListAndRun(TemplateModel model, String modelPath, BiFunction<ListModelType<?>, ModelList, R> callback) {
    assert model != null;
    assert modelPath != null;
    assert callback != null;
    BeanModelType<?> modelType = TemplateModelProxyHandler.getModelTypeForProxy(model);
    ModelType listType = modelType.resolveType(modelPath);
    if (listType instanceof ListModelType<?>) {
        StateNode stateNode = TemplateModelProxyHandler.getStateNodeForProxy(model);
        ModelMap modelMap = ModelMap.get(stateNode);
        ModelList modelList = modelMap.resolveModelList(modelPath);
        return callback.apply((ListModelType<?>) listType, modelList);
    } else {
        throw new IllegalArgumentException(modelPath + " does not resolve to a list");
    }
}
Also used : ModelList(com.vaadin.flow.internal.nodefeature.ModelList) ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) StateNode(com.vaadin.flow.internal.StateNode) ModelType(com.vaadin.flow.templatemodel.ModelType)

Example 5 with ModelType

use of com.vaadin.flow.templatemodel.ModelType in project flow by vaadin.

the class PolymerTemplate method getModelType.

/**
 * Get the {@code ModelType} for given class.
 *
 * @param type
 *            Type to get the ModelType for
 * @return ModelType for given Type
 */
public ModelType getModelType(Type type) {
    List<ModelType> modelTypes = ModelDescriptor.get(getModelType()).getPropertyNames().map(this::getModelType).collect(Collectors.toList());
    for (ModelType mtype : modelTypes) {
        if (type.equals(mtype.getJavaType())) {
            return mtype;
        } else if (mtype instanceof ListModelType) {
            ModelType modelType = getModelTypeForListModel(type, mtype);
            if (modelType != null) {
                return modelType;
            }
        }
    }
    String msg = String.format("Couldn't find ModelType for requested class %s", type.getTypeName());
    throw new IllegalArgumentException(msg);
}
Also used : ListModelType(com.vaadin.flow.templatemodel.ListModelType) BeanModelType(com.vaadin.flow.templatemodel.BeanModelType) ListModelType(com.vaadin.flow.templatemodel.ListModelType) ModelType(com.vaadin.flow.templatemodel.ModelType)

Aggregations

ModelType (com.vaadin.flow.templatemodel.ModelType)12 BasicModelType (com.vaadin.flow.templatemodel.BasicModelType)6 StateNode (com.vaadin.flow.internal.StateNode)5 ModelMap (com.vaadin.flow.internal.nodefeature.ModelMap)5 ParameterizedType (java.lang.reflect.ParameterizedType)4 Type (java.lang.reflect.Type)4 BeanModelType (com.vaadin.flow.template.angular.model.BeanModelType)3 ComplexModelType (com.vaadin.flow.templatemodel.ComplexModelType)3 InvalidTemplateModelException (com.vaadin.flow.templatemodel.InvalidTemplateModelException)3 Test (org.junit.Test)3 ListModelType (com.vaadin.flow.template.angular.model.ListModelType)2 BeanModelType (com.vaadin.flow.templatemodel.BeanModelType)2 ListModelType (com.vaadin.flow.templatemodel.ListModelType)2 TemplateElementStateProvider (com.vaadin.flow.dom.impl.TemplateElementStateProvider)1 ReflectTools (com.vaadin.flow.internal.ReflectTools)1 ModelList (com.vaadin.flow.internal.nodefeature.ModelList)1 TemplateMap (com.vaadin.flow.internal.nodefeature.TemplateMap)1 JsExpressionBindingProvider (com.vaadin.flow.template.angular.JsExpressionBindingProvider)1 TestModelDescriptor (com.vaadin.flow.template.angular.model.TestModelDescriptor)1 PropertyFilter (com.vaadin.flow.templatemodel.PropertyFilter)1