forked from Tencent/Shadow
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core.transform-kit): 去掉javassist replaceNew中的DUP检查
javac总是生成new+dup,但这并不是字节码规范。proguard等工具可能会优化掉dup。 fix Tencent#1155
- Loading branch information
Showing
3 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
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 |
---|---|---|
@@ -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()); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
projects/sdk/core/transform-kit/src/main/java/javassist/convert/TransformNewClassFix.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,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; | ||
} | ||
} |
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