Java reflection demo

Java reflection learning code A method of

reflectively calling static private member variables of a class
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 ();
    }
  }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326098775&siteId=291194637