Skip to content

Commit

Permalink
修复Aspectj处理后,static方法中含有super,导致无法patch的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
wanglinglong committed Apr 11, 2018
1 parent 3d09197 commit c160ab5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*

Expand Down Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -453,6 +452,7 @@ class ReflectUtils {
return (modifiers & AccessFlag.STATIC) != 0;
}

@Deprecated
def static String invokeSuperString(MethodCall m) {
StringBuilder stringBuilder = new StringBuilder();

Expand All @@ -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;
}
Expand Down

0 comments on commit c160ab5

Please sign in to comment.