Skip to content

Instantly share code, notes, and snippets.

@unnonouno
Created July 28, 2014 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unnonouno/e0c06cd074f29503ec9d to your computer and use it in GitHub Desktop.
Save unnonouno/e0c06cd074f29503ec9d to your computer and use it in GitHub Desktop.
JarChecker
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class JarChecker {
public static void main(String[] args) throws Exception {
List<String> classes = new ArrayList<String>();
List<URL> urls = new ArrayList<URL>();
for (String path : args) {
File file = new File(path);
classes.addAll(getAllClasses(file));
urls.add(file.toURI().toURL());
}
ClassLoader loader = URLClassLoader.newInstance(urls
.toArray(new URL[] {}));
List<String> names = new ArrayList<String>();
for (String className : classes) {
names.addAll(getAllMethodNames(loader, className));
}
Collections.sort(names);
for (String name : names) {
System.out.println(name);
}
}
private static List<String> getAllMethodNames(ClassLoader loader,
String className) throws ClassNotFoundException {
Class<?> klass = loader.loadClass(className);
Method[] methods = klass.getMethods();
List<String> names = new ArrayList<String>();
for (Method method : methods) {
if ((method.getModifiers() & Modifier.PRIVATE) != 0)
continue;
names.add(method.toString());
}
return names;
}
private static List<String> getAllClasses(File file) throws IOException {
JarFile jarFile = new JarFile(file);
try {
List<String> classes = new ArrayList<String>();
for (Enumeration<JarEntry> e = jarFile.entries(); e
.hasMoreElements();) {
JarEntry entry = e.nextElement();
String name = entry.getName();
if (name.endsWith(".class")) {
classes.add(name.replace('/', '.').replace(".class", ""));
}
}
return classes;
} finally {
jarFile.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment