Skip to content

Commit

Permalink
HHH-18976 Forbid any future new uses of Array.newInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere committed Jan 14, 2025
1 parent ac37839 commit 7d3531d
Show file tree
Hide file tree
Showing 32 changed files with 108 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.build.AllowReflection;
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.type.Type;
Expand All @@ -34,6 +35,7 @@
* @author Gavin King
*/
@Incubating
@AllowReflection // We need the ability to create arrays of the same type as in the model.
public class PersistentArrayHolder<E> extends AbstractPersistentCollection<E> {
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( PersistentArrayHolder.class );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Objects;

import org.hibernate.Internal;
import org.hibernate.internal.build.AllowReflection;
import org.hibernate.internal.util.CharSequenceHelper;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.metamodel.mapping.EmbeddableMappingType;
Expand Down Expand Up @@ -1612,6 +1613,7 @@ public Object[] toArray() {
}

@Override
@AllowReflection // We need the ability to create arrays of requested types dynamically.
public <T> T[] toArray(T[] a) {
//noinspection unchecked
final T[] r = a.length >= size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.Size;
import org.hibernate.internal.build.AllowReflection;
import org.hibernate.metamodel.mapping.JdbcMappingContainer;
import org.hibernate.metamodel.mapping.SqlTypedMapping;
import org.hibernate.metamodel.model.domain.DomainType;
Expand All @@ -24,6 +25,7 @@

public class DdlTypeHelper {
@SuppressWarnings("unchecked")
@AllowReflection
public static BasicType<?> resolveArrayType(DomainType<?> elementType, TypeConfiguration typeConfiguration) {
@SuppressWarnings("unchecked") final BasicPluralJavaType<Object> arrayJavaType = (BasicPluralJavaType<Object>) typeConfiguration.getJavaTypeRegistry()
.getDescriptor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;
import java.util.function.Supplier;

import org.hibernate.internal.build.AllowReflection;
import org.hibernate.metamodel.mapping.BasicValuedMapping;
import org.hibernate.metamodel.mapping.MappingModelExpressible;
import org.hibernate.metamodel.model.domain.DomainType;
Expand Down Expand Up @@ -78,6 +79,7 @@ public BasicValuedMapping resolveFunctionReturnType(
return null;
}

@AllowReflection
public static <T> BasicType<?> resolveJsonArrayType(DomainType<T> elementType, TypeConfiguration typeConfiguration) {
final Class<?> arrayClass = Array.newInstance( elementType.getBindableJavaType(), 0 ).getClass();
@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.hibernate.event.service.spi.EventListenerRegistrationException;
import org.hibernate.event.service.spi.JpaBootstrapSensitive;
import org.hibernate.event.spi.EventType;
import org.hibernate.internal.build.AllowReflection;
import org.hibernate.jpa.event.spi.CallbackRegistry;
import org.hibernate.jpa.event.spi.CallbackRegistryConsumer;

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

@SuppressWarnings("unchecked")
@AllowReflection // Possible array types are registered in org.hibernate.graalvm.internal.StaticClassLists.typesNeedingArrayCopy
private T[] createListenerArrayForWrite(int len) {
return (T[]) Array.newInstance( eventType.baseListenerInterface(), len );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.hibernate.event.service.spi.EventListenerRegistrationException;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventType;
import org.hibernate.internal.build.AllowReflection;
import org.hibernate.jpa.event.spi.CallbackRegistry;

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

@SafeVarargs
@AllowReflection // Possible array types are registered in org.hibernate.graalvm.internal.StaticClassLists.typesNeedingArrayCopy
private <T> T[] resolveListenerInstances(EventType<T> type, Class<? extends T>... listenerClasses) {
@SuppressWarnings("unchecked")
T[] listeners = (T[]) Array.newInstance( type.baseListenerInterface(), listenerClasses.length );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.internal.build;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;

@Retention( RetentionPolicy.CLASS )
@Target({ TYPE, METHOD, CONSTRUCTOR })
public @interface AllowReflection {
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.internal.build.AllowReflection;
import org.hibernate.type.Type;

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

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

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

@SuppressWarnings("unchecked")
@AllowReflection
public static <T> T[] newInstance(Class<T> elementType, int length) {
return (T[]) Array.newInstance( elementType, length );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import jakarta.persistence.PersistenceException;

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

@AllowReflection
public void registerCallbacks(Class<?> entityClass, Callback[] callbacks) {
if ( callbacks != null ) {
for ( Callback callback : callbacks ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.build.AllowReflection;
import org.hibernate.loader.ast.spi.CollectionBatchLoader;
import org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping;
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
Expand Down Expand Up @@ -126,6 +127,7 @@ protected void finishInitializingKey(Object key, SharedSessionContractImplemento

}

@AllowReflection
Object[] resolveKeysToInitialize(Object keyBeingLoaded, SharedSessionContractImplementor session) {
final int length = getDomainBatchSize();
final Object[] keysToInitialize = (Object[]) Array.newInstance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.event.spi.EventSource;
import org.hibernate.internal.build.AllowReflection;
import org.hibernate.loader.ast.spi.MultiIdEntityLoader;
import org.hibernate.loader.ast.spi.MultiIdLoadOptions;
import org.hibernate.loader.internal.CacheLoadHelper.PersistenceContextEntry;
Expand Down Expand Up @@ -42,6 +43,7 @@ public abstract class AbstractMultiIdEntityLoader<T> implements MultiIdEntityLoa
private final EntityIdentifierMapping identifierMapping;
protected final Object[] idArray;

@AllowReflection
public AbstractMultiIdEntityLoader(EntityMappingType entityDescriptor, SessionFactoryImplementor sessionFactory) {
this.entityDescriptor = entityDescriptor;
this.sessionFactory = sessionFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.engine.spi.SubselectFetch;
import org.hibernate.internal.build.AllowReflection;
import org.hibernate.loader.ast.spi.CollectionBatchLoader;
import org.hibernate.loader.ast.spi.SqlArrayMultiKeyLoader;
import org.hibernate.metamodel.mapping.ForeignKeyDescriptor;
Expand Down Expand Up @@ -51,6 +52,7 @@ public class CollectionBatchLoaderArrayParam
private final SelectStatement sqlSelect;
private final JdbcOperationQuerySelect jdbcSelectOperation;

@AllowReflection
public CollectionBatchLoaderArrayParam(
int domainBatchSize,
LoadQueryInfluencers loadQueryInfluencers,
Expand Down Expand Up @@ -115,6 +117,7 @@ public PersistentCollection<?> load(Object keyBeingLoaded, SharedSessionContract

}

@AllowReflection
private PersistentCollection<?> loadEmbeddable(
Object keyBeingLoaded,
SharedSessionContractImplementor session,
Expand Down Expand Up @@ -216,6 +219,7 @@ void finishInitializingKeys(Object[] keys, SharedSessionContractImplementor sess
}

@Override
@AllowReflection
Object[] resolveKeysToInitialize(Object keyBeingLoaded, SharedSessionContractImplementor session) {
final ForeignKeyDescriptor keyDescriptor = getLoadable().getKeyDescriptor();
if( keyDescriptor.isEmbedded()){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.hibernate.LockOptions;
import org.hibernate.engine.spi.LoadQueryInfluencers;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.build.AllowReflection;
import org.hibernate.loader.ast.spi.SqlArrayMultiKeyLoader;
import org.hibernate.metamodel.mapping.BasicEntityIdentifierMapping;
import org.hibernate.metamodel.mapping.EntityIdentifierMapping;
Expand Down Expand Up @@ -58,6 +59,7 @@ public class EntityBatchLoaderArrayParam<T>
* {@link EntityIdentifierMapping} is not available at that time. On first use, we know we
* have it available
*/
@AllowReflection
public EntityBatchLoaderArrayParam(
int domainBatchSize,
EntityMappingType entityDescriptor,
Expand Down Expand Up @@ -106,6 +108,7 @@ public int getDomainBatchSize() {
return domainBatchSize;
}

@AllowReflection
protected Object[] resolveIdsToInitialize(Object pkValue, SharedSessionContractImplementor session) {
//TODO: should this really be different to EntityBatchLoaderInPredicate impl?
final Class<?> idType = identifierMapping.getJavaType().getJavaTypeClass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.hibernate.event.monitor.spi.EventMonitor;
import org.hibernate.event.spi.EventSource;
import org.hibernate.event.monitor.spi.DiagnosticEvent;
import org.hibernate.internal.build.AllowReflection;
import org.hibernate.loader.LoaderLogging;
import org.hibernate.metamodel.mapping.BasicValuedModelPart;
import org.hibernate.metamodel.mapping.EntityMappingType;
Expand Down Expand Up @@ -202,6 +203,7 @@ public static <K> K[] normalizeKeys(
* @param elementClass The type of the array elements. See {@link Class#getComponentType()}
* @param length The length to which the array should be created. This is usually zero for Hibernate uses
*/
@AllowReflection
public static <X> X[] createTypedArray(Class<X> elementClass, @SuppressWarnings("SameParameterValue") int length) {
//noinspection unchecked
return (X[]) Array.newInstance( elementClass, length );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.List;

import org.hibernate.engine.spi.EntityKey;
import org.hibernate.internal.build.AllowReflection;
import org.hibernate.sql.results.graph.DomainResultAssembler;
import org.hibernate.sql.results.graph.Initializer;
import org.hibernate.sql.results.graph.InitializerData;
Expand Down Expand Up @@ -130,6 +131,7 @@ public boolean hasCollectionInitializers() {
}

@Override
@AllowReflection
public T readRow(RowProcessingState rowProcessingState) {
coordinateInitializers( rowProcessingState );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.build.AllowReflection;
import org.hibernate.metamodel.CollectionClassification;
import org.hibernate.persister.collection.CollectionPersister;

Expand All @@ -26,6 +27,7 @@
* A type for persistent arrays.
* @author Gavin King
*/
@AllowReflection
public class ArrayType extends CollectionType {

private final Class<?> elementClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.lang.reflect.Array;

import org.hibernate.internal.build.AllowReflection;
import org.hibernate.type.descriptor.converter.spi.BasicValueConverter;
import org.hibernate.type.descriptor.java.JavaType;

Expand All @@ -18,6 +19,7 @@
* @param <T> the unconverted array type
* @param <S> the converted array type
*/
@AllowReflection
public class ArrayConverter<T, S, E, F> implements BasicValueConverter<T, S> {

private final BasicValueConverter<E, F> elementConverter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.lang.reflect.Array;
import java.util.Collection;

import org.hibernate.internal.build.AllowReflection;
import org.hibernate.type.descriptor.converter.spi.BasicValueConverter;
import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.java.spi.BasicCollectionJavaType;
Expand Down Expand Up @@ -43,6 +44,7 @@ public X toDomainValue(Y relationalForm) {
}

@Override
@AllowReflection
public Y toRelationalValue(X domainForm) {
if ( domainForm == null ) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.hibernate.MappingException;
import org.hibernate.dialect.Dialect;
import org.hibernate.internal.build.AllowReflection;
import org.hibernate.tool.schema.extract.spi.ColumnTypeInformation;
import org.hibernate.type.descriptor.converter.internal.ArrayConverter;
import org.hibernate.type.BasicArrayType;
Expand All @@ -20,6 +21,7 @@
import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators;
import org.hibernate.type.spi.TypeConfiguration;

@AllowReflection
public abstract class AbstractArrayJavaType<T, E> extends AbstractClassJavaType<T>
implements BasicPluralJavaType<E> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.BinaryStream;
import org.hibernate.engine.jdbc.internal.ArrayBackedBinaryStream;
import org.hibernate.internal.build.AllowReflection;
import org.hibernate.internal.util.SerializationHelper;
import org.hibernate.tool.schema.extract.spi.ColumnTypeInformation;
import org.hibernate.type.BasicPluralType;
Expand All @@ -29,6 +30,7 @@
* @author Christian Beikov
* @author Jordan Gigov
*/
@AllowReflection
public class ArrayJavaType<T> extends AbstractArrayJavaType<T[], T> {

public ArrayJavaType(BasicType<T> baseDescriptor) {
Expand Down Expand Up @@ -376,6 +378,7 @@ private T[] fromBytes(byte[] bytes) {
}
}

@AllowReflection
private static class ArrayMutabilityPlan<T> implements MutabilityPlan<T[]> {

private final Class<T> componentClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.type.descriptor.java;
import org.hibernate.internal.build.AllowReflection;

import java.lang.reflect.Array;

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

@SuppressWarnings({ "unchecked", "SuspiciousSystemArraycopy" })
@AllowReflection
public T deepCopyNotNull(T value) {
if ( ! value.getClass().isArray() ) {
// ugh! cannot find a way to properly define the type signature here
Expand Down
Loading

0 comments on commit 7d3531d

Please sign in to comment.