use of org.jboss.forge.roaster.model.source.JavaClassSource in project camel by apache.
the class RoasterMyFieldMethodCallRouteBuilderConfigureTest method parse.
@Test
public void parse() throws Exception {
JavaClassSource clazz = (JavaClassSource) Roaster.parse(new File("src/test/java/org/apache/camel/parser/java/MyFieldMethodCallRouteBuilder.java"));
MethodSource<JavaClassSource> method = clazz.getMethod("configure");
List<ParserResult> list = CamelJavaParserHelper.parseCamelConsumerUris(method, true, true);
for (ParserResult result : list) {
LOG.info("Consumer: " + result.getElement());
}
Assert.assertEquals("netty-http:http://0.0.0.0:{{port}}/foo", list.get(0).getElement());
Assert.assertEquals("netty-http:http://0.0.0.0:{{getNextPort}}/bar", list.get(1).getElement());
list = CamelJavaParserHelper.parseCamelProducerUris(method, true, true);
for (ParserResult result : list) {
LOG.info("Producer: " + result.getElement());
}
Assert.assertEquals("mock:input1", list.get(0).getElement());
Assert.assertEquals("netty-http:http://0.0.0.0:{{getNextPort}}/bar", list.get(1).getElement());
Assert.assertEquals("mock:input2", list.get(2).getElement());
}
use of org.jboss.forge.roaster.model.source.JavaClassSource in project flow by vaadin.
the class ComponentGenerator method generateNestedPojo.
private JavaClassSource generateNestedPojo(JavaClassSource javaClass, ComponentObjectType type, String nameHint, String description) {
JavaClassSource nestedClass = new NestedClassGenerator().withType(type).withFluentSetters(fluentMethod).withNameHint(nameHint).build();
if (javaClass.getNestedType(nestedClass.getName()) != null) {
throw new ComponentGenerationException("Duplicated nested class: \"" + nestedClass.getName() + "\". Please make sure your webcomponent definition contains unique properties, events and method names.");
}
nestedClass.getJavaDoc().setText(description);
javaClass.addNestedType(nestedClass);
javaClass.addImport(JsonObject.class);
javaClass.addImport(JsonSerializable.class);
return nestedClass;
}
use of org.jboss.forge.roaster.model.source.JavaClassSource 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);
}
}
use of org.jboss.forge.roaster.model.source.JavaClassSource in project flow by vaadin.
the class ComponentGenerator method generateGetterFor.
private void generateGetterFor(JavaClassSource javaClass, ComponentMetadata metadata, ComponentPropertyData property, List<ComponentEventData> events, Map<String, MethodSource<JavaClassSource>> propertyToGetterMap) {
String propertyJavaName = getJavaNameForProperty(metadata, property.getName());
MethodSource<JavaClassSource> method = javaClass.addMethod();
propertyToGetterMap.put(propertyJavaName, method);
if (containsObjectType(property)) {
JavaClassSource nestedClass = generateNestedPojo(javaClass, property.getObjectType().get(0), propertyJavaName + "-property", String.format("Class that encapsulates the data of the '%s' property in the {@link %s} component.", property.getName(), javaClass.getName()));
method.setReturnType(nestedClass);
setMethodVisibility(method, property.getObjectType());
method.setName(ComponentGeneratorUtils.generateMethodNameForProperty("get", propertyJavaName));
method.setBody(String.format("return new %s().readJson((JsonObject) getElement().getPropertyRaw(\"%s\"));", nestedClass.getName(), property.getName()));
if (method.getVisibility() == Visibility.PROTECTED) {
method.setName(method.getName() + StringUtils.capitalize(nestedClass.getName()));
}
addSynchronizeAnnotationAndJavadocToGetter(method, property, events);
if ("value".equals(propertyJavaName) && shouldImplementHasValue(metadata)) {
javaClass.addInterface(HasValue.class.getName() + "<" + GENERIC_TYPE + ", " + nestedClass.getName() + ">");
method.addAnnotation(Override.class);
method.setBody(String.format("JsonObject _obj = (JsonObject) getElement().getPropertyRaw(\"%s\");" + "return _obj == null ? getEmptyValue() : new %s().readJson(_obj);", property.getName(), nestedClass.getName()));
} else {
method.setBody(String.format("return new %s().readJson((JsonObject) getElement().getPropertyRaw(\"%s\"));", nestedClass.getName(), property.getName()));
}
} else {
boolean postfixWithVariableType = property.getType().size() > 1;
if (postfixWithVariableType) {
property.setType(new TreeSet<>(property.getType()));
}
for (ComponentBasicType basicType : property.getType()) {
Class<?> javaType = ComponentGeneratorUtils.toJavaType(basicType);
method.setReturnType(javaType);
setMethodVisibility(method, basicType);
if (basicType == ComponentBasicType.BOOLEAN) {
if (!propertyJavaName.startsWith("is") && !propertyJavaName.startsWith("has") && !propertyJavaName.startsWith("have")) {
method.setName(ComponentGeneratorUtils.generateMethodNameForProperty("is", propertyJavaName));
} else {
method.setName(ComponentGeneratorUtils.formatStringToValidJavaIdentifier(propertyJavaName));
}
} else {
method.setName(ComponentGeneratorUtils.generateMethodNameForProperty("get", propertyJavaName) + (postfixWithVariableType ? StringUtils.capitalize(basicType.name().toLowerCase()) : ""));
}
if (method.getVisibility() == Visibility.PROTECTED) {
method.setName(method.getName() + StringUtils.capitalize(javaType.getSimpleName()));
}
addSynchronizeAnnotationAndJavadocToGetter(method, property, events);
if ("value".equals(propertyJavaName) && shouldImplementHasValue(metadata)) {
method.setName("getValue");
if (javaType.isPrimitive()) {
javaType = ClassUtils.primitiveToWrapper(javaType);
method.setReturnType(javaType);
}
javaClass.addInterface(HasValue.class.getName() + "<" + GENERIC_TYPE + ", " + javaType.getName() + ">");
method.addAnnotation(Override.class);
method.setBody(ComponentGeneratorUtils.generateElementApiValueGetterForType(basicType, property.getName()));
addGetEmptyValueIfString(javaClass, javaType);
} else {
method.setBody(ComponentGeneratorUtils.generateElementApiGetterForType(basicType, property.getName()));
}
}
}
}
use of org.jboss.forge.roaster.model.source.JavaClassSource in project flow by vaadin.
the class ComponentGenerator method generateEventListenerFor.
private void generateEventListenerFor(JavaClassSource javaClass, ComponentMetadata metadata, ComponentEventData event, Map<String, MethodSource<JavaClassSource>> propertyToGetterMap) {
String eventJavaApiName = getJavaNameForPropertyChangeEvent(metadata, event.getName());
// verify whether the HasValue interface is implemented.
if ("value-changed".equals(eventJavaApiName) && shouldImplementHasValue(metadata)) {
if (!eventJavaApiName.equals(event.getName())) {
overrideHasValueGetClientValuePropertyName(javaClass, event.getName().replace(PROPERTY_CHANGE_EVENT_POSTFIX, ""));
}
return;
}
eventJavaApiName = ComponentGeneratorUtils.formatStringToValidJavaIdentifier(eventJavaApiName);
String propertyNameCamelCase = null;
boolean isPropertyChange = eventJavaApiName.endsWith("Changed");
if (isPropertyChange) {
// removes the "d" in the end, to create addSomethingChangeListener
// and SomethingChangeEvent
eventJavaApiName = eventJavaApiName.substring(0, eventJavaApiName.length() - 1);
propertyNameCamelCase = eventJavaApiName.substring(0, eventJavaApiName.length() - "Change".length());
}
JavaClassSource eventClass = createEventListenerEventClass(javaClass, event, eventJavaApiName, propertyNameCamelCase, propertyToGetterMap);
javaClass.addNestedType(eventClass);
MethodSource<JavaClassSource> method = javaClass.addMethod().setName("add" + StringUtils.capitalize(eventJavaApiName + "Listener")).setReturnType(Registration.class);
setMethodVisibility(method);
method.addParameter("ComponentEventListener<" + eventClass.getName() + GENERIC_TYPE_DECLARATION + ">", "listener");
method.getJavaDoc().setText(String.format("Adds a listener for {@code %s} events fired by the webcomponent.", event.getName())).addTagValue(JAVADOC_PARAM, "listener the listener").addTagValue(JAVADOC_RETURN, "a {@link Registration} for removing the event listener");
if (isPropertyChange) {
String propertyNameBeforeRenaming = ComponentGeneratorUtils.formatStringToValidJavaIdentifier(event.getName().replace(PROPERTY_CHANGE_EVENT_POSTFIX, ""));
method.setBody(String.format("return getElement().addPropertyChangeListener(\"%s\", " + "event -> listener.onComponentEvent(new %s<%s>((R) this, event.isUserOriginated())));", propertyNameBeforeRenaming, eventClass.getName(), eventClass.getTypeVariables().get(0).getName()));
} else {
method.setBody(String.format("return addListener(%s.class, (ComponentEventListener) listener);", eventClass.getName()));
method.addAnnotation(SuppressWarnings.class).setStringArrayValue(new String[] { "rawtypes", "unchecked" });
}
}
Aggregations