@Test public void testGetFieldAt() throws Exception { Context context = Mockit.setUpMock(new MockContext()); C c = new C(); assertEquals(c, Configurator.getObjectContaining(c, "b")); assertEquals(c.getB(), Configurator.getObjectContaining(c, "b.a")); assertEquals(c.getB().getA(), Configurator.getObjectContaining(c, "b.a.d")); }
protected static Object getObjectContaining(Object obj, String path) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { if(obj == null) { throw new NullPointerException("Attempt to call getObjectContaining(Object obj, String path) " + "on a null Object instance. Path was: " + path); } Log.d(TAG, "Looking up object containing: " + path); int separatorIndex = path.indexOf("."); if (separatorIndex > 0) { String lhs = path.substring(0, separatorIndex); String rhs = path.substring(separatorIndex + 1, path.length()); Method m = getGetter(obj.getClass(), lhs); if(m == null) { throw new NullPointerException("No getter found for field: " + lhs + " within " + obj.getClass()); } Log.d(TAG, "Invoking " + m.getName() + " on instance of " + obj.getClass().getName()); Object o = m.invoke(obj); return getObjectContaining(o, rhs); } else { return obj; } }
Configurator { protected static Object getObjectContaining(Object obj, String path) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { if(obj == null) { throw new NullPointerException("Attempt to call getObjectContaining(Object obj, String path) " + "on a null Object instance. Path was: " + path); } Log.d(TAG, "Looking up object containing: " + path); int separatorIndex = path.indexOf("."); if (separatorIndex > 0) { String lhs = path.substring(0, separatorIndex); String rhs = path.substring(separatorIndex + 1, path.length()); Method m = getGetter(obj.getClass(), lhs); if(m == null) { throw new NullPointerException("No getter found for field: " + lhs + " within " + obj.getClass()); } Log.d(TAG, "Invoking " + m.getName() + " on instance of " + obj.getClass().getName()); Object o = m.invoke(obj); return getObjectContaining(o, rhs); } else { return obj; } } }
Configurator { protected static Object getObjectContaining(Object obj, String path) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { if(obj == null) { throw new NullPointerException("Attempt to call getObjectContaining(Object obj, String path) " + "on a null Object instance. Path was: " + path); } Log.d(TAG, "Looking up object containing: " + path); int separatorIndex = path.indexOf("."); if (separatorIndex > 0) { String lhs = path.substring(0, separatorIndex); String rhs = path.substring(separatorIndex + 1, path.length()); Method m = getGetter(obj.getClass(), lhs); if(m == null) { throw new NullPointerException("No getter found for field: " + lhs + " within " + obj.getClass()); } Log.d(TAG, "Invoking " + m.getName() + " on instance of " + obj.getClass().getName()); Object o = m.invoke(obj); return getObjectContaining(o, rhs); } else { return obj; } }  }
Configurator { protected static Object getObjectContaining(Object obj, String path) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { if(obj == null) { throw new NullPointerException("Attempt to call getObjectContaining(Object obj, String path) " + "on a null Object instance. Path was: " + path); } Log.d(TAG, "Looking up object containing: " + path); int separatorIndex = path.indexOf("."); if (separatorIndex > 0) { String lhs = path.substring(0, separatorIndex); String rhs = path.substring(separatorIndex + 1, path.length()); Method m = getGetter(obj.getClass(), lhs); if(m == null) { throw new NullPointerException("No getter found for field: " + lhs + " within " + obj.getClass()); } Log.d(TAG, "Invoking " + m.getName() + " on instance of " + obj.getClass().getName()); Object o = m.invoke(obj); return getObjectContaining(o, rhs); } else { return obj; } }  static void configure(Context ctx, Object obj, int xmlFileId); static void configure(Context ctx, Object obj, HashMap<String, String> params); }
Configurator { protected static Object getObjectContaining(Object obj, String path) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { if(obj == null) { throw new NullPointerException("Attempt to call getObjectContaining(Object obj, String path) " + "on a null Object instance. Path was: " + path); } Log.d(TAG, "Looking up object containing: " + path); int separatorIndex = path.indexOf("."); if (separatorIndex > 0) { String lhs = path.substring(0, separatorIndex); String rhs = path.substring(separatorIndex + 1, path.length()); Method m = getGetter(obj.getClass(), lhs); if(m == null) { throw new NullPointerException("No getter found for field: " + lhs + " within " + obj.getClass()); } Log.d(TAG, "Invoking " + m.getName() + " on instance of " + obj.getClass().getName()); Object o = m.invoke(obj); return getObjectContaining(o, rhs); } else { return obj; } }  static void configure(Context ctx, Object obj, int xmlFileId); static void configure(Context ctx, Object obj, HashMap<String, String> params);  }