use of com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableJavaClass in project cobigen by devonfw.
the class JavaMerger method mergeInnerClasses.
/**
* Merges all inner {@link JavaClass}es of the given {@link JavaClass}es
*
* @param baseClass {@link JavaClass}
* @param patchClass {@link JavaClass}
* @author mbrunnli (19.03.2013)
*/
private void mergeInnerClasses(ModifyableJavaClass baseClass, ModifyableJavaClass patchClass) {
for (JavaClass rawInnerPatchClass : patchClass.getNestedClasses()) {
ModifyableJavaClass innerPatchClass = (ModifyableJavaClass) rawInnerPatchClass;
ModifyableJavaClass nestedBaseClass = (ModifyableJavaClass) baseClass.getNestedClassByName(innerPatchClass.getName());
if (nestedBaseClass == null) {
baseClass.addClass(innerPatchClass);
} else {
merge(nestedBaseClass, innerPatchClass);
}
}
}
use of com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableJavaClass in project cobigen by devonfw.
the class JavaMerger method merge.
@Override
public String merge(File base, String patch, String targetCharset) throws MergeException {
ModifyableJavaClass baseClass;
String lineDelimiter;
Path path = Paths.get(base.getAbsolutePath());
try (FileInputStream stream = new FileInputStream(path.toString());
BufferedInputStream bis = new BufferedInputStream(stream);
InputStreamReader reader = new InputStreamReader(bis, targetCharset)) {
baseClass = (ModifyableJavaClass) JavaParserUtil.getFirstJavaClass(reader);
lineDelimiter = SystemUtil.determineLineDelimiter(path, targetCharset);
} catch (IOException e) {
throw new MergeException(base, "Cannot read base file.", e);
} catch (ParseException e) {
throw new MergeException(base, "The syntax of the base file is invalid. Error in line: " + e.getLine() + " / column: " + e.getColumn() + ": " + e.getMessage(), e);
}
ModifyableJavaClass patchClass;
try (StringReader reader = new StringReader(patch)) {
patchClass = (ModifyableJavaClass) JavaParserUtil.getFirstJavaClass(reader);
} catch (ParseException e) {
throw new MergeException(base, "The syntax of the generated patch is invalid. Error in line: " + e.getLine() + " / column: " + e.getColumn() + ": " + e.getMessage(), e);
}
if (baseClass == null) {
throw new MergeException(base, "The base file does not declare a valid JavaClass.");
} else if (patchClass == null) {
throw new MergeException(base, "The patch does not declare a valid JavaClass.");
}
ModifyableJavaClass mergedClass = merge(baseClass, patchClass);
return StringUtil.consolidateLineEndings(mergedClass.getSource().getCodeBlock(), lineDelimiter);
}
use of com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableJavaClass in project cobigen by devonfw.
the class JavaParserUtil method getFirstJavaClass.
/**
* Returns the first {@link JavaClass} parsed by the given {@link Reader}, all upcoming parsed java files will be
* added to the class library. Furthermore, a pre-built {@link ClassLibraryBuilder} should be passed, which should be
* previously enriched by all necessary {@link ClassLoader}s.
*
* @param classLibraryBuilder {@link ClassLibraryBuilder} to build the sources with
* @param reader {@link Reader}s which contents should be parsed
* @return the parsed {@link JavaClass}
*/
private static JavaClass getFirstJavaClass(ClassLibraryBuilder classLibraryBuilder, Reader... reader) {
JavaSource source = null;
ModifyableJavaClass targetClass = null;
for (Reader r : reader) {
source = classLibraryBuilder.addSource(r);
if (targetClass == null) {
targetClass = (ModifyableJavaClass) source.getClasses().get(0);
}
}
return targetClass;
}
use of com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableJavaClass in project cobigen by devonfw.
the class JavaMerger method mergeSupertypes.
/**
* Merges all super types of the two class sources
*
* @param baseClass {@link JavaClass}
* @param patchClass {@link JavaClass}
* @author mbrunnli (03.06.2013)
*/
private void mergeSupertypes(ModifyableJavaClass baseClass, ModifyableJavaClass patchClass) {
if (this.patchOverrides) {
baseClass.setImplementz(patchClass.getInterfaces());
if (patchClass.getSuperClass() != null && !patchClass.getSuperClass().getCanonicalName().equals("java.lang.Enum")) {
baseClass.setSuperClass(patchClass.getSuperClass());
}
} else {
List<JavaClass> baseClassInterfaces = baseClass.getInterfaces();
for (JavaClass pClass : patchClass.getInterfaces()) {
// TODO funktioniert noch nicht, da super klassen nicht im QDox Modell sind
if (!baseClassInterfaces.contains(pClass) && !baseClass.isA(pClass)) {
baseClassInterfaces.add(pClass);
}
}
baseClass.setImplementz(baseClassInterfaces);
if (baseClass.getSuperClass() == null || baseClass.getSuperClass().getCanonicalName().equals("java.lang.Object")) {
baseClass.setSuperClass(patchClass.getSuperClass());
}
}
}
Aggregations