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; final Class<?> javaXMemberClass = JavaXMember.class;
try { try {
getMemberMethod = javaXMemberClass.getDeclaredMethod( "getMember" ); getMemberMethod = javaXMemberClass.getDeclaredMethod( "getMember" );
// NOTE : no need to check accessibility here - we know it is protected
getMemberMethod.setAccessible( true ); getMemberMethod.setAccessible( true );
} }
catch (NoSuchMethodException e) { catch (NoSuchMethodException e) {

View File

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

View File

@ -7,6 +7,7 @@
package org.hibernate.internal.util; package org.hibernate.internal.util;
import java.beans.Introspector; import java.beans.Introspector;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Member; import java.lang.reflect.Member;
@ -273,7 +274,7 @@ public final class ReflectHelper {
try { try {
Constructor<T> constructor = clazz.getDeclaredConstructor( NO_PARAM_SIGNATURE ); Constructor<T> constructor = clazz.getDeclaredConstructor( NO_PARAM_SIGNATURE );
constructor.setAccessible( true ); ensureAccessibility( constructor );
return constructor; return constructor;
} }
catch ( NoSuchMethodException nme ) { catch ( NoSuchMethodException nme ) {
@ -333,7 +334,7 @@ public final class ReflectHelper {
} }
if ( found ) { if ( found ) {
numberOfMatchingConstructors ++; numberOfMatchingConstructors ++;
candidate.setAccessible( true ); ensureAccessibility( candidate );
constructor = candidate; constructor = candidate;
} }
} }
@ -376,10 +377,19 @@ public final class ReflectHelper {
); );
} }
field.setAccessible( true ); ensureAccessibility( field );
return field; return field;
} }
public static void ensureAccessibility(AccessibleObject accessibleObject) {
if ( accessibleObject.isAccessible() ) {
return;
}
accessibleObject.setAccessible( true );
}
private static Field locateField(Class clazz, String propertyName) { private static Field locateField(Class clazz, String propertyName) {
if ( clazz == null || Object.class.equals( clazz ) ) { if ( clazz == null || Object.class.equals( clazz ) ) {
return null; return null;
@ -423,7 +433,8 @@ public final class ReflectHelper {
); );
} }
getter.setAccessible( true ); ensureAccessibility( getter );
return getter; return getter;
} }
@ -599,7 +610,8 @@ public final class ReflectHelper {
); );
} }
setter.setAccessible( true ); ensureAccessibility( setter );
return 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.ReflectionManager;
import org.hibernate.annotations.common.reflection.XClass; import org.hibernate.annotations.common.reflection.XClass;
import org.hibernate.annotations.common.reflection.XMethod; 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.Callback;
import org.hibernate.jpa.event.spi.CallbackBuilder; import org.hibernate.jpa.event.spi.CallbackBuilder;
import org.hibernate.jpa.event.spi.CallbackType; import org.hibernate.jpa.event.spi.CallbackType;
@ -102,7 +103,7 @@ public class CallbackBuilderLegacyImpl implements CallbackBuilder {
+ callbackType.getCallbackAnnotation().getName() + " - " + xMethod + callbackType.getCallbackAnnotation().getName() + " - " + xMethod
); );
} }
method.setAccessible( true ); ReflectHelper.ensureAccessibility( method );
log.debugf( log.debugf(
"Adding %s as %s callback for entity %s", "Adding %s as %s callback for entity %s",
methodName, methodName,
@ -176,9 +177,7 @@ public class CallbackBuilderLegacyImpl implements CallbackBuilder {
+ callbackType.getCallbackAnnotation().getName() + " - " + method + callbackType.getCallbackAnnotation().getName() + " - " + method
); );
} }
if ( !method.isAccessible() ) { ReflectHelper.ensureAccessibility( method );
method.setAccessible( true );
}
log.debugf( log.debugf(
"Adding %s as %s callback for entity %s", "Adding %s as %s callback for entity %s",
methodName, methodName,

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -10,6 +10,8 @@ package org.hibernate.cache.ehcache.management.impl;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; 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. * 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 ); final Field field = getField( bean, propertyName );
if ( field != null ) { if ( field != null ) {
try { try {
field.setAccessible( true ); ReflectHelper.ensureAccessibility( field );
return field.get( bean ); return field.get( bean );
} }
catch (Exception e) { catch (Exception e) {

View File

@ -19,6 +19,7 @@ import org.hibernate.cache.CacheException;
import org.hibernate.cache.ehcache.EhCacheMessageLogger; import org.hibernate.cache.ehcache.EhCacheMessageLogger;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.internal.SessionFactoryRegistry; import org.hibernate.internal.SessionFactoryRegistry;
import org.hibernate.internal.util.ReflectHelper;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
@ -128,6 +129,7 @@ public class ProviderMBeanRegistrationHelper {
try { try {
final Class factoryType = SessionFactoryRegistry.class; final Class factoryType = SessionFactoryRegistry.class;
final Field instancesField = getField( factoryType, "sessionFactoryMap" ); final Field instancesField = getField( factoryType, "sessionFactoryMap" );
// NOTE : no need to check accessibility here - we know it is private
instancesField.setAccessible( true ); instancesField.setAccessible( true );
final Map map = (Map) instancesField.get( SessionFactoryRegistry.INSTANCE ); final Map map = (Map) instancesField.get( SessionFactoryRegistry.INSTANCE );
if ( map == null ) { if ( map == null ) {
@ -138,6 +140,7 @@ public class ProviderMBeanRegistrationHelper {
final Class sessionFactoryType = sessionFactory.getClass(); final Class sessionFactoryType = sessionFactory.getClass();
final Field propertiesField = getField( sessionFactoryType, "properties" ); final Field propertiesField = getField( sessionFactoryType, "properties" );
if ( propertiesField != null ) { if ( propertiesField != null ) {
// NOTE : no need to check accessibility here - we know it is private
propertiesField.setAccessible( true ); propertiesField.setAccessible( true );
final Properties props = (Properties) propertiesField.get( sessionFactory ); final Properties props = (Properties) propertiesField.get( sessionFactory );
if ( props != null && props.equals( properties ) ) { 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.EntityEntry;
import org.hibernate.engine.spi.SelfDirtinessTracker; import org.hibernate.engine.spi.SelfDirtinessTracker;
import org.hibernate.engine.spi.Status; import org.hibernate.engine.spi.Status;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.testing.junit4.BaseUnitTestCase; import org.hibernate.testing.junit4.BaseUnitTestCase;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -31,7 +33,7 @@ public abstract class EnhancerTestUtils extends BaseUnitTestCase {
public static Object getFieldByReflection(Object entity, String fieldName) { public static Object getFieldByReflection(Object entity, String fieldName) {
try { try {
Field field = entity.getClass().getDeclaredField( fieldName ); Field field = entity.getClass().getDeclaredField( fieldName );
field.setAccessible( true ); ReflectHelper.ensureAccessibility( field );
return field.get( entity ); return field.get( entity );
} }
catch (NoSuchFieldException | IllegalAccessException e) { catch (NoSuchFieldException | IllegalAccessException e) {