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;
}
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;
}
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));
}
}
Aggregations