mirror of
https://github.com/hibernate/hibernate-orm
synced 2025-02-16 08:05:05 +00:00
HHH-12189 - Only call setAccessible() when member is not accessible
This commit is contained in:
parent
d7d55f4e87
commit
300fa80016
@ -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) {
|
||||
|
@ -52,7 +52,6 @@ public static void validateFactory(Object object) {
|
||||
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 );
|
||||
}
|
||||
|
@ -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 static <T> Constructor<T> getDefaultConstructor(Class<T> clazz) throws Pr
|
||||
|
||||
try {
|
||||
Constructor<T> constructor = clazz.getDeclaredConstructor( NO_PARAM_SIGNATURE );
|
||||
constructor.setAccessible( true );
|
||||
ensureAccessibility( constructor );
|
||||
return constructor;
|
||||
}
|
||||
catch ( NoSuchMethodException nme ) {
|
||||
@ -333,7 +334,7 @@ public static Constructor getConstructor(Class clazz, Type[] types) throws Prope
|
||||
}
|
||||
if ( found ) {
|
||||
numberOfMatchingConstructors ++;
|
||||
candidate.setAccessible( true );
|
||||
ensureAccessibility( candidate );
|
||||
constructor = candidate;
|
||||
}
|
||||
}
|
||||
@ -376,10 +377,19 @@ else if ( containerClass == Object.class ) {
|
||||
);
|
||||
}
|
||||
|
||||
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 static Method findGetterMethod(Class containerClass, String propertyName)
|
||||
);
|
||||
}
|
||||
|
||||
getter.setAccessible( true );
|
||||
ensureAccessibility( getter );
|
||||
|
||||
return getter;
|
||||
}
|
||||
|
||||
@ -599,7 +610,8 @@ public static Method findSetterMethod(Class containerClass, String propertyName,
|
||||
);
|
||||
}
|
||||
|
||||
setter.setAccessible( true );
|
||||
ensureAccessibility( setter );
|
||||
|
||||
return setter;
|
||||
}
|
||||
|
||||
|
@ -24,6 +24,7 @@
|
||||
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 Callback[] resolveCallbacks(XClass beanClass, CallbackType callbackType,
|
||||
+ 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 Callback[] resolveCallbacks(XClass beanClass, CallbackType callbackType,
|
||||
+ callbackType.getCallbackAnnotation().getName() + " - " + method
|
||||
);
|
||||
}
|
||||
if ( !method.isAccessible() ) {
|
||||
method.setAccessible( true );
|
||||
}
|
||||
ReflectHelper.ensureAccessibility( method );
|
||||
log.debugf(
|
||||
"Adding %s as %s callback for entity %s",
|
||||
methodName,
|
||||
|
@ -23,6 +23,7 @@
|
||||
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 static class FieldAttributeAccess implements AttributeAccess {
|
||||
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 static class MethodAttributeAccess implements AttributeAccess {
|
||||
public MethodAttributeAccess(String attributeName, Method method) {
|
||||
this.name = attributeName;
|
||||
try {
|
||||
method.setAccessible( true );
|
||||
ReflectHelper.ensureAccessibility( method );
|
||||
}
|
||||
catch (Exception e) {
|
||||
this.method = null;
|
||||
|
@ -25,6 +25,7 @@
|
||||
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 @@ private <X> void registerAttribute(Class metamodelClass, Attribute<X, ?> attribu
|
||||
: 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) {
|
||||
|
@ -9,6 +9,7 @@
|
||||
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 @@ protected AbstractFieldSerialForm(Class declaringClass, String fieldName) {
|
||||
protected Field resolveField() {
|
||||
try {
|
||||
final Field field = declaringClass.getDeclaredField( fieldName );
|
||||
field.setAccessible( true );
|
||||
ReflectHelper.ensureAccessibility( field );
|
||||
return field;
|
||||
}
|
||||
catch (NoSuchFieldException e) {
|
||||
|
@ -16,6 +16,7 @@
|
||||
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 @@ private Object readResolve() {
|
||||
private Method resolveMethod() {
|
||||
try {
|
||||
final Method method = declaringClass.getDeclaredMethod( methodName );
|
||||
method.setAccessible( true );
|
||||
ReflectHelper.ensureAccessibility( method );
|
||||
return method;
|
||||
}
|
||||
catch (NoSuchMethodException e) {
|
||||
|
@ -15,6 +15,7 @@
|
||||
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 @@ private Object readResolve() {
|
||||
private Method resolveMethod() {
|
||||
try {
|
||||
final Method method = declaringClass.getDeclaredMethod( methodName, argumentType );
|
||||
method.setAccessible( true );
|
||||
ReflectHelper.ensureAccessibility( method );
|
||||
return method;
|
||||
}
|
||||
catch (NoSuchMethodException e) {
|
||||
|
@ -14,6 +14,7 @@
|
||||
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 static ManagedBeanRegistry fromBeanManagerReference(
|
||||
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) {
|
||||
|
@ -134,7 +134,7 @@ private Constructor<? extends ComponentTuplizer> getProperConstructor(Class<? ex
|
||||
try {
|
||||
constructor = clazz.getDeclaredConstructor( COMPONENT_TUP_CTOR_SIG );
|
||||
try {
|
||||
constructor.setAccessible( true );
|
||||
ReflectHelper.ensureAccessibility( constructor );
|
||||
}
|
||||
catch ( SecurityException e ) {
|
||||
constructor = null;
|
||||
|
@ -132,7 +132,7 @@ private Constructor<? extends EntityTuplizer> getProperConstructor(
|
||||
try {
|
||||
constructor = clazz.getDeclaredConstructor( constructorArgs );
|
||||
try {
|
||||
constructor.setAccessible( true );
|
||||
ReflectHelper.ensureAccessibility( constructor );
|
||||
}
|
||||
catch ( SecurityException e ) {
|
||||
constructor = null;
|
||||
|
@ -10,6 +10,8 @@
|
||||
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 static Object getBeanProperty(Object bean, String propertyName) {
|
||||
final Field field = getField( bean, propertyName );
|
||||
if ( field != null ) {
|
||||
try {
|
||||
field.setAccessible( true );
|
||||
ReflectHelper.ensureAccessibility( field );
|
||||
return field.get( bean );
|
||||
}
|
||||
catch (Exception e) {
|
||||
|
@ -19,6 +19,7 @@
|
||||
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 @@ private SessionFactory locateSessionFactory() {
|
||||
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 @@ private SessionFactory locateSessionFactory() {
|
||||
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 ) ) {
|
||||
|
@ -11,6 +11,8 @@
|
||||
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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user