List of utility methods to do Field from Class
Field | findDeclaredField(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); ... |
Field | getDeclaredField(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();
|
Field | getField(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"); |
Field | getField(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; |
String | getJsonValueFieldName(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": ... |
String | getProperField(Class objectClass) get Proper Field for (Field f : objectClass.getFields()) { if (f.getType() == SocketAddress.class) { return f.getName(); return "N/A"; |
void | setPrivateField(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); |