Back to project page ixming-android-inject.
The source code is released under:
Apache License
If you think the Android project ixming-android-inject listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package org.ixming.android.inject; // www.j a v a2 s . com import java.lang.reflect.Field; import java.lang.reflect.Method; import org.ixming.android.inject.core.BaseInjectLoader; import org.ixming.android.inject.core.InjectLoaderFactory; import android.app.Activity; import android.content.Context; import android.view.View; /** * ????????layout?????????????????????????????target?????????????? * * <p> * <strong>????</strong> ????????????????????????????????OOM?????<br/> * ????????????????????????<br/> * ????????????????????/???????????????????????????? * </p> * * @author Yin Yong * @version 1.0 */ public class InjectorUtils { final String TAG = InjectorUtils.class.getSimpleName(); private static InjectorUtils mSingleton = null; /** * ??????????????????????????????????????InjectConfigure?? */ public static synchronized InjectorUtils buildAsSingleton(InjectConfigure configure) { if (null == mSingleton) { mSingleton = new InjectorUtils(configure); } return mSingleton; } /** * ????????????? * * <p> * Tips:?????{@link InjectorUtils#buildAsSingleton(InjectConfigure)}??????????? * ?????????<br/> * ???????????????????????? * </p> */ public static InjectorUtils defaultInstance() { return null == mSingleton ? new InjectorUtils(new InjectConfigure()) : mSingleton; } /** * ???????????configure???????????? */ public static InjectorUtils instanceBuildFrom(InjectConfigure configure) { return new InjectorUtils(configure); } private final InjectConfigure mConfigure; InjectorUtils() { this(new InjectConfigure()); } InjectorUtils(InjectConfigure configure) { if (null == configure) { // new default configure = new InjectConfigure(); } mConfigure = configure; } /** * ?????Activity???????? * * @param context * ?????????????????Context */ public void inject(Activity context) { BaseInjectLoader baseLoader = InjectLoaderFactory.createFrom(context, context, null); innerInject(context, mConfigure, baseLoader); } /** * ????????View?????????????????????? * <p> * ?????{@link #inject(View, Context)}??View.getContext()?????????? * </p> * * <p> * ???????????View???Context???????{@link #inject(View, Context)} * </p> * * @param view * ?????????????????Context * * @see {@link #inject(View, Context)} */ public void inject(View view) { inject(view, view.getContext()); } /** * ????????View?????????????????????? * <p> * ?????{@link #inject(View, Context)}??View.getContext()?????????? * </p> * * <p> * ???????????View???Context???????{@link #inject(View, Context)} * </p> * * @param view * ??????? * @param localContext * ????Context * * @see {@link #inject(View, Context)} */ public void inject(View view, Context localContext) { BaseInjectLoader baseLoader = InjectLoaderFactory.createFrom(view, localContext, null); innerInject(view, mConfigure, baseLoader); } /** * ??????????????????????????Activity??View????????Fragment * * @param target * ????????????????? * @param contentView * ????????View????????Context?? */ public void inject(Object target, View contentView) { inject(target, contentView, contentView.getContext()); } /** * ??????????????????????????Activity??View????????Fragment * * @param target * ????????????????? * @param contentView * ????????View * @param localContext * ?????????Context?? */ public void inject(Object target, View contentView, Context localContext) { BaseInjectLoader baseLoader = InjectLoaderFactory.createFrom( contentView, localContext, null); innerInject(target, mConfigure, baseLoader); } private void innerInject(Object target, InjectConfigure config, BaseInjectLoader baseLoader) { boolean isInjectReses = config.isInjectReses(); boolean isInjectViews = config.isInjectViews(); boolean isInjectOnClickMethods = config.isInjectOnClickMethods(); // inject object if (isInjectReses || isInjectViews) Inject1: { Field[] fields = target.getClass().getDeclaredFields(); if (null == fields || fields.length == 0) { break Inject1; } for (Field field : fields) { if (isInjectViews && baseLoader.injectView(target, field)) { continue; } // next if (isInjectReses && baseLoader.injectRes(target, field)) { continue; } // to be continued } } if (isInjectOnClickMethods) Inject2: { Method[] methods = target.getClass().getDeclaredMethods(); if (null == methods || methods.length == 0) { break Inject2; } for (Method method : methods) { if (baseLoader.injectOnClickMethodListener(target, method)) { continue ; } } } // force GC System.gc(); } }