Skip to content

Commit 78a62ac

Browse files
committed
HHH-5289 remove unnecessary security checks in property accessors
1 parent 7bd5ef0 commit 78a62ac

File tree

11 files changed

+37
-61
lines changed

11 files changed

+37
-61
lines changed

hibernate-core/src/main/java/org/hibernate/cfg/beanvalidation/BeanValidationIntegrator.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ public static void validateFactory(Object object) {
6969
final Class activatorClass = BeanValidationIntegrator.class.getClassLoader().loadClass( ACTIVATOR_CLASS_NAME );
7070
try {
7171
final Method validateMethod = activatorClass.getMethod( VALIDATE_SUPPLIED_FACTORY_METHOD_NAME, Object.class );
72-
if ( ! validateMethod.isAccessible() ) {
73-
validateMethod.setAccessible( true );
74-
}
72+
validateMethod.setAccessible( true );
7573
try {
7674
validateMethod.invoke( null, object );
7775
}

hibernate-core/src/main/java/org/hibernate/internal/util/ReflectHelper.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,7 @@ public static <T> Constructor<T> getDefaultConstructor(Class<T> clazz) throws Pr
303303

304304
try {
305305
Constructor<T> constructor = clazz.getDeclaredConstructor( NO_PARAM_SIGNATURE );
306-
if ( !isPublic( clazz, constructor ) ) {
307-
constructor.setAccessible( true );
308-
}
306+
constructor.setAccessible( true );
309307
return constructor;
310308
}
311309
catch ( NoSuchMethodException nme ) {
@@ -363,9 +361,7 @@ public static Constructor getConstructor(Class clazz, Type[] types) throws Prope
363361
}
364362
}
365363
if ( found ) {
366-
if ( !isPublic( clazz, constructor ) ) {
367-
constructor.setAccessible( true );
368-
}
364+
constructor.setAccessible( true );
369365
return constructor;
370366
}
371367
}

hibernate-core/src/main/java/org/hibernate/property/BasicPropertyAccessor.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ private static BasicSetter getSetterOrNull(Class theClass, String propertyName)
260260
Method method = setterMethod(theClass, propertyName);
261261

262262
if (method!=null) {
263-
if ( !ReflectHelper.isPublic(theClass, method) ) method.setAccessible(true);
263+
method.setAccessible(true);
264264
return new BasicSetter(theClass, method, propertyName);
265265
}
266266
else {
@@ -325,9 +325,7 @@ private static BasicGetter getGetterOrNull(Class theClass, String propertyName)
325325
Method method = getterMethod(theClass, propertyName);
326326

327327
if (method!=null) {
328-
if ( !ReflectHelper.isPublic( theClass, method ) ) {
329-
method.setAccessible(true);
330-
}
328+
method.setAccessible(true);
331329
return new BasicGetter(theClass, method, propertyName);
332330
}
333331
else {

hibernate-core/src/main/java/org/hibernate/property/DirectPropertyAccessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ private static Field getField(Class clazz, String name) throws PropertyNotFoundE
157157
catch (NoSuchFieldException nsfe) {
158158
field = getField( clazz, clazz.getSuperclass(), name );
159159
}
160-
if ( !ReflectHelper.isPublic(clazz, field) ) field.setAccessible(true);
160+
field.setAccessible(true);
161161
return field;
162162
}
163163

@@ -172,7 +172,7 @@ private static Field getField(Class root, Class clazz, String name) throws Prope
172172
catch (NoSuchFieldException nsfe) {
173173
field = getField( root, clazz.getSuperclass(), name );
174174
}
175-
if ( !ReflectHelper.isPublic(clazz, field) ) field.setAccessible(true);
175+
field.setAccessible(true);
176176
return field;
177177
}
178178

hibernate-core/src/main/java/org/hibernate/proxy/pojo/javassist/JavassistLazyInitializer.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,7 @@ public Object invoke(
192192
returnValue = thisMethod.invoke( target, args );
193193
}
194194
else {
195-
if ( !thisMethod.isAccessible() ) {
196-
thisMethod.setAccessible( true );
197-
}
195+
thisMethod.setAccessible( true );
198196
returnValue = thisMethod.invoke( target, args );
199197
}
200198

hibernate-core/src/main/java/org/hibernate/tuple/component/ComponentTuplizerFactory.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,11 @@ private Constructor<? extends ComponentTuplizer> getProperConstructor(Class<? ex
135135
Constructor<? extends ComponentTuplizer> constructor = null;
136136
try {
137137
constructor = clazz.getDeclaredConstructor( COMPONENT_TUP_CTOR_SIG );
138-
if ( ! ReflectHelper.isPublic( constructor ) ) {
139-
try {
140-
// found a constructor, but it was not publicly accessible so try to request accessibility
141-
constructor.setAccessible( true );
142-
}
143-
catch ( SecurityException e ) {
144-
constructor = null;
145-
}
138+
try {
139+
constructor.setAccessible( true );
140+
}
141+
catch ( SecurityException e ) {
142+
constructor = null;
146143
}
147144
}
148145
catch ( NoSuchMethodException ignore ) {

hibernate-core/src/main/java/org/hibernate/tuple/entity/EntityTuplizerFactory.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,11 @@ private Constructor<? extends EntityTuplizer> getProperConstructor(
227227
Constructor<? extends EntityTuplizer> constructor = null;
228228
try {
229229
constructor = clazz.getDeclaredConstructor( constructorArgs );
230-
if ( ! ReflectHelper.isPublic( constructor ) ) {
231-
try {
232-
// found a constructor, but it was not publicly accessible so try to request accessibility
233-
constructor.setAccessible( true );
234-
}
235-
catch ( SecurityException e ) {
236-
constructor = null;
237-
}
230+
try {
231+
constructor.setAccessible( true );
232+
}
233+
catch ( SecurityException e ) {
234+
constructor = null;
238235
}
239236
}
240237
catch ( NoSuchMethodException ignore ) {

hibernate-entitymanager/src/main/java/org/hibernate/jpa/event/internal/jpa/CallbackProcessorImpl.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,7 @@ private Callback createListenerCallback(
132132
if (argType != Object.class && argType != entityClass) {
133133
continue;
134134
}
135-
if (!method.isAccessible()) {
136-
method.setAccessible( true );
137-
}
135+
method.setAccessible( true );
138136

139137
return new ListenerCallback( listenerInstance, method );
140138
}
@@ -151,7 +149,7 @@ private Callback createBeanCallback( Class<?> callbackClass,
151149
for (Method method : callbackClass.getDeclaredMethods()) {
152150
if (!method.getName().equals(methodName)) continue;
153151
if (method.getParameterTypes().length != 0) continue;
154-
if (!method.isAccessible()) method.setAccessible(true);
152+
method.setAccessible(true);
155153
return new EntityCallback(method);
156154
}
157155
return null;

hibernate-entitymanager/src/main/java/org/hibernate/jpa/event/internal/jpa/LegacyCallbackProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public Callback[] resolveCallbacks(XClass beanClass, Class annotation, Reflectio
104104
.getName() + " - " + xMethod
105105
);
106106
}
107-
if (!method.isAccessible()) method.setAccessible(true);
107+
method.setAccessible(true);
108108
log.debugf("Adding %s as %s callback for entity %s",
109109
methodName,
110110
annotation.getSimpleName(),

hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/metamodel/MetadataContext.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,8 @@ private <X> void registerAttribute(Class metamodelClass, Attribute<X, ?> attribu
428428
? metamodelClass.getField( name )
429429
: metamodelClass.getDeclaredField( name );
430430
try {
431-
if ( ! field.isAccessible() ) {
432-
// should be public anyway, but to be sure...
433-
field.setAccessible( true );
434-
}
431+
// should be public anyway, but to be sure...
432+
field.setAccessible( true );
435433
field.set( null, attribute );
436434
}
437435
catch ( IllegalAccessException e ) {

0 commit comments

Comments
 (0)