Skip to content

Commit

Permalink
fix(core): 支持Fragment override getContext方法
Browse files Browse the repository at this point in the history
redirectMethodCallToStatic方法复用了TransformCall的匹配逻辑,
即`if (c == INVOKEINTERFACE || c == INVOKESPECIAL || c == INVOKESTATIC || c == INVOKEVIRTUAL)`
其中INVOKESPECIAL即包含super调用。而我们在替换fragmentGetContext方法时,并不需要对super调用进行转换。
如果转换会导致fragmentGetContext静态方法中对fragment的getContext调用循环回自身。

因此对这种情况忽略INVOKESPECIAL调用,添加新方法redirectMethodCallExceptSuperCallToStatic。

fix #647
  • Loading branch information
shifujun committed Oct 22, 2021
1 parent 80bc328 commit ea59e39
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package javassist;

import javassist.convert.TransformCallExceptSuperCallToStatic;

public class EnhancedCodeConverter extends CodeConverter {

public void redirectMethodCallExceptSuperCallToStatic(CtMethod origMethod, CtMethod substMethod) throws CannotCompileException {
transformers = new TransformCallExceptSuperCallToStatic(transformers, origMethod,
substMethod);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package javassist.convert;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.NotFoundException;
import javassist.bytecode.BadBytecode;
import javassist.bytecode.CodeIterator;
import javassist.bytecode.ConstPool;

public class TransformCallExceptSuperCallToStatic extends TransformCallToStatic {
public TransformCallExceptSuperCallToStatic(Transformer next, CtMethod origMethod, CtMethod substMethod) {
super(next, origMethod, substMethod);
}

// COPY FROM TransformCall
@Override
public int transform(CtClass clazz, int pos, CodeIterator iterator, ConstPool cp) throws BadBytecode {
int c = iterator.byteAt(pos);
if (c == INVOKEINTERFACE || c == INVOKESTATIC || c == INVOKEVIRTUAL) { // THE ONLY DIFFERENCE WITH TransformCall
int index = iterator.u16bitAt(pos + 1);
String cname = cp.eqMember(methodname, methodDescriptor, index);
if (cname != null && matchClass(cname, clazz.getClassPool())) {
int ntinfo = cp.getMemberNameAndType(index);
pos = match(c, pos, iterator,
cp.getNameAndTypeDescriptor(ntinfo), cp);
}
}

return pos;
}

// COPY FROM TransformCall
private boolean matchClass(String name, ClassPool pool) {
if (classname.equals(name))
return true;

try {
CtClass clazz = pool.get(name);
CtClass declClazz = pool.get(classname);
if (clazz.subtypeOf(declClazz))
try {
CtMethod m = clazz.getMethod(methodname, methodDescriptor);
return m.getDeclaringClass().getName().equals(classname);
} catch (NotFoundException e) {
// maybe the original method has been removed.
return true;
}
} catch (NotFoundException e) {
return false;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,15 @@ class FragmentSupportTransform : SpecificTransform() {

override fun transform(ctClass: CtClass) {
ctClass.defrost()
val codeConverter = CodeConverter()
val codeConverter = EnhancedCodeConverter()
codeConverter.redirectMethodCallToStatic(
getActivityMethod,
fragmentGetActivityMethod
)
codeConverter.redirectMethodCallToStatic(getContextMethod, fragmentGetContextMethod)
codeConverter.redirectMethodCallExceptSuperCallToStatic(
getContextMethod,
fragmentGetContextMethod
)
codeConverter.redirectMethodCallToStatic(getHostMethod, fragmentGetHostMethod)
codeConverter.redirectMethodCallToStatic(
startActivityMethod1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,12 @@ public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanc
super.onInflate(activity, attrs, savedInstanceState);
commonLogic.onInflate(activity, attrs, savedInstanceState);
}

/**
* 测试Fragment override getContext的场景
*/
@Override
public Context getContext() {
return super.getContext();
}
}

0 comments on commit ea59e39

Please sign in to comment.