Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

通过测试集成play-services-ads修复两处transform的bug #1182

Merged
merged 2 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,7 +20,11 @@ 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.*
import javassist.CodeConverter
import javassist.CtClass
import javassist.CtMethod
import javassist.Modifier
import javassist.NotFoundException
import javassist.compiler.Javac.CtFieldWithInit

/**
Expand Down Expand Up @@ -70,7 +74,7 @@ class ReceiverSupportTransform : SpecificTransform() {
} catch (e: NotFoundException) {
null
}
if (onReceiveMethod != null) {
if (onReceiveMethod != null && !Modifier.isVolatile(onReceiveMethod.modifiers)) {
targetReceivers.add(ctClass)
}
}
Expand Down
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
27 changes: 27 additions & 0 deletions projects/sdk/core/transform/src/test/java/test/EggReceiver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import java.util.List;

abstract class EggReceiver extends BroadcastReceiver {
List<String> log;

EggReceiver(List<String> log) {
this.log = log;
}

@Override
public void onReceive(Context context, Intent intent) {
log.add("EggReceiver onReceive");
}

public static class FoxReceiver extends EggReceiver {
FoxReceiver(List<String> log) {
super(log);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class ReceiverSupportTransformTest : AbstractTransformTest() {
"test.BarReceiver",
"test.CatReceiver",
"test.DogReceiver",
"test.EggReceiver",
"test.EggReceiver\$FoxReceiver",
)
].toMutableSet()

Expand Down