public static <T> T getField(@NotNull Class objectClass, @Nullable Object object, @Nullable("null means any type") Class<T> fieldType, @NotNull @NonNls String fieldName) { try { final Field field = findAssignableField(objectClass, fieldType, fieldName); @SuppressWarnings("unchecked") T t = (T)field.get(object); return t; } catch (NoSuchFieldException e) { LOG.debug(e); return null; } catch (IllegalAccessException e) { LOG.debug(e); return null; } }
public static <T> T getStaticFieldValue(@NotNull Class objectClass, @Nullable("null means any type") Class<T> fieldType, @NotNull @NonNls String fieldName) { try { final Field field = findAssignableField(objectClass, fieldType, fieldName); if (!Modifier.isStatic(field.getModifiers())) { throw new IllegalArgumentException("Field " + objectClass + "." + fieldName + " is not static"); } @SuppressWarnings("unchecked") T t = (T)field.get(null); return t; } catch (NoSuchFieldException e) { LOG.debug(e); return null; } catch (IllegalAccessException e) { LOG.debug(e); return null; } }
public static <T> boolean setField(@NotNull Class objectClass, Object object, @Nullable("null means any type") Class<T> fieldType, @NotNull @NonNls String fieldName, T value) { try { final Field field = findAssignableField(objectClass, fieldType, fieldName); field.set(object, value); return true; } catch (NoSuchFieldException e) { LOG.debug(e); // this 'return' was moved into 'catch' block because otherwise reference to common super-class of these exceptions (ReflectiveOperationException) // which doesn't exist in JDK 1.6 will be added to class-file during instrumentation return false; } catch (IllegalAccessException e) { LOG.debug(e); return false; } }