/** * Get a field from a class. * <p> * The method will first try to look for a declared field in the same class. * If the method is not declared in this class it will look for the field in * the super class. This will continue throughout the whole class hierarchy. * If the field is not found an {@link IllegalArgumentException} is thrown. * * @param declaringClass * The declaringClass of the class where the method is located. * @param fieldName * The method names. * @return A <code>java.lang.reflect.Field</code>. * @throws FieldNotFoundException * If a field cannot be found in the hierarchy. */ public static Field field(Class<?> declaringClass, String fieldName) { return Whitebox.getField(declaringClass, fieldName); }
/** * Get a field from a class. * <p> * The method will first try to look for a declared field in the same class. * If the method is not declared in this class it will look for the field in * the super class. This will continue throughout the whole class hierarchy. * If the field is not found an {@link IllegalArgumentException} is thrown. * * @param declaringClass * The declaringClass of the class where the method is located. * @param fieldName * The method names. * @return A {@code java.lang.reflect.Field}. * @throws FieldNotFoundException * If a field cannot be found in the hierarchy. */ public static Field field(Class<?> declaringClass, String fieldName) { return Whitebox.getField(declaringClass, fieldName); }
final Field declaredField = Whitebox.getField(getType(source), field.getName()); declaredField.setAccessible(true); final Object object = declaredField.get(source);
final Field declaredField = Whitebox.getField(getType(source), field.getName()); declaredField.setAccessible(true); final Object object = declaredField.get(source);
@Test void testClassLoader() throws Exception { MockTransformerService mockTransformerService = new MockTransformerService() { @Nonnull @Override public List<ITransformer> transformers() { return Stream.of(classNodeTransformer).collect(Collectors.toList()); } }; TransformStore transformStore = new TransformStore(); LaunchPluginHandler lph = new LaunchPluginHandler(); TransformationServiceDecorator sd = Whitebox.invokeConstructor(TransformationServiceDecorator.class, mockTransformerService); sd.gatherTransformers(transformStore); final Constructor<TransformingClassLoader> constructor = Whitebox.getConstructor(TransformingClassLoader.class, transformStore.getClass(), lph.getClass(), Path[].class); TransformingClassLoader tcl = constructor.newInstance(transformStore, lph, new Path[] {FileSystems.getDefault().getPath(".")}); final Class<?> aClass = Class.forName("cheese.Puffs", true, tcl); assertEquals(Whitebox.getField(aClass, "testfield").getType(), String.class); assertEquals(Whitebox.getField(aClass, "testfield").get(null), "CHEESE!"); }