/** * Returns an object which provides accessors for invoking otherwise inaccessible methods and * fields. * * @param iClass an interface with methods matching private methods on the target * @param target the target object */ public static <T> T reflector(Class<T> iClass, Object target) { Class<?> targetClass = determineTargetClass(iClass); Constructor<? extends T> ctor = (Constructor<? extends T>) CACHE.get(iClass); try { if (ctor == null) { Class<? extends T> reflectorClass = createReflectorClass(iClass, targetClass); ctor = reflectorClass.getConstructor(targetClass); ctor.setAccessible(true); } CACHE.put(iClass, ctor); return ctor.newInstance(target); } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { throw new IllegalStateException(e); } }
/** * Returns an object which provides accessors for invoking otherwise inaccessible methods and * fields. * * @param iClass an interface with methods matching private methods on the target * @param target the target object */ public static <T> T reflector(Class<T> iClass, Object target) { Class<?> targetClass = determineTargetClass(iClass); Constructor<? extends T> ctor = (Constructor<? extends T>) CACHE.get(iClass); try { if (ctor == null) { Class<? extends T> reflectorClass = createReflectorClass(iClass, targetClass); ctor = reflectorClass.getConstructor(targetClass); ctor.setAccessible(true); } CACHE.put(iClass, ctor); return ctor.newInstance(target); } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { throw new IllegalStateException(e); } }