Skip to content

Commit 7d3531d

Browse files
committed
HHH-18976 Forbid any future new uses of Array.newInstance
1 parent ac37839 commit 7d3531d

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(

hibernate-core/src/main/java/org/hibernate/loader/ast/internal/AbstractMultiIdEntityLoader.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.EntityKey;
1212
import org.hibernate.engine.spi.SessionFactoryImplementor;
1313
import org.hibernate.event.spi.EventSource;
14+
import org.hibernate.internal.build.AllowReflection;
1415
import org.hibernate.loader.ast.spi.MultiIdEntityLoader;
1516
import org.hibernate.loader.ast.spi.MultiIdLoadOptions;
1617
import org.hibernate.loader.internal.CacheLoadHelper.PersistenceContextEntry;
@@ -42,6 +43,7 @@ public abstract class AbstractMultiIdEntityLoader<T> implements MultiIdEntityLoa
4243
private final EntityIdentifierMapping identifierMapping;
4344
protected final Object[] idArray;
4445

46+
@AllowReflection
4547
public AbstractMultiIdEntityLoader(EntityMappingType entityDescriptor, SessionFactoryImplementor sessionFactory) {
4648
this.entityDescriptor = entityDescriptor;
4749
this.sessionFactory = sessionFactory;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.hibernate.engine.spi.SessionFactoryImplementor;
1414
import org.hibernate.engine.spi.SharedSessionContractImplementor;
1515
import org.hibernate.engine.spi.SubselectFetch;
16+
import org.hibernate.internal.build.AllowReflection;
1617
import org.hibernate.loader.ast.spi.CollectionBatchLoader;
1718
import org.hibernate.loader.ast.spi.SqlArrayMultiKeyLoader;
1819
import org.hibernate.metamodel.mapping.ForeignKeyDescriptor;
@@ -51,6 +52,7 @@ public class CollectionBatchLoaderArrayParam
5152
private final SelectStatement sqlSelect;
5253
private final JdbcOperationQuerySelect jdbcSelectOperation;
5354

55+
@AllowReflection
5456
public CollectionBatchLoaderArrayParam(
5557
int domainBatchSize,
5658
LoadQueryInfluencers loadQueryInfluencers,
@@ -115,6 +117,7 @@ public PersistentCollection<?> load(Object keyBeingLoaded, SharedSessionContract
115117

116118
}
117119

120+
@AllowReflection
118121
private PersistentCollection<?> loadEmbeddable(
119122
Object keyBeingLoaded,
120123
SharedSessionContractImplementor session,
@@ -216,6 +219,7 @@ void finishInitializingKeys(Object[] keys, SharedSessionContractImplementor sess
216219
}
217220

218221
@Override
222+
@AllowReflection
219223
Object[] resolveKeysToInitialize(Object keyBeingLoaded, SharedSessionContractImplementor session) {
220224
final ForeignKeyDescriptor keyDescriptor = getLoadable().getKeyDescriptor();
221225
if( keyDescriptor.isEmbedded()){

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.hibernate.LockOptions;
1212
import org.hibernate.engine.spi.LoadQueryInfluencers;
1313
import org.hibernate.engine.spi.SharedSessionContractImplementor;
14+
import org.hibernate.internal.build.AllowReflection;
1415
import org.hibernate.loader.ast.spi.SqlArrayMultiKeyLoader;
1516
import org.hibernate.metamodel.mapping.BasicEntityIdentifierMapping;
1617
import org.hibernate.metamodel.mapping.EntityIdentifierMapping;
@@ -58,6 +59,7 @@ public class EntityBatchLoaderArrayParam<T>
5859
* {@link EntityIdentifierMapping} is not available at that time. On first use, we know we
5960
* have it available
6061
*/
62+
@AllowReflection
6163
public EntityBatchLoaderArrayParam(
6264
int domainBatchSize,
6365
EntityMappingType entityDescriptor,
@@ -106,6 +108,7 @@ public int getDomainBatchSize() {
106108
return domainBatchSize;
107109
}
108110

111+
@AllowReflection
109112
protected Object[] resolveIdsToInitialize(Object pkValue, SharedSessionContractImplementor session) {
110113
//TODO: should this really be different to EntityBatchLoaderInPredicate impl?
111114
final Class<?> idType = identifierMapping.getJavaType().getJavaTypeClass();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.hibernate.event.monitor.spi.EventMonitor;
2222
import org.hibernate.event.spi.EventSource;
2323
import org.hibernate.event.monitor.spi.DiagnosticEvent;
24+
import org.hibernate.internal.build.AllowReflection;
2425
import org.hibernate.loader.LoaderLogging;
2526
import org.hibernate.metamodel.mapping.BasicValuedModelPart;
2627
import org.hibernate.metamodel.mapping.EntityMappingType;
@@ -202,6 +203,7 @@ public static <K> K[] normalizeKeys(
202203
* @param elementClass The type of the array elements. See {@link Class#getComponentType()}
203204
* @param length The length to which the array should be created. This is usually zero for Hibernate uses
204205
*/
206+
@AllowReflection
205207
public static <X> X[] createTypedArray(Class<X> elementClass, @SuppressWarnings("SameParameterValue") int length) {
206208
//noinspection unchecked
207209
return (X[]) Array.newInstance( elementClass, length );

hibernate-core/src/main/java/org/hibernate/sql/results/internal/StandardRowReader.java

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

1111
import org.hibernate.engine.spi.EntityKey;
12+
import org.hibernate.internal.build.AllowReflection;
1213
import org.hibernate.sql.results.graph.DomainResultAssembler;
1314
import org.hibernate.sql.results.graph.Initializer;
1415
import org.hibernate.sql.results.graph.InitializerData;
@@ -130,6 +131,7 @@ public boolean hasCollectionInitializers() {
130131
}
131132

132133
@Override
134+
@AllowReflection
133135
public T readRow(RowProcessingState rowProcessingState) {
134136
coordinateInitializers( rowProcessingState );
135137

hibernate-core/src/main/java/org/hibernate/type/ArrayType.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.hibernate.collection.spi.PersistentCollection;
1717
import org.hibernate.engine.spi.SessionFactoryImplementor;
1818
import org.hibernate.engine.spi.SharedSessionContractImplementor;
19+
import org.hibernate.internal.build.AllowReflection;
1920
import org.hibernate.metamodel.CollectionClassification;
2021
import org.hibernate.persister.collection.CollectionPersister;
2122

@@ -26,6 +27,7 @@
2627
* A type for persistent arrays.
2728
* @author Gavin King
2829
*/
30+
@AllowReflection
2931
public class ArrayType extends CollectionType {
3032

3133
private final Class<?> elementClass;

hibernate-core/src/main/java/org/hibernate/type/descriptor/converter/internal/ArrayConverter.java

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

77
import java.lang.reflect.Array;
88

9+
import org.hibernate.internal.build.AllowReflection;
910
import org.hibernate.type.descriptor.converter.spi.BasicValueConverter;
1011
import org.hibernate.type.descriptor.java.JavaType;
1112

@@ -18,6 +19,7 @@
1819
* @param <T> the unconverted array type
1920
* @param <S> the converted array type
2021
*/
22+
@AllowReflection
2123
public class ArrayConverter<T, S, E, F> implements BasicValueConverter<T, S> {
2224

2325
private final BasicValueConverter<E, F> elementConverter;

hibernate-core/src/main/java/org/hibernate/type/descriptor/converter/internal/CollectionConverter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.lang.reflect.Array;
88
import java.util.Collection;
99

10+
import org.hibernate.internal.build.AllowReflection;
1011
import org.hibernate.type.descriptor.converter.spi.BasicValueConverter;
1112
import org.hibernate.type.descriptor.java.JavaType;
1213
import org.hibernate.type.descriptor.java.spi.BasicCollectionJavaType;
@@ -43,6 +44,7 @@ public X toDomainValue(Y relationalForm) {
4344
}
4445

4546
@Override
47+
@AllowReflection
4648
public Y toRelationalValue(X domainForm) {
4749
if ( domainForm == null ) {
4850
return null;

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/AbstractArrayJavaType.java

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

99
import org.hibernate.MappingException;
1010
import org.hibernate.dialect.Dialect;
11+
import org.hibernate.internal.build.AllowReflection;
1112
import org.hibernate.tool.schema.extract.spi.ColumnTypeInformation;
1213
import org.hibernate.type.descriptor.converter.internal.ArrayConverter;
1314
import org.hibernate.type.BasicArrayType;
@@ -20,6 +21,7 @@
2021
import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators;
2122
import org.hibernate.type.spi.TypeConfiguration;
2223

24+
@AllowReflection
2325
public abstract class AbstractArrayJavaType<T, E> extends AbstractClassJavaType<T>
2426
implements BasicPluralJavaType<E> {
2527

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ArrayJavaType.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.hibernate.dialect.Dialect;
1515
import org.hibernate.engine.jdbc.BinaryStream;
1616
import org.hibernate.engine.jdbc.internal.ArrayBackedBinaryStream;
17+
import org.hibernate.internal.build.AllowReflection;
1718
import org.hibernate.internal.util.SerializationHelper;
1819
import org.hibernate.tool.schema.extract.spi.ColumnTypeInformation;
1920
import org.hibernate.type.BasicPluralType;
@@ -29,6 +30,7 @@
2930
* @author Christian Beikov
3031
* @author Jordan Gigov
3132
*/
33+
@AllowReflection
3234
public class ArrayJavaType<T> extends AbstractArrayJavaType<T[], T> {
3335

3436
public ArrayJavaType(BasicType<T> baseDescriptor) {
@@ -376,6 +378,7 @@ private T[] fromBytes(byte[] bytes) {
376378
}
377379
}
378380

381+
@AllowReflection
379382
private static class ArrayMutabilityPlan<T> implements MutabilityPlan<T[]> {
380383

381384
private final Class<T> componentClass;

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ArrayMutabilityPlan.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright Red Hat Inc. and Hibernate Authors
44
*/
55
package org.hibernate.type.descriptor.java;
6+
import org.hibernate.internal.build.AllowReflection;
7+
68
import java.lang.reflect.Array;
79

810
/**
@@ -15,6 +17,7 @@ public class ArrayMutabilityPlan<T> extends MutableMutabilityPlan<T> {
1517
public static final ArrayMutabilityPlan INSTANCE = new ArrayMutabilityPlan();
1618

1719
@SuppressWarnings({ "unchecked", "SuspiciousSystemArraycopy" })
20+
@AllowReflection
1821
public T deepCopyNotNull(T value) {
1922
if ( ! value.getClass().isArray() ) {
2023
// ugh! cannot find a way to properly define the type signature here

0 commit comments

Comments
 (0)