Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HHH-18875 Stop using Array.newInstance in org.hibernate.internal.util.collections.StandardStack #9309

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public static void parseInto(

private final SessionFactoryImplementor sessionFactory;

private final Stack<GraphImplementor> graphStack = new StandardStack<>( GraphImplementor.class );
private final Stack<AttributeNodeImplementor> attributeNodeStack = new StandardStack<>( AttributeNodeImplementor.class );
private final Stack<SubGraphGenerator> graphSourceStack = new StandardStack<>(SubGraphGenerator.class);
private final Stack<GraphImplementor> graphStack = new StandardStack<>();
private final Stack<AttributeNodeImplementor> attributeNodeStack = new StandardStack<>();
private final Stack<SubGraphGenerator> graphSourceStack = new StandardStack<>();

public GraphParser(SessionFactoryImplementor sessionFactory) {
this.sessionFactory = sessionFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/
package org.hibernate.internal.util.collections;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.function.BiFunction;
Expand All @@ -21,24 +20,34 @@
* @author Marco Belladelli
*/
public final class StandardStack<T> implements Stack<T> {
private T[] elements;

private Object[] elements;
private int top = 0;

private Class<T> type;
public StandardStack() {
}

public StandardStack(T initialValue) {
push( initialValue );
}

/**
* @deprecated use the default constructor instead
*/
@Deprecated(forRemoval = true)
public StandardStack(Class<T> type) {
this.type = type;
}

/**
* @deprecated use {@link #StandardStack(Object)} instead.
*/
@Deprecated(forRemoval = true)
public StandardStack(Class<T> type, T initial) {
this( type );
push( initial );
}

@SuppressWarnings("unchecked")
private void init() {
elements = (T[]) Array.newInstance( type, 8 );
type = null;
elements = new Object[8];
}

@Override
Expand All @@ -57,7 +66,7 @@ public T pop() {
if ( isEmpty() ) {
throw new NoSuchElementException();
}
T e = elements[--top];
T e = (T) elements[--top];
elements[top] = null;
return e;
}
Expand All @@ -67,23 +76,23 @@ public T getCurrent() {
if ( isEmpty() ) {
return null;
}
return elements[top - 1];
return (T) elements[top - 1];
}

@Override
public T peek(int offsetFromTop) {
if ( isEmpty() ) {
return null;
}
return elements[top - offsetFromTop - 1];
return (T) elements[top - offsetFromTop - 1];
}

@Override
public T getRoot() {
if ( isEmpty() ) {
return null;
}
return elements[0];
return (T) elements[0];
}

@Override
Expand All @@ -107,14 +116,14 @@ public void clear() {
@Override
public void visitRootFirst(Consumer<T> action) {
for ( int i = 0; i < top; i++ ) {
action.accept( elements[i] );
action.accept( (T) elements[i] );
}
}

@Override
public <X> X findCurrentFirst(Function<T, X> function) {
for ( int i = top - 1; i >= 0; i-- ) {
final X result = function.apply( elements[i] );
final X result = function.apply( (T) elements[i] );
if ( result != null ) {
return result;
}
Expand All @@ -125,7 +134,7 @@ public <X> X findCurrentFirst(Function<T, X> function) {
@Override
public <X, Y> X findCurrentFirstWithParameter(Y parameter, BiFunction<T, Y, X> biFunction) {
for ( int i = top - 1; i >= 0; i-- ) {
final X result = biFunction.apply( elements[i], parameter );
final X result = biFunction.apply( (T) elements[i], parameter );
if ( result != null ) {
return result;
}
Expand All @@ -138,4 +147,5 @@ private void grow() {
final int jump = ( oldCapacity < 64 ) ? ( oldCapacity + 2 ) : ( oldCapacity >> 1 );
elements = Arrays.copyOf( elements, oldCapacity + jump );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ public static <R> SqmStatement<R> buildSemanticModel(

private final Stack<DotIdentifierConsumer> dotIdentifierConsumerStack;

private final Stack<ParameterDeclarationContext> parameterDeclarationContextStack = new StandardStack<>( ParameterDeclarationContext.class );
private final Stack<SqmCreationProcessingState> processingStateStack = new StandardStack<>( SqmCreationProcessingState.class );
private final Stack<ParameterDeclarationContext> parameterDeclarationContextStack = new StandardStack<>();
private final Stack<SqmCreationProcessingState> processingStateStack = new StandardStack<>();

private final BasicDomainType<Integer> integerDomainType;
private final JavaType<List<?>> listJavaType;
Expand Down Expand Up @@ -382,7 +382,6 @@ private SemanticQueryBuilder(
this.creationContext = creationContext;
this.query = query;
this.dotIdentifierConsumerStack = new StandardStack<>(
DotIdentifierConsumer.class,
new BasicDotIdentifierConsumer( this )
);
this.parameterStyle = creationOptions.useStrictJpaCompliance()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public class DomainResultCreationStateImpl
private final LegacyFetchResolver legacyFetchResolver;
private final SessionFactoryImplementor sessionFactory;

private final Stack<Function> fetchBuilderResolverStack = new StandardStack<>( Function.class, fetchableName -> null );
private final Stack<Function> fetchBuilderResolverStack = new StandardStack<>( fetchableName -> null );
private Map<String, LockMode> registeredLockModes;
private boolean processingKeyFetches = false;
private boolean resolvingCircularFetch;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
private boolean deduplicateSelectionItems;
private ForeignKeyDescriptor.Nature currentlyResolvingForeignKeySide;
private SqmStatement<?> currentSqmStatement;
private Stack<SqmQueryPart> sqmQueryPartStack = new StandardStack<>( SqmQueryPart.class );
private Stack<SqmQueryPart> sqmQueryPartStack = new StandardStack<>();
private CteContainer cteContainer;
/**
* A map from {@link SqmCteTable#getCteName()} to the final SQL name.
Expand All @@ -506,8 +506,8 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
private List<Map.Entry<OrderByFragment, TableGroup>> orderByFragments;

private final SqlAliasBaseManager sqlAliasBaseManager = new SqlAliasBaseManager();
private final Stack<SqlAstProcessingState> processingStateStack = new StandardStack<>( SqlAstProcessingState.class );
private final Stack<FromClauseIndex> fromClauseIndexStack = new StandardStack<>( FromClauseIndex.class );
private final Stack<SqlAstProcessingState> processingStateStack = new StandardStack<>();
private final Stack<FromClauseIndex> fromClauseIndexStack = new StandardStack<>();

// Captures all entity name uses under which a table group is being used within the current conjunct.
// Outside a top level conjunct, it represents the "global uses" i.e. select, from, group and order by clauses.
Expand All @@ -521,9 +521,9 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
private SqmJoin<?, ?> currentlyProcessingJoin;
protected Predicate additionalRestrictions;

private final Stack<Clause> currentClauseStack = new StandardStack<>( Clause.class );
private final Stack<Supplier> inferrableTypeAccessStack = new StandardStack<>( Supplier.class );
private final Stack<List> queryTransformers = new StandardStack<>( List.class );
private final Stack<Clause> currentClauseStack = new StandardStack<>();
private final Stack<Supplier> inferrableTypeAccessStack = new StandardStack<>();
private final Stack<List> queryTransformers = new StandardStack<>();
private boolean inTypeInference;
private boolean inImpliedResultTypeInference;
private boolean inNestedContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,9 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
private final ParameterMarkerStrategy parameterMarkerStrategy;


private final Stack<Clause> clauseStack = new StandardStack<>( Clause.class );
private final Stack<QueryPart> queryPartStack = new StandardStack<>( QueryPart.class );
private final Stack<Statement> statementStack = new StandardStack<>( Statement.class );
private final Stack<Clause> clauseStack = new StandardStack<>();
private final Stack<QueryPart> queryPartStack = new StandardStack<>();
private final Stack<Statement> statementStack = new StandardStack<>();

private final Dialect dialect;
private final Set<String> affectedTableNames = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static void logDomainResultGraph(String header, List<DomainResult<?>> dom
}

private final StringBuilder buffer;
private final Stack<FetchParent> fetchParentStack = new StandardStack<>( FetchParent.class );
private final Stack<FetchParent> fetchParentStack = new StandardStack<>();

private DomainResultGraphPrinter(String header) {
buffer = new StringBuilder( header + ":" + System.lineSeparator() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class LoadContexts {
private static final CoreMessageLogger log = CoreLogging.messageLogger( LoadContexts.class );

private final PersistenceContext persistenceContext;
private final StandardStack<JdbcValuesSourceProcessingState> jdbcValuesSourceProcessingStateStack = new StandardStack<>( JdbcValuesSourceProcessingState.class );
private final StandardStack<JdbcValuesSourceProcessingState> jdbcValuesSourceProcessingStateStack = new StandardStack<>();

public LoadContexts(PersistenceContext persistenceContext) {
this.persistenceContext = persistenceContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void testFindCurrentFirstWithParameterCleared() {
// utility functions

private Stack<Integer> allocateStack(int size) {
final Stack<Integer> stack = new StandardStack<>( Integer.class );
final Stack<Integer> stack = new StandardStack<>();
for ( int i = 0; i < size; i++ ) {
stack.push( i );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,7 @@
*/
package org.hibernate.graalvm.internal;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

import org.hibernate.graph.internal.parse.SubGraphGenerator;
import org.hibernate.graph.spi.AttributeNodeImplementor;
import org.hibernate.graph.spi.GraphImplementor;
import org.hibernate.query.hql.spi.DotIdentifierConsumer;
import org.hibernate.query.hql.spi.SqmCreationProcessingState;
import org.hibernate.query.sqm.spi.ParameterDeclarationContext;
import org.hibernate.query.sqm.sql.FromClauseIndex;
import org.hibernate.sql.ast.Clause;
import org.hibernate.sql.ast.spi.SqlAstProcessingState;
import org.hibernate.sql.ast.tree.Statement;
import org.hibernate.sql.ast.tree.select.QueryPart;
import org.hibernate.sql.results.graph.FetchParent;
import org.hibernate.sql.results.jdbc.spi.JdbcValuesSourceProcessingState;
import org.hibernate.tool.schema.internal.script.MultiLineSqlScriptExtractor;
import org.hibernate.type.EnumType;

Expand Down Expand Up @@ -94,25 +77,6 @@ public static Class[] typesNeedingArrayCopy() {
org.hibernate.event.spi.PostCollectionRecreateEventListener[].class,
org.hibernate.event.spi.PostCollectionRemoveEventListener[].class,
org.hibernate.event.spi.PostCollectionUpdateEventListener[].class,
//And other array types, necessary for allocation of generified instances of org.hibernate.internal.util.collections.StandardStack:
//TODO can this list be tested for consistency with the core module? Or generated? e.g. could use Jandex?
AttributeNodeImplementor[].class,
Clause[].class,
DotIdentifierConsumer[].class,
FetchParent[].class,
FromClauseIndex[].class,
Function[].class,
GraphImplementor[].class,
JdbcValuesSourceProcessingState[].class,
List[].class,
Map.Entry[].class,
ParameterDeclarationContext[].class,
QueryPart[].class,
SqlAstProcessingState[].class,
SqmCreationProcessingState[].class,
Statement[].class,
SubGraphGenerator[].class,
Supplier[].class,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,27 +158,7 @@ Stream<Class<?>> classes() {
// Putting anything here is running the risk of forgetting
// why it was necessary in the first place...
return Stream.of(
// Java classes -- the why is lost to history
java.util.function.Function[].class,
java.util.List[].class,
java.util.Map.Entry[].class,
java.util.function.Supplier[].class,
// Graphs -- the why is lost to history
org.hibernate.graph.spi.AttributeNodeImplementor[].class,
org.hibernate.sql.results.graph.FetchParent[].class,
org.hibernate.graph.spi.GraphImplementor[].class,
org.hibernate.graph.internal.parse.SubGraphGenerator[].class,
// AST/parsing -- no way to detect this automatically, you just have to know.
org.hibernate.sql.ast.Clause[].class,
org.hibernate.query.hql.spi.DotIdentifierConsumer[].class,
org.hibernate.query.sqm.sql.FromClauseIndex[].class,
org.hibernate.query.sqm.spi.ParameterDeclarationContext[].class,
org.hibernate.sql.ast.tree.select.QueryPart[].class,
org.hibernate.sql.ast.spi.SqlAstProcessingState[].class,
org.hibernate.query.hql.spi.SqmCreationProcessingState[].class,
org.hibernate.sql.ast.tree.Statement[].class,
// Various internals -- the why is lost to history
org.hibernate.sql.results.jdbc.spi.JdbcValuesSourceProcessingState[].class
// Hopefully to remain empty
);
}
};
Expand Down