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