Skip to content

Commit

Permalink
fix(core.transform-kit): 去掉javassist replaceNew中的DUP检查
Browse files Browse the repository at this point in the history
javac总是生成new+dup,但这并不是字节码规范。proguard等工具可能会优化掉dup。

fix Tencent#1155
  • Loading branch information
shifujun committed Apr 18, 2023
1 parent 9c80fa7 commit 8a10f67
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package javassist;

import javassist.convert.TransformCallExceptSuperCallToStatic;
import javassist.convert.TransformNewClassFix;

public class EnhancedCodeConverter extends CodeConverter {

public void redirectMethodCallExceptSuperCallToStatic(CtMethod origMethod, CtMethod substMethod) throws CannotCompileException {
transformers = new TransformCallExceptSuperCallToStatic(transformers, origMethod,
substMethod);
}

public void replaceNew(CtClass oldClass, CtClass newClass) {
transformers = new TransformNewClassFix(transformers, oldClass.getName(),
newClass.getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package javassist.convert;

import javassist.CannotCompileException;
import javassist.CtClass;
import javassist.bytecode.CodeAttribute;
import javassist.bytecode.CodeIterator;
import javassist.bytecode.ConstPool;

/**
* 完全复制自TransformNewClass代码
* 去掉“"NEW followed by no DUP was found"检查
*/
final public class TransformNewClassFix extends Transformer {
private int nested;
private String classname, newClassName;
private int newClassIndex, newMethodNTIndex, newMethodIndex;

public TransformNewClassFix(Transformer next,
String classname, String newClassName) {
super(next);
this.classname = classname;
this.newClassName = newClassName;
}

@Override
public void initialize(ConstPool cp, CodeAttribute attr) {
nested = 0;
newClassIndex = newMethodNTIndex = newMethodIndex = 0;
}

/**
* Modifies a sequence of
* NEW classname
* DUP
* ...
* INVOKESPECIAL classname:method
*/
@Override
public int transform(CtClass clazz, int pos, CodeIterator iterator,
ConstPool cp) throws CannotCompileException {
int index;
int c = iterator.byteAt(pos);
if (c == NEW) {
index = iterator.u16bitAt(pos + 1);
if (cp.getClassInfo(index).equals(classname)) {

if (newClassIndex == 0)
newClassIndex = cp.addClassInfo(newClassName);

iterator.write16bit(newClassIndex, pos + 1);
++nested;
}
} else if (c == INVOKESPECIAL) {
index = iterator.u16bitAt(pos + 1);
int typedesc = cp.isConstructor(classname, index);
if (typedesc != 0 && nested > 0) {
int nt = cp.getMethodrefNameAndType(index);
if (newMethodNTIndex != nt) {
newMethodNTIndex = nt;
newMethodIndex = cp.addMethodrefInfo(newClassIndex, nt);
}

iterator.write16bit(newMethodIndex, pos + 1);
--nested;
}
}

return pos;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ package com.tencent.shadow.core.transform.specific

import com.tencent.shadow.core.transform_kit.SpecificTransform
import com.tencent.shadow.core.transform_kit.TransformStep
import javassist.CodeConverter
import javassist.CtClass
import javassist.EnhancedCodeConverter

class WebViewTransform : SpecificTransform() {
companion object {
const val AndroidWebViewClassname = "android.webkit.WebView"
const val ShadowWebViewClassname = "com.tencent.shadow.core.runtime.ShadowWebView"
}

val codeConverter = CodeConverter()
val codeConverter = EnhancedCodeConverter()
override fun setup(allInputClass: Set<CtClass>) {
codeConverter.replaceNew(
mClassPool[AndroidWebViewClassname],
Expand Down

0 comments on commit 8a10f67

Please sign in to comment.