All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jdepend.framework.JavaClassBuilder Maven / Gradle / Ivy

There is a newer version: 2.9.5
Show newest version
package jdepend.framework;

import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

/**
 * The JavaClassBuilder builds JavaClass
 * instances from .class, .jar, .war, or .zip files.
 *
 * @author Mike Clark
 * @author Clarkware Consulting, Inc.
 */

public class JavaClassBuilder {

    private AbstractParser parser;
    private FileManager fileManager;


    public JavaClassBuilder() {
        this(new ClassFileParser(), new FileManager());
    }

    public JavaClassBuilder(FileManager fm) {
        this(new ClassFileParser(), fm);
    }

    public JavaClassBuilder(AbstractParser parser, FileManager fm) {
        this.parser = parser;
        this.fileManager = fm;
    }

    public int countClasses() {
        AbstractParser counter = new AbstractParser() {

            public JavaClass parse(InputStream is) {
                return new JavaClass("");
            }
        };

        JavaClassBuilder builder = new JavaClassBuilder(counter, fileManager);
        Collection classes = builder.build();
        return classes.size();
    }

    /**
     * Builds the JavaClass instances.
     *
     * @return Collection of JavaClass instances.
     */
    public Collection build() {
        Collection classes = new ArrayList();

        for (File nextFile : fileManager.extractFiles()) {
            try {
                classes.addAll(buildClasses(nextFile));
            } catch (IOException ioe) {
                System.err.println("\n" + ioe.getMessage());
            }
        }

        return classes;
    }

    /**
     * Builds the JavaClass instances from the
     * specified file.
     *
     * @param file Class or Jar file.
     * @return Collection of JavaClass instances.
     */
    public Collection buildClasses(File file) throws IOException {
        if (fileManager.acceptClassFile(file)) {
            InputStream is = null;
            try {
                is = new BufferedInputStream(new FileInputStream(file));
                JavaClass parsedClass = parser.parse(is);
                Collection javaClasses = new ArrayList();
                javaClasses.add(parsedClass);
                return javaClasses;
            } finally {
                if (is != null) {
                    is.close();
                }
            }
        } else if (fileManager.acceptJarFile(file)) {
            JarFile jarFile = new JarFile(file);
            Collection result = buildClasses(jarFile);
            jarFile.close();
            return result;
        } else {
            throw new IOException("File is not a valid " + ".class, .jar, .war, or .zip file: " + file.getPath());
        }
    }

    /**
     * Builds the JavaClass instances from the specified
     * jar, war, or zip file.
     *
     * @param file Jar, war, or zip file.
     * @return Collection of JavaClass instances.
     */
    public Collection buildClasses(JarFile file) throws IOException {
        Collection javaClasses = new ArrayList();

        Enumeration entries = file.entries();
        while (entries.hasMoreElements()) {
            ZipEntry e = (ZipEntry) entries.nextElement();
            if (fileManager.acceptClassFileName(e.getName())) {
                InputStream is = null;
                try {
                    is = new BufferedInputStream(file.getInputStream(e));
                    JavaClass jc = parser.parse(is);
                    javaClasses.add(jc);
                } finally {
                    if (is != null) {
                        is.close();
                    }
                }
            }
        }

        return javaClasses;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy