Search in sources :

Example 1 with ComponentBasicType

use of com.vaadin.generator.metadata.ComponentBasicType in project flow by vaadin.

the class ComponentGenerator method generateSetterFor.

private void generateSetterFor(JavaClassSource javaClass, ComponentMetadata metadata, ComponentPropertyData property) {
    String propertyJavaName = getJavaNameForProperty(metadata, property.getName());
    boolean isValue = "value".equals(propertyJavaName);
    if (containsObjectType(property)) {
        // the getter already created the nested pojo, so here we just need
        // to get the name
        String nestedClassName = ComponentGeneratorUtils.generateValidJavaClassName(propertyJavaName + "-property");
        MethodSource<JavaClassSource> method = javaClass.addMethod().setName(ComponentGeneratorUtils.generateMethodNameForProperty("set", propertyJavaName));
        setMethodVisibility(method, property.getObjectType());
        method.addParameter(nestedClassName, "property");
        method.setBody(String.format("getElement().setPropertyJson(\"%s\", property.toJson());", property.getName()));
        if (StringUtils.isNotEmpty(property.getDescription())) {
            addMarkdownJavaDoc(property.getDescription(), method.getJavaDoc());
        }
        method.getJavaDoc().addTagValue(JAVADOC_PARAM, "property the property to set");
        if (fluentMethod) {
            addFluentReturnToMethod(method);
        }
        if (isValue && shouldImplementHasValue(metadata)) {
            method.addAnnotation(Override.class);
            preventSettingTheSameValue(javaClass, "property", method);
        }
    } else {
        for (ComponentBasicType basicType : property.getType()) {
            MethodSource<JavaClassSource> method = javaClass.addMethod().setName(ComponentGeneratorUtils.generateMethodNameForProperty("set", propertyJavaName));
            setMethodVisibility(method, basicType);
            Class<?> setterType = ComponentGeneratorUtils.toJavaType(basicType);
            String parameterName = ComponentGeneratorUtils.formatStringToValidJavaIdentifier(propertyJavaName);
            ComponentGeneratorUtils.addMethodParameter(javaClass, method, setterType, parameterName);
            boolean nullable = !isValue || !shouldImplementHasValue(metadata) || String.class != setterType;
            method.setBody(ComponentGeneratorUtils.generateElementApiSetterForType(basicType, property.getName(), parameterName, nullable));
            if (StringUtils.isNotEmpty(property.getDescription())) {
                addMarkdownJavaDoc(property.getDescription(), method.getJavaDoc());
            }
            method.getJavaDoc().addTagValue(JAVADOC_PARAM, String.format("%s the %s value to set", parameterName, setterType.getSimpleName()));
            if (fluentMethod) {
                addFluentReturnToMethod(method);
            }
            if (isValue && shouldImplementHasValue(metadata)) {
                method.addAnnotation(Override.class);
                preventSettingTheSameValue(javaClass, parameterName, method);
                if (setterType.isPrimitive()) {
                    implementHasValueSetterWithPrimitiveType(javaClass, property, method, setterType, parameterName);
                } else if (!nullable) {
                    method.setBody(String.format("Objects.requireNonNull(%s, \"%s cannot be null\");", parameterName, parameterName) + method.getBody());
                }
            }
        }
    }
}
Also used : JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) ComponentBasicType(com.vaadin.generator.metadata.ComponentBasicType)

Example 2 with ComponentBasicType

use of com.vaadin.generator.metadata.ComponentBasicType in project flow by vaadin.

the class ComponentGenerator method generateMethodParameters.

/**
 * Adds the parameters and javadocs to the given method and generates nested
 * classes for complex object parameters if needed.
 *
 * @param javaClass
 *            the main class file
 * @param method
 *            the method to add parameters to
 * @param function
 *            the function data
 * @param typeVariant
 *            the list of types to use for each added parameter
 * @param nestedClassesMap
 *            map for memorizing already generated nested classes
 * @return a string of the parameters of the function, or an empty string if
 *         no parameters
 */
private String generateMethodParameters(JavaClassSource javaClass, MethodSource<JavaClassSource> method, ComponentFunctionData function, List<ComponentType> typeVariant, Map<ComponentObjectType, JavaClassSource> nestedClassesMap) {
    int paramIndex = 0;
    StringBuilder sb = new StringBuilder();
    for (ComponentType paramType : typeVariant) {
        String paramName = function.getParameters().get(paramIndex).getName();
        String paramDescription = function.getParameters().get(paramIndex).getDescription();
        String formattedName = StringUtils.uncapitalize(ComponentGeneratorUtils.formatStringToValidJavaIdentifier(function.getParameters().get(paramIndex).getName()));
        paramIndex++;
        if (paramType.isBasicType()) {
            ComponentBasicType bt = (ComponentBasicType) paramType;
            ComponentGeneratorUtils.addMethodParameter(javaClass, method, ComponentGeneratorUtils.toJavaType(bt), formattedName);
            sb.append(", ").append(formattedName);
        } else {
            ComponentObjectType ot = (ComponentObjectType) paramType;
            String nameHint = function.getName() + "-" + paramName;
            JavaClassSource nestedClass = nestedClassesMap.computeIfAbsent(ot, objectType -> generateNestedPojo(javaClass, objectType, nameHint, String.format("Class that encapsulates the data to be sent to the {@link %s#%s(%s)} method.", javaClass.getName(), method.getName(), ComponentGeneratorUtils.generateValidJavaClassName(nameHint))));
            sb.append(", ").append(formattedName).append(".toJson()");
            method.getJavaDoc().addTagValue(JAVADOC_SEE, nestedClass.getName());
            method.addParameter(nestedClass, formattedName);
        }
        method.getJavaDoc().addTagValue(JAVADOC_PARAM, String.format("%s %s", formattedName, paramDescription));
    }
    return sb.toString();
}
Also used : ComponentType(com.vaadin.generator.metadata.ComponentType) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) ComponentObjectType(com.vaadin.generator.metadata.ComponentObjectType) ComponentBasicType(com.vaadin.generator.metadata.ComponentBasicType)

Example 3 with ComponentBasicType

use of com.vaadin.generator.metadata.ComponentBasicType in project flow by vaadin.

the class NestedClassGenerator method build.

/**
 * Builds the Java class by using the defined settings.
 *
 * @return the final Java class source, ready to be embedded in the main
 *         class of the component.
 */
public JavaClassSource build() {
    JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
    javaClass.addInterface(JsonSerializable.class).setPublic().setStatic(true).setName(ComponentGeneratorUtils.generateValidJavaClassName(nameHint));
    javaClass.addField().setType(JsonObject.class).setPrivate().setName("internalObject");
    for (ComponentObjectTypeInnerType object : type.getInnerTypes()) {
        ComponentBasicType simpleType = getSimpleBasicType(object.getType());
        generateGetter(javaClass, object, simpleType);
        generateSetter(javaClass, object, simpleType);
    }
    generateToJson(javaClass);
    generateReadJson(javaClass);
    return javaClass;
}
Also used : JsonSerializable(com.vaadin.flow.component.JsonSerializable) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) ComponentObjectTypeInnerType(com.vaadin.generator.metadata.ComponentObjectType.ComponentObjectTypeInnerType) ComponentBasicType(com.vaadin.generator.metadata.ComponentBasicType)

Example 4 with ComponentBasicType

use of com.vaadin.generator.metadata.ComponentBasicType in project flow by vaadin.

the class ComponentGenerator method generateGetterFor.

private void generateGetterFor(JavaClassSource javaClass, ComponentMetadata metadata, ComponentPropertyData property, List<ComponentEventData> events, Map<String, MethodSource<JavaClassSource>> propertyToGetterMap) {
    String propertyJavaName = getJavaNameForProperty(metadata, property.getName());
    MethodSource<JavaClassSource> method = javaClass.addMethod();
    propertyToGetterMap.put(propertyJavaName, method);
    if (containsObjectType(property)) {
        JavaClassSource nestedClass = generateNestedPojo(javaClass, property.getObjectType().get(0), propertyJavaName + "-property", String.format("Class that encapsulates the data of the '%s' property in the {@link %s} component.", property.getName(), javaClass.getName()));
        method.setReturnType(nestedClass);
        setMethodVisibility(method, property.getObjectType());
        method.setName(ComponentGeneratorUtils.generateMethodNameForProperty("get", propertyJavaName));
        method.setBody(String.format("return new %s().readJson((JsonObject) getElement().getPropertyRaw(\"%s\"));", nestedClass.getName(), property.getName()));
        if (method.getVisibility() == Visibility.PROTECTED) {
            method.setName(method.getName() + StringUtils.capitalize(nestedClass.getName()));
        }
        addSynchronizeAnnotationAndJavadocToGetter(method, property, events);
        if ("value".equals(propertyJavaName) && shouldImplementHasValue(metadata)) {
            javaClass.addInterface(HasValue.class.getName() + "<" + GENERIC_TYPE + ", " + nestedClass.getName() + ">");
            method.addAnnotation(Override.class);
            method.setBody(String.format("JsonObject _obj = (JsonObject) getElement().getPropertyRaw(\"%s\");" + "return _obj == null ? getEmptyValue() : new %s().readJson(_obj);", property.getName(), nestedClass.getName()));
        } else {
            method.setBody(String.format("return new %s().readJson((JsonObject) getElement().getPropertyRaw(\"%s\"));", nestedClass.getName(), property.getName()));
        }
    } else {
        boolean postfixWithVariableType = property.getType().size() > 1;
        if (postfixWithVariableType) {
            property.setType(new TreeSet<>(property.getType()));
        }
        for (ComponentBasicType basicType : property.getType()) {
            Class<?> javaType = ComponentGeneratorUtils.toJavaType(basicType);
            method.setReturnType(javaType);
            setMethodVisibility(method, basicType);
            if (basicType == ComponentBasicType.BOOLEAN) {
                if (!propertyJavaName.startsWith("is") && !propertyJavaName.startsWith("has") && !propertyJavaName.startsWith("have")) {
                    method.setName(ComponentGeneratorUtils.generateMethodNameForProperty("is", propertyJavaName));
                } else {
                    method.setName(ComponentGeneratorUtils.formatStringToValidJavaIdentifier(propertyJavaName));
                }
            } else {
                method.setName(ComponentGeneratorUtils.generateMethodNameForProperty("get", propertyJavaName) + (postfixWithVariableType ? StringUtils.capitalize(basicType.name().toLowerCase()) : ""));
            }
            if (method.getVisibility() == Visibility.PROTECTED) {
                method.setName(method.getName() + StringUtils.capitalize(javaType.getSimpleName()));
            }
            addSynchronizeAnnotationAndJavadocToGetter(method, property, events);
            if ("value".equals(propertyJavaName) && shouldImplementHasValue(metadata)) {
                method.setName("getValue");
                if (javaType.isPrimitive()) {
                    javaType = ClassUtils.primitiveToWrapper(javaType);
                    method.setReturnType(javaType);
                }
                javaClass.addInterface(HasValue.class.getName() + "<" + GENERIC_TYPE + ", " + javaType.getName() + ">");
                method.addAnnotation(Override.class);
                method.setBody(ComponentGeneratorUtils.generateElementApiValueGetterForType(basicType, property.getName()));
                addGetEmptyValueIfString(javaClass, javaType);
            } else {
                method.setBody(ComponentGeneratorUtils.generateElementApiGetterForType(basicType, property.getName()));
            }
        }
    }
}
Also used : JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) ComponentBasicType(com.vaadin.generator.metadata.ComponentBasicType)

Aggregations

ComponentBasicType (com.vaadin.generator.metadata.ComponentBasicType)4 JavaClassSource (org.jboss.forge.roaster.model.source.JavaClassSource)4 JsonSerializable (com.vaadin.flow.component.JsonSerializable)1 ComponentObjectType (com.vaadin.generator.metadata.ComponentObjectType)1 ComponentObjectTypeInnerType (com.vaadin.generator.metadata.ComponentObjectType.ComponentObjectTypeInnerType)1 ComponentType (com.vaadin.generator.metadata.ComponentType)1