Search in sources :

Example 1 with ComponentObjectTypeInnerType

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

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

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

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

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

ComponentObjectTypeInnerType (com.vaadin.generator.metadata.ComponentObjectType.ComponentObjectTypeInnerType)6 ComponentObjectType (com.vaadin.generator.metadata.ComponentObjectType)4 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)4 Test (org.junit.Test)4 ComponentFunctionData (com.vaadin.generator.metadata.ComponentFunctionData)2 ComponentFunctionParameterData (com.vaadin.generator.metadata.ComponentFunctionParameterData)2 JsonSerializable (com.vaadin.flow.component.JsonSerializable)1 ComponentBasicType (com.vaadin.generator.metadata.ComponentBasicType)1 ComponentEventData (com.vaadin.generator.metadata.ComponentEventData)1 ComponentPropertyBaseData (com.vaadin.generator.metadata.ComponentPropertyBaseData)1 ComponentPropertyData (com.vaadin.generator.metadata.ComponentPropertyData)1 JavaClassSource (org.jboss.forge.roaster.model.source.JavaClassSource)1