Search in sources :

Example 81 with JavaClass

use of org.apache.bcel.classfile.JavaClass in project jop by jop-devel.

the class ClassWriter method write.

public void write(String writeDir) throws IOException {
    AppInfo appInfo = AppInfo.getSingleton();
    if (logger.isInfoEnabled()) {
        logger.info("Start writing classes to '" + writeDir + "' ..");
    }
    File classDir = new File(writeDir);
    if (classDir.exists()) {
        if (classDir.isFile()) {
            throw new IOException("Output directory '" + classDir + "' is a file.");
        }
    } else if (!classDir.mkdirs()) {
        throw new IOException("Could not create output directory " + classDir);
    }
    for (ClassInfo cls : appInfo.getClassInfos()) {
        if (logger.isDebugEnabled()) {
            logger.debug("Writing class: " + cls.getClassName());
        }
        JavaClass jc = cls.compile();
        String filename = classDir + File.separator + cls.getClassName().replace(".", File.separator) + ".class";
        File file = new File(filename);
        String parent = file.getParent();
        if (parent != null) {
            File pDir = new File(parent);
            // noinspection ResultOfMethodCallIgnored
            pDir.mkdirs();
        }
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
        DataOutputStream stream = new DataOutputStream(out);
        jc.dump(stream);
        stream.close();
    }
    if (logger.isInfoEnabled()) {
        logger.info(appInfo.getClassInfos().size() + " classes written.");
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) DataOutputStream(java.io.DataOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) AppInfo(com.jopdesign.common.AppInfo) ClassInfo(com.jopdesign.common.ClassInfo)

Example 82 with JavaClass

use of org.apache.bcel.classfile.JavaClass 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)

Example 83 with JavaClass

use of org.apache.bcel.classfile.JavaClass in project contribution by checkstyle.

the class ClassFileSetCheck method visitSet.

/**
 * @see com.puppycrawl.tools.checkstyle.bcel.IObjectSetVisitor
 */
public void visitSet(Set aSet) {
    // register the JavaClasses in the Repository
    Repository.clearCache();
    Iterator it = aSet.iterator();
    while (it.hasNext()) {
        final JavaClass javaClass = (JavaClass) it.next();
        Repository.addClass(javaClass);
    }
    // visit the visitors
    it = getObjectSetVisitors().iterator();
    while (it.hasNext()) {
        final IObjectSetVisitor visitor = (IObjectSetVisitor) it.next();
        visitor.visitSet(aSet);
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) Iterator(java.util.Iterator)

Example 84 with JavaClass

use of org.apache.bcel.classfile.JavaClass in project contribution by checkstyle.

the class ClassFileSetCheck method process.

/**
 * @see com.puppycrawl.tools.checkstyle.api.FileSetCheck
 */
public void process(File[] aFiles) {
    registerVisitors();
    // get all the JavaClasses in the files
    final Set javaClasses = extractJavaClasses(aFiles);
    visitSet(javaClasses);
    // walk each Java class parse tree
    final JavaClassWalker walker = new JavaClassWalker();
    walker.setVisitor(getTreeVisitor());
    final Iterator it = javaClasses.iterator();
    while (it.hasNext()) {
        final JavaClass clazz = (JavaClass) it.next();
        visitObject(clazz);
        walker.walk(clazz);
        leaveObject(clazz);
    }
    leaveSet(javaClasses);
    fireErrors();
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) JavaClass(org.apache.bcel.classfile.JavaClass) Iterator(java.util.Iterator)

Example 85 with JavaClass

use of org.apache.bcel.classfile.JavaClass in project contribution by checkstyle.

the class ClassFileSetCheck method extractJavaClasses.

/**
 * Extracts the JavaClasses from .class, .zip, and .jar files.
 * @param aFile the file to extract from.
 * @return the set of JavaClasses from aFile.
 * @throws IOException if there is an error.
 */
private Set extractJavaClasses(File aFile) throws IOException {
    final Set result = new HashSet();
    final String fileName = aFile.getPath();
    if (fileName.endsWith(".jar") || fileName.endsWith(".zip")) {
        final ZipFile zipFile = new ZipFile(fileName);
        final Enumeration entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            final ZipEntry entry = (ZipEntry) entries.nextElement();
            final String entryName = entry.getName();
            if (entryName.endsWith(".class")) {
                final InputStream in = zipFile.getInputStream(entry);
                final JavaClass javaClass = new ClassParser(in, entryName).parse();
                result.add(javaClass);
            }
        }
    } else {
        final JavaClass javaClass = new ClassParser(fileName).parse();
        result.add(javaClass);
    }
    return result;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Enumeration(java.util.Enumeration) ZipFile(java.util.zip.ZipFile) JavaClass(org.apache.bcel.classfile.JavaClass) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) HashSet(java.util.HashSet) ClassParser(org.apache.bcel.classfile.ClassParser)

Aggregations

JavaClass (org.apache.bcel.classfile.JavaClass)144 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)45 BugInstance (edu.umd.cs.findbugs.BugInstance)43 Method (org.apache.bcel.classfile.Method)28 ToString (com.mebigfatguy.fbcontrib.utils.ToString)27 Field (org.apache.bcel.classfile.Field)17 HashSet (java.util.HashSet)14 HashMap (java.util.HashMap)11 ClassParser (org.apache.bcel.classfile.ClassParser)10 ArrayList (java.util.ArrayList)9 IOException (java.io.IOException)8 ExceptionTable (org.apache.bcel.classfile.ExceptionTable)8 XField (edu.umd.cs.findbugs.ba.XField)7 Nullable (javax.annotation.Nullable)7 AnnotationEntry (org.apache.bcel.classfile.AnnotationEntry)7 Type (org.apache.bcel.generic.Type)7 Iterator (java.util.Iterator)6 List (java.util.List)6 Map (java.util.Map)6 Set (java.util.Set)6