Search in sources :

Example 1 with InvalidTemplateModelException

use of com.vaadin.flow.templatemodel.InvalidTemplateModelException 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 2 with InvalidTemplateModelException

use of com.vaadin.flow.templatemodel.InvalidTemplateModelException 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)

Aggregations

InvalidTemplateModelException (com.vaadin.flow.templatemodel.InvalidTemplateModelException)2 ModelType (com.vaadin.flow.templatemodel.ModelType)2 ModelMap (com.vaadin.flow.internal.nodefeature.ModelMap)1 BasicModelType (com.vaadin.flow.templatemodel.BasicModelType)1 ComplexModelType (com.vaadin.flow.templatemodel.ComplexModelType)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 RuntimeType (net.bytebuddy.implementation.bind.annotation.RuntimeType)1