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

简化RePlugin对宿主PathClassLoader的处理,去掉对私有API的依赖。 #840

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.qihoo360.i.Factory;
import com.qihoo360.i.Factory2;
import com.qihoo360.i.IModule;
import com.qihoo360.loader.utils.PatchClassLoaderUtils;
import com.qihoo360.replugin.PMFClassLoader;
import com.qihoo360.replugin.helper.LogRelease;

import java.io.FileDescriptor;
Expand Down Expand Up @@ -74,7 +74,7 @@ public static final void init(Application application) {
Factory.sPluginManager = PMF.getLocal();
Factory2.sPLProxy = PMF.getInternal();

PatchClassLoaderUtils.patch(application);
PMFClassLoader.hackHostClassLoader();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.qihoo360.replugin;

import com.qihoo360.loader2.PMF;

import java.lang.reflect.Field;

/**
* 给宿主的ClassLoader新增一层parent ClassLoader.因为正常的ClassLoader都会遵循"双亲委派"机制,
* 所以宿主的PathClassLoader会尊重这个ClassLoader返回的结果.从而可以在这个ClassLoader中返回PMF想要返回的类.
* <p>
* 方案来自:
* https://github.com/Tencent/Shadow/blob/dev/projects/sdk/dynamic/dynamic-host/src/main/java/com/tencent/shadow/dynamic/host/DynamicRuntime.java
*
* @author [email protected]
*/
public class PMFClassLoader extends ClassLoader {

private PMFClassLoader(ClassLoader parent) {
super(parent);
}

@Override
protected Class<?> loadClass(String className, boolean resolve) throws ClassNotFoundException {
//
Class<?> c = null;
c = PMF.loadClass(className, resolve);
if (c != null) {
return c;
}
return super.loadClass(className, resolve);
}

@Override
protected Package getPackage(String name) {
final Package aPackage = super.getPackage(name);
if (aPackage == null) {
return definePackage(name, "Unknown", "0.0", "Unknown", "Unknown", "0.0", "Unknown", null);
} else {
return aPackage;
}
}

public static void hackHostClassLoader() {
ClassLoader hostClassLoader = PMF.class.getClassLoader();
final PMFClassLoader newParentClassLoader = new PMFClassLoader(hostClassLoader.getParent());
try {
PMFClassLoader.hackParentClassLoader(hostClassLoader, newParentClassLoader);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

/**
* 修改ClassLoader的parent
*
* @param classLoader 需要修改的ClassLoader
* @param newParentClassLoader classLoader的新的parent
*/
private static void hackParentClassLoader(ClassLoader classLoader,
ClassLoader newParentClassLoader) throws Exception {
Field field = getParentField();
if (field == null) {
throw new Exception("在ClassLoader.class中没找到类型为ClassLoader的parent域");
}
field.setAccessible(true);
field.set(classLoader, newParentClassLoader);
}

/**
* 安全地获取到ClassLoader类的parent域
*
* @return ClassLoader类的parent域.或不能通过反射访问该域时返回null.
*/
private static Field getParentField() {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
ClassLoader parent = classLoader.getParent();
Field field = null;
for (Field f : ClassLoader.class.getDeclaredFields()) {
try {
boolean accessible = f.isAccessible();
f.setAccessible(true);
Object o = f.get(classLoader);
f.setAccessible(accessible);
if (o == parent) {
field = f;
break;
}
} catch (IllegalAccessException ignore) {
}
}
return field;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,6 @@ public RePluginCallbacks(Context context) {
mContext = context;
}

/**
* 创建【宿主用的】 RePluginClassLoader 对象以支持大多数插件化特征。默认为:RePluginClassLoader的实例
* <p>
* 子类可复写此方法,来创建自己的ClassLoader,做相应的事情(如Hook宿主的ClassLoader做一些特殊的事)
*
* @param parent 该ClassLoader的父亲,通常为BootClassLoader
* @param original 宿主的原ClassLoader,通常为PathClassLoader
* @return 支持插件化方案的ClassLoader对象,可直接返回RePluginClassLoader
* @see RePluginClassLoader
*/
public RePluginClassLoader createClassLoader(ClassLoader parent, ClassLoader original) {
return new RePluginClassLoader(parent, original);
}

/**
* 插件【插件用的】 ClassLoader对象。默认为:PluginDexClassLoader的实例
* <p>
Expand Down
Loading