Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions core/src/main/java/org/acegisecurity/util/FieldUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@

package org.acegisecurity.util;

import edu.umd.cs.findbugs.annotations.NonNull;
import java.lang.reflect.Field;

/**
* @deprecated use {@link org.apache.commons.lang.reflect.FieldUtils}
* @deprecated use {@code org.apache.commons.lang.reflect.FieldUtils}
*/
@Deprecated
public final class FieldUtils {

public static Object getProtectedFieldValue(String protectedField, Object object) {
public static Object getProtectedFieldValue(@NonNull String protectedField, @NonNull Object object) {
try {
return org.apache.commons.lang.reflect.FieldUtils.readField(object, protectedField, true);
Field field = getField(object.getClass(), protectedField);
return field.get(object);
} catch (IllegalAccessException x) {
throw new RuntimeException(x);
}
Expand All @@ -46,14 +48,51 @@ public static void setProtectedFieldValue(String protectedField, Object object,
// FieldUtils.writeField(Object, field, true) only sets accessible on *non* public fields
// and then fails with IllegalAccessException (even if you make the field accessible in the interim!
// for backwards compatability we need to use a few steps
Field field = org.apache.commons.lang.reflect.FieldUtils.getField(object.getClass(), protectedField, true);
Field field = getField(object.getClass(), protectedField);
field.setAccessible(true);
field.set(object, newValue);
} catch (Exception x) {
throw new RuntimeException(x);
}
}

/**
* Return the field with the given name from the class or its superclasses.
* If the field is not found, an {@link IllegalArgumentException} is thrown.
*
* @param clazz the class to search for the field
* @param fieldName the name of the field to find
* @return the {@link Field} object representing the field
* @throws IllegalArgumentException if the field is not found
*/
private static Field getField(@NonNull final Class<?> clazz, @NonNull final String fieldName) {
// Check class and its superclasses
Class<?> current = clazz;
while (current != null) {
try {
Field field = current.getDeclaredField(fieldName);
field.setAccessible(true);
return field;
} catch (NoSuchFieldException e) {
// Continue to check superclass
}
current = current.getSuperclass();
}

// Check interfaces
for (Class<?> iface : clazz.getInterfaces()) {
try {
Field field = iface.getDeclaredField(fieldName);
field.setAccessible(true);
return field;
} catch (NoSuchFieldException e) {
// Continue to check next interface
}
}

throw new IllegalArgumentException("Field '" + fieldName + "' not found in class " + clazz.getName());
}

// TODO other methods as needed

private FieldUtils() {}
Expand Down