Java Utililty Methods Field from Class

List of utility methods to do Field from Class

Description

The list of methods to do Field from Class are organized into topic(s).

Method

FieldfindDeclaredField(Class javaClass, String fieldName)
Finding a field within a class potentially has to navigate through it's superclasses to eventually find the field.
try {
    return javaClass.getDeclaredField(fieldName);
} catch (NoSuchFieldException ex) {
    Class superclass = javaClass.getSuperclass();
    if (superclass == null) {
        throw ex;
    } else {
        return findDeclaredField(superclass, fieldName);
...
FieldgetDeclaredField(final Class javaClass, final String fieldName, final boolean shouldSetAccessible)
Get a field actually declared in a class and wrap the call in doPrivileged if necessary.
Field field = javaClass.getDeclaredField(fieldName);
if (shouldSetAccessible) {
    field.setAccessible(true);
return field;
Field[]getDeclaredFields(final Class clazz)
Get the list of fields in a class.
return clazz.getDeclaredFields();
FieldgetField(Class type, String name)
returns field for given name in given type
try {
    Field result = type.getDeclaredField(name);
    result.setAccessible(true);
    return result;
} catch (NoSuchFieldException e) {
    throw new IllegalArgumentException("Could not access " + type.getName() + "." + name + " field");
FieldgetField(final Class javaClass, final String fieldName, final boolean shouldSetAccessible)
Get a field in a class or its superclasses and wrap the call in doPrivileged if necessary.
Field field = (Field) findDeclaredField(javaClass, fieldName);
if (shouldSetAccessible) {
    field.setAccessible(true);
return field;
StringgetJsonValueFieldName(String className)
get Json Value Field Name
switch (className) {
case "java.net.URL":
    return "toString";
case "java.lang.StringBuilder":
    return "toString";
case "java.lang.StringBuffer":
    return "toString";
case "java.util.UUID":
...
StringgetProperField(Class objectClass)
get Proper Field
for (Field f : objectClass.getFields()) {
    if (f.getType() == SocketAddress.class) {
        return f.getName();
return "N/A";
voidsetPrivateField(String fieldName, Class containingClass, Object target, Object value)
set Private Field
try {
    Field field = containingClass.getDeclaredField(fieldName);
    field.setAccessible(true);
    field.set(target, value);
} catch (Exception e) {
    throw new RuntimeException(e);