Search in sources :

Example 1 with ComponentPropertyBaseData

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

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

the class ComponentGenerator method addEventDataParameters.

private void addEventDataParameters(JavaClassSource javaClass, ComponentEventData event, String eventClassName, JavaClassSource eventClass, MethodSource<JavaClassSource> eventConstructor) {
    for (ComponentPropertyBaseData property : event.getProperties()) {
        // Add new parameter to constructor
        final String propertyName = property.getName();
        String normalizedProperty = ComponentGeneratorUtils.formatStringToValidJavaIdentifier(propertyName);
        Class<?> propertyJavaType;
        if (containsObjectType(property)) {
            JavaClassSource nestedClass = generateNestedPojo(javaClass, property.getObjectType().get(0), eventClassName + "-" + propertyName, String.format("Class that encapsulates the data received on the '%s' property of @{link %s} events, from the @{link %s} component.", propertyName, eventClass.getName(), javaClass.getName()));
            propertyJavaType = JsonObject.class;
            eventClass.addField().setType(propertyJavaType).setPrivate().setFinal(true).setName(normalizedProperty);
            eventClass.addMethod().setName(ComponentGeneratorUtils.generateMethodNameForProperty("get", propertyName)).setPublic().setReturnType(nestedClass).setBody(String.format("return new %s().readJson(%s);", nestedClass.getName(), normalizedProperty));
        } else {
            if (!property.getType().isEmpty()) {
                // for varying types, using the first type declared in
                // the
                // JSDoc
                // it is anyway very rare to have varying property type
                propertyJavaType = ComponentGeneratorUtils.toJavaType(property.getType().iterator().next());
            } else {
                // object property
                propertyJavaType = JsonObject.class;
            }
            // Create private field
            eventClass.addProperty(propertyJavaType, normalizedProperty).setAccessible(true).setMutable(false);
        }
        ParameterSource<JavaClassSource> parameter = ComponentGeneratorUtils.addMethodParameter(javaClass, eventConstructor, propertyJavaType, normalizedProperty);
        parameter.addAnnotation(EventData.class).setStringValue(String.format("event.%s", propertyName));
        // Set value to private field
        eventConstructor.setBody(String.format("%s%nthis.%s = %s;", eventConstructor.getBody(), normalizedProperty, normalizedProperty));
        // Add the EventData as a import
        javaClass.addImport(EventData.class);
    }
}
Also used : ComponentPropertyBaseData(com.vaadin.generator.metadata.ComponentPropertyBaseData) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) EventData(com.vaadin.flow.component.EventData) ComponentEventData(com.vaadin.generator.metadata.ComponentEventData)

Example 3 with ComponentPropertyBaseData

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

the class ComponentGeneratorTest method generateClassWithEventWithEventDataContainingDotNotation_classTypedComponentEventWithEventData.

@Test
public void generateClassWithEventWithEventDataContainingDotNotation_classTypedComponentEventWithEventData() {
    ComponentEventData eventData = new ComponentEventData();
    eventData.setName("change");
    eventData.setDescription("Component change event.");
    componentMetadata.setEvents(Collections.singletonList(eventData));
    ComponentPropertyBaseData property = new ComponentPropertyBaseData();
    property.setName("details.property");
    property.setType(Collections.singleton(ComponentBasicType.NUMBER));
    eventData.setProperties(Collections.singletonList(property));
    String generatedClass = generator.generateClass(componentMetadata, "com.my.test", null);
    // Using matcher as the formatter may cut the method.
    Pattern pattern = Pattern.compile("public ChangeEvent\\(R source, boolean fromClient,(\\w?)(\\s*?)@EventData\\(\"event\\.details\\.property\"\\) double detailsProperty\\)");
    Matcher matcher = pattern.matcher(generatedClass);
    Assert.assertTrue("Couldn't find constructor with EventData.", matcher.find());
    Assert.assertTrue("Couldn't find variable reference", generatedClass.contains("private final double detailsProperty;"));
    Assert.assertTrue("Couldn't find getter for event data", generatedClass.contains("public double getDetailsProperty() {"));
    Assert.assertFalse("Found setter even though one shouldn't exist", generatedClass.contains("public void setDetailsProperty("));
    Assert.assertTrue("Missing EventData import", generatedClass.contains("import " + EventData.class.getName() + ";"));
}
Also used : Pattern(java.util.regex.Pattern) ComponentEventData(com.vaadin.generator.metadata.ComponentEventData) Matcher(java.util.regex.Matcher) ComponentPropertyBaseData(com.vaadin.generator.metadata.ComponentPropertyBaseData) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 4 with ComponentPropertyBaseData

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

the class ComponentGeneratorTest method generateClassWithEventWithEventData_classTypedComponentEventWithEventData.

@Test
public void generateClassWithEventWithEventData_classTypedComponentEventWithEventData() {
    ComponentEventData eventData = new ComponentEventData();
    eventData.setName("change");
    eventData.setDescription("Component change event.");
    componentMetadata.setEvents(Collections.singletonList(eventData));
    ComponentPropertyBaseData property = new ComponentPropertyBaseData();
    property.setName("button");
    property.setType(Collections.singleton(ComponentBasicType.NUMBER));
    eventData.setProperties(Collections.singletonList(property));
    String generatedClass = generator.generateClass(componentMetadata, "com.my.test", null);
    // Using matcher as the formatter may cut the method.
    Pattern pattern = Pattern.compile("public ChangeEvent\\(R source, boolean fromClient,(\\w?)(\\s*?)@EventData\\(\"event\\.button\"\\) double button\\)");
    Matcher matcher = pattern.matcher(generatedClass);
    Assert.assertTrue("Couldn't find constructor with EventData.", matcher.find());
    Assert.assertTrue("Couldn't find variable reference", generatedClass.contains("private final double button;"));
    Assert.assertTrue("Couldn't find getter for event data", generatedClass.contains("public double getButton() {"));
    Assert.assertFalse("Found setter even though one shouldn't exist", generatedClass.contains("public void setButton("));
    Assert.assertTrue("Missing EventData import", generatedClass.contains("import " + EventData.class.getName() + ";"));
}
Also used : Pattern(java.util.regex.Pattern) ComponentEventData(com.vaadin.generator.metadata.ComponentEventData) Matcher(java.util.regex.Matcher) ComponentPropertyBaseData(com.vaadin.generator.metadata.ComponentPropertyBaseData) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Aggregations

ComponentEventData (com.vaadin.generator.metadata.ComponentEventData)4 ComponentPropertyBaseData (com.vaadin.generator.metadata.ComponentPropertyBaseData)4 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 Test (org.junit.Test)3 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 EventData (com.vaadin.flow.component.EventData)1 ComponentObjectType (com.vaadin.generator.metadata.ComponentObjectType)1 ComponentObjectTypeInnerType (com.vaadin.generator.metadata.ComponentObjectType.ComponentObjectTypeInnerType)1 JavaClassSource (org.jboss.forge.roaster.model.source.JavaClassSource)1