Search in sources :

Example 1 with ClassLibraryBuilder

use of com.thoughtworks.qdox.library.ClassLibraryBuilder 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 ClassLibraryBuilder

use of com.thoughtworks.qdox.library.ClassLibraryBuilder 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 ClassLibraryBuilder

use of com.thoughtworks.qdox.library.ClassLibraryBuilder in project maven-plugins by apache.

the class AbstractFixJavadocMojo method getQdoxClasses.

/**
 * Calling Qdox to find {@link JavaClass} objects from the Maven project sources.
 * Ignore java class if Qdox has parsing errors.
 *
 * @return an array of {@link JavaClass} found by QDox
 * @throws IOException            if any
 * @throws MojoExecutionException if any
 */
private Collection<JavaClass> getQdoxClasses() throws IOException, MojoExecutionException {
    if ("pom".equalsIgnoreCase(project.getPackaging())) {
        getLog().warn("This project has 'pom' packaging, no Java sources is available.");
        return null;
    }
    List<File> javaFiles = new LinkedList<>();
    for (String sourceRoot : getProjectSourceRoots(project)) {
        File f = new File(sourceRoot);
        if (f.isDirectory()) {
            javaFiles.addAll(FileUtils.getFiles(f, includes, excludes, true));
        } else {
            if (getLog().isWarnEnabled()) {
                getLog().warn(f + " doesn't exist. Ignored it.");
            }
        }
    }
    ClassLibraryBuilder classLibraryBuilder = new OrderedClassLibraryBuilder();
    classLibraryBuilder.appendClassLoader(getProjectClassLoader());
    JavaProjectBuilder builder = new JavaProjectBuilder(classLibraryBuilder);
    builder.setEncoding(encoding);
    for (File f : javaFiles) {
        if (!f.getAbsolutePath().toLowerCase(Locale.ENGLISH).endsWith(".java") && getLog().isWarnEnabled()) {
            getLog().warn("'" + f + "' is not a Java file. Ignored it.");
            continue;
        }
        try {
            builder.addSource(f);
        } catch (ParseException e) {
            if (getLog().isWarnEnabled()) {
                getLog().warn("QDOX ParseException: " + e.getMessage() + ". Can't fix it.");
            }
        }
    }
    return builder.getClasses();
}
Also used : OrderedClassLibraryBuilder(com.thoughtworks.qdox.library.OrderedClassLibraryBuilder) OrderedClassLibraryBuilder(com.thoughtworks.qdox.library.OrderedClassLibraryBuilder) ClassLibraryBuilder(com.thoughtworks.qdox.library.ClassLibraryBuilder) JavaProjectBuilder(com.thoughtworks.qdox.JavaProjectBuilder) ParseException(com.thoughtworks.qdox.parser.ParseException) File(java.io.File) LinkedList(java.util.LinkedList)

Example 4 with ClassLibraryBuilder

use of com.thoughtworks.qdox.library.ClassLibraryBuilder 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 5 with ClassLibraryBuilder

use of com.thoughtworks.qdox.library.ClassLibraryBuilder 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

ClassLibraryBuilder (com.thoughtworks.qdox.library.ClassLibraryBuilder)5 ModifyableClassLibraryBuilder (com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableClassLibraryBuilder)4 File (java.io.File)3 JavaClass (com.thoughtworks.qdox.model.JavaClass)2 JavaSource (com.thoughtworks.qdox.model.JavaSource)2 FileInputStream (java.io.FileInputStream)2 LinkedList (java.util.LinkedList)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 JavaProjectBuilder (com.thoughtworks.qdox.JavaProjectBuilder)1 OrderedClassLibraryBuilder (com.thoughtworks.qdox.library.OrderedClassLibraryBuilder)1 JavaMethod (com.thoughtworks.qdox.model.JavaMethod)1 ParseException (com.thoughtworks.qdox.parser.ParseException)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 StringReader (java.io.StringReader)1 Test (org.junit.Test)1