Search in sources :

Example 1 with ComponentObjectType

use of com.vaadin.generator.metadata.ComponentObjectType 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 ComponentObjectType

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

the class ComponentGeneratorTest method classContainsEventWithObjectParameter_generatedClassContainsInnerClass.

@Test
public void classContainsEventWithObjectParameter_generatedClassContainsInnerClass() {
    // note: the tests for the nested class are covered by the
    // NestedClassGeneratorTest
    ComponentObjectTypeInnerType stringObjectType = new ComponentObjectTypeInnerType();
    stringObjectType.setName("internalString");
    stringObjectType.setType(Collections.singletonList(ComponentBasicType.STRING));
    ComponentObjectType objectType = new ComponentObjectType();
    objectType.setInnerTypes(Collections.singletonList(stringObjectType));
    ComponentPropertyBaseData eventData = new ComponentPropertyBaseData();
    eventData.setName("details");
    eventData.setObjectType(Collections.singletonList(objectType));
    ComponentEventData event = new ComponentEventData();
    event.setName("something-happened");
    event.setProperties(Collections.singletonList(eventData));
    componentMetadata.setEvents(Collections.singletonList(event));
    String generatedClass = generator.generateClass(componentMetadata, "com.my.test", null);
    generatedClass = ComponentGeneratorTestUtils.removeIndentation(generatedClass);
    Assert.assertTrue("Generated class should contain the SomethingHappenedDetails nested class", generatedClass.contains("public static class SomethingHappenedDetails implements JsonSerializable"));
    Assert.assertTrue("Generated class should contain the addSomethingChangHappenedListener method", generatedClass.contains("public Registration addSomethingHappenedListener( ComponentEventListener<SomethingHappenedEvent<R>> listener)"));
    int indexOfEventDeclaration = generatedClass.indexOf("public static class SomethingHappenedEvent<R extends MyComponent<R>> extends ComponentEvent<R> {");
    int endIndexOfEventDeclaration = generatedClass.indexOf("} }", indexOfEventDeclaration);
    String eventDeclaration = generatedClass.substring(indexOfEventDeclaration, endIndexOfEventDeclaration + 3);
    Assert.assertTrue("Generated event should contain the getDetails method", eventDeclaration.contains("public SomethingHappenedDetails getDetails() { return new SomethingHappenedDetails().readJson(details); } }"));
}
Also used : ComponentEventData(com.vaadin.generator.metadata.ComponentEventData) ComponentPropertyBaseData(com.vaadin.generator.metadata.ComponentPropertyBaseData) ComponentObjectTypeInnerType(com.vaadin.generator.metadata.ComponentObjectType.ComponentObjectTypeInnerType) ComponentObjectType(com.vaadin.generator.metadata.ComponentObjectType) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 3 with ComponentObjectType

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

the class ComponentGeneratorTest method classContainsMethodWithObjectParameter_generatedClassContainsInnerClass.

@Test
public void classContainsMethodWithObjectParameter_generatedClassContainsInnerClass() {
    // note: the tests for the nested class are covered by the
    // NestedClassGeneratorTest
    ComponentObjectTypeInnerType stringObjectType = new ComponentObjectTypeInnerType();
    stringObjectType.setName("internalString");
    stringObjectType.setType(Collections.singletonList(ComponentBasicType.STRING));
    ComponentObjectType objectType = new ComponentObjectType();
    objectType.setInnerTypes(Collections.singletonList(stringObjectType));
    ComponentFunctionParameterData parameter = new ComponentFunctionParameterData();
    parameter.setName("somethingParam");
    parameter.setObjectType(Collections.singletonList(objectType));
    ComponentFunctionData function = new ComponentFunctionData();
    function.setName("callSomething");
    function.setParameters(Collections.singletonList(parameter));
    componentMetadata.setMethods(Collections.singletonList(function));
    String generatedClass = generator.generateClass(componentMetadata, "com.my.test", null);
    generatedClass = ComponentGeneratorTestUtils.removeIndentation(generatedClass);
    Assert.assertTrue("Generated class should contain the CallSomethingSomethingParam nested class", generatedClass.contains("public static class CallSomethingSomethingParam implements JsonSerializable"));
    Assert.assertTrue("Generated class should contain the callSomething method", generatedClass.contains("public void callSomething(CallSomethingSomethingParam somethingParam)"));
}
Also used : ComponentFunctionParameterData(com.vaadin.generator.metadata.ComponentFunctionParameterData) ComponentObjectTypeInnerType(com.vaadin.generator.metadata.ComponentObjectType.ComponentObjectTypeInnerType) ComponentObjectType(com.vaadin.generator.metadata.ComponentObjectType) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ComponentFunctionData(com.vaadin.generator.metadata.ComponentFunctionData) Test(org.junit.Test)

Example 4 with ComponentObjectType

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

the class ComponentGeneratorTest method classContainsOverloadedMethodsForMethodsThatAcceptMultipleTypes_withObjectTypes.

@Test
public void classContainsOverloadedMethodsForMethodsThatAcceptMultipleTypes_withObjectTypes() {
    ComponentObjectTypeInnerType stringObjectTypeInnerType = new ComponentObjectTypeInnerType();
    stringObjectTypeInnerType.setName("internalString");
    stringObjectTypeInnerType.setType(Collections.singletonList(ComponentBasicType.STRING));
    ComponentObjectType stringObjectType = new ComponentObjectType();
    stringObjectType.setInnerTypes(Collections.singletonList(stringObjectTypeInnerType));
    ComponentFunctionParameterData firstParameter = new ComponentFunctionParameterData();
    firstParameter.setName("firstParam");
    firstParameter.setObjectType(Collections.singletonList(stringObjectType));
    ComponentFunctionParameterData secondParameter = new ComponentFunctionParameterData();
    secondParameter.setName("secondParam");
    secondParameter.setType(new HashSet<>(Arrays.asList(ComponentBasicType.STRING, ComponentBasicType.BOOLEAN)));
    ComponentFunctionData function = new ComponentFunctionData();
    function.setName("callSomething");
    function.setParameters(Arrays.asList(firstParameter, secondParameter));
    componentMetadata.setMethods(Collections.singletonList(function));
    String generatedClass = generator.generateClass(componentMetadata, "com.my.test", null);
    generatedClass = ComponentGeneratorTestUtils.removeIndentation(generatedClass);
    Assert.assertTrue(generatedClass.contains("public void callSomething(CallSomethingFirstParam firstParam, String secondParam)"));
    Assert.assertTrue(generatedClass.contains("public void callSomething(CallSomethingFirstParam firstParam, boolean secondParam)"));
}
Also used : ComponentFunctionParameterData(com.vaadin.generator.metadata.ComponentFunctionParameterData) ComponentObjectTypeInnerType(com.vaadin.generator.metadata.ComponentObjectType.ComponentObjectTypeInnerType) ComponentObjectType(com.vaadin.generator.metadata.ComponentObjectType) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ComponentFunctionData(com.vaadin.generator.metadata.ComponentFunctionData) Test(org.junit.Test)

Example 5 with ComponentObjectType

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

the class ComponentGeneratorTest method classContainsObjectProperty_generatedClassContainsInnerClass.

@Test
public void classContainsObjectProperty_generatedClassContainsInnerClass() {
    // note: the tests for the nested class are covered by the
    // NestedClassGeneratorTest
    ComponentObjectTypeInnerType stringObjectType = new ComponentObjectTypeInnerType();
    stringObjectType.setName("internalString");
    stringObjectType.setType(Collections.singletonList(ComponentBasicType.STRING));
    ComponentObjectType objectType = new ComponentObjectType();
    objectType.setInnerTypes(Collections.singletonList(stringObjectType));
    ComponentPropertyData property = new ComponentPropertyData();
    property.setName("something");
    property.setObjectType(Collections.singletonList(objectType));
    componentMetadata.setProperties(Collections.singletonList(property));
    String generatedClass = generator.generateClass(componentMetadata, "com.my.test", null);
    generatedClass = ComponentGeneratorTestUtils.removeIndentation(generatedClass);
    Assert.assertTrue("Generated class should contain the SomethingProperty nested class", generatedClass.contains("public static class SomethingProperty implements JsonSerializable"));
    Assert.assertTrue("Generated class should contain the getSomething method", generatedClass.contains("public SomethingProperty getSomething()"));
    Assert.assertTrue("Generated class should contain the setSomething method", generatedClass.contains("public void setSomething(SomethingProperty property)"));
}
Also used : ComponentObjectTypeInnerType(com.vaadin.generator.metadata.ComponentObjectType.ComponentObjectTypeInnerType) ComponentObjectType(com.vaadin.generator.metadata.ComponentObjectType) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ComponentPropertyData(com.vaadin.generator.metadata.ComponentPropertyData) Test(org.junit.Test)

Aggregations

ComponentObjectType (com.vaadin.generator.metadata.ComponentObjectType)7 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)5 Test (org.junit.Test)5 ComponentObjectTypeInnerType (com.vaadin.generator.metadata.ComponentObjectType.ComponentObjectTypeInnerType)4 ComponentEventData (com.vaadin.generator.metadata.ComponentEventData)2 ComponentFunctionData (com.vaadin.generator.metadata.ComponentFunctionData)2 ComponentFunctionParameterData (com.vaadin.generator.metadata.ComponentFunctionParameterData)2 ComponentPropertyData (com.vaadin.generator.metadata.ComponentPropertyData)2 ComponentBasicType (com.vaadin.generator.metadata.ComponentBasicType)1 ComponentPropertyBaseData (com.vaadin.generator.metadata.ComponentPropertyBaseData)1 ComponentType (com.vaadin.generator.metadata.ComponentType)1 JavaClassSource (org.jboss.forge.roaster.model.source.JavaClassSource)1 Before (org.junit.Before)1