use of org.jboss.forge.roaster.model.source.JavaClassSource in project camel by apache.
the class RoasterConcatFieldRouteBuilderConfigureTest method parse.
@Test
public void parse() throws Exception {
JavaClassSource clazz = (JavaClassSource) Roaster.parse(new File("src/test/java/org/apache/camel/parser/java/MyConcatFieldRouteBuilder.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("ftp:localhost:{{ftpPort}}/myapp?password=admin&username=admin", list.get(0).getElement());
list = CamelJavaParserHelper.parseCamelProducerUris(method, true, true);
for (ParserResult result : list) {
LOG.info("Producer: " + result.getElement());
}
Assert.assertEquals("log:b", list.get(0).getElement());
}
use of org.jboss.forge.roaster.model.source.JavaClassSource in project camel by apache.
the class RoasterEndpointInjectTest method parse.
@Test
public void parse() throws Exception {
JavaClassSource clazz = (JavaClassSource) Roaster.parse(new File("src/test/java/org/apache/camel/parser/java/MyCdiRouteBuilder.java"));
MethodSource<JavaClassSource> method = CamelJavaParserHelper.findConfigureMethod(clazz);
List<CamelEndpointDetails> details = new ArrayList<>();
RouteBuilderParser.parseRouteBuilderEndpoints(clazz, ".", "src/test/java/org/apache/camel/parser/java/MyCdiRouteBuilder.java", details);
LOG.info("{}", details);
Assert.assertEquals("timer:foo?period=4999", details.get(0).getEndpointUri());
Assert.assertEquals("28", details.get(0).getLineNumber());
Assert.assertEquals("log:a", details.get(1).getEndpointUri());
Assert.assertEquals("32", details.get(1).getLineNumber());
Assert.assertEquals("netty4-http:http:someserver:80/hello", details.get(2).getEndpointUri());
Assert.assertEquals("36", details.get(2).getLineNumber());
List<ParserResult> list = CamelJavaParserHelper.parseCamelConsumerUris(method, true, true);
for (ParserResult result : list) {
LOG.info("Consumer: " + result.getElement());
}
Assert.assertEquals("timer:foo?period=4999", list.get(0).getElement());
list = CamelJavaParserHelper.parseCamelProducerUris(method, true, true);
for (ParserResult result : list) {
LOG.info("Producer: " + result.getElement());
}
Assert.assertEquals(2, list.size());
Assert.assertEquals(5, details.size());
Assert.assertEquals("log:a", details.get(3).getEndpointUri());
}
use of org.jboss.forge.roaster.model.source.JavaClassSource in project camel by apache.
the class RoasterMethodCallRouteBuilderConfigureTest method parse.
@Test
public void parse() throws Exception {
JavaClassSource clazz = (JavaClassSource) Roaster.parse(new File("src/test/java/org/apache/camel/parser/java/MyMethodCallRouteBuilder.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={{whatToDoWhenExists}}", list.get(0).getElement());
Assert.assertEquals("log:b", list.get(1).getElement());
}
use of org.jboss.forge.roaster.model.source.JavaClassSource in project camel by apache.
the class SpringBootAutoConfigurationMojo method createDataFormatAutoConfigurationSource.
private void createDataFormatAutoConfigurationSource(String packageName, DataFormatModel model, List<String> dataFormatAliases, boolean hasOptions, String overrideDataFormatName) throws MojoFailureException {
final JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
int pos = model.getJavaType().lastIndexOf(".");
String name = model.getJavaType().substring(pos + 1);
name = name.replace("DataFormat", "DataFormatAutoConfiguration");
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("DataFormatAutoConfiguration", "DataFormatConfiguration");
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("org.apache.camel.CamelContextAware");
javaClass.addImport(model.getJavaType());
javaClass.addImport("org.apache.camel.CamelContext");
javaClass.addImport("org.apache.camel.RuntimeCamelException");
javaClass.addImport("org.apache.camel.spi.DataFormat");
javaClass.addImport("org.apache.camel.spi.DataFormatFactory");
String body = createDataFormatBody(model.getShortJavaType(), hasOptions);
String methodName = "configure" + model.getShortJavaType() + "Factory";
MethodSource<JavaClassSource> method = javaClass.addMethod().setName(methodName).setPublic().setBody(body).setReturnType("org.apache.camel.spi.DataFormatFactory");
method.addParameter("CamelContext", "camelContext").setFinal(true);
if (hasOptions) {
method.addParameter(configurationName, "configuration").setFinal(true);
}
// Determine all the aliases
// adding the '-dataformat' suffix to prevent collision with component names
String[] springBeanAliases = dataFormatAliases.stream().map(alias -> alias + "-dataformat-factory").toArray(size -> new String[size]);
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.dataformat", (overrideDataFormatName != null ? overrideDataFormatName : model.getName()).toLowerCase(Locale.US)));
sortImports(javaClass);
String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
writeSourceIfChanged(javaClass, fileName);
writeAdditionalSpringMetaData("camel", "dataformat", (overrideDataFormatName != null ? overrideDataFormatName : model.getName()).toLowerCase(Locale.US));
}
use of org.jboss.forge.roaster.model.source.JavaClassSource in project flow by vaadin.
the class ComponentGenerator method generateClass.
/**
* Generates the Java class by using the {@link ComponentMetadata} object.
*
* @param metadata
* The webcomponent metadata.
* @param targetPath
* The output base directory for the generated Java file.
* @param basePackage
* The base package to be used for the generated Java class. The
* final package of the class is basePackage plus the
* {@link ComponentMetadata#getBaseUrl()}.
* @param licenseNote
* A note to be added on top of the class as a comment. Usually
* used for license headers.
* @throws ComponentGenerationException
* If an error occurs when generating the class.
*/
public void generateClass(ComponentMetadata metadata, File targetPath, String basePackage, String licenseNote) {
JavaClassSource javaClass = generateClassSource(metadata, basePackage);
String source = addLicenseHeaderIfAvailable(javaClass.toString(), licenseNote);
String fileName = ComponentGeneratorUtils.generateValidJavaClassName(javaClass.getName()) + ".java";
if (!targetPath.isDirectory() && !targetPath.mkdirs()) {
throw new ComponentGenerationException("Could not create target directory \"" + targetPath + "\"");
}
try {
Files.write(new File(ComponentGeneratorUtils.convertPackageToDirectory(targetPath, javaClass.getPackage(), true), fileName).toPath(), source.getBytes(UTF_8));
} catch (IOException ex) {
throw new ComponentGenerationException("Error writing the generated Java source file \"" + fileName + "\" at \"" + targetPath + "\" for component \"" + metadata.getName() + "\"", ex);
}
}
Aggregations