-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): 支持Fragment override getContext方法
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
Showing
4 changed files
with
79 additions
and
2 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
projects/sdk/core/transform-kit/src/main/java/javassist/EnhancedCodeConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...e/transform-kit/src/main/java/javassist/convert/TransformCallExceptSuperCallToStatic.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters