diff --git a/auto-patch-plugin/src/main/groovy/com/meituan/robust/autopatch/PatchesFactory.groovy b/auto-patch-plugin/src/main/groovy/com/meituan/robust/autopatch/PatchesFactory.groovy index d06b12d9..236b0d88 100644 --- a/auto-patch-plugin/src/main/groovy/com/meituan/robust/autopatch/PatchesFactory.groovy +++ b/auto-patch-plugin/src/main/groovy/com/meituan/robust/autopatch/PatchesFactory.groovy @@ -5,6 +5,7 @@ import com.meituan.robust.utils.JavaUtils import javassist.* import javassist.bytecode.AccessFlag import javassist.bytecode.ClassFile +import javassist.bytecode.LocalVariableAttribute import javassist.bytecode.MethodInfo import javassist.expr.* @@ -170,7 +171,18 @@ class PatchesFactory { int index = invokeSuperMethodList.indexOf(m.getMethod()); CtMethod superMethod = invokeSuperMethodList.get(index); if (superMethod.getLongName() != null && superMethod.getLongName() == m.getMethod().getLongName()) { - m.replace(ReflectUtils.invokeSuperString(m)); + String firstVariable = ""; + if (ReflectUtils.isStatic(method.getModifiers())) { + //修复static 方法中含有super的问题,比如Aspectj处理后的方法 + MethodInfo methodInfo = method.getMethodInfo(); + LocalVariableAttribute table = methodInfo.getCodeAttribute().getAttribute(LocalVariableAttribute.tag); + int numberOfLocalVariables = table.tableLength(); + if (numberOfLocalVariables > 0) { + int frameWithNameAtConstantPool = table.nameIndex(0); + firstVariable = methodInfo.getConstPool().getUtf8Info(frameWithNameAtConstantPool) + } + } + m.replace(ReflectUtils.invokeSuperString(m, firstVariable)); return; } } diff --git a/auto-patch-plugin/src/main/groovy/com/meituan/robust/autopatch/ReflectUtils.groovy b/auto-patch-plugin/src/main/groovy/com/meituan/robust/autopatch/ReflectUtils.groovy index 915d36b4..307eb0f3 100644 --- a/auto-patch-plugin/src/main/groovy/com/meituan/robust/autopatch/ReflectUtils.groovy +++ b/auto-patch-plugin/src/main/groovy/com/meituan/robust/autopatch/ReflectUtils.groovy @@ -12,7 +12,6 @@ import javassist.expr.NewExpr import org.apache.commons.io.FileUtils import robust.gradle.plugin.AutoPatchTransform -import java.lang.reflect.Field import java.util.jar.JarEntry import java.util.jar.JarFile import java.util.regex.Matcher @@ -453,6 +452,7 @@ class ReflectUtils { return (modifiers & AccessFlag.STATIC) != 0; } + @Deprecated def static String invokeSuperString(MethodCall m) { StringBuilder stringBuilder = new StringBuilder(); @@ -471,6 +471,32 @@ class ReflectUtils { return stringBuilder.toString(); } + def static String invokeSuperString(MethodCall m, String originClass) { + StringBuilder stringBuilder = new StringBuilder(); + + stringBuilder.append("{"); + if (!m.method.returnType.equals(CtClass.voidType)) { + stringBuilder.append("\$_=(\$r)"); + } + if (m.method.parameterTypes.length > 0) { + if (!originClass.isEmpty()) { + stringBuilder.append(getStaticSuperMethodName(m.methodName) + "(null," + originClass + ",\$\$);"); + } else { + stringBuilder.append(getStaticSuperMethodName(m.methodName) + "(this," + Constants.ORIGINCLASS + ",\$\$);"); + } + } else { + if (!originClass.isEmpty()) { + stringBuilder.append(getStaticSuperMethodName(m.methodName) + "(null," + originClass + ");"); + } else { + stringBuilder.append(getStaticSuperMethodName(m.methodName) + "(this," + Constants.ORIGINCLASS + ");"); + } + } + + stringBuilder.append("}"); +// println("invokeSuperString " + m.methodName + " " + stringBuilder.toString()) + return stringBuilder.toString(); + } + def static String getStaticSuperMethodName(String methodName) { return Constants.STATICFLAG + methodName; }