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());
}
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));
}
}
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));
}
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");
}
}
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);
}
Aggregations