Search in sources :

Example 1 with ModifyableClassLibraryBuilder

use of com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableClassLibraryBuilder in project cobigen by devonfw.

the class JavaInputReader method getInputObjects.

/**
 * Returns all input objects for the given container input.
 *
 * @param input container input (only {@link PackageFolder} instances will be supported)
 * @param inputCharset {@link Charset} to be used to read the children
 * @param recursively states, whether the children should be retrieved recursively
 * @return the list of children. In this case {@link File} objects
 */
public List<Object> getInputObjects(Object input, Charset inputCharset, boolean recursively) {
    LOG.debug("Retrieve input object for input {} {}", input, recursively ? "recursively" : "");
    List<Object> javaClasses = new LinkedList<>();
    if (input instanceof PackageFolder) {
        File packageFolder = new File(((PackageFolder) input).getLocation());
        List<File> files = retrieveAllJavaSourceFiles(packageFolder, recursively);
        for (File f : files) {
            ClassLibraryBuilder classLibraryBuilder = new ModifyableClassLibraryBuilder();
            classLibraryBuilder.appendDefaultClassLoaders();
            ClassLoader containerClassloader = ((PackageFolder) input).getClassLoader();
            if (containerClassloader != null) {
                classLibraryBuilder.appendClassLoader(containerClassloader);
            }
            try (FileInputStream fis = new FileInputStream(f);
                InputStreamReader isr = new InputStreamReader(fis, inputCharset)) {
                classLibraryBuilder.addSource(isr);
                JavaSource source = null;
                for (JavaSource s : classLibraryBuilder.getClassLibrary().getJavaSources()) {
                    source = s;
                    // only consider one class per file
                    break;
                }
                if (source != null) {
                    // save cast as given by the customized builder
                    if (source.getClasses().size() > 0) {
                        JavaClass javaClass = source.getClasses().get(0);
                        // try loading class
                        if (containerClassloader != null) {
                            try {
                                Class<?> loadedClass = containerClassloader.loadClass(javaClass.getCanonicalName());
                                javaClasses.add(new Object[] { javaClass, loadedClass });
                            } catch (ClassNotFoundException e) {
                                LOG.info("Could not load Java type '{}' with the containers class loader. " + "Just returning the parsed Java model.", javaClass.getCanonicalName());
                                javaClasses.add(javaClass);
                            }
                        } else {
                            javaClasses.add(javaClass);
                        }
                    }
                }
            } catch (IOException e) {
                LOG.error("The file {} could not be parsed as a java class", f.getAbsolutePath().toString(), e);
            }
        }
    }
    LOG.debug("{} java classes found!", javaClasses.size());
    return javaClasses;
}
Also used : InputStreamReader(java.io.InputStreamReader) ModifyableClassLibraryBuilder(com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableClassLibraryBuilder) ClassLibraryBuilder(com.thoughtworks.qdox.library.ClassLibraryBuilder) ModifyableClassLibraryBuilder(com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableClassLibraryBuilder) IOException(java.io.IOException) LinkedList(java.util.LinkedList) FileInputStream(java.io.FileInputStream) JavaClass(com.thoughtworks.qdox.model.JavaClass) PackageFolder(com.devonfw.cobigen.javaplugin.inputreader.to.PackageFolder) JavaSource(com.thoughtworks.qdox.model.JavaSource) File(java.io.File)

Example 2 with ModifyableClassLibraryBuilder

use of com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableClassLibraryBuilder 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
 *
 * @param reader {@link Reader}s which contents should be parsed
 * @return the parsed {@link JavaClass}
 */
public static JavaClass getFirstJavaClass(Reader... reader) {
    ClassLibraryBuilder classLibraryBuilder = new ModifyableClassLibraryBuilder();
    classLibraryBuilder.appendDefaultClassLoaders();
    return getFirstJavaClass(classLibraryBuilder, reader);
}
Also used : ModifyableClassLibraryBuilder(com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableClassLibraryBuilder) ClassLibraryBuilder(com.thoughtworks.qdox.library.ClassLibraryBuilder) ModifyableClassLibraryBuilder(com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableClassLibraryBuilder)

Example 3 with ModifyableClassLibraryBuilder

use of com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableClassLibraryBuilder 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. By passing a {@link ClassLoader}, you can take impact on the class name resolving
 *
 * @param classLoader which should be used for class name resolving
 * @param reader {@link Reader}s which contents should be parsed
 * @return the parsed {@link JavaClass}
 */
public static JavaClass getFirstJavaClass(ClassLoader classLoader, Reader... reader) {
    ClassLibraryBuilder classLibraryBuilder = new ModifyableClassLibraryBuilder();
    classLibraryBuilder.appendClassLoader(classLoader);
    return getFirstJavaClass(classLibraryBuilder, reader);
}
Also used : ModifyableClassLibraryBuilder(com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableClassLibraryBuilder) ClassLibraryBuilder(com.thoughtworks.qdox.library.ClassLibraryBuilder) ModifyableClassLibraryBuilder(com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableClassLibraryBuilder)

Example 4 with ModifyableClassLibraryBuilder

use of com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableClassLibraryBuilder in project cobigen by devonfw.

the class JavaMergerTest method testMergeMethodsWithoutExtendingMethodBodyWithWhitespaces.

/**
 * Tests merging java without adding new lines to method bodies (was a bug)
 *
 * @throws IOException test fails
 * @throws MergeException test fails
 */
@Test
public void testMergeMethodsWithoutExtendingMethodBodyWithWhitespaces() throws IOException, MergeException {
    File file = new File(testFileRootPath + "PatchFile_method.java");
    ClassLibraryBuilder classLibraryBuilder = new ModifyableClassLibraryBuilder();
    try (FileInputStream fileInputStream = new FileInputStream(file)) {
        JavaSource source = classLibraryBuilder.addSource(fileInputStream);
        JavaClass origClazz = source.getClasses().get(0);
        String mergedContents = new JavaMerger("", true).merge(file, FileUtils.readFileToString(file, Charset.forName("UTF-8")), "UTF-8");
        classLibraryBuilder = new ModifyableClassLibraryBuilder();
        try (StringReader stringReader = new StringReader(mergedContents)) {
            source = classLibraryBuilder.addSource(stringReader);
            JavaClass resultClazz = source.getClasses().get(0);
            for (JavaMethod method : resultClazz.getMethods()) {
                JavaMethod origMethod = origClazz.getMethodBySignature(method.getName(), method.getParameterTypes());
                assertThat(method.getCodeBlock()).isEqualTo(origMethod.getCodeBlock());
            }
        }
    }
}
Also used : JavaParserUtil.getFirstJavaClass(com.devonfw.cobigen.javaplugin.inputreader.JavaParserUtil.getFirstJavaClass) JavaClass(com.thoughtworks.qdox.model.JavaClass) ModifyableClassLibraryBuilder(com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableClassLibraryBuilder) ClassLibraryBuilder(com.thoughtworks.qdox.library.ClassLibraryBuilder) JavaMerger(com.devonfw.cobigen.javaplugin.merger.JavaMerger) JavaSource(com.thoughtworks.qdox.model.JavaSource) StringReader(java.io.StringReader) JavaMethod(com.thoughtworks.qdox.model.JavaMethod) ModifyableClassLibraryBuilder(com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableClassLibraryBuilder) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

ModifyableClassLibraryBuilder (com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableClassLibraryBuilder)4 ClassLibraryBuilder (com.thoughtworks.qdox.library.ClassLibraryBuilder)4 JavaClass (com.thoughtworks.qdox.model.JavaClass)2 JavaSource (com.thoughtworks.qdox.model.JavaSource)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 JavaParserUtil.getFirstJavaClass (com.devonfw.cobigen.javaplugin.inputreader.JavaParserUtil.getFirstJavaClass)1 PackageFolder (com.devonfw.cobigen.javaplugin.inputreader.to.PackageFolder)1 JavaMerger (com.devonfw.cobigen.javaplugin.merger.JavaMerger)1 JavaMethod (com.thoughtworks.qdox.model.JavaMethod)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 StringReader (java.io.StringReader)1 LinkedList (java.util.LinkedList)1 Test (org.junit.Test)1