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;
}
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); } }"));
}
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)"));
}
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)"));
}
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)"));
}
Aggregations