Java平臺(tái)的反射機(jī)制是代碼動(dòng)態(tài)加載和調(diào)用的基本途徑天揖,在安卓系統(tǒng)源碼中也用到了大量的反射動(dòng)態(tài)加載類。反射也是安卓平臺(tái)插件化實(shí)現(xiàn)的必要掌握的基礎(chǔ)知識(shí)。代理是客戶端靈活操作對(duì)象,間接的低耦合度操作對(duì)象的有效途徑,也是插件化必要掌握知識(shí)激蹲。
安卓插件化基礎(chǔ) java反射與代理
一难裆、反射
java中反射機(jī)理比較常用瓢棒,這里主要以代碼實(shí)例展示其用法口糕。
什么是反射缅阳?
指程序運(yùn)行時(shí) 加載、獲取一個(gè)未知類(已知類名)及類屬性和方法的java技術(shù)機(jī)理景描。加載完成后會(huì)在虛擬機(jī)中產(chǎn)生一個(gè)class對(duì)象,一個(gè)類只有一個(gè)class對(duì)象秀撇,這個(gè)class對(duì)象包含了類的相關(guān)結(jié)構(gòu)信息超棺。
這種動(dòng)態(tài)獲取的信息以及動(dòng)態(tài)調(diào)用對(duì)象的方法的功能稱為java語(yǔ)言的反射機(jī)制。
Class對(duì)象如何獲群茄唷棠绘?
- 運(yùn)用Class.forName(String className)動(dòng)態(tài)加載類;
- .class 屬性(輕量級(jí))再扭;
- getClass()方法氧苍;
對(duì)象的創(chuàng)建
- 使用Class對(duì)象的newInstance()方法來(lái)創(chuàng)建該Class對(duì)象對(duì)應(yīng)類的實(shí)例,這種方式要求該Class對(duì)象的對(duì)應(yīng)類有默認(rèn)構(gòu)造器泛范。
- 先使用Class對(duì)象獲取指定的Constructor對(duì)象, 再調(diào)用Constructor對(duì)象的newInstance()让虐。通過(guò)這種方式可以選擇指定的構(gòu)造器來(lái)創(chuàng)建實(shí)例。
//Class 類 newInstance源碼
@CallerSensitive
public T newInstance()
throws InstantiationException, IllegalAccessException
{
if (System.getSecurityManager() != null) {
checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), false);
}
// NOTE: the following code may not be strictly correct under
// the current Java memory model.
// Constructor lookup
if (cachedConstructor == null) {
if (this == Class.class) {
throw new IllegalAccessException(
"Can not call newInstance() on the Class for java.lang.Class"
);
}
try {
Class<?>[] empty = {};
final Constructor<T> c = getConstructor0(empty, Member.DECLARED);//默認(rèn)構(gòu)造器
// Disable accessibility checks on the constructor
// since we have to do the security check here anyway
// (the stack depth is wrong for the Constructor's
// security check to work)
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<Void>() {
public Void run() {
c.setAccessible(true);
return null;
}
});
cachedConstructor = c;
} catch (NoSuchMethodException e) {
throw (InstantiationException)
new InstantiationException(getName()).initCause(e);
}
}
Constructor<T> tmpConstructor = cachedConstructor;
// Security check (same as in java.lang.reflect.Constructor)
int modifiers = tmpConstructor.getModifiers();
if (!Reflection.quickCheckMemberAccess(this, modifiers)) {
Class<?> caller = Reflection.getCallerClass();
if (newInstanceCallerCache != caller) {
Reflection.ensureMemberAccess(caller, this, null, modifiers);
newInstanceCallerCache = caller;
}
}
// Run constructor
try {
return tmpConstructor.newInstance((Object[])null);
} catch (InvocationTargetException e) {
Unsafe.getUnsafe().throwException(e.getTargetException());
// Not reached
return null;
}
}
相關(guān)Api相關(guān)實(shí)例
先定義一個(gè)bean類作為反射對(duì)象
public class CankingBean extends Canking implements ICanking {
private String name;
private int id;
public CankingBean() {
//Nothing
}
public CankingBean(int id, String name) {
this.id = id;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String n) {
this.name = n;
}
public int getId() {
return id;
}
public void setId(int i) {
this.id = i;
}
@Override
public void createApp() {
//nothing
}
}
1.對(duì)象實(shí)例化
CankingBean bean = null, bean1 = null;
Class test = null;
try {
test = Class.forName("com.canking.CankingBean");
} catch (Exception e) {
e.printStackTrace();
}
Constructor cons[] = test.getConstructors();
System.out.println("cons.len:" + cons.length + " name:" + test.getName() + " simpleName:" + test.getSimpleName() + " Canonical:" + test.getCanonicalName() + " type:" + test.getTypeName());
try {
bean = (CankingBean) cons[0].newInstance();
bean1 = (CankingBean) cons[1].newInstance(1, "canking");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("bean.id:" + bean.getId() + " bean.name:" + bean.getName() + " b1.id:" + bean1.getId() + " b1.name:" + bean1.getName());
2. 獲取實(shí)現(xiàn)接口列表
Class<?> intes[]=test.getInterfaces();
3.獲取父類
Class<?> temp=demo.getSuperclass();
4.獲取類屬性
Field[] fields = test.getDeclaredFields();//本類屬性
for (Field f : fields) {
int modifier = f.getModifiers();//修飾符
String modiStr = Modifier.toString(modifier);
Class type = f.getType();
System.out.println(f.getName() + " || modifier:" + modiStr + " type:" + type.getName());
}
System.out.println("\n");
Field[] fieldsAll = test.getFields();//接口中屬性
for (Field f : fieldsAll) {
int modifier = f.getModifiers();//修飾符
String modiStr = Modifier.toString(modifier);
Class type = f.getType();
System.out.println(f.getName() + " || modifier:" + modiStr + " type:" + type.getName());
}
想要獲取父類中屬性怎么辦罢荡?getSuperclass()先獲取父類實(shí)例赡突,然后調(diào)用getDeclaredFields()獲取∏裕可以依次遍歷super類惭缰,獲得全部繼承關(guān)系中的屬性。
5.獲取類方法
Method method[] = test.getMethods(); //獲取類及繼承關(guān)系中的所有類方法
for(Method m : method){
Class<?> returnType = m.getReturnType();
Class<?> para[] = m.getParameterTypes();
int temp = m.getModifiers();
System.out.println(m.getName()+" returnType:"+returnType+" modifiers:"+Modifier.toString(temp));
for(Class c : para){
System.out.println(" c name:"+c.getName());
}
}
6.方法及屬性的操作
try {
Method ms = test.getMethod("setName", String.class);
Method mg = test.getMethod("getName");
Object obj = test.newInstance();
ms.invoke(obj, "New name");
System.out.println("name:" + mg.invoke(obj));
Field canking = test.getDeclaredField("name");
canking.setAccessible(true);//禁用權(quán)限檢測(cè)
canking.set(obj, "Name Modified!");
System.out.println("name:" + canking.get(obj));
} catch (Exception e) {
e.printStackTrace();
}
二笼才、代理
什么是java代理模式漱受?
代理用到類反射的知識(shí),這里進(jìn)一步單獨(dú)講解加深理解骡送。
對(duì)某個(gè)對(duì)象提供一個(gè)代理對(duì)象昂羡,使得我們可以通過(guò)代理對(duì)象間接的操作原對(duì)象。
靜態(tài)代理:代碼層通過(guò)設(shè)計(jì)類獲得一個(gè)中間層代理各谚,是在編譯時(shí)已經(jīng)實(shí)現(xiàn)好的紧憾,可以說(shuō)是一個(gè)class文件。
動(dòng)態(tài)代理:代理類是在運(yùn)行時(shí)由java機(jī)理產(chǎn)生昌渤。起到動(dòng)態(tài)調(diào)用和操作代碼的目的赴穗。
由于靜態(tài)代理比較簡(jiǎn)單,易于理解,本文主要講解動(dòng)態(tài)代理般眉。
基本概念
InvocationHandler接口
代理類調(diào)用任何方法都會(huì)經(jīng)過(guò)這個(gè)調(diào)用處理器類的invoke方法了赵。
Proxy
主要用于產(chǎn)生代理類,通過(guò) Proxy 類生成的代理類都繼承了 Proxy 類甸赃。newProxyInstance方法封裝了獲取代理對(duì)象柿汛。
newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h);方法傳如的參數(shù)可以看出,代理一個(gè)對(duì)象埠对,必須要要滿足這個(gè)對(duì)象實(shí)現(xiàn)一個(gè)接口络断。
動(dòng)態(tài)代理實(shí)現(xiàn)方法
個(gè)人學(xué)習(xí)習(xí)慣,學(xué)習(xí)新知識(shí)项玛,先了解大概貌笨,然后就是代碼,最后看原理襟沮,有精力的話就寫(xiě)點(diǎn)東西分享出來(lái)锥惋。這里就先看下代碼如何實(shí)現(xiàn)一個(gè)動(dòng)態(tài)代理。
只要一個(gè)類滿足代理?xiàng)l件开伏,即可實(shí)現(xiàn)代理膀跌。
public class CankingProxyHandler implements InvocationHandler {
private Object object;
public CankingProxyHandler(Object obj){
this.object= obj;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("Proxy method in");
if ("setName".equals(method.getName())) {
return method.invoke(object, "Proxy");
}
return method.invoke(object, args);
}
}
CankingProxyHandler proxyHandler = new CankingProxyHandler(bean1);
ICanking cProxy = (ICanking) Proxy.newProxyInstance(CankingBean.class.getClassLoader(), CankingBean.class.getInterfaces(), proxyHandler);
cProxy.createApp();
代理類的每次方法調(diào)用,都會(huì)觸發(fā)proxyHandler的invoke方法固灵,這樣我們就可以在此時(shí)做一些自己的事情捅伤。
android系統(tǒng)中有些類也是滿足代理?xiàng)l件的,這樣我們同樣可以做一個(gè)代理類怎虫,來(lái)實(shí)現(xiàn)hook系統(tǒng)部分方法的目的暑认。
實(shí)現(xiàn)原理
首先來(lái)看下newProxyInstance源碼
@CallerSensitive
public static Object newProxyInstance(ClassLoader loader,
Class<?>[] interfaces,
InvocationHandler h)
throws IllegalArgumentException
{
Objects.requireNonNull(h);
final Class<?>[] intfs = interfaces.clone();
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
checkProxyAccess(Reflection.getCallerClass(), loader, intfs);
}
/*
* Look up or generate the designated proxy class.
*/
Class<?> cl = getProxyClass0(loader, intfs); //代理類生成關(guān)鍵方法
/*
* Invoke its constructor with the designated invocation handler.
*/
try {
if (sm != null) {
checkNewProxyPermission(Reflection.getCallerClass(), cl);
}
final Constructor<?> cons = cl.getConstructor(constructorParams);
final InvocationHandler ih = h;
if (!Modifier.isPublic(cl.getModifiers())) {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
cons.setAccessible(true);
return null;
}
});
}
return cons.newInstance(new Object[]{h});
} catch (IllegalAccessException|InstantiationException e) {
throw new InternalError(e.toString(), e);
} catch (InvocationTargetException e) {
Throwable t = e.getCause();
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else {
throw new InternalError(t.toString(), t);
}
} catch (NoSuchMethodException e) {
throw new InternalError(e.toString(), e);
}
}
可以看出getProxyClass0()方法為生成代理類關(guān)鍵方法
/**
* Generate a proxy class. Must call the checkProxyAccess method
* to perform permission checks before calling this.
*/
private static Class<?> getProxyClass0(ClassLoader loader,
Class<?>... interfaces) {
if (interfaces.length > 65535) {
throw new IllegalArgumentException("interface limit exceeded");
}
// If the proxy class defined by the given loader implementing
// the given interfaces exists, this will simply return the cached copy;
// otherwise, it will create the proxy class via the ProxyClassFactory
return proxyClassCache.get(loader, interfaces);
}
看到源碼注釋,如果代理類在傳入的classloader中已經(jīng)加載則直接返回大审,否則會(huì)在ProxyClassFactory中生成蘸际。
proxyClassCache.get方法中有以下代碼
// create subKey and retrieve the possible Supplier<V> stored by that
// subKey from valuesMap
Object subKey = Objects.requireNonNull(subKeyFactory.apply(key, parameter));
Supplier<V> supplier = valuesMap.get(subKey);
結(jié)合代碼和注釋可以大概看出subKeyFactory.apply(key, parameter)返回的就是代理類
那我們具體看看apply是怎么生產(chǎn)我們的代理類的:
@Override
public Class<?> apply(ClassLoader loader, Class<?>[] interfaces) {
Map<Class<?>, Boolean> interfaceSet = new IdentityHashMap<>(interfaces.length);
for (Class<?> intf : interfaces) {
/*
* Verify that the class loader resolves the name of this
* interface to the same Class object.
*/
Class<?> interfaceClass = null;
try {
interfaceClass = Class.forName(intf.getName(), false, loader);//加載原始接口
} catch (ClassNotFoundException e) {
}
if (interfaceClass != intf) {
throw new IllegalArgumentException(
intf + " is not visible from class loader");
}
/*
* Verify that the Class object actually represents an
* interface.
*/
if (!interfaceClass.isInterface()) {
throw new IllegalArgumentException(
interfaceClass.getName() + " is not an interface");
}
/*
* Verify that this interface is not a duplicate.
*/
if (interfaceSet.put(interfaceClass, Boolean.TRUE) != null) {
throw new IllegalArgumentException(
"repeated interface: " + interfaceClass.getName());
}
}
String proxyPkg = null; // package to define proxy class in
int accessFlags = Modifier.PUBLIC | Modifier.FINAL;
/*
* Record the package of a non-public proxy interface so that the
* proxy class will be defined in the same package. Verify that
* all non-public proxy interfaces are in the same package.
*/
for (Class<?> intf : interfaces) {//解析原始接口
int flags = intf.getModifiers();
if (!Modifier.isPublic(flags)) {
accessFlags = Modifier.FINAL;
String name = intf.getName();
int n = name.lastIndexOf('.');
String pkg = ((n == -1) ? "" : name.substring(0, n + 1));
if (proxyPkg == null) {
proxyPkg = pkg;
} else if (!pkg.equals(proxyPkg)) {
throw new IllegalArgumentException(
"non-public interfaces from different packages");
}
}
}
if (proxyPkg == null) {
// if no non-public proxy interfaces, use com.sun.proxy package
proxyPkg = ReflectUtil.PROXY_PACKAGE + ".";
}
/*
* Choose a name for the proxy class to generate.
*/
long num = nextUniqueNumber.getAndIncrement();
String proxyName = proxyPkg + proxyClassNamePrefix + num;//構(gòu)造代理類名
/*
* Generate the specified proxy class.
*/
byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
proxyName, interfaces, accessFlags);//生產(chǎn)代理類
try {
return defineClass0(loader, proxyName,
proxyClassFile, 0, proxyClassFile.length);
} catch (ClassFormatError e) {
/*
* A ClassFormatError here means that (barring bugs in the
* proxy class generation code) there was some other
* invalid aspect of the arguments supplied to the proxy
* class creation (such as virtual machine limitations
* exceeded).
*/
throw new IllegalArgumentException(e.toString());
}
}
最終ProxyGenerator.generateProxyClass方法中產(chǎn)生類代理類并寫(xiě)入磁盤(pán)。這種操作代碼的代碼java總稱為元編程徒扶,java SOURCE級(jí)別的注解中也會(huì)大量用到粮彤。
代理類最終生產(chǎn)的樣子如下:
public final class $Proxy1 extends Proxy implements Subject{
private InvocationHandler h;
private $Proxy1(){}
public $Proxy1(InvocationHandler h){
this.h = h;
}
public int request(int i){
Method method = Subject.class.getMethod("method", new Class[]{int.class}); //創(chuàng)建method對(duì)象
return (Integer)h.invoke(this, method, new Object[]{new Integer(i)}); //調(diào)用了invoke方法
}
}
從代理來(lái)結(jié)構(gòu),可以看出為什么每次方法的調(diào)用都會(huì)掉到invocationHandler的invoke方法姜骡。
——————
歡迎轉(zhuǎn)載导坟,請(qǐng)標(biāo)明出處:常興E站 www.canking.win