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);
}
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);
}
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));
}
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);
}
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());
}
Aggregations