Search in sources :

Example 66 with JavaClassSource

use of org.jboss.forge.roaster.model.source.JavaClassSource in project camel by apache.

the class SpringBootAutoConfigurationMojo method createComponentConfigurationSource.

private void createComponentConfigurationSource(String packageName, ComponentModel model, String overrideComponentName) throws MojoFailureException {
    final JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
    int pos = model.getJavaType().lastIndexOf(".");
    String name = model.getJavaType().substring(pos + 1);
    name = name.replace("Component", "ComponentConfiguration");
    javaClass.setPackage(packageName).setName(name);
    String doc = "Generated by camel-package-maven-plugin - do not edit this file!";
    if (!Strings.isBlank(model.getDescription())) {
        doc = model.getDescription() + "\n\n" + doc;
    }
    javaClass.getJavaDoc().setFullText(doc);
    String prefix = "camel.component." + (overrideComponentName != null ? overrideComponentName : model.getScheme());
    // make sure prefix is in lower case
    prefix = prefix.toLowerCase(Locale.US);
    javaClass.addAnnotation("org.springframework.boot.context.properties.ConfigurationProperties").setStringValue("prefix", prefix);
    Set<JavaClassSource> nestedTypes = new HashSet<>();
    for (ComponentOptionModel option : model.getComponentOptions()) {
        if (skipComponentOption(model, option)) {
            // some component options should be skipped
            continue;
        }
        String type = option.getJavaType();
        // generate inner class for non-primitive options
        type = getSimpleJavaType(type);
        JavaClassSource javaClassSource = readJavaType(type);
        if (isNestedProperty(nestedTypes, javaClassSource)) {
            type = option.getShortJavaType() + INNER_TYPE_SUFFIX;
        }
        PropertySource<JavaClassSource> prop = javaClass.addProperty(type, option.getName());
        if (!type.endsWith(INNER_TYPE_SUFFIX) && type.indexOf('[') == -1 && !EXCLUDE_INNER_PATTERN.matcher(type).matches() && Strings.isBlank(option.getEnums()) && (javaClassSource == null || (javaClassSource.isClass() && !javaClassSource.isAbstract()))) {
            // add nested configuration annotation for complex properties
            prop.getField().addAnnotation(NestedConfigurationProperty.class);
        }
        if ("true".equals(option.getDeprecated())) {
            prop.getField().addAnnotation(Deprecated.class);
            prop.getAccessor().addAnnotation(Deprecated.class);
            prop.getMutator().addAnnotation(Deprecated.class);
            // DeprecatedConfigurationProperty must be on getter when deprecated
            prop.getAccessor().addAnnotation(DeprecatedConfigurationProperty.class);
        }
        if (!Strings.isBlank(option.getDescription())) {
            prop.getField().getJavaDoc().setFullText(option.getDescription());
        }
        if (!Strings.isBlank(option.getDefaultValue())) {
            if ("java.lang.String".equals(option.getJavaType())) {
                prop.getField().setStringInitializer(option.getDefaultValue());
            } else if ("long".equals(option.getJavaType()) || "java.lang.Long".equals(option.getJavaType())) {
                // the value should be a Long number
                String value = option.getDefaultValue() + "L";
                prop.getField().setLiteralInitializer(value);
            } else if ("integer".equals(option.getType()) || "boolean".equals(option.getType())) {
                prop.getField().setLiteralInitializer(option.getDefaultValue());
            } else if (!Strings.isBlank(option.getEnums())) {
                String enumShortName = type.substring(type.lastIndexOf(".") + 1);
                prop.getField().setLiteralInitializer(enumShortName + "." + option.getDefaultValue());
                javaClass.addImport(model.getJavaType());
            }
        }
    }
    // add inner classes for nested AutoConfiguration options
    ClassLoader projectClassLoader = getProjectClassLoader();
    for (JavaClassSource nestedType : nestedTypes) {
        final JavaClassSource innerClass = javaClass.addNestedType("public static class " + nestedType.getName() + INNER_TYPE_SUFFIX);
        // add source class name as a static field
        innerClass.addField().setPublic().setStatic(true).setFinal(true).setType(Class.class).setName("CAMEL_NESTED_CLASS").setLiteralInitializer(nestedType.getCanonicalName() + ".class");
        // parse option type
        for (ResolvedProperty resolvedProperty : getProperties(nestedType)) {
            String optionType = resolvedProperty.propertyType;
            PropertySource<JavaClassSource> sourceProp = resolvedProperty.propertySource;
            Type<JavaClassSource> propType = sourceProp.getType();
            final PropertySource<JavaClassSource> prop = innerClass.addProperty(optionType, sourceProp.getName());
            boolean anEnum;
            Class optionClass;
            if (!propType.isArray()) {
                optionClass = loadClass(projectClassLoader, optionType);
                anEnum = optionClass.isEnum();
            } else {
                optionClass = null;
                anEnum = false;
            }
            // add nested configuration annotation for complex properties
            if (!EXCLUDE_INNER_PATTERN.matcher(optionType).matches() && !propType.isArray() && !anEnum && optionClass != null && !optionClass.isInterface() && !optionClass.isAnnotation() && !Modifier.isAbstract(optionClass.getModifiers())) {
                prop.getField().addAnnotation(NestedConfigurationProperty.class);
            }
            if (sourceProp.hasAnnotation(Deprecated.class)) {
                prop.getField().addAnnotation(Deprecated.class);
                prop.getAccessor().addAnnotation(Deprecated.class);
                prop.getMutator().addAnnotation(Deprecated.class);
                // DeprecatedConfigurationProperty must be on getter when deprecated
                prop.getAccessor().addAnnotation(DeprecatedConfigurationProperty.class);
            }
            String description = null;
            final MethodSource<JavaClassSource> mutator = sourceProp.getMutator();
            if (mutator.hasJavaDoc()) {
                description = mutator.getJavaDoc().getFullText();
            } else if (sourceProp.hasField()) {
                description = sourceProp.getField().getJavaDoc().getFullText();
            }
            if (!Strings.isBlank(description)) {
                prop.getField().getJavaDoc().setFullText(description);
            }
            // as the annotation can refer to a constant field which we wont have accessible at this point
            if (sourceProp.hasAnnotation(UriParam.class) || sourceProp.hasAnnotation(UriPath.class)) {
                String defaultValue = null;
                String javaType = null;
                String type = null;
                String fileName = model.getJavaType();
                fileName = fileName.substring(0, fileName.lastIndexOf("."));
                fileName = fileName.replace('.', '/');
                File jsonFile = new File(classesDir, fileName + "/" + model.getScheme() + ".json");
                if (jsonFile.isFile() && jsonFile.exists()) {
                    try {
                        String json = FileUtils.readFileToString(jsonFile);
                        List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("properties", json, true);
                        // grab name from annotation
                        String optionName;
                        if (sourceProp.hasAnnotation(UriParam.class)) {
                            optionName = sourceProp.getAnnotation(UriParam.class).getStringValue("name");
                        } else {
                            optionName = sourceProp.getAnnotation(UriPath.class).getStringValue("name");
                        }
                        if (optionName == null) {
                            optionName = sourceProp.hasField() ? sourceProp.getField().getName() : null;
                        }
                        if (optionName != null) {
                            javaType = getPropertyJavaType(rows, optionName);
                            type = getPropertyType(rows, optionName);
                            defaultValue = getPropertyDefaultValue(rows, optionName);
                        }
                    } catch (IOException e) {
                    // ignore
                    }
                }
                if (!Strings.isBlank(defaultValue)) {
                    // roaster can create the wrong type for some options so use the correct type we found in the json schema
                    String wrapperType = getSimpleJavaType(javaType);
                    if (wrapperType.startsWith("java.lang.")) {
                        // skip java.lang. as prefix for wrapper type
                        wrapperType = wrapperType.substring(10);
                        prop.setType(wrapperType);
                    }
                    if ("long".equals(javaType) || "java.lang.Long".equals(javaType)) {
                        // the value should be a Long number
                        String value = defaultValue + "L";
                        prop.getField().setLiteralInitializer(value);
                    } else if ("integer".equals(type) || "boolean".equals(type)) {
                        prop.getField().setLiteralInitializer(defaultValue);
                    } else if ("string".equals(type)) {
                        prop.getField().setStringInitializer(defaultValue);
                    } else if (anEnum) {
                        String enumShortName = optionClass.getSimpleName();
                        prop.getField().setLiteralInitializer(enumShortName + "." + defaultValue);
                        javaClass.addImport(model.getJavaType());
                    }
                }
            }
        }
    }
    sortImports(javaClass);
    String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
    writeSourceIfChanged(javaClass, fileName);
}
Also used : JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) IOException(java.io.IOException) UriParam(org.apache.camel.spi.UriParam) UriPath(org.apache.camel.spi.UriPath) URLClassLoader(java.net.URLClassLoader) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) ComponentOptionModel(org.apache.camel.maven.packaging.model.ComponentOptionModel) HashSet(java.util.HashSet)

Example 67 with JavaClassSource

use of org.jboss.forge.roaster.model.source.JavaClassSource in project camel by apache.

the class SpringBootAutoConfigurationMojo method createLanguageConfigurationSource.

private void createLanguageConfigurationSource(String packageName, LanguageModel model, String overrideLanguageName) throws MojoFailureException {
    final JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
    int pos = model.getJavaType().lastIndexOf(".");
    String name = model.getJavaType().substring(pos + 1);
    name = name.replace("Language", "LanguageConfiguration");
    javaClass.setPackage(packageName).setName(name);
    String doc = "Generated by camel-package-maven-plugin - do not edit this file!";
    if (!Strings.isBlank(model.getDescription())) {
        doc = model.getDescription() + "\n\n" + doc;
    }
    javaClass.getJavaDoc().setFullText(doc);
    String prefix = "camel.language." + (overrideLanguageName != null ? overrideLanguageName : model.getName());
    // make sure prefix is in lower case
    prefix = prefix.toLowerCase(Locale.US);
    javaClass.addAnnotation("org.springframework.boot.context.properties.ConfigurationProperties").setStringValue("prefix", prefix);
    for (LanguageOptionModel option : model.getLanguageOptions()) {
        // skip option with name id, or expression in language as we do not need that and skip resultType as they are not global options
        if ("id".equals(option.getName()) || "expression".equals(option.getName()) || "resultType".equals(option.getName())) {
            continue;
        }
        // CHECKSTYLE:OFF
        if ("bean".equals(model.getName())) {
            // and skip following as they are not global options
            if ("bean".equals(option.getName()) || "ref".equals(option.getName()) || "method".equals(option.getName()) || "beanType".equals(option.getName())) {
                continue;
            }
        } else if ("tokenize".equals(model.getName())) {
            // and skip following as they are not global options
            if ("token".equals(option.getName()) || "endToken".equals(option.getName()) || "inheritNamespaceTagName".equals(option.getName()) || "headerName".equals(option.getName()) || "regex".equals(option.getName()) || "xml".equals(option.getName()) || "includeTokens".equals(option.getName()) || "group".equals(option.getName()) || "skipFirst".equals(option.getName())) {
                continue;
            }
        } else if ("xtokenize".equals(model.getName())) {
            // and skip following as they are not global options
            if ("headerName".equals(option.getName()) || "group".equals(option.getName())) {
                continue;
            }
        } else if ("xpath".equals(model.getName())) {
            // and skip following as they are not global options
            if ("headerName".equals(option.getName())) {
                continue;
            }
        } else if ("xquery".equals(model.getName())) {
            // and skip following as they are not global options
            if ("headerName".equals(option.getName())) {
                continue;
            }
        }
        // CHECKSTYLE:ON
        String type = option.getJavaType();
        type = getSimpleJavaType(type);
        PropertySource<JavaClassSource> prop = javaClass.addProperty(type, option.getName());
        if ("true".equals(option.getDeprecated())) {
            prop.getField().addAnnotation(Deprecated.class);
            prop.getAccessor().addAnnotation(Deprecated.class);
            prop.getMutator().addAnnotation(Deprecated.class);
            // DeprecatedConfigurationProperty must be on getter when deprecated
            prop.getAccessor().addAnnotation(DeprecatedConfigurationProperty.class);
        }
        if (!Strings.isBlank(option.getDescription())) {
            prop.getField().getJavaDoc().setFullText(option.getDescription());
        }
        if (!Strings.isBlank(option.getDefaultValue())) {
            if ("java.lang.String".equals(option.getType())) {
                prop.getField().setStringInitializer(option.getDefaultValue());
            } else if ("long".equals(option.getJavaType()) || "java.lang.Long".equals(option.getJavaType())) {
                // the value should be a Long number
                String value = option.getDefaultValue() + "L";
                prop.getField().setLiteralInitializer(value);
            } else if ("integer".equals(option.getType()) || "boolean".equals(option.getType())) {
                prop.getField().setLiteralInitializer(option.getDefaultValue());
            } else if (!Strings.isBlank(option.getEnumValues())) {
                String enumShortName = type.substring(type.lastIndexOf(".") + 1);
                prop.getField().setLiteralInitializer(enumShortName + "." + option.getDefaultValue());
                javaClass.addImport(model.getJavaType());
            }
        }
    }
    sortImports(javaClass);
    String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
    writeSourceIfChanged(javaClass, fileName);
}
Also used : LanguageOptionModel(org.apache.camel.maven.packaging.model.LanguageOptionModel) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource)

Example 68 with JavaClassSource

use of org.jboss.forge.roaster.model.source.JavaClassSource in project camel by apache.

the class SpringBootAutoConfigurationMojo method createComponentAutoConfigurationSource.

private void createComponentAutoConfigurationSource(String packageName, ComponentModel model, List<String> componentAliases, boolean hasOptions, String overrideComponentName) throws MojoFailureException {
    final JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
    int pos = model.getJavaType().lastIndexOf(".");
    String name = model.getJavaType().substring(pos + 1);
    name = name.replace("Component", "ComponentAutoConfiguration");
    javaClass.setPackage(packageName).setName(name);
    String doc = "Generated by camel-package-maven-plugin - do not edit this file!";
    javaClass.getJavaDoc().setFullText(doc);
    javaClass.addAnnotation(Configuration.class);
    javaClass.addAnnotation(ConditionalOnBean.class).setStringValue("type", "org.apache.camel.spring.boot.CamelAutoConfiguration");
    javaClass.addAnnotation(Conditional.class).setLiteralValue(name + ".Condition.class");
    javaClass.addAnnotation(AutoConfigureAfter.class).setStringValue("name", "org.apache.camel.spring.boot.CamelAutoConfiguration");
    String configurationName = name.replace("ComponentAutoConfiguration", "ComponentConfiguration");
    if (hasOptions) {
        AnnotationSource<JavaClassSource> ann = javaClass.addAnnotation(EnableConfigurationProperties.class);
        ann.setLiteralValue("value", configurationName + ".class");
        javaClass.addImport("java.util.HashMap");
        javaClass.addImport("java.util.Map");
        javaClass.addImport("org.apache.camel.util.IntrospectionSupport");
    }
    javaClass.addImport(model.getJavaType());
    javaClass.addImport("org.apache.camel.CamelContext");
    // add method for auto configure
    String body = createComponentBody(model.getShortJavaType(), hasOptions);
    String methodName = "configure" + model.getShortJavaType();
    MethodSource<JavaClassSource> method = javaClass.addMethod().setName(methodName).setPublic().setBody(body).setReturnType(model.getShortJavaType()).addThrows(Exception.class);
    method.addParameter("CamelContext", "camelContext");
    if (hasOptions) {
        method.addParameter(configurationName, "configuration");
    }
    // Determine all the aliases
    String[] springBeanAliases = componentAliases.stream().map(alias -> alias + "-component").toArray(size -> new String[size]);
    method.addAnnotation(Lazy.class);
    method.addAnnotation(Bean.class).setStringArrayValue("name", springBeanAliases);
    method.addAnnotation(ConditionalOnClass.class).setLiteralValue("value", "CamelContext.class");
    method.addAnnotation(ConditionalOnMissingBean.class).setLiteralValue("value", model.getShortJavaType() + ".class");
    // Generate Condition
    javaClass.addNestedType(createConditionType(javaClass, "camel.component", (overrideComponentName != null ? overrideComponentName : model.getScheme()).toLowerCase(Locale.US)));
    sortImports(javaClass);
    String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
    writeSourceIfChanged(javaClass, fileName);
    writeAdditionalSpringMetaData("camel", "component", (overrideComponentName != null ? overrideComponentName : model.getScheme()).toLowerCase(Locale.US));
}
Also used : NestedConfigurationProperty(org.springframework.boot.context.properties.NestedConfigurationProperty) Arrays(java.util.Arrays) AnnotationSource(org.jboss.forge.roaster.model.source.AnnotationSource) URL(java.net.URL) Type(org.jboss.forge.roaster.model.Type) ConditionMessage(org.springframework.boot.autoconfigure.condition.ConditionMessage) GsonBuilder(com.google.gson.GsonBuilder) UriParam(org.apache.camel.spi.UriParam) ConditionContext(org.springframework.context.annotation.ConditionContext) URLClassLoader(java.net.URLClassLoader) Matcher(java.util.regex.Matcher) Roaster(org.jboss.forge.roaster.Roaster) MavenProject(org.apache.maven.project.MavenProject) Locale(java.util.Locale) Gson(com.google.gson.Gson) Map(java.util.Map) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) MethodSource(org.jboss.forge.roaster.model.source.MethodSource) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Set(java.util.Set) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) Configuration(org.springframework.context.annotation.Configuration) JSonSchemaHelper.getPropertyJavaType(org.apache.camel.maven.packaging.JSonSchemaHelper.getPropertyJavaType) List(java.util.List) RelaxedPropertyResolver(org.springframework.boot.bind.RelaxedPropertyResolver) Resource(org.apache.maven.model.Resource) Formatter(org.jboss.forge.roaster.model.util.Formatter) DataFormatOptionModel(org.apache.camel.maven.packaging.model.DataFormatOptionModel) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) Modifier(java.lang.reflect.Modifier) Strings(org.jboss.forge.roaster.model.util.Strings) Lazy(org.springframework.context.annotation.Lazy) JSonSchemaHelper.getSafeValue(org.apache.camel.maven.packaging.JSonSchemaHelper.getSafeValue) Pattern(java.util.regex.Pattern) Conditional(org.springframework.context.annotation.Conditional) AbstractMojo(org.apache.maven.plugin.AbstractMojo) Import(org.jboss.forge.roaster.model.source.Import) LanguageOptionModel(org.apache.camel.maven.packaging.model.LanguageOptionModel) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) UriPath(org.apache.camel.spi.UriPath) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) LanguageModel(org.apache.camel.maven.packaging.model.LanguageModel) Scope(org.springframework.context.annotation.Scope) ArrayList(java.util.ArrayList) AutoConfigureAfter(org.springframework.boot.autoconfigure.AutoConfigureAfter) HashSet(java.util.HashSet) JavaType(org.jboss.forge.roaster.model.JavaType) ComponentOptionModel(org.apache.camel.maven.packaging.model.ComponentOptionModel) ComponentModel(org.apache.camel.maven.packaging.model.ComponentModel) EndpointOptionModel(org.apache.camel.maven.packaging.model.EndpointOptionModel) LinkedList(java.util.LinkedList) PropertySource(org.jboss.forge.roaster.model.source.PropertySource) JSonSchemaHelper.getPropertyDefaultValue(org.apache.camel.maven.packaging.JSonSchemaHelper.getPropertyDefaultValue) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) Iterator(java.util.Iterator) MalformedURLException(java.net.MalformedURLException) DeprecatedConfigurationProperty(org.springframework.boot.context.properties.DeprecatedConfigurationProperty) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) FileInputStream(java.io.FileInputStream) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) File(java.io.File) MojoFailureException(org.apache.maven.plugin.MojoFailureException) SpringBootCondition(org.springframework.boot.autoconfigure.condition.SpringBootCondition) ConditionOutcome(org.springframework.boot.autoconfigure.condition.ConditionOutcome) PackageHelper.loadText(org.apache.camel.maven.packaging.PackageHelper.loadText) JSonSchemaHelper.getPropertyType(org.apache.camel.maven.packaging.JSonSchemaHelper.getPropertyType) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Bean(org.springframework.context.annotation.Bean) AnnotatedTypeMetadata(org.springframework.core.type.AnnotatedTypeMetadata) Collections(java.util.Collections) DataFormatModel(org.apache.camel.maven.packaging.model.DataFormatModel) InputStream(java.io.InputStream) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) AutoConfigureAfter(org.springframework.boot.autoconfigure.AutoConfigureAfter) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) Conditional(org.springframework.context.annotation.Conditional) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) Bean(org.springframework.context.annotation.Bean)

Example 69 with JavaClassSource

use of org.jboss.forge.roaster.model.source.JavaClassSource in project camel by apache.

the class SpringBootAutoConfigurationMojo method createConnectorAutoConfigurationSource.

private void createConnectorAutoConfigurationSource(String packageName, boolean hasOptions, String javaType, String connectorScheme) throws MojoFailureException {
    final JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
    int pos = javaType.lastIndexOf(".");
    String name = javaType.substring(pos + 1);
    name = name.replace("Component", "ConnectorAutoConfiguration");
    javaClass.setPackage(packageName).setName(name);
    String doc = "Generated by camel-connector-maven-plugin - do not edit this file!";
    javaClass.getJavaDoc().setFullText(doc);
    javaClass.addAnnotation(Configuration.class);
    javaClass.addAnnotation(ConditionalOnBean.class).setStringValue("type", "org.apache.camel.spring.boot.CamelAutoConfiguration");
    javaClass.addAnnotation(AutoConfigureAfter.class).setStringValue("name", "org.apache.camel.spring.boot.CamelAutoConfiguration");
    String configurationName = name.replace("ConnectorAutoConfiguration", "ConnectorConfiguration");
    if (hasOptions) {
        AnnotationSource<JavaClassSource> ann = javaClass.addAnnotation(EnableConfigurationProperties.class);
        ann.setLiteralValue("value", configurationName + ".class");
        javaClass.addImport("java.util.HashMap");
        javaClass.addImport("java.util.Map");
        javaClass.addImport("org.apache.camel.util.IntrospectionSupport");
    }
    javaClass.addImport(javaType);
    javaClass.addImport("org.apache.camel.CamelContext");
    // add method for auto configure
    String shortJavaType = getShortJavaType(javaType);
    String body = createComponentBody(shortJavaType, hasOptions);
    String methodName = "configure" + shortJavaType;
    MethodSource<JavaClassSource> method = javaClass.addMethod().setName(methodName).setPublic().setBody(body).setReturnType(shortJavaType).addThrows(Exception.class);
    method.addParameter("CamelContext", "camelContext");
    if (hasOptions) {
        method.addParameter(configurationName, "configuration");
    }
    // must be named -component because camel-spring-boot uses that to lookup components
    String beanName = connectorScheme + "-component";
    method.addAnnotation(Lazy.class);
    method.addAnnotation(Bean.class).setStringValue("name", beanName);
    method.addAnnotation(ConditionalOnClass.class).setLiteralValue("value", "CamelContext.class");
    method.addAnnotation(ConditionalOnMissingBean.class).setLiteralValue("value", javaType + ".class");
    sortImports(javaClass);
    String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
    writeSourceIfChanged(javaClass, fileName);
}
Also used : ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) AutoConfigureAfter(org.springframework.boot.autoconfigure.AutoConfigureAfter) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 70 with JavaClassSource

use of org.jboss.forge.roaster.model.source.JavaClassSource in project camel by apache.

the class RoasterFieldRouteBuilderConfigureTest method parse.

@Test
public void parse() throws Exception {
    JavaClassSource clazz = (JavaClassSource) Roaster.parse(new File("src/test/java/org/apache/camel/parser/java/MyFieldRouteBuilder.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("timer:foo", list.get(0).getElement());
    list = CamelJavaParserHelper.parseCamelProducerUris(method, true, true);
    for (ParserResult result : list) {
        LOG.info("Producer: " + result.getElement());
    }
    Assert.assertEquals("file:output?fileExist=Override", list.get(0).getElement());
    Assert.assertEquals("log:b", list.get(1).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)

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