/** * Reads an accessible {@code static} {@link Field}. * * @param field * to read * @return the field value * @throws IllegalArgumentException * if the field is {@code null}, or not {@code static} * @throws IllegalAccessException * if the field is not accessible */ public static Object readStaticField(final Field field) throws IllegalAccessException { return readStaticField(field, false); }
/** * Reads the named {@code public static} {@link Field}. Superclasses 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 readStaticField(final Class<?> cls, final String fieldName) throws IllegalAccessException { return readStaticField(cls, fieldName, false); }
@SuppressWarnings("unchecked") protected Map<String, ? extends BroadleafEnumerationType> getTypes(Class<? extends BroadleafEnumerationType> clazz) { try { return (Map<String, ? extends BroadleafEnumerationType>) FieldUtils.readStaticField(clazz, "TYPES", true); } catch (IllegalAccessException e) { throw new RuntimeException(e); } }
/** * Overridden to remove deprecated options */ @Override protected Map<String, ? extends BroadleafEnumerationType> getTypes(Class<? extends BroadleafEnumerationType> clazz) { try { Map<String, ? extends BroadleafEnumerationType> options = (Map<String, ? extends BroadleafEnumerationType>) FieldUtils.readStaticField(clazz, "TYPES", true); options.remove("NONE"); options.remove("BASIC"); return options; } catch (IllegalAccessException e) { throw new RuntimeException(e); } }
/** * Reads the named {@code static} {@link Field}. Superclasses will be considered. * * @param cls * the {@link Class} to reflect, must not be {@code null} * @param fieldName * the field name to obtain * @param forceAccess * whether to break scope restrictions using the * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will only * match {@code public} fields. * @return the Field object * @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 made accessible */ public static Object readStaticField(final Class<?> cls, final String fieldName, final boolean forceAccess) throws IllegalAccessException { final Field field = getField(cls, fieldName, forceAccess); Validate.isTrue(field != null, "Cannot locate field '%s' on %s", fieldName, cls); // already forced access above, don't repeat it here: return readStaticField(field, false); }
/** * Gets the value of a {@code static} {@link Field} by name. 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 * @param forceAccess * whether to break scope restrictions using the * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will only * match {@code public} fields. * @return the Field object * @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 made accessible */ public static Object readDeclaredStaticField(final Class<?> cls, final String fieldName, final boolean forceAccess) throws IllegalAccessException { final Field field = getDeclaredField(cls, fieldName, forceAccess); Validate.isTrue(field != null, "Cannot locate declared field %s.%s", cls.getName(), fieldName); // already forced access above, don't repeat it here: return readStaticField(field, false); }
@Test(expected = IllegalArgumentException.class) public void testReadStaticFieldForceAccessIllegalArgumentException1() throws Exception { FieldUtils.readStaticField(null, true); }
@Test(expected = IllegalArgumentException.class) public void testReadStaticFieldIllegalArgumentException1() throws Exception { FieldUtils.readStaticField(null); }
@Test public void testReadStaticFieldForceAccess() throws Exception { assertEquals(Foo.VALUE, FieldUtils.readStaticField(FieldUtils.getField(Foo.class, "VALUE"))); assertEquals(Foo.VALUE, FieldUtils.readStaticField(FieldUtils.getField(PublicChild.class, "VALUE"))); }
@Test public void testReadNamedStaticField() throws Exception { assertEquals(Foo.VALUE, FieldUtils.readStaticField(Foo.class, "VALUE")); assertEquals(Foo.VALUE, FieldUtils.readStaticField(PubliclyShadowedChild.class, "VALUE")); assertEquals(Foo.VALUE, FieldUtils.readStaticField(PrivatelyShadowedChild.class, "VALUE")); assertEquals(Foo.VALUE, FieldUtils.readStaticField(PublicChild.class, "VALUE")); FieldUtils.readStaticField(null, "none"); fail("null class should cause an IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.readStaticField(Foo.class, null); fail("null field name should cause an IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.readStaticField(Foo.class, ""); fail("empty field name should cause an IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.readStaticField(Foo.class, " "); fail("blank field name should cause an IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.readStaticField(Foo.class, "does_not_exist"); fail("a field that doesn't exist should cause an IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.readStaticField(PublicChild.class, "s"); fail("non-static field should cause an IllegalArgumentException"); } catch (final IllegalArgumentException e) {
@Test public void testReadNamedStaticFieldForceAccess() throws Exception { assertEquals(Foo.VALUE, FieldUtils.readStaticField(Foo.class, "VALUE", true)); assertEquals(Foo.VALUE, FieldUtils.readStaticField(PubliclyShadowedChild.class, "VALUE", true)); assertEquals(Foo.VALUE, FieldUtils.readStaticField(PrivatelyShadowedChild.class, "VALUE", true)); assertEquals("child", FieldUtils.readStaticField(PublicChild.class, "VALUE", true)); FieldUtils.readStaticField(null, "none", true); fail("null class should cause an IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.readStaticField(Foo.class, null, true); fail("null field name should cause an IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.readStaticField(Foo.class, "", true); fail("empty field name should cause an IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.readStaticField(Foo.class, " ", true); fail("blank field name should cause an IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.readStaticField(Foo.class, "does_not_exist", true); fail("a field that doesn't exist should cause an IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.readStaticField(PublicChild.class, "s", false); fail("non-static field should cause an IllegalArgumentException"); } catch (final IllegalArgumentException e) {
@Test(expected = IllegalArgumentException.class) public void testReadStaticFieldIllegalArgumentException2() throws Exception { assertEquals(Foo.VALUE, FieldUtils.readStaticField(FieldUtils.getField(Foo.class, "VALUE"))); final Field nonStaticField = FieldUtils.getField(PublicChild.class, "s"); assumeNotNull(nonStaticField); FieldUtils.readStaticField(nonStaticField); }
@Test(expected = IllegalArgumentException.class) public void testReadStaticFieldForceAccessIllegalArgumentException2() throws Exception { final Field nonStaticField = FieldUtils.getField(PublicChild.class, "s", true); assumeNotNull(nonStaticField); FieldUtils.readStaticField(nonStaticField); }
@Test public void testReadStaticField() throws Exception { assertEquals(Foo.VALUE, FieldUtils.readStaticField(FieldUtils.getField(Foo.class, "VALUE"))); }
final Option optionAnnotation = field.getAnnotation(Option.class); final Class<?> type = field.getType(); final Object beforeValue = FieldUtils.readStaticField(field); final Object afterValue;
/** * Reads an accessible {@code static} {@link Field}. * * @param field * to read * @return the field value * @throws IllegalArgumentException * if the field is {@code null}, or not {@code static} * @throws IllegalAccessException * if the field is not accessible */ public static Object readStaticField(final Field field) throws IllegalAccessException { return readStaticField(field, false); }
public static Object isOs(ActionContext actionContext) throws IllegalAccessException{ Thing self = actionContext.getObject("self"); String os = (String) self.doAction("getOs", actionContext); return FieldUtils.readStaticField(SystemUtils.class, os); }
public static Object getSystemProperty(ActionContext actionContext) throws IllegalAccessException{ Thing self = actionContext.getObject("self"); String name = (String) self.doAction("getPropertyName", actionContext); return FieldUtils.readStaticField(SystemUtils.class, name); } }
public static Object getStaticField(ActionContext actionContext) throws ClassNotFoundException, IllegalAccessException{ Thing self = (Thing) actionContext.get("self"); Thing realSelf = getSelf(actionContext); Object value = realSelf.doAction(self.getString("actionName"), actionContext); if(value instanceof String){ Class<?> className = Class.forName(self.getString("className")); return FieldUtils.readStaticField(className, (String) value); }else{ return value; } }
public static Object getStaticFieldValue(ActionContext actionContext) throws IllegalAccessException{ Thing self = actionContext.getObject("self"); Class<?> cls = (Class<?>) self.doAction("getClass", actionContext); String fieldName = (String) self.doAction("getFieldName", actionContext); Boolean forceAccess = (Boolean) self.doAction("isForceAccess", actionContext); return FieldUtils.readStaticField(cls, fieldName, forceAccess); }