Search in sources :

Example 11 with ClassParser

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

Example 12 with ClassParser

use of org.apache.bcel.classfile.ClassParser in project android-classyshark by google.

the class RootBuilder method fillFromJar.

private ClassNode fillFromJar(File file) {
    ClassNode rootNode = new ClassNode(file.getName());
    try {
        JarFile theJar = new JarFile(file);
        Enumeration<? extends JarEntry> en = theJar.entries();
        while (en.hasMoreElements()) {
            JarEntry entry = en.nextElement();
            if (entry.getName().endsWith(".class")) {
                ClassParser cp = new ClassParser(theJar.getInputStream(entry), entry.getName());
                JavaClass jc = cp.parse();
                ClassInfo classInfo = new ClassInfo(jc.getClassName(), jc.getMethods().length);
                rootNode.add(classInfo);
            }
        }
    } catch (IOException e) {
        System.err.println("Error reading file: " + file + ". " + e.getMessage());
        e.printStackTrace(System.err);
    }
    return rootNode;
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) ClassParser(org.apache.bcel.classfile.ClassParser)

Example 13 with ClassParser

use of org.apache.bcel.classfile.ClassParser in project fb-contrib by mebigfatguy.

the class SuspiciousJDKVersionUse method isValid.

private boolean isValid(Map<String, Set<String>> validMethods, String clsName) throws IOException, ClassNotFoundException {
    Set<String> methodInfos = validMethods.get(clsName);
    if (methodInfos == null) {
        ZipEntry ze = jdkZip.getEntry(clsName + ".class");
        if (ze == null) {
            if (isJavaXExternal(clsName)) {
                return true;
            }
            bugReporter.reportBug(new BugInstance(this, BugType.SJVU_SUSPICIOUS_JDK_VERSION_USE.name(), HIGH_PRIORITY).addClass(this).addMethod(this).addSourceLine(this).addClass(clsName));
        } else if (clsName.startsWith("java/")) {
            JavaClass calledClass = null;
            try (InputStream is = new BufferedInputStream(jdkZip.getInputStream(ze))) {
                ClassParser parser = new ClassParser(is, clsName);
                calledClass = parser.parse();
            }
            superNames.put(clsName, calledClass.getSuperclassName().replace('.', '/'));
            Method[] methods = calledClass.getMethods();
            methodInfos = new HashSet<>(methods.length);
            validMethods.put(clsName, methodInfos);
            for (Method m : methods) {
                methodInfos.add(m.getName() + m.getSignature());
            }
        }
    }
    if (methodInfos == null) {
        return true;
    }
    String wantedMethod = getNameConstantOperand() + getSigConstantOperand();
    if (methodInfos.contains(wantedMethod)) {
        return true;
    } else if (Values.SLASHED_JAVA_LANG_OBJECT.equals(clsName)) {
        return false;
    } else {
        return isValid(validMethods, superNames.get(clsName));
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) BugInstance(edu.umd.cs.findbugs.BugInstance) Method(org.apache.bcel.classfile.Method) ClassParser(org.apache.bcel.classfile.ClassParser) HashSet(java.util.HashSet)

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