use of org.jboss.forge.roaster.model.source.MethodSource in project camel by apache.
the class RouteBuilderParser method parseRouteBuilderEndpoints.
/**
* Parses the java source class to discover Camel endpoints.
*
* @param clazz the java source class
* @param baseDir the base of the source code
* @param fullyQualifiedFileName the fully qualified source code file name
* @param endpoints list to add discovered and parsed endpoints
* @param unparsable list of unparsable nodes
* @param includeInlinedRouteBuilders whether to include inlined route builders in the parsing
*/
public static void parseRouteBuilderEndpoints(JavaClassSource clazz, String baseDir, String fullyQualifiedFileName, List<CamelEndpointDetails> endpoints, List<String> unparsable, boolean includeInlinedRouteBuilders) {
// look for fields which are not used in the route
for (FieldSource<JavaClassSource> field : clazz.getFields()) {
// is the field annotated with a Camel endpoint
String uri = null;
Expression exp = null;
for (Annotation ann : field.getAnnotations()) {
boolean valid = "org.apache.camel.EndpointInject".equals(ann.getQualifiedName()) || "org.apache.camel.cdi.Uri".equals(ann.getQualifiedName());
if (valid) {
exp = (Expression) ann.getInternal();
if (exp instanceof SingleMemberAnnotation) {
exp = ((SingleMemberAnnotation) exp).getValue();
} else if (exp instanceof NormalAnnotation) {
List values = ((NormalAnnotation) exp).values();
for (Object value : values) {
MemberValuePair pair = (MemberValuePair) value;
if ("uri".equals(pair.getName().toString())) {
exp = pair.getValue();
break;
}
}
}
uri = CamelJavaParserHelper.getLiteralValue(clazz, null, exp);
}
}
// we only want to add fields which are not used in the route
if (!Strings.isBlank(uri) && findEndpointByUri(endpoints, uri) == null) {
// we only want the relative dir name from the
String fileName = fullyQualifiedFileName;
if (fileName.startsWith(baseDir)) {
fileName = fileName.substring(baseDir.length() + 1);
}
String id = field.getName();
CamelEndpointDetails detail = new CamelEndpointDetails();
detail.setFileName(fileName);
detail.setClassName(clazz.getQualifiedName());
detail.setEndpointInstance(id);
detail.setEndpointUri(uri);
detail.setEndpointComponentName(endpointComponentName(uri));
// favor the position of the expression which had the actual uri
Object internal = exp != null ? exp : field.getInternal();
// find position of field/expression
if (internal instanceof ASTNode) {
int pos = ((ASTNode) internal).getStartPosition();
int line = findLineNumber(fullyQualifiedFileName, pos);
if (line > -1) {
detail.setLineNumber("" + line);
}
}
// we do not know if this field is used as consumer or producer only, but we try
// to find out by scanning the route in the configure method below
endpoints.add(detail);
}
}
// find all the configure methods
List<MethodSource<JavaClassSource>> methods = new ArrayList<>();
MethodSource<JavaClassSource> method = CamelJavaParserHelper.findConfigureMethod(clazz);
if (method != null) {
methods.add(method);
}
if (includeInlinedRouteBuilders) {
List<MethodSource<JavaClassSource>> inlinedMethods = CamelJavaParserHelper.findInlinedConfigureMethods(clazz);
if (!inlinedMethods.isEmpty()) {
methods.addAll(inlinedMethods);
}
}
// determine this to ensure when we edit the endpoint we should only the options accordingly
for (MethodSource<JavaClassSource> configureMethod : methods) {
// consumers only
List<ParserResult> uris = CamelJavaParserHelper.parseCamelConsumerUris(configureMethod, true, true);
for (ParserResult result : uris) {
if (!result.isParsed()) {
if (unparsable != null) {
unparsable.add(result.getElement());
}
} else {
CamelEndpointDetails detail = findEndpointByUri(endpoints, result.getElement());
if (detail != null) {
// its a consumer only
detail.setConsumerOnly(true);
} else {
String fileName = fullyQualifiedFileName;
if (fileName.startsWith(baseDir)) {
fileName = fileName.substring(baseDir.length() + 1);
}
detail = new CamelEndpointDetails();
detail.setFileName(fileName);
detail.setClassName(clazz.getQualifiedName());
detail.setMethodName(configureMethod.getName());
detail.setEndpointInstance(null);
detail.setEndpointUri(result.getElement());
int line = findLineNumber(fullyQualifiedFileName, result.getPosition());
if (line > -1) {
detail.setLineNumber("" + line);
}
detail.setEndpointComponentName(endpointComponentName(result.getElement()));
detail.setConsumerOnly(true);
detail.setProducerOnly(false);
endpoints.add(detail);
}
}
}
// producer only
uris = CamelJavaParserHelper.parseCamelProducerUris(configureMethod, true, true);
for (ParserResult result : uris) {
if (!result.isParsed()) {
if (unparsable != null) {
unparsable.add(result.getElement());
}
} else {
CamelEndpointDetails detail = findEndpointByUri(endpoints, result.getElement());
if (detail != null) {
if (detail.isConsumerOnly()) {
// its both a consumer and producer
detail.setConsumerOnly(false);
detail.setProducerOnly(false);
} else {
// its a producer only
detail.setProducerOnly(true);
}
}
// the same endpoint uri may be used in multiple places in the same route
// so we should maybe add all of them
String fileName = fullyQualifiedFileName;
if (fileName.startsWith(baseDir)) {
fileName = fileName.substring(baseDir.length() + 1);
}
detail = new CamelEndpointDetails();
detail.setFileName(fileName);
detail.setClassName(clazz.getQualifiedName());
detail.setMethodName(configureMethod.getName());
detail.setEndpointInstance(null);
detail.setEndpointUri(result.getElement());
int line = findLineNumber(fullyQualifiedFileName, result.getPosition());
if (line > -1) {
detail.setLineNumber("" + line);
}
detail.setEndpointComponentName(endpointComponentName(result.getElement()));
detail.setConsumerOnly(false);
detail.setProducerOnly(true);
endpoints.add(detail);
}
}
}
}
use of org.jboss.forge.roaster.model.source.MethodSource 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.MethodSource in project camel by apache.
the class RoasterMyLocalAddRouteBuilderTest method parse.
@Test
public void parse() throws Exception {
JavaClassSource clazz = (JavaClassSource) Roaster.parse(new File("src/test/java/org/apache/camel/parser/java/MyLocalAddRouteBuilderTest.java"));
MethodSource<JavaClassSource> method = CamelJavaParserHelper.findConfigureMethod(clazz);
Assert.assertNull(method);
List<MethodSource<JavaClassSource>> methods = CamelJavaParserHelper.findInlinedConfigureMethods(clazz);
Assert.assertEquals(1, methods.size());
method = methods.get(0);
List<ParserResult> list = CamelJavaParserHelper.parseCamelConsumerUris(method, true, true);
for (ParserResult result : list) {
LOG.info("Consumer: " + result.getElement());
}
list = CamelJavaParserHelper.parseCamelProducerUris(method, true, true);
for (ParserResult result : list) {
LOG.info("Producer: " + result.getElement());
}
}
use of org.jboss.forge.roaster.model.source.MethodSource in project camel by apache.
the class SpringBootAutoConfigurationMojo method createLanguageAutoConfigurationSource.
private void createLanguageAutoConfigurationSource(String packageName, LanguageModel model, List<String> languageAliases, boolean hasOptions, 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", "LanguageAutoConfiguration");
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("LanguageAutoConfiguration", "LanguageConfiguration");
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");
String body = createLanguageBody(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");
method.addParameter(configurationName, "configuration");
// Determine all the aliases
// adding the '-language' suffix to prevent collision with component names
String[] springBeanAliases = languageAliases.stream().map(alias -> alias + "-language").toArray(size -> new String[size]);
method.addAnnotation(Bean.class).setStringArrayValue("name", springBeanAliases);
method.addAnnotation(Scope.class).setStringValue("prototype");
method.addAnnotation(ConditionalOnClass.class).setLiteralValue("value", "CamelContext.class");
method.addAnnotation(ConditionalOnMissingBean.class).setLiteralValue("value", model.getShortJavaType() + ".class");
// Generate Condition
javaClass.addNestedType(createConditionType(javaClass, "camel.language", (overrideLanguageName != null ? overrideLanguageName : model.getName()).toLowerCase(Locale.US)));
sortImports(javaClass);
String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
writeSourceIfChanged(javaClass, fileName);
writeAdditionalSpringMetaData("camel", "language", (overrideLanguageName != null ? overrideLanguageName : model.getName()).toLowerCase(Locale.US));
}
use of org.jboss.forge.roaster.model.source.MethodSource 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));
}
Aggregations