HHH-12189 - Only call setAccessible() when member is not accessible

This commit is contained in:
Steve Ebersole 2017-12-27 09:53:07 -06:00
parent d7d55f4e87
commit 300fa80016
15 changed files with 45 additions and 21 deletions

View File

@ -28,6 +28,7 @@ public class HCANNHelper {
final Class<?> javaXMemberClass = JavaXMember.class;
try {
getMemberMethod = javaXMemberClass.getDeclaredMethod( "getMember" );
// NOTE : no need to check accessibility here - we know it is protected
getMemberMethod.setAccessible( true );
}
catch (NoSuchMethodException e) {

View File

@ -52,7 +52,6 @@ public class BeanValidationIntegrator implements Integrator {
final Class activatorClass = BeanValidationIntegrator.class.getClassLoader().loadClass( ACTIVATOR_CLASS_NAME );
try {
final Method validateMethod = activatorClass.getMethod( VALIDATE_SUPPLIED_FACTORY_METHOD_NAME, Object.class );
validateMethod.setAccessible( true );
try {
validateMethod.invoke( null, object );
}

View File

@ -7,6 +7,7 @@
package org.hibernate.internal.util;
import java.beans.Introspector;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
@ -273,7 +274,7 @@ public final class ReflectHelper {
try {
Constructor<T> constructor = clazz.getDeclaredConstructor( NO_PARAM_SIGNATURE );
constructor.setAccessible( true );
ensureAccessibility( constructor );
return constructor;
}
catch ( NoSuchMethodException nme ) {
@ -333,7 +334,7 @@ public final class ReflectHelper {
}
if ( found ) {
numberOfMatchingConstructors ++;
candidate.setAccessible( true );
ensureAccessibility( candidate );
constructor = candidate;
}
}
@ -376,10 +377,19 @@ public final class ReflectHelper {
);
}
field.setAccessible( true );
ensureAccessibility( field );
return field;
}
public static void ensureAccessibility(AccessibleObject accessibleObject) {
if ( accessibleObject.isAccessible() ) {
return;
}
accessibleObject.setAccessible( true );
}
private static Field locateField(Class clazz, String propertyName) {
if ( clazz == null || Object.class.equals( clazz ) ) {
return null;
@ -423,7 +433,8 @@ public final class ReflectHelper {
);
}
getter.setAccessible( true );
ensureAccessibility( getter );
return getter;
}
@ -599,7 +610,8 @@ public final class ReflectHelper {
);
}
setter.setAccessible( true );
ensureAccessibility( setter );
return setter;
}

View File

@ -24,6 +24,7 @@ import org.hibernate.annotations.common.reflection.ClassLoadingException;
import org.hibernate.annotations.common.reflection.ReflectionManager;
import org.hibernate.annotations.common.reflection.XClass;
import org.hibernate.annotations.common.reflection.XMethod;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.jpa.event.spi.Callback;
import org.hibernate.jpa.event.spi.CallbackBuilder;
import org.hibernate.jpa.event.spi.CallbackType;
@ -102,7 +103,7 @@ public class CallbackBuilderLegacyImpl implements CallbackBuilder {
+ callbackType.getCallbackAnnotation().getName() + " - " + xMethod
);
}
method.setAccessible( true );
ReflectHelper.ensureAccessibility( method );
log.debugf(
"Adding %s as %s callback for entity %s",
methodName,
@ -176,9 +177,7 @@ public class CallbackBuilderLegacyImpl implements CallbackBuilder {
+ callbackType.getCallbackAnnotation().getName() + " - " + method
);
}
if ( !method.isAccessible() ) {
method.setAccessible( true );
}
ReflectHelper.ensureAccessibility( method );
log.debugf(
"Adding %s as %s callback for entity %s",
methodName,

View File

@ -23,6 +23,7 @@ import org.hibernate.HibernateException;
import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.PersistentAttributeInterceptable;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer;
@ -242,7 +243,7 @@ public final class PersistenceUtilHelper {
public FieldAttributeAccess(Field field) {
this.name = field.getName();
try {
field.setAccessible( true );
ReflectHelper.ensureAccessibility( field );
}
catch (Exception e) {
this.field = null;
@ -276,7 +277,7 @@ public final class PersistenceUtilHelper {
public MethodAttributeAccess(String attributeName, Method method) {
this.name = attributeName;
try {
method.setAccessible( true );
ReflectHelper.ensureAccessibility( method );
}
catch (Exception e) {
this.method = null;

View File

@ -25,6 +25,7 @@ import org.hibernate.annotations.common.AssertionFailure;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.EntityManagerMessageLogger;
import org.hibernate.internal.HEMLogging;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.mapping.Component;
import org.hibernate.mapping.KeyValue;
@ -437,7 +438,7 @@ class MetadataContext {
: metamodelClass.getDeclaredField( name );
try {
// should be public anyway, but to be sure...
field.setAccessible( true );
ReflectHelper.ensureAccessibility( field );
field.set( null, attribute );
}
catch (IllegalAccessException e) {

View File

@ -9,6 +9,7 @@ package org.hibernate.property.access.internal;
import java.io.Serializable;
import java.lang.reflect.Field;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.property.access.spi.PropertyAccessSerializationException;
/**
@ -32,7 +33,7 @@ public abstract class AbstractFieldSerialForm implements Serializable {
protected Field resolveField() {
try {
final Field field = declaringClass.getDeclaredField( fieldName );
field.setAccessible( true );
ReflectHelper.ensureAccessibility( field );
return field;
}
catch (NoSuchFieldException e) {

View File

@ -16,6 +16,7 @@ import java.util.Map;
import org.hibernate.PropertyAccessException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.ReflectHelper;
import static org.hibernate.internal.CoreLogging.messageLogger;
@ -122,7 +123,7 @@ public class GetterMethodImpl implements Getter {
private Method resolveMethod() {
try {
final Method method = declaringClass.getDeclaredMethod( methodName );
method.setAccessible( true );
ReflectHelper.ensureAccessibility( method );
return method;
}
catch (NoSuchMethodException e) {

View File

@ -15,6 +15,7 @@ import org.hibernate.PropertyAccessException;
import org.hibernate.PropertySetterAccessException;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.ReflectHelper;
import static org.hibernate.internal.CoreLogging.messageLogger;
@ -146,7 +147,7 @@ public class SetterMethodImpl implements Setter {
private Method resolveMethod() {
try {
final Method method = declaringClass.getDeclaredMethod( methodName, argumentType );
method.setAccessible( true );
ReflectHelper.ensureAccessibility( method );
return method;
}
catch (NoSuchMethodException e) {

View File

@ -14,6 +14,7 @@ import org.hibernate.HibernateException;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.config.spi.StandardConverters;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.jpa.AvailableSettings;
import org.hibernate.resource.beans.spi.ExtendedBeanManager;
import org.hibernate.resource.beans.spi.ManagedBeanRegistry;
@ -63,7 +64,7 @@ public class ManagedBeanRegistryCdiBuilder {
try {
final Constructor<? extends ManagedBeanRegistry> ctor = registryClass.getDeclaredConstructor( ctorArgType );
try {
ctor.setAccessible( true );
ReflectHelper.ensureAccessibility( ctor );
return ctor.newInstance( ctorArgType.cast( beanManagerRef ) );
}
catch (InvocationTargetException e) {

View File

@ -134,7 +134,7 @@ public class ComponentTuplizerFactory implements Serializable {
try {
constructor = clazz.getDeclaredConstructor( COMPONENT_TUP_CTOR_SIG );
try {
constructor.setAccessible( true );
ReflectHelper.ensureAccessibility( constructor );
}
catch ( SecurityException e ) {
constructor = null;

View File

@ -132,7 +132,7 @@ public class EntityTuplizerFactory implements Serializable {
try {
constructor = clazz.getDeclaredConstructor( constructorArgs );
try {
constructor.setAccessible( true );
ReflectHelper.ensureAccessibility( constructor );
}
catch ( SecurityException e ) {
constructor = null;

View File

@ -10,6 +10,8 @@ package org.hibernate.cache.ehcache.management.impl;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.hibernate.internal.util.ReflectHelper;
/**
* Reflective utilities for dealing with backward-incompatible change to statistics types in Hibernate 3.5.
*
@ -85,7 +87,7 @@ public class BeanUtils {
final Field field = getField( bean, propertyName );
if ( field != null ) {
try {
field.setAccessible( true );
ReflectHelper.ensureAccessibility( field );
return field.get( bean );
}
catch (Exception e) {

View File

@ -19,6 +19,7 @@ import org.hibernate.cache.CacheException;
import org.hibernate.cache.ehcache.EhCacheMessageLogger;
import org.hibernate.cfg.Environment;
import org.hibernate.internal.SessionFactoryRegistry;
import org.hibernate.internal.util.ReflectHelper;
import org.jboss.logging.Logger;
@ -128,6 +129,7 @@ public class ProviderMBeanRegistrationHelper {
try {
final Class factoryType = SessionFactoryRegistry.class;
final Field instancesField = getField( factoryType, "sessionFactoryMap" );
// NOTE : no need to check accessibility here - we know it is private
instancesField.setAccessible( true );
final Map map = (Map) instancesField.get( SessionFactoryRegistry.INSTANCE );
if ( map == null ) {
@ -138,6 +140,7 @@ public class ProviderMBeanRegistrationHelper {
final Class sessionFactoryType = sessionFactory.getClass();
final Field propertiesField = getField( sessionFactoryType, "properties" );
if ( propertiesField != null ) {
// NOTE : no need to check accessibility here - we know it is private
propertiesField.setAccessible( true );
final Properties props = (Properties) propertiesField.get( sessionFactory );
if ( props != null && props.equals( properties ) ) {

View File

@ -11,6 +11,8 @@ import org.hibernate.engine.internal.MutableEntityEntryFactory;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.SelfDirtinessTracker;
import org.hibernate.engine.spi.Status;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import java.lang.reflect.Field;
@ -31,7 +33,7 @@ public abstract class EnhancerTestUtils extends BaseUnitTestCase {
public static Object getFieldByReflection(Object entity, String fieldName) {
try {
Field field = entity.getClass().getDeclaredField( fieldName );
field.setAccessible( true );
ReflectHelper.ensureAccessibility( field );
return field.get( entity );
}
catch (NoSuchFieldException | IllegalAccessException e) {