use of org.jboss.forge.roaster.model.source.JavaClassSource in project kie-wb-common by kiegroup.
the class RoasterApiTest method createClass.
public JavaClassSource createClass(String pkg, String name) {
JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
javaClass.setPackage(pkg);
javaClass.setName(name);
return javaClass;
}
use of org.jboss.forge.roaster.model.source.JavaClassSource in project kie-wb-common by kiegroup.
the class DriverUtils method createClassTypeResolver.
public static ClassTypeResolver createClassTypeResolver(JavaSource javaSource, ClassLoader classLoader) {
String packageName;
Set<String> classImports = new HashSet<String>();
// Importer.getImports() returns both normal and static imports
// You can see if an Import is static by calling hte
// Import.isStatic() method
List<Import> imports = javaSource.getImports();
if (imports != null) {
for (Import currentImport : imports) {
String importName = currentImport.getQualifiedName();
if (currentImport.isWildcard()) {
importName = importName + ".*";
}
classImports.add(importName);
}
}
packageName = javaSource.getPackage();
// add current package too, if not added, the class type resolver don't resolve current package classes.
if (packageName != null && !"".equals(packageName)) {
classImports.add(packageName + ".*");
}
if (javaSource instanceof JavaClassSource) {
JavaClassSource javaClassSource = (JavaClassSource) javaSource;
// add current file inner types as import clauses to help the ClassTypeResolver to find variables of inner types
// It was detected that current ClassTypeResolver don't resolve inner classes well.
// workaround for BZ https://bugzilla.redhat.com/show_bug.cgi?id=1172711
List<JavaSource<?>> innerTypes = javaClassSource.getNestedTypes();
if (innerTypes != null) {
for (JavaSource<?> type : innerTypes) {
classImports.add(packageName + "." + javaClassSource.getName() + "." + type.getName());
}
}
}
return new ClassTypeResolver(classImports, classLoader);
}
use of org.jboss.forge.roaster.model.source.JavaClassSource in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriver method addMethod.
private void addMethod(JavaClassSource javaClassSource, org.kie.workbench.common.services.datamodeller.core.Method method, ClassTypeResolver classTypeResolver) throws ClassNotFoundException {
MethodSource<JavaClassSource> methodSource = javaClassSource.addMethod();
methodSource.setName(method.getName());
methodSource.setReturnType(buildMethodReturnTypeString(method.getReturnType(), classTypeResolver));
methodSource.setParameters(buildMethodParameterString(method.getParameters(), classTypeResolver));
methodSource.setBody(method.getBody());
methodSource.setVisibility(buildVisibility(method.getVisibilty()));
for (Annotation annotation : method.getAnnotations()) {
addAnnotation(methodSource, annotation);
}
}
use of org.jboss.forge.roaster.model.source.JavaClassSource in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriver method updateCollectionField.
private void updateCollectionField(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 = true;
FieldSource<JavaClassSource> currentField;
currentField = javaClassSource.getField(fieldName);
Type currentType = currentField.getType();
StringBuilder fieldSource = new StringBuilder();
fieldSource.append(engine.generateCompleteFieldString(context, property));
javaClassSource.removeField(currentField);
javaClassSource.addField(fieldSource.toString());
if (updateAccessors) {
String accessorName;
String methodSource;
String oldClassName;
// remove old accessors
// TODO check primitive types
Class<?> oldClass = classTypeResolver.resolveType(currentType.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.source.JavaClassSource in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriver method removeField.
/**
* Takes care of field and the corresponding setter/getter removal.
*/
public void removeField(JavaClassSource javaClassSource, String fieldName, ClassTypeResolver classTypeResolver) throws Exception {
logger.debug("Removing field: " + fieldName + ", from class: " + javaClassSource.getName());
FieldSource<JavaClassSource> field;
GenerationTools genTools = new GenerationTools();
String methodName;
field = javaClassSource.getField(fieldName);
if (field != null) {
// check if the class has a setter/getter for the given field.
Class<?> fieldClass = classTypeResolver.resolveType(field.getType().getName());
methodName = genTools.toJavaGetter(fieldName, fieldClass.getName());
removeMethodByParamsClass(javaClassSource, methodName);
methodName = genTools.toJavaSetter(fieldName);
removeMethodByParamsClass(javaClassSource, methodName, fieldClass);
// finally remove the field.
javaClassSource.removeField(field);
} else {
logger.debug("Field field: " + fieldName + " was not found in class: " + javaClassSource.getName());
}
}
Aggregations