From d5ad823abecf169475db8cf95f40a412213bf671 Mon Sep 17 00:00:00 2001 From: Marc <55074569+marcello-dev@users.noreply.github.com> Date: Sun, 25 Feb 2024 15:53:18 +0800 Subject: [PATCH 1/5] fix path issue on windows --- assembly-dyn.xml | 2 +- assembly-st.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 - / + From 08c667f6fa5d78ea97c6d6bdfeb287a504d05bd1 Mon Sep 17 00:00:00 2001 From: Marc <55074569+marcello-dev@users.noreply.github.com> Date: Sun, 25 Feb 2024 15:54:18 +0800 Subject: [PATCH 2/5] ignore vs code files --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) 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 From 1746ddc923509a05ac1d8267a22cba0d03291204 Mon Sep 17 00:00:00 2001 From: Marc <55074569+marcello-dev@users.noreply.github.com> Date: Sun, 25 Feb 2024 15:56:14 +0800 Subject: [PATCH 3/5] fix ArrayOutOfBoundsException https://github.com/marcello-dev/java-call-graph-plotter/issues/5 --- .../javacg/stat/DynamicCallManager.java | 28 +++++++-- .../gr/gousiosg/javacg/stat/JCallGraph.java | 59 ++++++------------- 2 files changed, 41 insertions(+), 46 deletions(-) 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); - } } From 1a45a726a3c747746d95e6c16d09cb5df14b85b0 Mon Sep 17 00:00:00 2001 From: Marc <55074569+marcello-dev@users.noreply.github.com> Date: Sun, 25 Feb 2024 15:56:20 +0800 Subject: [PATCH 4/5] increase version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index a606ecad..7fbcc75c 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ gr.gousiosg javacg - 0.1-SNAPSHOT + 0.2-SNAPSHOT jar javacg From 4bd31137dc880b04cbb6e3565f13f2971f0b071b Mon Sep 17 00:00:00 2001 From: Marc <55074569+marcello-dev@users.noreply.github.com> Date: Sun, 25 Feb 2024 16:01:55 +0800 Subject: [PATCH 5/5] revert version increase follow main repo for now --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 7fbcc75c..a606ecad 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ gr.gousiosg javacg - 0.2-SNAPSHOT + 0.1-SNAPSHOT jar javacg