diff --git a/.gitignore b/.gitignore
index 4568c376..02382d38 100644
--- a/.gitignore
+++ b/.gitignore
@@ -15,3 +15,5 @@ bin/
.idea/
*.iml
+# VS Code
+.vscode
\ No newline at end of file
diff --git a/assembly-dyn.xml b/assembly-dyn.xml
index 5c342dcf..3b6ab8fa 100644
--- a/assembly-dyn.xml
+++ b/assembly-dyn.xml
@@ -24,7 +24,7 @@
gr/gousiosg/javacg/dyn/*.class
target/classes
- /
+
diff --git a/assembly-st.xml b/assembly-st.xml
index 80845349..f78e163a 100644
--- a/assembly-st.xml
+++ b/assembly-st.xml
@@ -24,7 +24,7 @@
gr/gousiosg/javacg/stat/*.class
target/classes
- /
+
diff --git a/src/main/java/gr/gousiosg/javacg/stat/DynamicCallManager.java b/src/main/java/gr/gousiosg/javacg/stat/DynamicCallManager.java
index 2c4e8813..f13963dc 100644
--- a/src/main/java/gr/gousiosg/javacg/stat/DynamicCallManager.java
+++ b/src/main/java/gr/gousiosg/javacg/stat/DynamicCallManager.java
@@ -21,10 +21,12 @@
import org.apache.bcel.classfile.Attribute;
import org.apache.bcel.classfile.BootstrapMethod;
import org.apache.bcel.classfile.BootstrapMethods;
+import org.apache.bcel.classfile.Constant;
import org.apache.bcel.classfile.ConstantCP;
import org.apache.bcel.classfile.ConstantMethodHandle;
import org.apache.bcel.classfile.ConstantNameAndType;
import org.apache.bcel.classfile.ConstantPool;
+import org.apache.bcel.classfile.ConstantString;
import org.apache.bcel.classfile.ConstantUtf8;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.classfile.Method;
@@ -81,7 +83,11 @@ public void retrieveCalls(Method method, JavaClass jc) {
while (matcher.find()) {
int bootIndex = Integer.parseInt(matcher.group(1));
BootstrapMethod bootMethod = boots[bootIndex];
- int calledIndex = bootMethod.getBootstrapArguments()[CALL_HANDLE_INDEX_ARGUMENT];
+ int[] bootstrapArguments = bootMethod.getBootstrapArguments();
+ if (bootstrapArguments.length <= 1) {
+ continue;
+ }
+ int calledIndex = bootstrapArguments[CALL_HANDLE_INDEX_ARGUMENT];
String calledName = getMethodNameFromHandleIndex(cp, calledIndex);
String callerName = method.getName();
dynamicCallers.put(calledName, callerName);
@@ -89,10 +95,20 @@ public void retrieveCalls(Method method, JavaClass jc) {
}
private String getMethodNameFromHandleIndex(ConstantPool cp, int callIndex) {
- ConstantMethodHandle handle = (ConstantMethodHandle) cp.getConstant(callIndex);
- ConstantCP ref = (ConstantCP) cp.getConstant(handle.getReferenceIndex());
- ConstantNameAndType nameAndType = (ConstantNameAndType) cp.getConstant(ref.getNameAndTypeIndex());
- return nameAndType.getName(cp);
+ Constant constant = cp.getConstant(callIndex);
+ String methodName;
+ if (constant instanceof ConstantMethodHandle) {
+ ConstantMethodHandle constantMethodHandle = (ConstantMethodHandle) constant;
+ ConstantCP ref = (ConstantCP) cp.getConstant(constantMethodHandle.getReferenceIndex());
+ ConstantNameAndType nameAndType = (ConstantNameAndType) cp.getConstant(ref.getNameAndTypeIndex());
+ methodName = nameAndType.getName(cp);
+ } else if (constant instanceof ConstantString) {
+ ConstantString constantString = (ConstantString) constant;
+ methodName = constantString.getBytes(cp);
+ } else {
+ methodName = "UnknownMethod";
+ }
+ return methodName;
}
/**
@@ -120,6 +136,6 @@ private BootstrapMethod[] getBootstrapMethods(JavaClass jc) {
return ((BootstrapMethods) attribute).getBootstrapMethods();
}
}
- return new BootstrapMethod[]{};
+ return new BootstrapMethod[] {};
}
}
diff --git a/src/main/java/gr/gousiosg/javacg/stat/JCallGraph.java b/src/main/java/gr/gousiosg/javacg/stat/JCallGraph.java
index cb40dc96..d6a3567a 100644
--- a/src/main/java/gr/gousiosg/javacg/stat/JCallGraph.java
+++ b/src/main/java/gr/gousiosg/javacg/stat/JCallGraph.java
@@ -48,16 +48,16 @@ public class JCallGraph {
public static void main(String[] args) {
- Function getClassVisitor =
- (ClassParser cp) -> {
- try {
- return new ClassVisitor(cp.parse());
- } catch (IOException e) {
- throw new UncheckedIOException(e);
- }
- };
+ Function getClassVisitor = (ClassParser cp) -> {
+ try {
+ return new ClassVisitor(cp.parse());
+ } catch (IOException e) {
+ throw new UncheckedIOException(e);
+ }
+ };
try {
+
for (String arg : args) {
File f = new File(arg);
@@ -67,24 +67,18 @@ public static void main(String[] args) {
}
try (JarFile jar = new JarFile(f)) {
- Stream entries = enumerationAsStream(jar.entries());
-
- String methodCalls = entries.
- flatMap(e -> {
- if (e.isDirectory() || !e.getName().endsWith(".class"))
- return (new ArrayList()).stream();
-
- ClassParser cp = new ClassParser(arg, e.getName());
- return getClassVisitor.apply(cp).start().methodCalls().stream();
- }).
- map(s -> s + "\n").
- reduce(new StringBuilder(),
- StringBuilder::append,
- StringBuilder::append).toString();
- BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
- log.write(methodCalls);
- log.close();
+ Enumeration jarEntries = jar.entries();
+ while (jarEntries.hasMoreElements()) {
+ JarEntry jarEntry = jarEntries.nextElement();
+ if (jarEntry.isDirectory() || !jarEntry.getName().endsWith(".class")) {
+ continue;
+ }
+ ClassParser cp = new ClassParser(arg, jarEntry.getName());
+ getClassVisitor.apply(cp).start()
+ .methodCalls()
+ .forEach(System.out::println);
+ }
}
}
} catch (IOException e) {
@@ -92,19 +86,4 @@ public static void main(String[] args) {
e.printStackTrace();
}
}
-
- public static Stream enumerationAsStream(Enumeration e) {
- return StreamSupport.stream(
- Spliterators.spliteratorUnknownSize(
- new Iterator() {
- public T next() {
- return e.nextElement();
- }
-
- public boolean hasNext() {
- return e.hasMoreElements();
- }
- },
- Spliterator.ORDERED), false);
- }
}