Skip to content

Commit

Permalink
HV-1439 Group the reflection and the setAccessible priviledged actions
Browse files Browse the repository at this point in the history
  • Loading branch information
gsmet committed Aug 3, 2017
1 parent 6ea2fc2 commit b52f1fb
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 23 deletions.
Expand Up @@ -19,7 +19,6 @@
import org.hibernate.validator.internal.metadata.facets.Cascadable;
import org.hibernate.validator.internal.util.ReflectionHelper;
import org.hibernate.validator.internal.util.privilegedactions.GetDeclaredField;
import org.hibernate.validator.internal.util.privilegedactions.SetAccessibility;

/**
* A {@link Cascadable} backed by a field of a Java bean.
Expand Down Expand Up @@ -100,11 +99,8 @@ private Field getAccessible(Field original) {
}

Class<?> clazz = original.getDeclaringClass();
Field member = run( GetDeclaredField.action( clazz, original.getName() ) );

run( SetAccessibility.action( member ) );

return member;
return run( GetDeclaredField.andMakeAccessible( clazz, original.getName() ) );
}

/**
Expand Down
Expand Up @@ -43,7 +43,6 @@
import org.hibernate.validator.internal.util.ReflectionHelper;
import org.hibernate.validator.internal.util.TypeResolutionHelper;
import org.hibernate.validator.internal.util.privilegedactions.GetDeclaredMethod;
import org.hibernate.validator.internal.util.privilegedactions.SetAccessibility;
import org.hibernate.validator.internal.util.stereotypes.Immutable;

/**
Expand Down Expand Up @@ -325,11 +324,8 @@ private Method getAccessible(Method original) {
}

Class<?> clazz = original.getDeclaringClass();
Method member = run( GetDeclaredMethod.action( clazz, original.getName() ) );

run( SetAccessibility.action( member ) );

return member;
return run( GetDeclaredMethod.andMakeAccessible( clazz, original.getName() ) );
}

private <T> T run(PrivilegedAction<T> action) {
Expand Down
Expand Up @@ -18,7 +18,6 @@
import org.hibernate.validator.internal.util.ReflectionHelper;
import org.hibernate.validator.internal.util.StringHelper;
import org.hibernate.validator.internal.util.privilegedactions.GetDeclaredField;
import org.hibernate.validator.internal.util.privilegedactions.SetAccessibility;

/**
* Field constraint location.
Expand Down Expand Up @@ -131,13 +130,8 @@ private static Field getAccessible(Field original) {
}

Class<?> clazz = original.getDeclaringClass();
Field accessibleField;

accessibleField = run( GetDeclaredField.action( clazz, original.getName() ) );

run( SetAccessibility.action( accessibleField ) );

return accessibleField;
return run( GetDeclaredField.andMakeAccessible( clazz, original.getName() ) );
}

/**
Expand Down
Expand Up @@ -17,21 +17,33 @@
public final class GetDeclaredField implements PrivilegedAction<Field> {
private final Class<?> clazz;
private final String fieldName;
private final boolean makeAccessible;

public static GetDeclaredField action(Class<?> clazz, String fieldName) {
return new GetDeclaredField( clazz, fieldName );
return new GetDeclaredField( clazz, fieldName, false );
}

private GetDeclaredField(Class<?> clazz, String fieldName) {
/**
* Before using this method, you need to check the {@code HibernateValidatorPermission.ACCESS_PRIVATE_MEMBERS}
* permission against the security manager.
*/
public static GetDeclaredField andMakeAccessible(Class<?> clazz, String fieldName) {
return new GetDeclaredField( clazz, fieldName, true );
}

private GetDeclaredField(Class<?> clazz, String fieldName, boolean makeAccessible) {
this.clazz = clazz;
this.fieldName = fieldName;
this.makeAccessible = makeAccessible;
}

@Override
public Field run() {
try {
final Field field = clazz.getDeclaredField( fieldName );
field.setAccessible( true );
if ( makeAccessible ) {
field.setAccessible( true );
}
return field;
}
catch (NoSuchFieldException e) {
Expand Down
Expand Up @@ -18,21 +18,37 @@ public final class GetDeclaredMethod implements PrivilegedAction<Method> {
private final Class<?> clazz;
private final String methodName;
private final Class<?>[] parameterTypes;
private final boolean makeAccessible;

public static GetDeclaredMethod action(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
return new GetDeclaredMethod( clazz, methodName, parameterTypes );
return new GetDeclaredMethod( clazz, methodName, false, parameterTypes );
}

private GetDeclaredMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
/**
* Before using this method, you need to check the {@code HibernateValidatorPermission.ACCESS_PRIVATE_MEMBERS}
* permission against the security manager.
*/
public static GetDeclaredMethod andMakeAccessible(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
return new GetDeclaredMethod( clazz, methodName, true, parameterTypes );
}

private GetDeclaredMethod(Class<?> clazz, String methodName, boolean makeAccessible, Class<?>... parameterTypes) {
this.clazz = clazz;
this.methodName = methodName;
this.parameterTypes = parameterTypes;
this.makeAccessible = makeAccessible;
}

@Override
public Method run() {
try {
return clazz.getDeclaredMethod( methodName, parameterTypes );
Method method = clazz.getDeclaredMethod( methodName, parameterTypes );

if ( makeAccessible ) {
method.setAccessible( true );
}

return method;
}
catch (NoSuchMethodException e) {
return null;
Expand Down
Expand Up @@ -17,6 +17,10 @@
public final class SetAccessibility implements PrivilegedAction<Object> {
private final Member member;

/**
* Before using this method, you need to check the {@code HibernateValidatorPermission.ACCESS_PRIVATE_MEMBERS}
* permission against the security manager.
*/
public static SetAccessibility action(Member member) {
return new SetAccessibility( member );
}
Expand Down

0 comments on commit b52f1fb

Please sign in to comment.