Java反射demo

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();
    }
  }

猜你喜欢

转载自jonson.iteye.com/blog/2407308
今日推荐