From c160ab5b04b8781e51783d7952441fd96d03ecf0 Mon Sep 17 00:00:00 2001 From: wanglinglong Date: Wed, 11 Apr 2018 16:53:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DAspectj=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=90=8E=EF=BC=8Cstatic=E6=96=B9=E6=B3=95=E4=B8=AD=E5=90=AB?= =?UTF-8?q?=E6=9C=89super=EF=BC=8C=E5=AF=BC=E8=87=B4=E6=97=A0=E6=B3=95patc?= =?UTF-8?q?h=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../robust/autopatch/PatchesFactory.groovy | 14 +++++++++- .../robust/autopatch/ReflectUtils.groovy | 28 ++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) 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; }