Search in sources :

Example 6 with ClassParser

use of org.apache.bcel.classfile.ClassParser in project ant by apache.

the class AncestorAnalyzer method determineDependencies.

/**
 * Determine the dependencies of the configured root classes.
 *
 * @param files a vector to be populated with the files which contain
 *      the dependency classes
 * @param classes a vector to be populated with the names of the
 *      dependency classes.
 */
@Override
protected void determineDependencies(Vector<File> files, Vector<String> classes) {
    // we get the root classes and build up a set of
    // classes upon which they depend
    Set<String> dependencies = new HashSet<>();
    Set<File> containers = new HashSet<>();
    Set<String> toAnalyze = new HashSet<>();
    Set<String> nextAnalyze = new HashSet<>();
    for (Enumeration<String> e = getRootClasses(); e.hasMoreElements(); ) {
        toAnalyze.add(e.nextElement());
    }
    int count = 0;
    int maxCount = isClosureRequired() ? MAX_LOOPS : 2;
    while (!toAnalyze.isEmpty() && count++ < maxCount) {
        nextAnalyze.clear();
        for (String classname : toAnalyze) {
            dependencies.add(classname);
            try {
                File container = getClassContainer(classname);
                if (container == null) {
                    continue;
                }
                containers.add(container);
                ClassParser parser;
                if (container.getName().endsWith(".class")) {
                    parser = new ClassParser(container.getPath());
                } else {
                    parser = new ClassParser(container.getPath(), classname.replace('.', '/') + ".class");
                }
                JavaClass javaClass = parser.parse();
                for (String interfaceName : javaClass.getInterfaceNames()) {
                    if (!dependencies.contains(interfaceName)) {
                        nextAnalyze.add(interfaceName);
                    }
                }
                if (javaClass.isClass()) {
                    String superClass = javaClass.getSuperclassName();
                    if (!dependencies.contains(superClass)) {
                        nextAnalyze.add(superClass);
                    }
                }
            } catch (IOException ioe) {
            // ignore
            }
        }
        Set<String> temp = toAnalyze;
        toAnalyze = nextAnalyze;
        nextAnalyze = temp;
    }
    files.clear();
    files.addAll(containers);
    classes.clear();
    classes.addAll(dependencies);
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) IOException(java.io.IOException) File(java.io.File) HashSet(java.util.HashSet) ClassParser(org.apache.bcel.classfile.ClassParser)

Example 7 with ClassParser

use of org.apache.bcel.classfile.ClassParser in project ant by apache.

the class FullAnalyzer method determineDependencies.

/**
 * Determine the dependencies of the configured root classes.
 *
 * @param files a vector to be populated with the files which contain
 *      the dependency classes
 * @param classes a vector to be populated with the names of the
 *      dependency classes.
 */
@Override
protected void determineDependencies(Vector<File> files, Vector<String> classes) {
    // we get the root classes and build up a set of
    // classes upon which they depend
    Set<String> dependencies = new HashSet<>();
    Set<File> containers = new HashSet<>();
    Set<String> toAnalyze = new HashSet<>(Collections.list(getRootClasses()));
    int count = 0;
    int maxCount = isClosureRequired() ? MAX_LOOPS : 2;
    while (!toAnalyze.isEmpty() && count++ < maxCount) {
        DependencyVisitor dependencyVisitor = new DependencyVisitor();
        for (String classname : toAnalyze) {
            dependencies.add(classname);
            try {
                File container = getClassContainer(classname);
                if (container == null) {
                    continue;
                }
                containers.add(container);
                ClassParser parser;
                if (container.getName().endsWith(".class")) {
                    parser = new ClassParser(container.getPath());
                } else {
                    parser = new ClassParser(container.getPath(), classname.replace('.', '/') + ".class");
                }
                JavaClass javaClass = parser.parse();
                DescendingVisitor traverser = new DescendingVisitor(javaClass, dependencyVisitor);
                traverser.visit();
            } catch (IOException ioe) {
            // ignore
            }
        }
        toAnalyze.clear();
        // now recover all the dependencies collected and add to the list.
        Enumeration<String> depsEnum = dependencyVisitor.getDependencies();
        while (depsEnum.hasMoreElements()) {
            String className = depsEnum.nextElement();
            if (!dependencies.contains(className)) {
                toAnalyze.add(className);
            }
        }
    }
    files.clear();
    files.addAll(containers);
    classes.clear();
    classes.addAll(dependencies);
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) IOException(java.io.IOException) File(java.io.File) DescendingVisitor(org.apache.bcel.classfile.DescendingVisitor) HashSet(java.util.HashSet) ClassParser(org.apache.bcel.classfile.ClassParser)

Example 8 with ClassParser

use of org.apache.bcel.classfile.ClassParser in project candle-decompiler by bradsdavis.

the class CandleDecompiler method decompile.

public void decompile(File clz, File src) throws DecompilerException {
    Validate.notNull(clz, "Input file is required, but not provided.");
    Validate.notNull(src, "Output file is required, but not provided.");
    LOG.info("Processing: " + clz.getAbsolutePath());
    Writer writer = null;
    try {
        writer = new FileWriter(src);
        ClassParser cp = new ClassParser(clz.getAbsolutePath());
        decompile(cp.parse(), writer);
    } catch (Exception e) {
        throw new DecompilerException("Exception while decompiling.", e);
    } finally {
        if (writer != null) {
            try {
                writer.flush();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : FileWriter(java.io.FileWriter) IOException(java.io.IOException) FileWriter(java.io.FileWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) ClassParser(org.apache.bcel.classfile.ClassParser)

Example 9 with ClassParser

use of org.apache.bcel.classfile.ClassParser in project OpenGrok by OpenGrok.

the class JavaClassAnalyzer method analyze.

void analyze(Document doc, InputStream in, Writer xrefOut, JFieldBuilder jfbuilder) throws IOException {
    List<String> defs = new ArrayList<>();
    List<String> refs = new ArrayList<>();
    List<String> full = new ArrayList<>();
    StringWriter dout, rout, fout;
    if (jfbuilder == null) {
        dout = new StringWriter();
        rout = new StringWriter();
        fout = new StringWriter();
    } else {
        dout = jfbuilder.write(QueryBuilder.DEFS);
        rout = jfbuilder.write(QueryBuilder.REFS);
        fout = jfbuilder.write(QueryBuilder.FULL);
    }
    ClassParser classparser = new ClassParser(in, doc.get(QueryBuilder.PATH));
    StringWriter xout = new StringWriter();
    getContent(xout, fout, classparser.parse(), defs, refs, full);
    String xref = xout.toString();
    if (xrefOut != null) {
        xrefOut.append(xref);
        try {
            xrefOut.flush();
        } catch (IOException ex) {
            LOGGER.log(Level.WARNING, "Couldn''t flush. Will retry once added to doc", ex);
        }
    }
    appendValues(dout, defs);
    appendValues(rout, refs);
    appendValues(fout, full);
    if (jfbuilder == null) {
        String dstr = dout.toString();
        doc.add(new OGKTextField(QueryBuilder.DEFS, dstr, Store.NO));
        String rstr = rout.toString();
        doc.add(new OGKTextField(QueryBuilder.REFS, rstr, Store.NO));
        String fstr = fout.toString();
        doc.add(new OGKTextField(QueryBuilder.FULL, fstr, Store.NO));
    }
}
Also used : OGKTextField(org.opengrok.indexer.analysis.OGKTextField) StringWriter(java.io.StringWriter) ArrayList(java.util.ArrayList) ConstantString(org.apache.bcel.classfile.ConstantString) IOException(java.io.IOException) ClassParser(org.apache.bcel.classfile.ClassParser)

Example 10 with ClassParser

use of org.apache.bcel.classfile.ClassParser in project tycho by eclipse.

the class ExecutionEnvironmentTest method testCompilerSourceTargetConfigurationViaManifest.

@Test
public void testCompilerSourceTargetConfigurationViaManifest() throws Exception {
    Verifier verifier = getVerifier("TYCHO476", false);
    verifier.executeGoal("compile");
    // compile only succeeds with source level 1.6 which
    // is configured indirectly via Bundle-RequiredExecutionEnvironment: JavaSE-1.6
    verifier.verifyErrorFreeLog();
    File classFile = new File(verifier.getBasedir(), "target/classes/TestRunnable.class");
    Assert.assertTrue(classFile.canRead());
    JavaClass javaClass = new ClassParser(classFile.getAbsolutePath()).parse();
    // bytecode major level 50 == target 1.6
    Assert.assertEquals(50, javaClass.getMajor());
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) Verifier(org.apache.maven.it.Verifier) File(java.io.File) ClassParser(org.apache.bcel.classfile.ClassParser) Test(org.junit.Test) AbstractTychoIntegrationTest(org.eclipse.tycho.test.AbstractTychoIntegrationTest)

Aggregations

ClassParser (org.apache.bcel.classfile.ClassParser)13 JavaClass (org.apache.bcel.classfile.JavaClass)10 IOException (java.io.IOException)7 File (java.io.File)4 HashSet (java.util.HashSet)4 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 StringWriter (java.io.StringWriter)2 Set (java.util.Set)2 ZipEntry (java.util.zip.ZipEntry)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 TypeAdapter (com.google.gson.TypeAdapter)1 Expose (com.google.gson.annotations.Expose)1 JsonReader (com.google.gson.stream.JsonReader)1 JsonWriter (com.google.gson.stream.JsonWriter)1 JavaClassFormatError (com.jopdesign.common.misc.JavaClassFormatError)1 Parameter (com.scythe.instrumenter.InstrumentationProperties.Parameter)1 BugInstance (edu.umd.cs.findbugs.BugInstance)1 PropertyChangeEvent (java.beans.PropertyChangeEvent)1