Search in sources :

Example 1 with ComponentType

use of com.vaadin.generator.metadata.ComponentType 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 2 with ComponentType

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

the class FunctionParameterVariantCombinator method generateCombinations.

private static Set<List<ComponentType>> generateCombinations(ComponentFunctionParameterData paramData, List<ComponentFunctionParameterData> rest) {
    Comparator<List<ComponentType>> listComparator = Comparator.comparing(List::toString);
    if (rest.isEmpty()) {
        return getTypeVariants(paramData).stream().map(Arrays::asList).sorted(listComparator).collect(Collectors.toSet());
    }
    List<ComponentFunctionParameterData> copy = new ArrayList<>(rest);
    Set<List<ComponentType>> ret = new TreeSet<>(listComparator);
    for (List<ComponentType> subCombinations : generateCombinations(copy.remove(0), copy)) {
        for (ComponentType typeVariants : getTypeVariants(paramData)) {
            List<ComponentType> tmp = new ArrayList<>(subCombinations);
            tmp.add(0, typeVariants);
            ret.add(tmp);
        }
    }
    return ret;
}
Also used : ComponentFunctionParameterData(com.vaadin.generator.metadata.ComponentFunctionParameterData) ComponentType(com.vaadin.generator.metadata.ComponentType) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Arrays(java.util.Arrays)

Aggregations

ComponentType (com.vaadin.generator.metadata.ComponentType)2 ComponentBasicType (com.vaadin.generator.metadata.ComponentBasicType)1 ComponentFunctionParameterData (com.vaadin.generator.metadata.ComponentFunctionParameterData)1 ComponentObjectType (com.vaadin.generator.metadata.ComponentObjectType)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 TreeSet (java.util.TreeSet)1 JavaClassSource (org.jboss.forge.roaster.model.source.JavaClassSource)1