反射机制的实现
反射机制:代码示例:
被引用类
package com.zcdog.dogtv.manager.interval; import com.zcdog.dogtv.entity.RedDotEntity; import com.zcdog.dogtv.model.RedDotModel2; import com.zcdog.dogtv.view.user.RedDotViewAdapter; import com.zcdog.util.info.android.Logger; import com.zcdog.zchat.ui.view.ZChatRedDotView; /** * 警告:该类被 ZChat 模块通过反射调用,改动需谨慎 * <p/> * Created by Ivey on 2016/5/21. */ public class RefreshFriendHallRedDotHelper { private static final String TAG = "RefreshFriendHallRedDotHelper"; public static void refreshVisitorsRedDotStatus(Boolean show) { if (RedDotModel2.redDotCache != null && RedDotModel2.redDotCache.getMap() != null) { RedDotEntity redDotEntity = RedDotModel2.redDotCache.getMap().get(RedDotModel2.ZCHAT_VISITOR_TYPE); if (redDotEntity != null) { Logger.d(TAG, "更新 最近访客 的红点状态"); if (show) { redDotEntity.setNumber(0); } redDotEntity.setShow(show); RedDotModel2.saveCache(); } } } public static void refreshMsgRedDotStatus(Integer number) { if (RedDotModel2.redDotCache != null && RedDotModel2.redDotCache.getMap() != null) { RedDotEntity redDotEntity = RedDotModel2.redDotCache.getMap().get(RedDotModel2.ZCHAT_MESSAGE_ROOT_TYPE); if (redDotEntity != null) { Logger.d(TAG, "更新 未读消息 的红点状态"); if (number <= 0) { redDotEntity.setShow(false); } else { redDotEntity.setNumber(number); redDotEntity.setShow(true); } } } } public static void setRedDotIcon(ZChatRedDotView redDotView) { if (RedDotModel2.redDotCache != null && RedDotModel2.redDotCache.getMap() != null) { RedDotEntity redDotEntity = RedDotModel2.redDotCache.getMap().get(redDotView.getType()); if (redDotEntity != null) { redDotEntity.setRedDotIcon(new RedDotViewAdapter(redDotView)); } } } }
引用类:
package com.zcdog.zchat.manager; import com.zcdog.zchat.ui.view.ZChatRedDotView; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * SmartLocker 模块中 RefreshFriendHallRedDotHelper 类的映射 * <p/> * Created by Ivey on 2016/5/21. */ public class ZChatRefreshFriendHallRedDotHelper { private static final String TAG = "ZChatRefreshFriendHallRedDotHelper"; private static final String CLASS_NAME = "com.zcdog.dogtv.manager.interval.RefreshFriendHallRedDotHelper"; public static void refreshVisitorsRedDotStatus(boolean show) { try { Class RefreshFriendHallRedDotHelper = Class.forName(CLASS_NAME); Method refreshVisitorsRedDotStatus = RefreshFriendHallRedDotHelper.getMethod("refreshVisitorsRedDotStatus", Boolean.class); refreshVisitorsRedDotStatus.invoke(null, show); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } public static void refreshMsgRedDotStatus(int number) { try { Class RefreshFriendHallRedDotHelper = Class.forName(CLASS_NAME); Method refreshVisitorsRedDotStatus = RefreshFriendHallRedDotHelper.getMethod("refreshMsgRedDotStatus", Integer.class); refreshVisitorsRedDotStatus.invoke(null, number); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } public static void setRedDotIcon(ZChatRedDotView redDotView) { try { Class RefreshFriendHallRedDotHelper = Class.forName(CLASS_NAME); Method refreshVisitorsRedDotStatus = RefreshFriendHallRedDotHelper.getMethod("setRedDotIcon", ZChatRedDotView.class); refreshVisitorsRedDotStatus.invoke(null, redDotView); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } }
自己手动总结如下:
public class ATest(){ private static final TAG = "ATest"; public static void functionOne(Boolean ValueA){ //do something } public static void functionTwo(Integer ValueB){ //do something } } public class BTest(){ private static String TAG = "BTest"; private static final String CLASS_NAME = "包名.ATest" public static void functionOne(boolean a){ try { Class ATest= Class.forName(CLASS_NAME); Method functionOne= ATest.getMethod("functionOne", Boolean.class); functionOne .invoke(null, a); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } }
public static void functionTwo(int b){
try {
Class ATest= Class.forName(CLASS_NAME);
Method functionTwo= ATest.getMethod("functionTwo", Integer.class);
functionTwo .invoke(null, b);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}