HHH-10349 : PersistenceUtilHelper should call getDeclaredField/Method inside of privileged block

(cherry picked from commit 986b2b65ad)
This commit is contained in:
Gail Badner 2015-12-10 16:41:05 -08:00
parent 4acd4bff8e
commit e150a6140e
1 changed files with 23 additions and 16 deletions

View File

@ -10,6 +10,8 @@ import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -352,24 +354,29 @@ public final class PersistenceUtilHelper {
return attributeAccess;
}
private AttributeAccess buildAttributeAccess(String attributeName) {
for ( Class clazz : classHierarchy ) {
try {
final Field field = clazz.getDeclaredField( attributeName );
if ( field != null ) {
return new FieldAttributeAccess( field );
private AttributeAccess buildAttributeAccess(final String attributeName) {
final PrivilegedAction<AttributeAccess> action = new PrivilegedAction<AttributeAccess>() {
@Override
public AttributeAccess run() {
for ( Class clazz : classHierarchy ) {
try {
final Field field = clazz.getDeclaredField( attributeName );
if ( field != null ) {
return new FieldAttributeAccess( field );
}
}
catch ( NoSuchFieldException e ) {
final Method method = getMethod( clazz, attributeName );
if ( method != null ) {
return new MethodAttributeAccess( attributeName, method );
}
}
}
//we could not find any match
return new NoSuchAttributeAccess( specifiedClass, attributeName );
}
catch ( NoSuchFieldException e ) {
final Method method = getMethod( clazz, attributeName );
if ( method != null ) {
return new MethodAttributeAccess( attributeName, method );
}
}
}
//we could not find any match
return new NoSuchAttributeAccess( specifiedClass, attributeName );
};
return System.getSecurityManager() != null ? AccessController.doPrivileged( action ) : action.run();
}
}