Skip to content

Commit 719a5e4

Browse files
committed
HHH-18976 Forbid any future new uses of Array.newInstance
1 parent 4115f97 commit 719a5e4

32 files changed

+108
-1
lines changed

hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentArrayHolder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.hibernate.engine.spi.SharedSessionContractImplementor;
1919
import org.hibernate.internal.CoreLogging;
2020
import org.hibernate.internal.CoreMessageLogger;
21+
import org.hibernate.internal.build.AllowReflection;
2122
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
2223
import org.hibernate.persister.collection.CollectionPersister;
2324
import org.hibernate.type.Type;
@@ -34,6 +35,7 @@
3435
* @author Gavin King
3536
*/
3637
@Incubating
38+
@AllowReflection // We need the ability to create arrays of the same type as in the model.
3739
public class PersistentArrayHolder<E> extends AbstractPersistentCollection<E> {
3840
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( PersistentArrayHolder.class );
3941

hibernate-core/src/main/java/org/hibernate/dialect/JsonHelper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Objects;
1919

2020
import org.hibernate.Internal;
21+
import org.hibernate.internal.build.AllowReflection;
2122
import org.hibernate.internal.util.CharSequenceHelper;
2223
import org.hibernate.internal.util.collections.ArrayHelper;
2324
import org.hibernate.metamodel.mapping.EmbeddableMappingType;
@@ -1612,6 +1613,7 @@ public Object[] toArray() {
16121613
}
16131614

16141615
@Override
1616+
@AllowReflection // We need the ability to create arrays of requested types dynamically.
16151617
public <T> T[] toArray(T[] a) {
16161618
//noinspection unchecked
16171619
final T[] r = a.length >= size

hibernate-core/src/main/java/org/hibernate/dialect/function/array/DdlTypeHelper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import org.hibernate.dialect.Dialect;
1212
import org.hibernate.engine.jdbc.Size;
13+
import org.hibernate.internal.build.AllowReflection;
1314
import org.hibernate.metamodel.mapping.JdbcMappingContainer;
1415
import org.hibernate.metamodel.mapping.SqlTypedMapping;
1516
import org.hibernate.metamodel.model.domain.DomainType;
@@ -24,6 +25,7 @@
2425

2526
public class DdlTypeHelper {
2627
@SuppressWarnings("unchecked")
28+
@AllowReflection
2729
public static BasicType<?> resolveArrayType(DomainType<?> elementType, TypeConfiguration typeConfiguration) {
2830
@SuppressWarnings("unchecked") final BasicPluralJavaType<Object> arrayJavaType = (BasicPluralJavaType<Object>) typeConfiguration.getJavaTypeRegistry()
2931
.getDescriptor(

hibernate-core/src/main/java/org/hibernate/dialect/function/array/JsonArrayViaElementArgumentReturnTypeResolver.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.List;
99
import java.util.function.Supplier;
1010

11+
import org.hibernate.internal.build.AllowReflection;
1112
import org.hibernate.metamodel.mapping.BasicValuedMapping;
1213
import org.hibernate.metamodel.mapping.MappingModelExpressible;
1314
import org.hibernate.metamodel.model.domain.DomainType;
@@ -78,6 +79,7 @@ public BasicValuedMapping resolveFunctionReturnType(
7879
return null;
7980
}
8081

82+
@AllowReflection
8183
public static <T> BasicType<?> resolveJsonArrayType(DomainType<T> elementType, TypeConfiguration typeConfiguration) {
8284
final Class<?> arrayClass = Array.newInstance( elementType.getBindableJavaType(), 0 ).getClass();
8385
@SuppressWarnings("unchecked")

hibernate-core/src/main/java/org/hibernate/event/service/internal/EventListenerGroupImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.hibernate.event.service.spi.EventListenerRegistrationException;
2323
import org.hibernate.event.service.spi.JpaBootstrapSensitive;
2424
import org.hibernate.event.spi.EventType;
25+
import org.hibernate.internal.build.AllowReflection;
2526
import org.hibernate.jpa.event.spi.CallbackRegistry;
2627
import org.hibernate.jpa.event.spi.CallbackRegistryConsumer;
2728

@@ -350,6 +351,7 @@ private void handleListenerAddition(T listener, Consumer<T> additionHandler) {
350351
}
351352

352353
@SuppressWarnings("unchecked")
354+
@AllowReflection // Possible array types are registered in org.hibernate.graalvm.internal.StaticClassLists.typesNeedingArrayCopy
353355
private T[] createListenerArrayForWrite(int len) {
354356
return (T[]) Array.newInstance( eventType.baseListenerInterface(), len );
355357
}

hibernate-core/src/main/java/org/hibernate/event/service/internal/EventListenerRegistryImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.hibernate.event.service.spi.EventListenerRegistrationException;
3939
import org.hibernate.event.service.spi.EventListenerRegistry;
4040
import org.hibernate.event.spi.EventType;
41+
import org.hibernate.internal.build.AllowReflection;
4142
import org.hibernate.jpa.event.spi.CallbackRegistry;
4243

4344
import static org.hibernate.event.spi.EventType.AUTO_FLUSH;
@@ -122,6 +123,7 @@ public final <T> void setListeners(EventType<T> type, Class<? extends T>... list
122123
}
123124

124125
@SafeVarargs
126+
@AllowReflection // Possible array types are registered in org.hibernate.graalvm.internal.StaticClassLists.typesNeedingArrayCopy
125127
private <T> T[] resolveListenerInstances(EventType<T> type, Class<? extends T>... listenerClasses) {
126128
@SuppressWarnings("unchecked")
127129
T[] listeners = (T[]) Array.newInstance( type.baseListenerInterface(), listenerClasses.length );
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.internal.build;
6+
7+
import java.lang.annotation.Retention;
8+
import java.lang.annotation.RetentionPolicy;
9+
import java.lang.annotation.Target;
10+
11+
import static java.lang.annotation.ElementType.CONSTRUCTOR;
12+
import static java.lang.annotation.ElementType.METHOD;
13+
import static java.lang.annotation.ElementType.TYPE;
14+
15+
@Retention( RetentionPolicy.CLASS )
16+
@Target({ TYPE, METHOD, CONSTRUCTOR })
17+
public @interface AllowReflection {
18+
}

hibernate-core/src/main/java/org/hibernate/internal/util/collections/ArrayHelper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.hibernate.HibernateException;
1919
import org.hibernate.LockMode;
2020
import org.hibernate.LockOptions;
21+
import org.hibernate.internal.build.AllowReflection;
2122
import org.hibernate.type.Type;
2223

2324
public final class ArrayHelper {
@@ -59,6 +60,7 @@ public static int indexOf(Object[] array, int end, Object object) {
5960
}
6061

6162
@SuppressWarnings("unchecked")
63+
@AllowReflection
6264
public static <T> T[] filledArray(T value, Class<T> valueJavaType, int size) {
6365
final T[] array = (T[]) Array.newInstance( valueJavaType, size );
6466
Arrays.fill( array, value );
@@ -202,6 +204,7 @@ public static int[] join(int[] x, int[] y) {
202204
}
203205

204206
@SuppressWarnings("unchecked")
207+
@AllowReflection
205208
public static <T> T[] join(T[] x, T... y) {
206209
T[] result = (T[]) Array.newInstance( x.getClass().getComponentType(), x.length + y.length );
207210
System.arraycopy( x, 0, result, 0, x.length );
@@ -520,6 +523,7 @@ public static <T> void forEach(T[] array, Consumer<T> consumer) {
520523
}
521524

522525
@SuppressWarnings("unchecked")
526+
@AllowReflection
523527
public static <T> T[] newInstance(Class<T> elementType, int length) {
524528
return (T[]) Array.newInstance( elementType, length );
525529
}

hibernate-core/src/main/java/org/hibernate/jpa/event/internal/CallbackRegistryImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import jakarta.persistence.PersistenceException;
1111

12+
import org.hibernate.internal.build.AllowReflection;
1213
import org.hibernate.internal.util.collections.ArrayHelper;
1314
import org.hibernate.internal.util.collections.MapBackedClassValue;
1415
import org.hibernate.internal.util.collections.ReadOnlyMap;
@@ -168,6 +169,7 @@ public static class Builder {
168169
private final Map<Class<?>, Callback[]> postUpdates = new HashMap<>();
169170
private final Map<Class<?>, Callback[]> postLoads = new HashMap<>();
170171

172+
@AllowReflection
171173
public void registerCallbacks(Class<?> entityClass, Callback[] callbacks) {
172174
if ( callbacks != null ) {
173175
for ( Callback callback : callbacks ) {

hibernate-core/src/main/java/org/hibernate/loader/ast/internal/AbstractCollectionBatchLoader.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.hibernate.engine.spi.PersistenceContext;
1212
import org.hibernate.engine.spi.SessionFactoryImplementor;
1313
import org.hibernate.engine.spi.SharedSessionContractImplementor;
14+
import org.hibernate.internal.build.AllowReflection;
1415
import org.hibernate.loader.ast.spi.CollectionBatchLoader;
1516
import org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping;
1617
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
@@ -126,6 +127,7 @@ protected void finishInitializingKey(Object key, SharedSessionContractImplemento
126127

127128
}
128129

130+
@AllowReflection
129131
Object[] resolveKeysToInitialize(Object keyBeingLoaded, SharedSessionContractImplementor session) {
130132
final int length = getDomainBatchSize();
131133
final Object[] keysToInitialize = (Object[]) Array.newInstance(

0 commit comments

Comments
 (0)