Search in sources :

Example 1 with ModifyableJavaClass

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);
        }
    }
}
Also used : ModifyableJavaClass(com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableJavaClass) JavaClass(com.thoughtworks.qdox.model.JavaClass) ModifyableJavaClass(com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableJavaClass)

Example 2 with ModifyableJavaClass

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);
}
Also used : Path(java.nio.file.Path) InputStreamReader(java.io.InputStreamReader) BufferedInputStream(java.io.BufferedInputStream) MergeException(com.devonfw.cobigen.api.exception.MergeException) StringReader(java.io.StringReader) IOException(java.io.IOException) ParseException(com.thoughtworks.qdox.parser.ParseException) FileInputStream(java.io.FileInputStream) ModifyableJavaClass(com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableJavaClass)

Example 3 with ModifyableJavaClass

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;
}
Also used : JavaSource(com.thoughtworks.qdox.model.JavaSource) Reader(java.io.Reader) ModifyableJavaClass(com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableJavaClass)

Example 4 with ModifyableJavaClass

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());
        }
    }
}
Also used : ModifyableJavaClass(com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableJavaClass) JavaClass(com.thoughtworks.qdox.model.JavaClass)

Aggregations

ModifyableJavaClass (com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableJavaClass)4 JavaClass (com.thoughtworks.qdox.model.JavaClass)2 MergeException (com.devonfw.cobigen.api.exception.MergeException)1 JavaSource (com.thoughtworks.qdox.model.JavaSource)1 ParseException (com.thoughtworks.qdox.parser.ParseException)1 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 Path (java.nio.file.Path)1