use of org.jboss.forge.roaster.model.source.JavaClassSource in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriver method parseManagedTypesProperties.
public List<ObjectProperty> parseManagedTypesProperties(JavaClassSource javaClassSource, ClassTypeResolver classTypeResolver) throws ModelDriverException {
List<FieldSource<JavaClassSource>> fields = javaClassSource.getFields();
List<ObjectProperty> properties = new ArrayList<ObjectProperty>();
ObjectProperty property;
for (FieldSource<JavaClassSource> field : fields) {
if (DriverUtils.isManagedType(field.getType(), classTypeResolver)) {
property = parseProperty(field, classTypeResolver);
properties.add(property);
} 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.");
}
}
return properties;
}
use of org.jboss.forge.roaster.model.source.JavaClassSource in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriver method updateSource.
public void updateSource(JavaClassSource javaClassSource, DataObject dataObject, UpdateInfo updateInfo, ClassTypeResolver classTypeResolver) throws Exception {
if (javaClassSource == null || !javaClassSource.isClass()) {
logger.warn("A null javaClassSource or javaClassSouce is not a Class, no processing will be done. javaClassSource: " + javaClassSource + " className: " + (javaClassSource != null ? javaClassSource.getName() : null));
return;
}
Map<String, FieldSource<JavaClassSource>> currentClassFields = new HashMap<String, FieldSource<JavaClassSource>>();
List<FieldSource<JavaClassSource>> classFields = javaClassSource.getFields();
Map<String, String> preservedFields = new HashMap<String, String>();
// update package, class name, and super class name if needed.
updatePackage(javaClassSource, dataObject.getPackageName());
updateImports(javaClassSource, dataObject.getImports(), updateInfo);
updateAnnotations(javaClassSource, dataObject.getAnnotations(), classTypeResolver);
updateMethods(javaClassSource, dataObject.getMethods(), classTypeResolver);
updateClassName(javaClassSource, dataObject.getName());
updateSuperClassName(javaClassSource, dataObject.getSuperClassName(), classTypeResolver);
if (classFields != null) {
for (FieldSource<JavaClassSource> field : classFields) {
currentClassFields.put(field.getName(), field);
}
}
List<ObjectProperty> currentManagedProperties = parseManagedTypesProperties(javaClassSource, classTypeResolver);
currentManagedProperties = DataModelUtils.filterAssignableFields(currentManagedProperties);
// prior to touch the class fields get the constructors candidates to be the current all fields, position annotated fields, and key annotated fields constructors.
List<MethodSource<JavaClassSource>> allFieldsConstructorCandidates = findAllFieldsConstructorCandidates(javaClassSource, currentManagedProperties, classTypeResolver);
List<MethodSource<JavaClassSource>> keyFieldsConstructorCandidates = findKeyFieldsConstructorCandidates(javaClassSource, currentManagedProperties, classTypeResolver);
List<MethodSource<JavaClassSource>> positionFieldsConstructorCandidates = findPositionFieldsConstructorCandidates(javaClassSource, currentManagedProperties, classTypeResolver);
// create new fields and update existing.
for (ObjectProperty property : dataObject.getProperties()) {
if (property.isFinal() || property.isStatic()) {
preservedFields.put(property.getName(), property.getName());
continue;
}
if (currentClassFields.containsKey(property.getName())) {
updateField(javaClassSource, property.getName(), property, classTypeResolver);
} else {
createField(javaClassSource, property, classTypeResolver);
}
preservedFields.put(property.getName(), property.getName());
}
// update constructors, equals and hashCode methods.
updateConstructors(javaClassSource, dataObject, allFieldsConstructorCandidates, keyFieldsConstructorCandidates, positionFieldsConstructorCandidates, classTypeResolver);
// delete fields from .java file that not exists in the DataObject.
List<String> removableFields = new ArrayList<String>();
for (FieldSource<JavaClassSource> field : currentClassFields.values()) {
if (!preservedFields.containsKey(field.getName()) && isManagedField(field, classTypeResolver)) {
removableFields.add(field.getName());
}
}
for (String fieldName : removableFields) {
removeField(javaClassSource, fieldName, classTypeResolver);
}
// update nested classes
List<JavaSource<?>> nestedTypes = javaClassSource.getNestedTypes();
if (nestedTypes != null) {
for (JavaSource nestedJavaSource : nestedTypes) {
if (isAccepted(nestedJavaSource)) {
javaClassSource.removeNestedType(nestedJavaSource);
}
}
}
GenerationEngine engine = GenerationEngine.getInstance();
GenerationContext context = new GenerationContext(null);
for (org.kie.workbench.common.services.datamodeller.core.JavaClass nestedJavaClass : dataObject.getNestedClasses()) {
javaClassSource.addNestedType(engine.generateNestedClassString(context, nestedJavaClass, ""));
}
}
use of org.jboss.forge.roaster.model.source.JavaClassSource in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriverTest method updateAnnotationsTest.
@Test
public void updateAnnotationsTest() {
try {
// First read the AnnotationsUpdateTest
Path annotationsUpdateTestFilePath = rootPath.resolve("package3").resolve("AnnotationsUpdateTest.java");
String source = ioService.readAllString(annotationsUpdateTestFilePath);
JavaClassSource annotationsUpdateTestJavaClassSource = (JavaClassSource) Roaster.parse(source);
ClassLoader classLoader = getClass().getClassLoader();
ClassTypeResolver classTypeResolver = DriverUtils.createClassTypeResolver(annotationsUpdateTestJavaClassSource, 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, annotationsUpdateTestFilePath);
// 1) read the AnnotationsUpdateTest
DataObject annotationsUpdateTest = result.getDataModel().getDataObject("org.kie.workbench.common.services.datamodeller.driver.package3.AnnotationsUpdateTest");
// 2) modify the AnnotationsUpdateTest according to the expected result, basically updating all the needed annotations.
// update the AnnotationValuesAnnotation
Annotation annotationValuesAnnotation = annotationsUpdateTest.getAnnotation(AnnotationValuesAnnotation.class.getName());
// annotation.setValue( "primitivesParam", @PrimitivesAnnotation(stringParam="\"line1\" \n line2 \\ \n line3") );
Annotation primitivesParamValue = createAnnotation(PrimitivesAnnotation.class);
// construct the value -> "\"line1\" \n line2 \\ \n line3"
StringBuilder stringParamBuilder = new StringBuilder();
stringParamBuilder.append('"').append("line1").append('"').append(" ").append('\n');
stringParamBuilder.append(" line2 ").append('\\').append(" ").append('\n');
stringParamBuilder.append(" line3");
primitivesParamValue.setValue("stringParam", stringParamBuilder.toString());
annotationValuesAnnotation.setValue("primitivesParam", primitivesParamValue);
// @PrimitivesAnnotation(intParam=1)
Annotation primitiveValue1 = createAnnotation(PrimitivesAnnotation.class);
primitiveValue1.setValue("intParam", 2);
// @PrimitivesAnnotation(intParam=3)
Annotation primitiveValue2 = createAnnotation(PrimitivesAnnotation.class);
primitiveValue2.setValue("intParam", 3);
List<Annotation> primitivesArrayParamValue = new ArrayList<Annotation>();
primitivesArrayParamValue.add(primitiveValue1);
primitivesArrayParamValue.add(primitiveValue2);
// annotation.setValue( "primitivesArrayParam", "{@PrimitivesAnnotation(intParam=2),@PrimitivesAnnotation(intParam=3)}" );
annotationValuesAnnotation.setValue("primitivesArrayParam", primitivesArrayParamValue);
// annotation.setValue( "enumsParam", "@EnumsAnnotation(enum1Param=TestEnums.ENUM1.VALUE2)" );
Annotation enumsParamValue = createAnnotation(EnumsAnnotation.class);
enumsParamValue.setValue("enum1Param", TestEnums.ENUM1.VALUE2.toString());
annotationValuesAnnotation.setValue("enumsParam", enumsParamValue);
// annotation.setValue( "enumsArrayParam", "{@EnumsAnnotation(enum1Param=TestEnums.ENUM1.VALUE2),@EnumsAnnotation(enum1Param=TestEnums.ENUM1.VALUE3)}" );
Annotation enumArrayParam1 = createAnnotation(EnumsAnnotation.class);
enumArrayParam1.setValue("enum1Param", TestEnums.ENUM1.VALUE2.toString());
Annotation enumArrayParam2 = createAnnotation(EnumsAnnotation.class);
enumArrayParam2.setValue("enum1Param", TestEnums.ENUM1.VALUE3.toString());
List<Annotation> enumVarrayValues = new ArrayList<Annotation>();
enumVarrayValues.add(enumArrayParam1);
enumVarrayValues.add(enumArrayParam2);
annotationValuesAnnotation.setValue("enumsArrayParam", enumVarrayValues);
// annotation.setValue( "classAnnotationParam", "@ClassAnnotation(classParam=Set.class)" );
Annotation classAnnotationParamValue = createAnnotation(ClassAnnotation.class);
classAnnotationParamValue.setValue("classParam", "Set.class");
annotationValuesAnnotation.setValue("classAnnotationParam", classAnnotationParamValue);
// annotation.setValue( "classAnnotationArrayParam", "{@ClassAnnotation(classParam=Set.class),@ClassAnnotation(classParam=Set.class)}" );
Annotation classAnnotationArrayParamValue1 = createAnnotation(ClassAnnotation.class);
classAnnotationArrayParamValue1.setValue("classParam", "Set.class");
Annotation classAnnotationArrayParamValue2 = createAnnotation(ClassAnnotation.class);
classAnnotationArrayParamValue2.setValue("classParam", "Set.class");
List<Annotation> classAnnotationArrayParamValue = new ArrayList<Annotation>();
classAnnotationArrayParamValue.add(classAnnotationArrayParamValue1);
classAnnotationArrayParamValue.add(classAnnotationArrayParamValue2);
annotationValuesAnnotation.setValue("classAnnotationArrayParam", classAnnotationArrayParamValue);
// update the ClassAnnotation
Annotation classAnnotation = createAnnotation(ClassAnnotation.class);
classAnnotation.setValue("classParam", "java.util.Collection.class");
classAnnotation.setValue("classArrayParam", createArrayParam("List.class"));
annotationsUpdateTest.addAnnotation(classAnnotation);
// update the EnumsAnnotation
Annotation enumsAnnotation = createAnnotation(EnumsAnnotation.class);
enumsAnnotation.setValue("enum1Param", TestEnums.ENUM1.VALUE2.toString());
// enumsAnnotation.setValue( "enum1ArrayParam", "{TestEnums.ENUM1.VALUE3}" );
enumsAnnotation.setValue("enum1ArrayParam", createEnumArrayParam(TestEnums.ENUM1.VALUE3));
enumsAnnotation.setValue("enum2Param", TestEnums.ENUM2.VALUE2.toString());
// enumsAnnotation.setValue( "enum2ArrayParam", "{TestEnums.ENUM2.VALUE3}" );
enumsAnnotation.setValue("enum2ArrayParam", createEnumArrayParam(TestEnums.ENUM2.VALUE3));
annotationsUpdateTest.addAnnotation(enumsAnnotation);
// update the PrimitivesAnnotation
Annotation primitivesAnnotation = createAnnotation(PrimitivesAnnotation.class);
primitivesAnnotation.setValue("byteParam", new Byte("2"));
// primitivesAnnotation.setValue( "byteArrayParam", "{3,4}" );
primitivesAnnotation.setValue("byteArrayParam", createArrayParam((byte) 3, (byte) 4));
primitivesAnnotation.setValue("shortParam", new Short("2"));
// primitivesAnnotation.setValue( "shortArrayParam", "{3,4}" );
primitivesAnnotation.setValue("shortArrayParam", createArrayParam((short) 3, (short) 4));
primitivesAnnotation.setValue("stringParam", "2");
primitivesAnnotation.setValue("stringArrayParam", createArrayParam("3", "4"));
annotationsUpdateTest.addAnnotation(primitivesAnnotation);
// add the MarkerAnnotation
annotationsUpdateTest.addAnnotation(createMarkerAnnotation());
annotationsUpdateTest.setName("AnnotationsUpdateTestResult");
// 3) compare the modified data object with the expected data object.
Path expectedFilePath = rootPath.resolve("package3").resolve("AnnotationsUpdateTestResult.java");
String expectedSource = ioService.readAllString(expectedFilePath);
JavaClassSource expectedJavaClassSource = (JavaClassSource) Roaster.parse(expectedSource);
ModelDriverResult expectedResult = javaRoasterModelDriver.loadDataObject(expectedSource, expectedFilePath);
DataObject annotationsUpdateTestResult = expectedResult.getDataModel().getDataObject("org.kie.workbench.common.services.datamodeller.driver.package3.AnnotationsUpdateTestResult");
// First check, the modified data object in memory should be the same as the readed from the model.
DataModelerAssert.assertEqualsDataObject(annotationsUpdateTestResult, annotationsUpdateTest);
// Second check, update the JavaClassSource corresponding to the AnnotationsUpdateTest
javaRoasterModelDriver.updateSource(annotationsUpdateTestJavaClassSource, annotationsUpdateTest, new UpdateInfo(), classTypeResolver);
ModelDriverResult updatedResult = javaRoasterModelDriver.loadDataObject(annotationsUpdateTestJavaClassSource.toString(), annotationsUpdateTestFilePath);
// and now compare the updatedResult with the expected value.
DataModelerAssert.assertEqualsDataObject(annotationsUpdateTestResult, updatedResult.getDataModel().getDataObject("org.kie.workbench.common.services.datamodeller.driver.package3.AnnotationsUpdateTestResult"));
logger.debug(annotationsUpdateTestJavaClassSource.toString());
} catch (Exception e) {
e.printStackTrace();
fail("Test failed: " + e.getMessage());
}
}
use of org.jboss.forge.roaster.model.source.JavaClassSource in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriverTest method nestedClassUpdateTest.
@Test
public void nestedClassUpdateTest() {
try {
// First read the NestedClassUpdateTest
Path nestedClassUpdateTestFilePath = rootPath.resolve("package5").resolve("NestedClassUpdateTest.java");
String source = ioService.readAllString(nestedClassUpdateTestFilePath);
JavaClassSource nestedClassUpdateTestJavaClassSource = (JavaClassSource) Roaster.parse(source);
ClassLoader classLoader = getClass().getClassLoader();
ClassTypeResolver classTypeResolver = DriverUtils.createClassTypeResolver(nestedClassUpdateTestJavaClassSource, 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, nestedClassUpdateTestFilePath);
// 1) read the NestedClassUpdateTest
DataObject nestedClassUpdateTest = result.getDataModel().getDataObject("org.kie.workbench.common.services.datamodeller.driver.package5.NestedClassUpdateTest");
// 2) modify the NestedClassUpdateTest according to the expected result
JavaClass nestedClass = nestedClassUpdateTest.getNestedClasses().stream().filter(t -> t.getName().equals("NestedClass")).findFirst().get();
assertNotNull(nestedClass);
nestedClass.setName("UpdatedNestedClass");
String removedInterface = nestedClass.removeInterface("ParametrizedInterface<Integer>");
assertNotNull(removedInterface);
nestedClass.addInterface("ParametrizedInterface<Double>");
Method method = nestedClass.getMethod("method", Collections.EMPTY_LIST);
assertNotNull(method);
method.setName("updatedMethod");
nestedClassUpdateTest.setName("NestedClassUpdateTestResult");
// 3) compare the modified data object with the expected data object.
Path expectedFilePath = rootPath.resolve("package5").resolve("NestedClassUpdateTestResult.java");
String expectedSource = ioService.readAllString(expectedFilePath);
JavaClassSource expectedJavaClassSource = (JavaClassSource) Roaster.parse(expectedSource);
ModelDriverResult expectedResult = javaRoasterModelDriver.loadDataObject(expectedSource, expectedFilePath);
DataObject nestedClassUpdateTestResult = expectedResult.getDataModel().getDataObject("org.kie.workbench.common.services.datamodeller.driver.package5.NestedClassUpdateTestResult");
// First check, the modified data object in memory should be the same as the readed from the model.
DataModelerAssert.assertEqualsDataObject(nestedClassUpdateTestResult, nestedClassUpdateTest);
// Second check, update the JavaClassSource corresponding to the NestedClassUpdateTest
javaRoasterModelDriver.updateSource(nestedClassUpdateTestJavaClassSource, nestedClassUpdateTest, new UpdateInfo(), classTypeResolver);
ModelDriverResult updatedResult = javaRoasterModelDriver.loadDataObject(nestedClassUpdateTestJavaClassSource.toString(), nestedClassUpdateTestFilePath);
// and now compare the updatedResult with the expected value.
DataModelerAssert.assertEqualsDataObject(nestedClassUpdateTestResult, updatedResult.getDataModel().getDataObject("org.kie.workbench.common.services.datamodeller.driver.package5.NestedClassUpdateTestResult"));
logger.debug(nestedClassUpdateTestJavaClassSource.toString());
} catch (Exception e) {
e.printStackTrace();
fail("Test failed: " + e.getMessage());
}
}
use of org.jboss.forge.roaster.model.source.JavaClassSource in project EhViewer by seven332.
the class EhDaoGenerator method adjustLocalFavoriteInfo.
private static void adjustLocalFavoriteInfo() throws Exception {
JavaClassSource javaClass = Roaster.parse(JavaClassSource.class, new File(LOCAL_FAVORITE_INFO_PATH));
// Remove field from GalleryInfo
javaClass.removeField(javaClass.getField("gid"));
javaClass.removeField(javaClass.getField("token"));
javaClass.removeField(javaClass.getField("title"));
javaClass.removeField(javaClass.getField("titleJpn"));
javaClass.removeField(javaClass.getField("thumb"));
javaClass.removeField(javaClass.getField("category"));
javaClass.removeField(javaClass.getField("posted"));
javaClass.removeField(javaClass.getField("uploader"));
javaClass.removeField(javaClass.getField("rating"));
javaClass.removeField(javaClass.getField("simpleLanguage"));
// Set all field public
javaClass.getField("time").setPublic();
// Add Parcelable stuff
javaClass.addMethod("\t@Override\n" + "\tpublic int describeContents() {\n" + "\t\treturn 0;\n" + "\t}");
javaClass.addMethod("\t@Override\n" + "\tpublic void writeToParcel(Parcel dest, int flags) {\n" + "\t\tsuper.writeToParcel(dest, flags);\n" + "\t\tdest.writeLong(this.time);\n" + "\t}");
javaClass.addMethod("\tprotected LocalFavoriteInfo(Parcel in) {\n" + "\t\tsuper(in);\n" + "\t\tthis.time = in.readLong();\n" + "\t}").setConstructor(true);
javaClass.addField("\tpublic static final Creator<LocalFavoriteInfo> CREATOR = new Creator<LocalFavoriteInfo>() {\n" + "\t\t@Override\n" + "\t\tpublic LocalFavoriteInfo createFromParcel(Parcel source) {\n" + "\t\t\treturn new LocalFavoriteInfo(source);\n" + "\t\t}\n" + "\n" + "\t\t@Override\n" + "\t\tpublic LocalFavoriteInfo[] newArray(int size) {\n" + "\t\t\treturn new LocalFavoriteInfo[size];\n" + "\t\t}\n" + "\t};");
javaClass.addImport("android.os.Parcel");
// Add from GalleryInfo constructor
javaClass.addMethod("\tpublic LocalFavoriteInfo(GalleryInfo galleryInfo) {\n" + "\t\tthis.gid = galleryInfo.gid;\n" + "\t\tthis.token = galleryInfo.token;\n" + "\t\tthis.title = galleryInfo.title;\n" + "\t\tthis.titleJpn = galleryInfo.titleJpn;\n" + "\t\tthis.thumb = galleryInfo.thumb;\n" + "\t\tthis.category = galleryInfo.category;\n" + "\t\tthis.posted = galleryInfo.posted;\n" + "\t\tthis.uploader = galleryInfo.uploader;\n" + "\t\tthis.rating = galleryInfo.rating;\n" + "\t\tthis.simpleTags = galleryInfo.simpleTags;\n" + "\t\tthis.simpleLanguage = galleryInfo.simpleLanguage;\n" + "\t}").setConstructor(true);
javaClass.addImport("com.hippo.ehviewer.client.data.GalleryInfo");
FileWriter fileWriter = new FileWriter(LOCAL_FAVORITE_INFO_PATH);
fileWriter.write(javaClass.toString());
fileWriter.close();
}
Aggregations