Skip to content

Commit

Permalink
HHH-17999 use Constructor<Expectation> instead of Class<Expectation>
Browse files Browse the repository at this point in the history
for Quarkus

Signed-off-by: Gavin King <gavin@hibernate.org>
  • Loading branch information
gavinking committed Apr 23, 2024
1 parent f423567 commit 0468e04
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
import static org.hibernate.boot.model.internal.BinderHelper.extractFromPackage;
import static org.hibernate.boot.model.source.internal.hbm.ModelBinder.useEntityWhereClauseForCollections;
import static org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle.fromResultCheckStyle;
import static org.hibernate.internal.util.ReflectHelper.getDefaultConstructor;
import static org.hibernate.internal.util.StringHelper.getNonEmptyOrConjunctionIfBothNonEmpty;
import static org.hibernate.internal.util.StringHelper.isEmpty;
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
Expand Down Expand Up @@ -1336,7 +1337,7 @@ private void bindLoader() {
fromResultCheckStyle( sqlInsert.check() )
);
if ( sqlInsert.verify() != Expectation.class ) {
collection.setInsertExpectation( sqlInsert.verify() );
collection.setInsertExpectation( getDefaultConstructor( sqlInsert.verify() ) );
}
}

Expand All @@ -1348,7 +1349,7 @@ private void bindLoader() {
fromResultCheckStyle( sqlUpdate.check() )
);
if ( sqlUpdate.verify() != Expectation.class ) {
collection.setUpdateExpectation( sqlUpdate.verify() );
collection.setUpdateExpectation( getDefaultConstructor( sqlUpdate.verify() ) );
}
}

Expand All @@ -1360,7 +1361,7 @@ private void bindLoader() {
fromResultCheckStyle( sqlDelete.check() )
);
if ( sqlDelete.verify() != Expectation.class ) {
collection.setDeleteExpectation( sqlDelete.verify() );
collection.setDeleteExpectation( getDefaultConstructor( sqlDelete.verify() ) );
}
}

Expand All @@ -1372,7 +1373,7 @@ private void bindLoader() {
fromResultCheckStyle( sqlDeleteAll.check() )
);
if ( sqlDeleteAll.verify() != Expectation.class ) {
collection.setDeleteAllExpectation( sqlDeleteAll.verify() );
collection.setDeleteAllExpectation( getDefaultConstructor( sqlDeleteAll.verify() ) );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
import static org.hibernate.boot.model.naming.Identifier.toIdentifier;
import static org.hibernate.engine.OptimisticLockStyle.fromLockType;
import static org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle.fromResultCheckStyle;
import static org.hibernate.internal.util.ReflectHelper.getDefaultConstructor;
import static org.hibernate.internal.util.StringHelper.isEmpty;
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
import static org.hibernate.internal.util.StringHelper.nullIfEmpty;
Expand Down Expand Up @@ -1390,7 +1391,7 @@ private void bindCustomSql() {
fromResultCheckStyle( sqlInsert.check() )
);
if ( sqlInsert.verify() != Expectation.class ) {
persistentClass.setInsertExpectation( sqlInsert.verify() );
persistentClass.setInsertExpectation( getDefaultConstructor( sqlInsert.verify() ) );
}
}

Expand All @@ -1405,7 +1406,7 @@ private void bindCustomSql() {
fromResultCheckStyle( sqlUpdate.check() )
);
if ( sqlUpdate.verify() != Expectation.class ) {
persistentClass.setUpdateExpectation( sqlUpdate.verify() );
persistentClass.setUpdateExpectation( getDefaultConstructor( sqlUpdate.verify() ) );
}
}

Expand All @@ -1420,7 +1421,7 @@ private void bindCustomSql() {
fromResultCheckStyle( sqlDelete.check() )
);
if ( sqlDelete.verify() != Expectation.class ) {
persistentClass.setDeleteExpectation( sqlDelete.verify() );
persistentClass.setDeleteExpectation( getDefaultConstructor( sqlDelete.verify() ) );
}
}

Expand Down Expand Up @@ -2267,7 +2268,7 @@ private void processSecondaryTableCustomSql(Join join) {
fromResultCheckStyle( sqlInsert.check() )
);
if ( sqlInsert.verify() != Expectation.class ) {
join.setInsertExpectation( sqlInsert.verify() );
join.setInsertExpectation( getDefaultConstructor( sqlInsert.verify() ) );
}
}
else if ( matchingTable != null ) {
Expand All @@ -2290,7 +2291,7 @@ else if ( matchingTable != null ) {
fromResultCheckStyle( sqlUpdate.check() )
);
if ( sqlUpdate.verify() != Expectation.class ) {
join.setUpdateExpectation( sqlUpdate.verify() );
join.setUpdateExpectation( getDefaultConstructor( sqlUpdate.verify() ) );
}
}
else if ( matchingTable != null ) {
Expand All @@ -2313,7 +2314,7 @@ else if ( matchingTable != null ) {
fromResultCheckStyle( sqlDelete.check() )
);
if ( sqlDelete.verify() != Expectation.class ) {
join.setDeleteExpectation( sqlDelete.verify() );
join.setDeleteExpectation( getDefaultConstructor( sqlDelete.verify() ) );
}
}
else if ( matchingTable != null ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.checkerframework.checker.nullness.qual.Nullable;
import org.hibernate.jdbc.Expectation;

import java.lang.reflect.Constructor;

/**
* For persistence operations (INSERT, UPDATE, DELETE) what style of
* determining results (success/failure) is to be used.
Expand Down Expand Up @@ -90,12 +92,17 @@ public static ExecuteUpdateResultCheckStyle determineDefault(@Nullable String cu
return customSql != null && callable ? PARAM : COUNT;
}

public static @Nullable Class<? extends Expectation> expectationClass(@Nullable ExecuteUpdateResultCheckStyle style) {
if ( style == null ) {
return null;
public static @Nullable Constructor<? extends Expectation> expectationConstructor(
@Nullable ExecuteUpdateResultCheckStyle style) {
return style == null ? null : style.expectationConstructor();
}

public Constructor<? extends Expectation> expectationConstructor() {
try {
return expectationClass().getDeclaredConstructor();
}
else {
return style.expectationClass();
catch ( NoSuchMethodException e ) {
throw new AssertionFailure("Could not instantiate Expectation", e);
}
}

Expand Down
31 changes: 20 additions & 11 deletions hibernate-core/src/main/java/org/hibernate/jdbc/Expectations.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package org.hibernate.jdbc;

import java.lang.reflect.Constructor;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;

Expand Down Expand Up @@ -41,19 +42,27 @@ public class Expectations {
* @since 6.5
*/
@Internal
public static Expectation createExpectation(Class<? extends Expectation> expectation, boolean callable) {
if ( expectation == null ) {
expectation = callable ? Expectation.OutParameter.class : Expectation.RowCount.class;
}
final Expectation instance;
try {
instance = expectation.newInstance();
public static Expectation createExpectation(Constructor<? extends Expectation> expectation, boolean callable) {
final Expectation instance = instantiate( expectation, callable );
instance.validate( callable );
return instance;
}

private static Expectation instantiate(Constructor<? extends Expectation> constructor, boolean callable) {
if ( constructor == null ) {
return callable
? new Expectation.OutParameter()
: new Expectation.RowCount();
}
catch ( Exception e ) {
throw new InstantiationException( "Could not instantiate Expectation", expectation, e );
else {
try {
return constructor.newInstance();
}
catch ( Exception e ) {
throw new InstantiationException( "Could not instantiate Expectation",
constructor.getDeclaringClass(), e );
}
}
instance.validate(callable);
return instance;
}

static CallableStatement toCallableStatement(PreparedStatement statement) {
Expand Down
35 changes: 18 additions & 17 deletions hibernate-core/src/main/java/org/hibernate/mapping/Collection.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package org.hibernate.mapping;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
Expand Down Expand Up @@ -37,7 +38,7 @@
import org.hibernate.usertype.UserCollectionType;

import static org.hibernate.internal.util.collections.ArrayHelper.EMPTY_BOOLEAN_ARRAY;
import static org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle.expectationClass;
import static org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle.expectationConstructor;

/**
* A mapping model object representing a collection. Subclasses specialize to particular kinds of collection.
Expand Down Expand Up @@ -107,10 +108,10 @@ public abstract class Collection implements Fetchable, Value, Filterable, SoftDe

private String loaderName;

private Class<? extends Expectation> insertExpectation;
private Class<? extends Expectation> updateExpectation;
private Class<? extends Expectation> deleteExpectation;
private Class<? extends Expectation> deleteAllExpectation;
private Constructor<? extends Expectation> insertExpectation;
private Constructor<? extends Expectation> updateExpectation;
private Constructor<? extends Expectation> deleteExpectation;
private Constructor<? extends Expectation> deleteAllExpectation;

/**
* hbm.xml binding
Expand Down Expand Up @@ -593,7 +594,7 @@ public void setCustomSQLInsert(String customSQLInsert, boolean callable, Execute
this.customSQLInsert = customSQLInsert;
this.customInsertCallable = callable;
this.insertCheckStyle = checkStyle;
this.insertExpectation = expectationClass( checkStyle );
this.insertExpectation = expectationConstructor( checkStyle );
}

public String getCustomSQLInsert() {
Expand All @@ -616,7 +617,7 @@ public void setCustomSQLUpdate(String customSQLUpdate, boolean callable, Execute
this.customSQLUpdate = customSQLUpdate;
this.customUpdateCallable = callable;
this.updateCheckStyle = checkStyle;
this.updateExpectation = expectationClass( checkStyle );
this.updateExpectation = expectationConstructor( checkStyle );
}

public String getCustomSQLUpdate() {
Expand All @@ -639,7 +640,7 @@ public void setCustomSQLDelete(String customSQLDelete, boolean callable, Execute
this.customSQLDelete = customSQLDelete;
this.customDeleteCallable = callable;
this.deleteCheckStyle = checkStyle;
this.deleteExpectation = expectationClass( checkStyle );
this.deleteExpectation = expectationConstructor( checkStyle );
}

public String getCustomSQLDelete() {
Expand All @@ -665,7 +666,7 @@ public void setCustomSQLDeleteAll(
this.customSQLDeleteAll = customSQLDeleteAll;
this.customDeleteAllCallable = callable;
this.deleteAllCheckStyle = checkStyle;
this.deleteAllExpectation = expectationClass( checkStyle );
this.deleteAllExpectation = expectationConstructor( checkStyle );
}

public String getCustomSQLDeleteAll() {
Expand Down Expand Up @@ -876,35 +877,35 @@ public Column getSoftDeleteColumn() {
return softDeleteColumn;
}

public Class<? extends Expectation> getInsertExpectation() {
public Constructor<? extends Expectation> getInsertExpectation() {
return insertExpectation;
}

public void setInsertExpectation(Class<? extends Expectation> insertExpectation) {
public void setInsertExpectation(Constructor<? extends Expectation> insertExpectation) {
this.insertExpectation = insertExpectation;
}

public Class<? extends Expectation> getUpdateExpectation() {
public Constructor<? extends Expectation> getUpdateExpectation() {
return updateExpectation;
}

public void setUpdateExpectation(Class<? extends Expectation> updateExpectation) {
public void setUpdateExpectation(Constructor<? extends Expectation> updateExpectation) {
this.updateExpectation = updateExpectation;
}

public Class<? extends Expectation> getDeleteExpectation() {
public Constructor<? extends Expectation> getDeleteExpectation() {
return deleteExpectation;
}

public void setDeleteExpectation(Class<? extends Expectation> deleteExpectation) {
public void setDeleteExpectation(Constructor<? extends Expectation> deleteExpectation) {
this.deleteExpectation = deleteExpectation;
}

public Class<? extends Expectation> getDeleteAllExpectation() {
public Constructor<? extends Expectation> getDeleteAllExpectation() {
return deleteAllExpectation;
}

public void setDeleteAllExpectation(Class<? extends Expectation> deleteAllExpectation) {
public void setDeleteAllExpectation(Constructor<? extends Expectation> deleteAllExpectation) {
this.deleteAllExpectation = deleteAllExpectation;
}
}
27 changes: 14 additions & 13 deletions hibernate-core/src/main/java/org/hibernate/mapping/Join.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.hibernate.mapping;

import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand All @@ -15,7 +16,7 @@
import org.hibernate.jdbc.Expectation;
import org.hibernate.sql.Alias;

import static org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle.expectationClass;
import static org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle.expectationConstructor;

/**
* A mapping model object representing some sort of auxiliary table, for
Expand Down Expand Up @@ -50,9 +51,9 @@ public class Join implements AttributeContainer, Serializable {
private boolean customDeleteCallable;
private ExecuteUpdateResultCheckStyle deleteCheckStyle;

private Class<? extends Expectation> insertExpectation;
private Class<? extends Expectation> updateExpectation;
private Class<? extends Expectation> deleteExpectation;
private Constructor<? extends Expectation> insertExpectation;
private Constructor<? extends Expectation> updateExpectation;
private Constructor<? extends Expectation> deleteExpectation;

@Override
public void addProperty(Property property) {
Expand Down Expand Up @@ -140,7 +141,7 @@ public void setCustomSQLInsert(String customSQLInsert, boolean callable, Execute
this.customSQLInsert = customSQLInsert;
this.customInsertCallable = callable;
this.insertCheckStyle = checkStyle;
this.insertExpectation = expectationClass( checkStyle );
this.insertExpectation = expectationConstructor( checkStyle );
}

public String getCustomSQLInsert() {
Expand All @@ -163,7 +164,7 @@ public void setCustomSQLUpdate(String customSQLUpdate, boolean callable, Execute
this.customSQLUpdate = customSQLUpdate;
this.customUpdateCallable = callable;
this.updateCheckStyle = checkStyle;
this.updateExpectation = expectationClass( checkStyle );
this.updateExpectation = expectationConstructor( checkStyle );
}

public String getCustomSQLUpdate() {
Expand All @@ -186,7 +187,7 @@ public void setCustomSQLDelete(String customSQLDelete, boolean callable, Execute
this.customSQLDelete = customSQLDelete;
this.customDeleteCallable = callable;
this.deleteCheckStyle = checkStyle;
this.deleteExpectation = expectationClass( checkStyle );
this.deleteExpectation = expectationConstructor( checkStyle );
}

public String getCustomSQLDelete() {
Expand Down Expand Up @@ -234,27 +235,27 @@ public void setOptional(boolean nullable) {
this.optional = nullable;
}

public Class<? extends Expectation> getInsertExpectation() {
public Constructor<? extends Expectation> getInsertExpectation() {
return insertExpectation;
}

public void setInsertExpectation(Class<? extends Expectation> insertExpectation) {
public void setInsertExpectation(Constructor<? extends Expectation> insertExpectation) {
this.insertExpectation = insertExpectation;
}

public Class<? extends Expectation> getUpdateExpectation() {
public Constructor<? extends Expectation> getUpdateExpectation() {
return updateExpectation;
}

public void setUpdateExpectation(Class<? extends Expectation> updateExpectation) {
public void setUpdateExpectation(Constructor<? extends Expectation> updateExpectation) {
this.updateExpectation = updateExpectation;
}

public Class<? extends Expectation> getDeleteExpectation() {
public Constructor<? extends Expectation> getDeleteExpectation() {
return deleteExpectation;
}

public void setDeleteExpectation(Class<? extends Expectation> deleteExpectation) {
public void setDeleteExpectation(Constructor<? extends Expectation> deleteExpectation) {
this.deleteExpectation = deleteExpectation;
}
}

0 comments on commit 0468e04

Please sign in to comment.