use of org.jboss.forge.roaster.model.Type in project camel by apache.
the class SpringBootAutoConfigurationMojo method writeAdditionalSpringMetaData.
private void writeAdditionalSpringMetaData(String prefix, String type, String name) throws MojoFailureException {
String fullQualifiedName = prefix + "." + type + "." + name + "." + "enabled";
String fileName = "META-INF/additional-spring-configuration-metadata.json";
File target = new File(SpringBootHelper.starterResourceDir(baseDir, project.getArtifactId()), fileName);
deleteFileOnMainArtifact(target);
try {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Map<String, Object> map = null;
List<Map<String, Object>> properties = null;
if (target.exists()) {
BufferedReader br = new BufferedReader(new FileReader(target));
map = gson.fromJson(br, Map.class);
properties = (List<Map<String, Object>>) map.get("properties");
if (properties != null && properties.stream().anyMatch(m -> fullQualifiedName.equals(m.get("name")))) {
getLog().debug("No changes to existing file: " + target);
return;
}
}
Map<String, Object> meta = new HashMap();
meta.put("name", fullQualifiedName);
meta.put("type", "java.lang.Boolean");
meta.put("defaultValue", true);
meta.put("description", "Enable " + name + " " + type);
if (properties == null) {
properties = new ArrayList<>(1);
}
if (map == null) {
map = new HashMap();
}
properties.add(meta);
map.put("properties", properties);
FileUtils.write(target, gson.toJson(map));
} catch (Exception e) {
throw new MojoFailureException("IOError with file " + target, e);
}
}
use of org.jboss.forge.roaster.model.Type 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.Type in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriver method resolveTypeArguments.
private void resolveTypeArguments(List<Type> typeArguments, List<org.kie.workbench.common.services.datamodeller.core.Type> resultTypeArguments) {
if (typeArguments != null) {
for (Type typeArgument : typeArguments) {
org.kie.workbench.common.services.datamodeller.core.impl.TypeImpl resultType = new org.kie.workbench.common.services.datamodeller.core.impl.TypeImpl(typeArgument.getQualifiedName(), new ArrayList<>());
resultTypeArguments.add(resultType);
resolveTypeArguments(typeArgument.getTypeArguments(), resultType.getTypeArguments());
}
}
}
use of org.jboss.forge.roaster.model.Type in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriver method updateField.
public void updateField(JavaClassSource javaClassSource, String fieldName, ObjectProperty property, ClassTypeResolver classTypeResolver) throws Exception {
GenerationTools genTools = new GenerationTools();
GenerationEngine engine = GenerationEngine.getInstance();
GenerationContext context = new GenerationContext(null);
boolean updateAccessors = false;
FieldSource<JavaClassSource> field;
field = javaClassSource.getField(fieldName);
Type oldType = field.getType();
if (hasChangedToCollectionType(field, property, classTypeResolver)) {
// fields that changed to a collection like java.util.List<SomeEntity>
// needs to be removed and created again due to Roaster. Ideally it shouldn't be so.
updateCollectionField(javaClassSource, fieldName, property, classTypeResolver);
} else {
if (!fieldName.equals(property.getName())) {
field.setName(property.getName());
// the field was renamed, accessors must be updated.
updateAccessors = true;
}
if (DriverUtils.isManagedType(field.getType(), classTypeResolver) && !DriverUtils.equalsType(field.getType(), property.getClassName(), property.isMultiple(), property.getBag(), classTypeResolver)) {
// the has type changed, and not to a collection type.
String newClassName = property.getClassName();
field.setType(newClassName);
if (field.getLiteralInitializer() != null) {
// valid for the new type.
if (NamingUtils.isPrimitiveTypeId(newClassName)) {
setPrimitiveTypeDefaultInitializer(field, newClassName);
} else {
field.setLiteralInitializer(null);
}
}
updateAccessors = true;
}
updateAnnotations(field, property.getAnnotations(), classTypeResolver);
if (updateAccessors) {
String accessorName;
String methodSource;
String oldClassName;
// remove old accessors
// TODO check primitive types
Class<?> oldClass = classTypeResolver.resolveType(oldType.getName());
oldClassName = oldClass.getName();
accessorName = genTools.toJavaGetter(fieldName, oldClassName);
removeMethodByParamsClass(javaClassSource, accessorName);
accessorName = genTools.toJavaSetter(fieldName);
removeMethodByParamsClass(javaClassSource, accessorName, oldClass);
// and generate the new ones
methodSource = genTools.indent(engine.generateFieldGetterString(context, property));
javaClassSource.addMethod(methodSource);
methodSource = genTools.indent(engine.generateFieldSetterString(context, property));
javaClassSource.addMethod(methodSource);
}
}
}
use of org.jboss.forge.roaster.model.Type in project kie-wb-common by kiegroup.
the class JavaSourceVisitor method visit.
public void visit(FieldSource<? extends JavaSource> fieldSource) {
Type fieldType = fieldSource.getType();
String fieldClassName;
// the javadoc for Named.getName() is misleading:
// the FieldSource.getName() (which is implemented by FieldImpl.getName())
// returns the (fully-qualified!) name of the field
String fieldName = fieldSource.getName();
resParts.addPart(fieldName, PartType.FIELD);
try {
if (DriverUtils.isManagedType(fieldType, classTypeResolver)) {
if (fieldType.isPrimitive()) {
fieldClassName = fieldType.getName();
} else if (DriverUtils.isSimpleClass(fieldType)) {
fieldClassName = classTypeResolver.getFullTypeName(fieldType.getName());
} else {
// if this point was reached, we know it's a Collection.
// Managed type check was done previously.
Type elementsType = ((List<Type>) fieldType.getTypeArguments()).get(0);
fieldClassName = classTypeResolver.getFullTypeName(elementsType.getName());
}
} else {
// mriet: not complete sure why we don't just do this instead of using DriverUtils?
fieldClassName = fieldType.getQualifiedName();
}
addJavaResourceReference(fieldClassName);
} catch (Exception e) {
logger.error("Unable to index java class field for class: " + javaSource.getQualifiedName() + ", fieldName: " + fieldName + " fieldType: " + fieldType);
}
// Field annotations
for (AnnotationSource annoSource : fieldSource.getAnnotations()) {
visit(annoSource);
}
}
Aggregations