From 9c6cde4f598f8a59ff1d54d197284be0efc1fa69 Mon Sep 17 00:00:00 2001
From: Nick Botticelli <nick.s.botticelli@gmail.com>
Date: Sat, 4 Sep 2021 20:09:33 -0700
Subject: [PATCH 1/2] Fix inheritance issue in ClassCastException causing
 InvokePolymorphicExpr

NewTransformer#findInvokeExpr attempts to cast InvokePolymorphicExpr to InvokeExpr under certain inputs, which causes a ClassCastException. It appears that InvokePolymorphicExpr was meant to extend InvokeExpr for this reason.
---
 .../googlecode/dex2jar/ir/expr/InvokePolymorphicExpr.java  | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/dex-ir/src/main/java/com/googlecode/dex2jar/ir/expr/InvokePolymorphicExpr.java b/dex-ir/src/main/java/com/googlecode/dex2jar/ir/expr/InvokePolymorphicExpr.java
index fa9d06f45..4e0b13dcb 100644
--- a/dex-ir/src/main/java/com/googlecode/dex2jar/ir/expr/InvokePolymorphicExpr.java
+++ b/dex-ir/src/main/java/com/googlecode/dex2jar/ir/expr/InvokePolymorphicExpr.java
@@ -5,12 +5,10 @@
 import com.googlecode.dex2jar.ir.LabelAndLocalMapper;
 import com.googlecode.dex2jar.ir.Util;
 
-public class InvokePolymorphicExpr extends AbstractInvokeExpr {
+public class InvokePolymorphicExpr extends InvokeExpr {
 
     public Proto proto;
 
-    public Method method;
-
     @Override
     protected void releaseMemory() {
         method = null;
@@ -24,9 +22,8 @@ public Proto getProto() {
     }
 
     public InvokePolymorphicExpr(VT type, Value[] args, Proto proto, Method method) {
-        super(type, args);
+        super(type, args, method);
         this.proto = proto;
-        this.method = method;
     }
 
     @Override

From 9c0db1d636d7291bcf9e0265c132c6a4bccd0706 Mon Sep 17 00:00:00 2001
From: Nico Mexis <nico.mexis@kabelmail.de>
Date: Sun, 5 Sep 2021 10:44:32 +0200
Subject: [PATCH 2/2] Fix InvokeExpr subclasses even more

Maybe this fix is superfluous, but well... Why not? :)
---
 .../java/com/googlecode/dex2jar/ir/Util.java  |  3 ++
 .../dex2jar/ir/expr/InvokeCustomExpr.java     |  8 ++---
 .../dex2jar/ir/expr/InvokeExpr.java           | 29 +++++++++----------
 .../ir/expr/InvokePolymorphicExpr.java        |  5 ++--
 4 files changed, 23 insertions(+), 22 deletions(-)

diff --git a/dex-ir/src/main/java/com/googlecode/dex2jar/ir/Util.java b/dex-ir/src/main/java/com/googlecode/dex2jar/ir/Util.java
index 261d583b0..e4fada27f 100644
--- a/dex-ir/src/main/java/com/googlecode/dex2jar/ir/Util.java
+++ b/dex-ir/src/main/java/com/googlecode/dex2jar/ir/Util.java
@@ -99,6 +99,9 @@ public static void appendString(final StringBuffer buf, final String s) {
     }
 
     public static String toShortClassName(String desc) {
+        if (desc == null || desc.isEmpty()) {
+            return "";
+        }
         switch (desc.charAt(0)) {
         case 'Z':
             return "boolean";
diff --git a/dex-ir/src/main/java/com/googlecode/dex2jar/ir/expr/InvokeCustomExpr.java b/dex-ir/src/main/java/com/googlecode/dex2jar/ir/expr/InvokeCustomExpr.java
index bb42e40b2..c171d5777 100644
--- a/dex-ir/src/main/java/com/googlecode/dex2jar/ir/expr/InvokeCustomExpr.java
+++ b/dex-ir/src/main/java/com/googlecode/dex2jar/ir/expr/InvokeCustomExpr.java
@@ -4,7 +4,7 @@
 import com.googlecode.d2j.Proto;
 import com.googlecode.dex2jar.ir.LabelAndLocalMapper;
 
-public class InvokeCustomExpr extends AbstractInvokeExpr {
+public class InvokeCustomExpr extends InvokeExpr {
 
     public String name;
 
@@ -30,7 +30,7 @@ public Proto getProto() {
 
     public InvokeCustomExpr(VT type, Value[] args, String methodName, Proto proto, MethodHandle handle,
                             Object[] bsmArgs) {
-        super(type, args);
+        super(type, args, handle == null ? null : handle.getMethod());
         this.proto = proto;
         this.name = methodName;
         this.handle = handle;
@@ -38,12 +38,12 @@ public InvokeCustomExpr(VT type, Value[] args, String methodName, Proto proto, M
     }
 
     @Override
-    public Value clone() {
+    public InvokeCustomExpr clone() {
         return new InvokeCustomExpr(vt, cloneOps(), name, proto, handle, bsmArgs);
     }
 
     @Override
-    public Value clone(LabelAndLocalMapper mapper) {
+    public InvokeCustomExpr clone(LabelAndLocalMapper mapper) {
         return new InvokeCustomExpr(vt, cloneOps(mapper), name, proto, handle, bsmArgs);
     }
 
diff --git a/dex-ir/src/main/java/com/googlecode/dex2jar/ir/expr/InvokeExpr.java b/dex-ir/src/main/java/com/googlecode/dex2jar/ir/expr/InvokeExpr.java
index 4b21c697b..ff755f884 100644
--- a/dex-ir/src/main/java/com/googlecode/dex2jar/ir/expr/InvokeExpr.java
+++ b/dex-ir/src/main/java/com/googlecode/dex2jar/ir/expr/InvokeExpr.java
@@ -30,13 +30,12 @@ protected void releaseMemory() {
 
     @Override
     public Proto getProto() {
-        return method.getProto();
+        return method == null ? null : method.getProto();
     }
 
     public InvokeExpr(VT type, Value[] args, String ownerType, String methodName, String[] argmentTypes,
                       String returnType) {
-        super(type, args);
-        this.method = new Method(ownerType, methodName, argmentTypes, returnType);
+        this(type, args, new Method(ownerType, methodName, argmentTypes, returnType));
     }
 
     public InvokeExpr(VT type, Value[] args, Method method) {
@@ -45,12 +44,12 @@ public InvokeExpr(VT type, Value[] args, Method method) {
     }
 
     @Override
-    public Value clone() {
+    public InvokeExpr clone() {
         return new InvokeExpr(vt, cloneOps(), method);
     }
 
     @Override
-    public Value clone(LabelAndLocalMapper mapper) {
+    public InvokeExpr clone(LabelAndLocalMapper mapper) {
         return new InvokeExpr(vt, cloneOps(mapper), method);
     }
 
@@ -59,13 +58,13 @@ public String toString0() {
         StringBuilder sb = new StringBuilder();
 
         int i = 0;
-        if (super.vt == VT.INVOKE_NEW) {
-            sb.append("new ").append(Util.toShortClassName(method.getOwner()));
-        } else if (super.vt == VT.INVOKE_STATIC) {
-            sb.append(Util.toShortClassName(method.getOwner())).append('.')
-                    .append(this.method.getName());
+        if (vt == VT.INVOKE_NEW) {
+            sb.append("new ").append(Util.toShortClassName(getOwner()));
+        } else if (vt == VT.INVOKE_STATIC) {
+            sb.append(Util.toShortClassName(getOwner())).append('.')
+                    .append(getName());
         } else {
-            sb.append(ops[i++]).append('.').append(this.method.getName());
+            sb.append(ops[i++]).append('.').append(getName());
         }
         sb.append('(');
         boolean first = true;
@@ -82,19 +81,19 @@ public String toString0() {
     }
 
     public String getOwner() {
-        return method.getOwner();
+        return method == null ? null : method.getOwner();
     }
 
     public String getRet() {
-        return method.getReturnType();
+        return method == null ? null : method.getReturnType();
     }
 
     public String getName() {
-        return method.getName();
+        return method == null ? null : method.getName();
     }
 
     public String[] getArgs() {
-        return method.getParameterTypes();
+        return method == null ? null : method.getParameterTypes();
     }
 
 }
diff --git a/dex-ir/src/main/java/com/googlecode/dex2jar/ir/expr/InvokePolymorphicExpr.java b/dex-ir/src/main/java/com/googlecode/dex2jar/ir/expr/InvokePolymorphicExpr.java
index 4e0b13dcb..00e13e7ed 100644
--- a/dex-ir/src/main/java/com/googlecode/dex2jar/ir/expr/InvokePolymorphicExpr.java
+++ b/dex-ir/src/main/java/com/googlecode/dex2jar/ir/expr/InvokePolymorphicExpr.java
@@ -11,7 +11,6 @@ public class InvokePolymorphicExpr extends InvokeExpr {
 
     @Override
     protected void releaseMemory() {
-        method = null;
         proto = null;
         super.releaseMemory();
     }
@@ -27,12 +26,12 @@ public InvokePolymorphicExpr(VT type, Value[] args, Proto proto, Method method)
     }
 
     @Override
-    public Value clone() {
+    public InvokePolymorphicExpr clone() {
         return new InvokePolymorphicExpr(vt, cloneOps(), proto, method);
     }
 
     @Override
-    public Value clone(LabelAndLocalMapper mapper) {
+    public InvokePolymorphicExpr clone(LabelAndLocalMapper mapper) {
         return new InvokePolymorphicExpr(vt, cloneOps(mapper), proto, method);
     }