Search in sources :

Example 1 with ComponentMetadata

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

the class ComponentGenerator method generateClassSource.

/*
     * Gets the JavaClassSource object (note, the license is added externally to
     * the source, since JavaClassSource doesn't support adding a comment to the
     * beginning of the file).
     */
private JavaClassSource generateClassSource(ComponentMetadata metadata, String basePackage) {
    String targetPackage = basePackage;
    String baseUrl = metadata.getBaseUrl();
    if (StringUtils.isNotBlank(baseUrl)) {
        // this is a fugly way to remove that
        if (baseUrl.contains("/src/")) {
            baseUrl = baseUrl.replace("/src/", "/");
        }
        String subPackage = ComponentGeneratorUtils.convertFilePathToPackage(baseUrl);
        if (StringUtils.isNotBlank(subPackage)) {
            int firstDot = subPackage.indexOf('.');
            if (firstDot > 0) {
                String firstSegment = subPackage.substring(0, firstDot);
                String lastSegment = subPackage.substring(firstDot + 1);
                subPackage = lastSegment.replace(".", "");
                if (!"vaadin".equals(firstSegment)) {
                    subPackage = firstSegment + "." + subPackage;
                }
            }
            targetPackage += "." + subPackage;
        }
    }
    JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
    javaClass.setPackage(targetPackage).setPublic().setAbstract(abstractClass).setName(getGeneratedClassName(metadata.getTag()));
    if (metadata.getParentTagName() != null) {
        javaClass.setSuperType(getGeneratedClassName(metadata.getParentTagName()) + GENERIC_TYPE_DECLARATION);
    } else {
        javaClass.setSuperType(Component.class);
        addInterfaces(metadata, javaClass);
    }
    javaClass.addTypeVariable().setName(GENERIC_TYPE).setBounds(javaClass.getName() + GENERIC_TYPE_DECLARATION);
    addClassAnnotations(metadata, javaClass);
    Map<String, MethodSource<JavaClassSource>> propertyToGetterMap = new HashMap<String, MethodSource<JavaClassSource>>();
    if (metadata.getProperties() != null) {
        generateEventsForPropertiesWithNotify(metadata);
        generateGettersAndSetters(metadata, javaClass, propertyToGetterMap);
    }
    if (metadata.getMethods() != null) {
        metadata.getMethods().stream().filter(function -> !ExclusionRegistry.isMethodExcluded(metadata.getTag(), function.getName())).forEach(function -> generateMethodFor(javaClass, function));
    }
    if (metadata.getEvents() != null) {
        metadata.getEvents().stream().filter(event -> !ExclusionRegistry.isEventExcluded(metadata.getTag(), event.getName())).forEach(event -> generateEventListenerFor(javaClass, metadata, event, propertyToGetterMap));
    }
    if (metadata.getSlots() != null && !metadata.getSlots().isEmpty()) {
        generateAdders(metadata, javaClass);
    }
    if (StringUtils.isNotEmpty(metadata.getDescription())) {
        addMarkdownJavaDoc(metadata.getDescription(), javaClass.getJavaDoc());
    }
    generateConstructors(javaClass);
    return javaClass;
}
Also used : Component(com.vaadin.flow.component.Component) Registration(com.vaadin.flow.shared.Registration) LoggerFactory(org.slf4j.LoggerFactory) ComponentFunctionData(com.vaadin.generator.metadata.ComponentFunctionData) ComponentBasicType(com.vaadin.generator.metadata.ComponentBasicType) StringUtils(org.apache.commons.lang3.StringUtils) ParameterSource(org.jboss.forge.roaster.model.source.ParameterSource) BehaviorRegistry(com.vaadin.generator.registry.BehaviorRegistry) ClassUtils(org.apache.commons.lang3.ClassUtils) Generated(javax.annotation.Generated) Roaster(org.jboss.forge.roaster.Roaster) ComponentPropertyData(com.vaadin.generator.metadata.ComponentPropertyData) Map(java.util.Map) Element(com.vaadin.flow.dom.Element) Synchronize(com.vaadin.flow.component.Synchronize) HasComponents(com.vaadin.flow.component.HasComponents) MethodSource(org.jboss.forge.roaster.model.source.MethodSource) Visibility(org.jboss.forge.roaster.model.Visibility) HasStyle(com.vaadin.flow.component.HasStyle) JavaDocSource(org.jboss.forge.roaster.model.source.JavaDocSource) Collection(java.util.Collection) Set(java.util.Set) Objects(java.util.Objects) List(java.util.List) JsonSerializable(com.vaadin.flow.component.JsonSerializable) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) ComponentPropertyBaseData(com.vaadin.generator.metadata.ComponentPropertyBaseData) Optional(java.util.Optional) ComponentEventListener(com.vaadin.flow.component.ComponentEventListener) HasValue(com.vaadin.flow.component.HasValue) ComponentGenerationException(com.vaadin.generator.exception.ComponentGenerationException) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) DomEvent(com.vaadin.flow.component.DomEvent) Tag(com.vaadin.flow.component.Tag) Properties(java.util.Properties) JsonParser(com.fasterxml.jackson.core.JsonParser) Files(java.nio.file.Files) EventData(com.vaadin.flow.component.EventData) ComponentObjectType(com.vaadin.generator.metadata.ComponentObjectType) UTF_8(java.nio.charset.StandardCharsets.UTF_8) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PropertyNameRemapRegistry(com.vaadin.generator.registry.PropertyNameRemapRegistry) IOException(java.io.IOException) ExclusionRegistry(com.vaadin.generator.registry.ExclusionRegistry) ComponentEvent(com.vaadin.flow.component.ComponentEvent) File(java.io.File) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) JsonFactory(com.fasterxml.jackson.core.JsonFactory) ComponentMetadata(com.vaadin.generator.metadata.ComponentMetadata) ComponentType(com.vaadin.generator.metadata.ComponentType) JsonObject(elemental.json.JsonObject) ComponentEventData(com.vaadin.generator.metadata.ComponentEventData) HasText(com.vaadin.flow.component.HasText) NotSupported(com.vaadin.flow.component.NotSupported) Collections(java.util.Collections) InputStream(java.io.InputStream) HashMap(java.util.HashMap) MethodSource(org.jboss.forge.roaster.model.source.MethodSource) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource)

Example 2 with ComponentMetadata

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

the class ComponentGeneratorTest method init.

@Before
public void init() {
    generator = new ComponentGenerator();
    componentMetadata = new ComponentMetadata();
    componentMetadata.setTag("my-component");
    componentMetadata.setName("MyComponent");
    componentMetadata.setBaseUrl("my-component/my-component.html");
    componentMetadata.setVersion("0.0.1");
    componentMetadata.setDescription("Test java doc creation for class file");
}
Also used : ComponentMetadata(com.vaadin.generator.metadata.ComponentMetadata) Before(org.junit.Before)

Example 3 with ComponentMetadata

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

the class PropertyNameRemapRegistryTest method hasValueWithRemapping_remapFromOtherToValue.

@Test
public void hasValueWithRemapping_remapFromOtherToValue() {
    ComponentGenerator generator = new ComponentGenerator();
    ComponentMetadata componentMetadata = new ComponentMetadata();
    componentMetadata.setName("test-name");
    componentMetadata.setTag(TEST_COMPONENT_TAG);
    componentMetadata.setBaseUrl("");
    ComponentPropertyData propertyData = new ComponentPropertyData();
    propertyData.setName("map-to-value");
    propertyData.setType(Collections.singleton(ComponentBasicType.STRING));
    componentMetadata.setProperties(Arrays.asList(propertyData));
    ComponentEventData eventData = new ComponentEventData();
    eventData.setName("map-to-value-changed");
    componentMetadata.setEvents(Arrays.asList(eventData));
    String generated = generator.generateClass(componentMetadata, "com.my.test", null);
    ComponentGeneratorTestUtils.assertClassImplementsInterface(generated, "TestTag", HasValue.class);
    Assert.assertFalse("Remapped value change event should not be generated", generated.contains("ValueChangeEvent"));
    Assert.assertTrue("HasValue getClientValuePropertyName overridden", generated.contains("String getClientValuePropertyName()"));
    Assert.assertTrue("HasValue getClientValuePropertyName overridden", generated.contains("return \"map-to-value\";"));
}
Also used : ComponentGenerator(com.vaadin.generator.ComponentGenerator) ComponentEventData(com.vaadin.generator.metadata.ComponentEventData) ComponentMetadata(com.vaadin.generator.metadata.ComponentMetadata) ComponentPropertyData(com.vaadin.generator.metadata.ComponentPropertyData) Test(org.junit.Test)

Example 4 with ComponentMetadata

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

the class PropertyNameRemapRegistryTest method init.

@Before
public void init() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException {
    Field registryField = PropertyNameRemapRegistry.class.getDeclaredField("REGISTRY");
    registryField.setAccessible(true);
    @SuppressWarnings("rawtypes") Map registry = (Map) registryField.get(null);
    registry.clear();
    Method putMethod = PropertyNameRemapRegistry.class.getDeclaredMethod("put", String.class, String.class, String.class);
    putMethod.setAccessible(true);
    putMethod.invoke(null, TEST_COMPONENT_TAG, "original-property-name", "renamed");
    putMethod.invoke(null, TEST_COMPONENT_TAG, "value", "other-value");
    putMethod.invoke(null, TEST_COMPONENT_TAG, "map-to-value", "value");
    ComponentGenerator generator = new ComponentGenerator();
    ComponentMetadata componentMetadata = new ComponentMetadata();
    componentMetadata.setName("test-name");
    componentMetadata.setTag(TEST_COMPONENT_TAG);
    componentMetadata.setBaseUrl("");
    ComponentPropertyData propertyData = new ComponentPropertyData();
    propertyData.setName("original-property-name");
    propertyData.setType(Collections.singleton(ComponentBasicType.STRING));
    componentMetadata.setProperties(Arrays.asList(propertyData));
    ComponentEventData eventData = new ComponentEventData();
    eventData.setName("original-property-name-changed");
    componentMetadata.setEvents(Arrays.asList(eventData));
    generatedSource = generator.generateClass(componentMetadata, "com.my.test", null);
}
Also used : Field(java.lang.reflect.Field) ComponentGenerator(com.vaadin.generator.ComponentGenerator) ComponentEventData(com.vaadin.generator.metadata.ComponentEventData) Method(java.lang.reflect.Method) ComponentMetadata(com.vaadin.generator.metadata.ComponentMetadata) ComponentPropertyData(com.vaadin.generator.metadata.ComponentPropertyData) Map(java.util.Map) Before(org.junit.Before)

Example 5 with ComponentMetadata

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

the class PropertyNameRemapRegistryTest method hasValueWithRemapping_remapFromValueToOther.

@Test
public void hasValueWithRemapping_remapFromValueToOther() {
    ComponentGenerator generator = new ComponentGenerator();
    ComponentMetadata componentMetadata = new ComponentMetadata();
    componentMetadata.setName("test-name");
    componentMetadata.setTag(TEST_COMPONENT_TAG);
    componentMetadata.setBaseUrl("");
    ComponentPropertyData propertyData = new ComponentPropertyData();
    propertyData.setName("value");
    propertyData.setType(Collections.singleton(ComponentBasicType.STRING));
    componentMetadata.setProperties(Arrays.asList(propertyData));
    ComponentEventData eventData = new ComponentEventData();
    eventData.setName("value-changed");
    componentMetadata.setEvents(Arrays.asList(eventData));
    String generated = generator.generateClass(componentMetadata, "com.my.test", null);
    Assert.assertFalse("Remapped value property should not generate the HasValue interface", generated.contains("HasValue"));
    Assert.assertTrue("Remapped change event should be generated", generated.contains("OtherValueChangeEvent"));
}
Also used : ComponentGenerator(com.vaadin.generator.ComponentGenerator) ComponentEventData(com.vaadin.generator.metadata.ComponentEventData) ComponentMetadata(com.vaadin.generator.metadata.ComponentMetadata) ComponentPropertyData(com.vaadin.generator.metadata.ComponentPropertyData) Test(org.junit.Test)

Aggregations

ComponentMetadata (com.vaadin.generator.metadata.ComponentMetadata)5 ComponentEventData (com.vaadin.generator.metadata.ComponentEventData)4 ComponentPropertyData (com.vaadin.generator.metadata.ComponentPropertyData)4 ComponentGenerator (com.vaadin.generator.ComponentGenerator)3 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Component (com.vaadin.flow.component.Component)1 ComponentEvent (com.vaadin.flow.component.ComponentEvent)1 ComponentEventListener (com.vaadin.flow.component.ComponentEventListener)1 DomEvent (com.vaadin.flow.component.DomEvent)1 EventData (com.vaadin.flow.component.EventData)1 HasComponents (com.vaadin.flow.component.HasComponents)1 HasStyle (com.vaadin.flow.component.HasStyle)1 HasText (com.vaadin.flow.component.HasText)1 HasValue (com.vaadin.flow.component.HasValue)1 JsonSerializable (com.vaadin.flow.component.JsonSerializable)1 NotSupported (com.vaadin.flow.component.NotSupported)1 Synchronize (com.vaadin.flow.component.Synchronize)1 Tag (com.vaadin.flow.component.Tag)1