ReflectionUtils { public static <T> T readStaticFieldOrNull(String classname, String fieldName) { try { Class<?> clazz = Class.forName(classname); return readStaticField(clazz, fieldName); } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException | SecurityException e) { return null; } } private  ReflectionUtils(); static T readStaticFieldOrNull(String classname, String fieldName); @Nonnull @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE", justification = "False positive on try-with-resources as of JDK11") static Collection<Class<?>> nestedClassesOf(Class<?>... classes); @Nonnull @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE", justification = "False positive on try-with-resources as of JDK11") static Resources resourcesOf(String... packages); static String toClassResourceId(String name); static Class<?> loadClass(ClassLoader classLoader, String name); static T newInstance(ClassLoader classLoader, String name);  }
@Test public void readStaticFieldOrNull_whenClassDoesNotExist_thenReturnNull() { Object field = ReflectionUtils.readStaticFieldOrNull("foo.bar.nonExistingClass", "field"); assertNull(field); }
@Test public void readStaticFieldOrNull_whenFieldDoesNotExist_thenReturnNull() { Object field = ReflectionUtils.readStaticFieldOrNull(MyClass.class.getName(), "nonExistingField"); assertNull(field); }
@Test public void readStaticFieldOrNull_readFromPrivateField() { String field = ReflectionUtils.readStaticFieldOrNull(MyClass.class.getName(), "staticPrivateField"); assertEquals("staticPrivateFieldContent", field); }
@Test public void readStaticFieldOrNull_readFromPublicField() { String field = ReflectionUtils.readStaticFieldOrNull(MyClass.class.getName(), "staticPublicField"); assertEquals("staticPublicFieldContent", field); }