/** * Gets the value of a {@code static} {@link Field} by name. The field must be {@code public}. Only the specified * class will be considered. * * @param cls * the {@link Class} to reflect, must not be {@code null} * @param fieldName * the field name to obtain * @return the value of the field * @throws IllegalArgumentException * if the class is {@code null}, or the field name is blank or empty, is not {@code static}, or could * not be found * @throws IllegalAccessException * if the field is not accessible */ public static Object readDeclaredStaticField(final Class<?> cls, final String fieldName) throws IllegalAccessException { return readDeclaredStaticField(cls, fieldName, false); }
@Test public void testReadDeclaredNamedStaticFieldForceAccess() throws Exception { assertEquals(Foo.VALUE, FieldUtils.readDeclaredStaticField(Foo.class, "VALUE", true)); assertEquals("child", FieldUtils.readDeclaredStaticField(PublicChild.class, "VALUE", true)); try { FieldUtils.readDeclaredStaticField(PubliclyShadowedChild.class, "VALUE", true); fail("expected IllegalArgumentException"); } catch (final IllegalArgumentException e) { // pass } try { FieldUtils.readDeclaredStaticField(PrivatelyShadowedChild.class, "VALUE", true); fail("expected IllegalArgumentException"); } catch (final IllegalArgumentException e) { // pass } }
@Test public void testReadDeclaredNamedStaticField() throws Exception { assertEquals(Foo.VALUE, FieldUtils.readDeclaredStaticField(Foo.class, "VALUE")); try { FieldUtils.readDeclaredStaticField(PublicChild.class, "VALUE"); fail("expected IllegalArgumentException"); } catch (final IllegalArgumentException e) { // pass } try { FieldUtils.readDeclaredStaticField(PubliclyShadowedChild.class, "VALUE"); fail("expected IllegalArgumentException"); } catch (final IllegalArgumentException e) { // pass } try { FieldUtils.readDeclaredStaticField(PrivatelyShadowedChild.class, "VALUE"); fail("expected IllegalArgumentException"); } catch (final IllegalArgumentException e) { // pass } }
/** * Gets the value of a {@code static} {@link Field} by name. The field must be {@code public}. Only the specified * class will be considered. * * @param cls * the {@link Class} to reflect, must not be {@code null} * @param fieldName * the field name to obtain * @return the value of the field * @throws IllegalArgumentException * if the class is {@code null}, or the field name is blank or empty, is not {@code static}, or could * not be found * @throws IllegalAccessException * if the field is not accessible */ public static Object readDeclaredStaticField(final Class<?> cls, final String fieldName) throws IllegalAccessException { return readDeclaredStaticField(cls, fieldName, false); }
/** * Gets the value of a {@code static} {@link Field} by name. The field must be {@code public}. Only the specified * class will be considered. * * @param cls * the {@link Class} to reflect, must not be {@code null} * @param fieldName * the field name to obtain * @return the value of the field * @throws IllegalArgumentException * if the class is {@code null}, or the field name is blank or empty, is not {@code static}, or could * not be found * @throws IllegalAccessException * if the field is not accessible */ public static Object readDeclaredStaticField(final Class<?> cls, final String fieldName) throws IllegalAccessException { return readDeclaredStaticField(cls, fieldName, false); }
/** * Gets the value of a {@code static} {@link Field} by name. The field must be {@code public}. Only the specified * class will be considered. * * @param cls * the {@link Class} to reflect, must not be {@code null} * @param fieldName * the field name to obtain * @return the value of the field * @throws IllegalArgumentException * if the class is {@code null}, or the field name is blank or empty, is not {@code static}, or could * not be found * @throws IllegalAccessException * if the field is not accessible */ public static Object readDeclaredStaticField(final Class<?> cls, final String fieldName) throws IllegalAccessException { return readDeclaredStaticField(cls, fieldName, false); }
public default void register(Class<IGenericFunction> clazz) throws IllegalAccessException { // generic function should always have a public static FunctionName NAME field. FunctionName functionName = (FunctionName) FieldUtils.readDeclaredStaticField(clazz, "NAME"); GenericFunctionRegistry.registerFunction(functionName, clazz); }
Class<?> comparatorClass = bundle.loadClass("org.eclipse.jetty.http2.HTTP2Cipher"); Comparator<String> cipherComparator = (Comparator<String>) FieldUtils.readDeclaredStaticField(comparatorClass, "COMPARATOR"); sslContextFactory.setCipherComparator(cipherComparator);