use of org.kie.workbench.common.services.datamodeller.core.impl.ImportImpl in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriverTest method importsUpdateTest.
@Test
public void importsUpdateTest() throws Exception {
Path importsUpdateTestFilePath = rootPath.resolve("package6").resolve("ImportsUpdateTest.java");
String source = ioService.readAllString(importsUpdateTestFilePath);
JavaClassSource importsUpdateTestJavaClassSource = (JavaClassSource) Roaster.parse(source);
ClassLoader classLoader = getClass().getClassLoader();
ClassTypeResolver classTypeResolver = DriverUtils.createClassTypeResolver(importsUpdateTestJavaClassSource, classLoader);
final SourceFilter pojo1Filter = javaType -> false;
final NestedClassFilter nestedClassFilter = javaType -> javaType.isClass() && javaType.getAnnotation(Generated.class) != null;
final MethodFilter methodFilter = method -> !method.isConstructor() && method.getAnnotation(Generated.class) != null;
FilterHolder filterHolder = mock(FilterHolder.class);
when(filterHolder.getSourceFilters()).thenReturn(Collections.singleton(pojo1Filter));
when(filterHolder.getNestedClassFilters()).thenReturn(Collections.singleton(nestedClassFilter));
when(filterHolder.getMethodFilters()).thenReturn(Collections.singleton(methodFilter));
JavaRoasterModelDriver javaRoasterModelDriver = new JavaRoasterModelDriver(ioService, rootPath, classLoader, filterHolder);
ModelDriverResult result = javaRoasterModelDriver.loadDataObject(source, importsUpdateTestFilePath);
DataObject importsUpdateTest = result.getDataModel().getDataObject("org.kie.workbench.common.services.datamodeller.driver.package6.ImportsUpdateTest");
importsUpdateTest.addImport(new ImportImpl(List.class.getName()));
importsUpdateTest.addImport(new ImportImpl(ArrayList.class.getName()));
importsUpdateTest.setName("ImportsUpdateTestResult");
Path expectedFilePath = rootPath.resolve("package6").resolve("ImportsUpdateTestResult.java");
String expectedSource = ioService.readAllString(expectedFilePath);
ModelDriverResult expectedResult = javaRoasterModelDriver.loadDataObject(expectedSource, expectedFilePath);
DataObject importsUpdateTestResult = expectedResult.getDataModel().getDataObject("org.kie.workbench.common.services.datamodeller.driver.package6.ImportsUpdateTestResult");
DataModelerAssert.assertEqualsDataObject(importsUpdateTestResult, importsUpdateTest);
DataModelerAssert.assertEqualsImports(importsUpdateTestResult.getImports(), importsUpdateTest.getImports());
javaRoasterModelDriver.updateSource(importsUpdateTestJavaClassSource, importsUpdateTest, new UpdateInfo(), classTypeResolver);
ModelDriverResult updatedResult = javaRoasterModelDriver.loadDataObject(importsUpdateTestJavaClassSource.toString(), importsUpdateTestFilePath);
DataModelerAssert.assertEqualsDataObject(importsUpdateTestResult, updatedResult.getDataModel().getDataObject("org.kie.workbench.common.services.datamodeller.driver.package6.ImportsUpdateTestResult"));
DataModelerAssert.assertEqualsImports(importsUpdateTestResult.getImports(), updatedResult.getDataModel().getDataObject("org.kie.workbench.common.services.datamodeller.driver.package6.ImportsUpdateTestResult").getImports());
}
use of org.kie.workbench.common.services.datamodeller.core.impl.ImportImpl in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriver method parseDataObject.
private Pair<DataObject, List<ObjectProperty>> parseDataObject(JavaClassSource javaClassSource) throws ModelDriverException {
String className;
String packageName;
String superClass;
String qualifiedName;
ClassTypeResolver classTypeResolver;
className = javaClassSource.getName();
packageName = javaClassSource.getPackage();
qualifiedName = NamingUtils.createQualifiedName(packageName, className);
if (logger.isDebugEnabled()) {
logger.debug("Building DataObject for, packageName: " + packageName + ", className: " + className);
}
classTypeResolver = DriverUtils.createClassTypeResolver(javaClassSource, classLoader);
Visibility visibility = DriverUtils.buildVisibility(javaClassSource.getVisibility());
DataObject dataObject = new DataObjectImpl(packageName, className, visibility, javaClassSource.isAbstract(), javaClassSource.isFinal());
List<ObjectProperty> unmanagedProperties = new ArrayList<ObjectProperty>();
try {
if (javaClassSource.getSuperType() != null) {
superClass = resolveTypeName(classTypeResolver, javaClassSource.getSuperType());
dataObject.setSuperClassName(superClass);
}
List<AnnotationSource<JavaClassSource>> annotations = javaClassSource.getAnnotations();
if (annotations != null) {
for (AnnotationSource annotation : annotations) {
addJavaClassAnnotation(dataObject, annotation, classTypeResolver);
}
}
List<MethodSource<JavaClassSource>> classMethods = javaClassSource.getMethods();
if (classMethods != null) {
for (MethodSource<JavaClassSource> classMethod : classMethods) {
if (isAccepted(classMethod)) {
addMethod(dataObject, classMethod, classTypeResolver);
}
}
}
List<JavaSource<?>> nestedTypes = javaClassSource.getNestedTypes();
if (nestedTypes != null) {
for (JavaSource nestedType : nestedTypes) {
if (isAccepted(nestedType)) {
if (nestedType instanceof JavaClassSource) {
JavaClassImpl nestedJavaClass = new JavaClassImpl("", nestedType.getName(), DriverUtils.buildVisibility(nestedType.getVisibility()));
dataObject.addNestedClass(nestedJavaClass);
if (javaClassSource.getInterfaces() != null) {
for (String interfaceDefinition : ((JavaClassSource) nestedType).getInterfaces()) {
nestedJavaClass.addInterface(interfaceDefinition);
}
}
List<AnnotationSource<JavaClassSource>> nestedClassAnnotations = nestedType.getAnnotations();
if (nestedClassAnnotations != null) {
for (AnnotationSource annotation : nestedClassAnnotations) {
addJavaClassAnnotation(nestedJavaClass, annotation, classTypeResolver);
}
}
List<MethodSource<JavaClassSource>> nestedClassMethods = ((JavaClassSource) nestedType).getMethods();
if (nestedClassMethods != null) {
for (Method nestedClassMethod : nestedClassMethods) {
if (isAccepted(nestedClassMethod)) {
addMethod(nestedJavaClass, nestedClassMethod, classTypeResolver);
}
}
}
}
}
}
}
List<FieldSource<JavaClassSource>> fields = javaClassSource.getFields();
if (fields != null) {
for (FieldSource<JavaClassSource> field : fields) {
if (DriverUtils.isManagedType(field.getType(), classTypeResolver)) {
addProperty(dataObject, field, classTypeResolver);
} else {
logger.debug("field: " + field + "with fieldName: " + field.getName() + " won't be loaded by the diver because type: " + field.getType().getName() + " isn't a managed type.");
unmanagedProperties.add(new ObjectPropertyImpl(field.getName(), field.getType().toString(), false, DriverUtils.buildVisibility(field.getVisibility()), field.isStatic(), field.isFinal()));
}
}
}
List<Import> imports = javaClassSource.getImports();
if (imports != null) {
for (Import _import : imports) {
dataObject.addImport(new ImportImpl(_import.getQualifiedName()));
}
}
return new Pair<DataObject, List<ObjectProperty>>(dataObject, unmanagedProperties);
} catch (ClassNotFoundException e) {
logger.error(errorMessage(DATA_OBJECT_LOAD_ERROR, qualifiedName), e);
throw new ModelDriverException(errorMessage(DATA_OBJECT_LOAD_ERROR, qualifiedName), e);
} catch (ModelDriverException e) {
logger.error(errorMessage(DATA_OBJECT_LOAD_ERROR, qualifiedName), e);
throw new ModelDriverException(errorMessage(DATA_OBJECT_LOAD_ERROR, qualifiedName), e);
}
}
use of org.kie.workbench.common.services.datamodeller.core.impl.ImportImpl in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriverTest method createImportsUpdateTest.
private DataObject createImportsUpdateTest() {
DataObject dataObject = createDataObject("org.kie.workbench.common.services.datamodeller.driver.package6", "ImportsUpdateTest", null);
dataObject.addImport(new ImportImpl(Generated.class.getName()));
return dataObject;
}
use of org.kie.workbench.common.services.datamodeller.core.impl.ImportImpl in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriverTest method createImportsUpdateTestResult.
private DataObject createImportsUpdateTestResult() {
DataObject dataObject = createDataObject("org.kie.workbench.common.services.datamodeller.driver.package6", "ImportsUpdateTestResult", null);
dataObject.addImport(new ImportImpl(Generated.class.getName()));
dataObject.addImport(new ImportImpl(List.class.getName()));
dataObject.addImport(new ImportImpl(ArrayList.class.getName()));
return dataObject;
}
Aggregations