-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
61 lines (55 loc) · 2.02 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.io.*;
import java.net.*;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.lang.reflect.*;
class Main {
public static void main(String[] args) throws Exception {
File jar = new File(args[0]);
jar.deleteOnExit(); // delete the jar when this exits to save space
JarFile jarFile = new JarFile(jar);
URLClassLoader ucl = new URLClassLoader(new URL[]{jar.toURI().toURL()});
for (JarEntry entry: new JarFileIterator(jarFile)) {
if (entry.isDirectory() || !entry.getName().endsWith(".class"))
continue;
try {
Class<?> cls = ucl.loadClass(entry.getName().replace("/", ".").replace(".class", ""));
for (Method m: cls.getMethods()) {
if (m.getReturnType().isAssignableFrom(String.class)) {
try {
String result = (String) m.invoke(null);
System.out.println(result);
} catch (Throwable t) {
continue;
}
}
}
} catch (Throwable t) {
continue;
}
}
ucl.close();
}
static class JarFileIterator implements Iterable<JarEntry> {
JarFile jar;
public JarFileIterator(JarFile jar) {
this.jar = jar;
}
@Override
public Iterator<JarEntry> iterator() {
return new Iterator<JarEntry>() {
Enumeration<JarEntry> jarentries = jar.entries(); // might not be needed, safer to keep it
@Override
public boolean hasNext() {
return jarentries.hasMoreElements();
}
@Override
public JarEntry next() {
return jarentries.nextElement();
}
};
}
}
}