Search in sources :

Example 71 with JavaClassSource

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());
}
Also used : ParserResult(org.apache.camel.parser.ParserResult) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) File(java.io.File) Test(org.junit.Test)

Example 72 with JavaClassSource

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;
}
Also used : JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) ComponentGenerationException(com.vaadin.generator.exception.ComponentGenerationException)

Example 73 with JavaClassSource

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);
    }
}
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 74 with JavaClassSource

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()));
            }
        }
    }
}
Also used : JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) ComponentBasicType(com.vaadin.generator.metadata.ComponentBasicType)

Example 75 with JavaClassSource

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" });
    }
}
Also used : JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource)

Aggregations

JavaClassSource (org.jboss.forge.roaster.model.source.JavaClassSource)95 File (java.io.File)37 ArrayList (java.util.ArrayList)28 Test (org.junit.Test)26 ParserResult (org.apache.camel.parser.ParserResult)19 List (java.util.List)15 IOException (java.io.IOException)14 MethodSource (org.jboss.forge.roaster.model.source.MethodSource)12 InputStream (java.io.InputStream)10 Collections (java.util.Collections)10 Roaster (org.jboss.forge.roaster.Roaster)10 BufferedReader (java.io.BufferedReader)8 HashMap (java.util.HashMap)8 Matcher (java.util.regex.Matcher)8 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)8 ObjectProperty (org.kie.workbench.common.services.datamodeller.core.ObjectProperty)8 Arrays (java.util.Arrays)7 Map (java.util.Map)7 Import (org.jboss.forge.roaster.model.source.Import)7 ClassTypeResolver (org.kie.soup.project.datamodel.commons.types.ClassTypeResolver)7