java反射学习代码
反射调用类的静态私有成员变量的一个方法
class="java">
public static void clearApiService() {
try {
Field field = APIServiceFactory.class.getDeclaredField("mServiceInstanceMap");
field.setAccessible(true);
Object obj = field.get(APIServiceFactory.class);
Method clearMethod = Map.class.getDeclaredMethod("clear");
clearMethod.invoke(obj);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}