Java反射调基础和进阶使用方法
一、类和方法调用
try {
Class<?> class1 = Class.forName("com.XXX.AAAManager");
Method method = class1.getMethod("getInstance");
Method method2 = class1.getMethod("initWith", new Class[]{Context.class,String.class});
method2.invoke(method.invoke(class1), context, appId);
} catch (Exception e) {
Log.d("error", e.toString());
e.printStackTrace();
}
二、带参数的构造函数的类
Class<?> clazz=Class.forName("com.sanmao10.Person");
Constructor con = clazz.getDeclaredConstructor(String.class,int.class);
Object obj = con.newInstance(str1, age);
三、使用反射方式去实现一个接口
1、非反射的写法
2、反射
try {
Class<?> splashADClass = Class.forName("com.qq.e.ads.splash.SplashAD");
Class<?> mSplashADListener = Class.forName("com.qq.e.ads.splash.SplashADListener");
Constructor con = splashADClass.getDeclaredConstructor(Context.class, String.class, mSplashADListener);
Object listener = Proxy.newProxyInstance(mSplashADListener.getClassLoader(),
new Class[] { mSplashADListener }, new SplashADListenerImpl(activity, posId, aaaListener, errorListener));
Object obj = con.newInstance(activity, posId, listener);
Method method2 = splashADClass.getMethod("fetchAndShowIn", new Class[]{ViewGroup.class});
method2.invoke(obj, container);
} catch (Exception e) {
e.printStackTrace();
}
private class SplashADListenerImpl implements InvocationHandler {
private Activity activity;
private String posId;
private AAAListener Listener;
private SDKErrorListener errorListener;
public SplashADListenerImpl(final Activity activity, String posId, final AAAListener aaaListener, final SDKErrorListener errorListener) {
this.activity = activity;
this.posId = posId;
this.aaaListener = aaaListener;
this.errorListener = errorListener;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
Log.e("TAG", "invoke, method: " + method.getName());
if("onADDismissed".equals(method.getName())) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
osetListener.onClose();
}
});
} else if("onNoAD".equals(method.getName())) {
Class<?> AdErrorClass = Class.forName("com.qq.e.comm.util.AdError");
Object mAdErrorObj = AdErrorClass.cast(args[0]);
Method method1 = AdErrorClass.getMethod("getErrorCode");
Method method2 = AdErrorClass.getMethod("getErrorMsg");
final String codeErr = (String) method1.invoke(mAdErrorObj);
final String codeErrMsg = (String) method2.invoke(mAdErrorObj);
Log.e("TAG", "invoke, args, path: " + method1.invoke(mAdErrorObj) + method2.invoke(mAdErrorObj));
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
errorListener.onerror();
}
});
}
} catch (Exception e) {
Log.e("Exception", e.toString());
}
return proxy;
}
}
构建者(Builder)模式
1、不是反射
TTAdSdk.init(context,
new TTAdConfig.Builder()
.appId(appId)
.useTextureView(false)
.appName(context.getApplicationInfo().name)
.titleBarTheme(TTAdConstant.TITLE_BAR_THEME_DARK)
.allowShowNotify(true)
.allowShowPageWhenScreenLock(true)
.debug(false)
.directDownloadNetworkType(TTAdConstant.NETWORK_STATE_WIFI, TTAdConstant.NETWORK_STATE_4G)
.supportMultiProcess(true)
.needClearTaskReset()
.build());
2、反射实现
try {
Class<?> TTAdSdkClass = Class.forName("com.bytedance.sdk.openadsdk.TTAdSdk");
Class<?> TTAdConfigClass = Class.forName("com.bytedance.sdk.openadsdk.TTAdConfig");
Class<?> TTAdConfigBuilderClass = Class.forName("com.bytedance.sdk.openadsdk.TTAdConfig$Builder");
Method method0 = TTAdSdkClass.getMethod("init", new Class[]{Context.class, TTAdConfigClass});
Method method2 = TTAdConfigBuilderClass.getMethod("appId", new Class[]{String.class});
Method method3 = TTAdConfigBuilderClass.getMethod("useTextureView", new Class[]{boolean.class});
Method method4 = TTAdConfigBuilderClass.getMethod("appName", new Class[]{String.class});
Method method5 = TTAdConfigBuilderClass.getMethod("allowShowNotify", new Class[]{boolean.class});
Method method6 = TTAdConfigBuilderClass.getMethod("build");
Object configObj = method6.invoke(method5.invoke(method4.invoke(method3.invoke(method2.invoke(TTAdConfigBuilderClass.newInstance(), appId), false), context.getApplicationInfo().name), false));
method0.invoke(TTAdSdkClass, context, configObj);
} catch (Exception e) {
e.printStackTrace();
}