very minor code cleanups

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-09-04 19:32:54 +02:00
parent dfe6a09f31
commit 7c30bbed2b
4 changed files with 41 additions and 54 deletions

View File

@ -11,7 +11,6 @@ import java.math.BigInteger;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator;
import java.util.List; import java.util.List;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
@ -28,7 +27,7 @@ public class NamingHelper {
public static final NamingHelper INSTANCE = new NamingHelper(); public static final NamingHelper INSTANCE = new NamingHelper();
public static NamingHelper withCharset(String charset) { public static NamingHelper withCharset(String charset) {
return new NamingHelper(charset); return new NamingHelper( charset );
} }
private final String charset; private final String charset;
@ -69,28 +68,17 @@ public class NamingHelper {
Identifier tableName, Identifier tableName,
Identifier referencedTableName, Identifier referencedTableName,
Identifier... columnNames) { Identifier... columnNames) {
// Use a concatenation that guarantees uniqueness, even if identical names // Use a concatenation that guarantees uniqueness, even if identical
// exist between all table and column identifiers. // names exist between all table and column identifiers.
final StringBuilder sb = new StringBuilder()
StringBuilder sb = new StringBuilder()
.append( "table`" ).append( tableName ).append( "`" ) .append( "table`" ).append( tableName ).append( "`" )
.append( "references`" ).append( referencedTableName ).append( "`" ); .append( "references`" ).append( referencedTableName ).append( "`" );
// Ensure a consistent ordering of columns, regardless of the order // Ensure a consistent ordering of columns, regardless of the order
// they were bound. // they were bound.
// Clone the list, as sometimes a set of order-dependent Column // Clone the list, as sometimes a set of order-dependent Column
// bindings are given. // bindings are given.
Identifier[] alphabeticalColumns = columnNames.clone(); final Identifier[] alphabeticalColumns = columnNames.clone();
Arrays.sort( Arrays.sort( alphabeticalColumns, comparing( Identifier::getCanonicalName ) );
alphabeticalColumns,
new Comparator<Identifier>() {
@Override
public int compare(Identifier o1, Identifier o2) {
return o1.getCanonicalName().compareTo( o2.getCanonicalName() );
}
}
);
for ( Identifier columnName : alphabeticalColumns ) { for ( Identifier columnName : alphabeticalColumns ) {
sb.append( "column`" ).append( columnName ).append( "`" ); sb.append( "column`" ).append( columnName ).append( "`" );
} }
@ -103,17 +91,16 @@ public class NamingHelper {
* *
* @return String The generated name * @return String The generated name
*/ */
public String generateHashedConstraintName(String prefix, Identifier tableName, Identifier... columnNames ) { public String generateHashedConstraintName(
// Use a concatenation that guarantees uniqueness, even if identical names String prefix, Identifier tableName, Identifier... columnNames ) {
// exist between all table and column identifiers. // Use a concatenation that guarantees uniqueness, even if identical
// names exist between all table and column identifiers.
StringBuilder sb = new StringBuilder( "table`" + tableName + "`" ); final StringBuilder sb = new StringBuilder( "table`" + tableName + "`" );
// Ensure a consistent ordering of columns, regardless of the order // Ensure a consistent ordering of columns, regardless of the order
// they were bound. // they were bound.
// Clone the list, as sometimes a set of order-dependent Column // Clone the list, as sometimes a set of order-dependent Column
// bindings are given. // bindings are given.
Identifier[] alphabeticalColumns = columnNames.clone(); final Identifier[] alphabeticalColumns = columnNames.clone();
Arrays.sort( alphabeticalColumns, comparing(Identifier::getCanonicalName) ); Arrays.sort( alphabeticalColumns, comparing(Identifier::getCanonicalName) );
for ( Identifier columnName : alphabeticalColumns ) { for ( Identifier columnName : alphabeticalColumns ) {
sb.append( "column`" ).append( columnName ).append( "`" ); sb.append( "column`" ).append( columnName ).append( "`" );
@ -127,8 +114,9 @@ public class NamingHelper {
* *
* @return String The generated name * @return String The generated name
*/ */
public String generateHashedConstraintName(String prefix, Identifier tableName, List<Identifier> columnNames) { public String generateHashedConstraintName(
Identifier[] columnNamesArray = new Identifier[columnNames.size()]; String prefix, Identifier tableName, List<Identifier> columnNames) {
final Identifier[] columnNamesArray = new Identifier[columnNames.size()];
for ( int i = 0; i < columnNames.size(); i++ ) { for ( int i = 0; i < columnNames.size(); i++ ) {
columnNamesArray[i] = columnNames.get( i ); columnNamesArray[i] = columnNames.get( i );
} }
@ -141,23 +129,22 @@ public class NamingHelper {
* that the length of the name will always be smaller than the 30 * that the length of the name will always be smaller than the 30
* character identifier restriction enforced by a few dialects. * character identifier restriction enforced by a few dialects.
* *
* @param s The name to be hashed. * @param name The name to be hashed.
* *
* @return String The hashed name. * @return String The hashed name.
*/ */
public String hashedName(String s) { public String hashedName(String name) {
try { try {
MessageDigest md = MessageDigest.getInstance( "MD5" ); final MessageDigest md5 = MessageDigest.getInstance( "MD5" );
md.reset(); md5.reset();
md.update( charset != null ? s.getBytes( charset ) : s.getBytes() ); md5.update( charset != null ? name.getBytes( charset ) : name.getBytes() );
byte[] digest = md.digest(); final BigInteger bigInt = new BigInteger( 1, md5.digest() );
BigInteger bigInt = new BigInteger( 1, digest );
// By converting to base 35 (full alphanumeric), we guarantee // By converting to base 35 (full alphanumeric), we guarantee
// that the length of the name will always be smaller than the 30 // that the length of the name will always be smaller than the 30
// character identifier restriction enforced by a few dialects. // character identifier restriction enforced by a few dialects.
return bigInt.toString( 35 ); return bigInt.toString( 35 );
} }
catch ( NoSuchAlgorithmException|UnsupportedEncodingException e ) { catch ( NoSuchAlgorithmException | UnsupportedEncodingException e ) {
throw new HibernateException( "Unable to generate a hashed name", e ); throw new HibernateException( "Unable to generate a hashed name", e );
} }
} }

View File

@ -589,9 +589,7 @@ public class HibernateProcessor extends AbstractProcessor {
&& alreadyExistingMetaEntity == null && alreadyExistingMetaEntity == null
// let a handwritten metamodel "override" the generated one // let a handwritten metamodel "override" the generated one
// (this is used in the Jakarta Data TCK) // (this is used in the Jakarta Data TCK)
&& element.getEnclosingElement().getEnclosedElements() && !hasHandwrittenMetamodel(element) ) {
.stream().noneMatch(e -> e.getSimpleName()
.contentEquals('_' + element.getSimpleName().toString()))) {
final AnnotationMetaEntity dataMetaEntity = final AnnotationMetaEntity dataMetaEntity =
AnnotationMetaEntity.create( typeElement, context, AnnotationMetaEntity.create( typeElement, context,
requiresLazyMemberInitialization, requiresLazyMemberInitialization,
@ -608,6 +606,12 @@ public class HibernateProcessor extends AbstractProcessor {
} }
} }
private static boolean hasHandwrittenMetamodel(Element element) {
return element.getEnclosingElement().getEnclosedElements()
.stream().anyMatch(e -> e.getSimpleName()
.contentEquals('_' + element.getSimpleName().toString()));
}
private void indexEntityName(TypeElement typeElement) { private void indexEntityName(TypeElement typeElement) {
final AnnotationMirror mirror = getAnnotationMirror( typeElement, ENTITY ); final AnnotationMirror mirror = getAnnotationMirror( typeElement, ENTITY );
if ( mirror != null ) { if ( mirror != null ) {

View File

@ -250,12 +250,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
@Override @Override
public @Nullable String getSupertypeName() { public @Nullable String getSupertypeName() {
if ( repository ) { return repository ? null : findMappedSuperClass( this, context );
return null;
}
else {
return findMappedSuperClass( this, context );
}
} }
@Override @Override
@ -275,7 +270,6 @@ public class AnnotationMetaEntity extends AnnotationMeta {
mergeInMembers( entityToMerge.getMembers() ); mergeInMembers( entityToMerge.getMembers() );
} }
} }
return new ArrayList<>( members.values() ); return new ArrayList<>( members.values() );
} }
@ -289,7 +283,6 @@ public class AnnotationMetaEntity extends AnnotationMeta {
// propagate types to be imported // propagate types to be imported
importType( attribute.getMetaType() ); importType( attribute.getMetaType() );
importType( attribute.getTypeDeclaration() ); importType( attribute.getTypeDeclaration() );
members.put( attribute.getPropertyName(), attribute ); members.put( attribute.getPropertyName(), attribute );
} }
} }
@ -298,7 +291,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
// store the entity in order do the merge lazily in case of // store the entity in order do the merge lazily in case of
// an uninitialized embeddedable or mapped superclass // an uninitialized embeddedable or mapped superclass
if ( !initialized ) { if ( !initialized ) {
this.entityToMerge = other; entityToMerge = other;
} }
else { else {
mergeInMembers( other.getMembers() ); mergeInMembers( other.getMembers() );
@ -1133,7 +1126,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
addQueryMethod( method, returnType, null ); addQueryMethod( method, returnType, null );
} }
else if ( kind == TypeKind.DECLARED ) { else if ( kind == TypeKind.DECLARED ) {
final DeclaredType declaredType = (DeclaredType) ununiIfPossible(method, returnType); final DeclaredType declaredType = (DeclaredType) unUniIfPossible(method, returnType);
final TypeElement typeElement = (TypeElement) declaredType.asElement(); final TypeElement typeElement = (TypeElement) declaredType.asElement();
final List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments(); final List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
switch ( typeArguments.size() ) { switch ( typeArguments.size() ) {
@ -1165,7 +1158,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
} }
} }
private TypeMirror ununiIfPossible(ExecutableElement method, TypeMirror returnType) { private TypeMirror unUniIfPossible(ExecutableElement method, TypeMirror returnType) {
final TypeMirror result = ununi( returnType ); final TypeMirror result = ununi( returnType );
if ( repository ) { if ( repository ) {
if ( usingReactiveSession( sessionType ) ) { if ( usingReactiveSession( sessionType ) ) {
@ -1324,7 +1317,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
} }
private void addLifecycleMethod(ExecutableElement method) { private void addLifecycleMethod(ExecutableElement method) {
final TypeMirror returnType = ununiIfPossible( method, method.getReturnType() ); final TypeMirror returnType = unUniIfPossible( method, method.getReturnType() );
if ( method.getParameters().size() != 1 ) { if ( method.getParameters().size() != 1 ) {
message( method, message( method,
"must have exactly one parameter", "must have exactly one parameter",

View File

@ -15,7 +15,7 @@ import org.checkerframework.checker.nullness.qual.Nullable;
* @author Hardy Ferentschik * @author Hardy Ferentschik
*/ */
public class AccessTypeInformation { public class AccessTypeInformation {
private final String fqcn; private final String fullyQualifiedName;
/** /**
* Access type explicitly specified in xml or on an entity. * Access type explicitly specified in xml or on an entity.
@ -30,8 +30,11 @@ public class AccessTypeInformation {
static final AccessType DEFAULT_ACCESS_TYPE = AccessType.FIELD; static final AccessType DEFAULT_ACCESS_TYPE = AccessType.FIELD;
public AccessTypeInformation(String fqcn, @Nullable AccessType explicitAccessType, @Nullable AccessType defaultAccessType) { public AccessTypeInformation(
this.fqcn = fqcn; String fullyQualifiedName,
@Nullable AccessType explicitAccessType,
@Nullable AccessType defaultAccessType) {
this.fullyQualifiedName = fullyQualifiedName;
this.explicitAccessType = explicitAccessType; this.explicitAccessType = explicitAccessType;
this.defaultAccessType = defaultAccessType; this.defaultAccessType = defaultAccessType;
} }
@ -69,7 +72,7 @@ public class AccessTypeInformation {
public String toString() { public String toString() {
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
sb.append( "AccessTypeInformation" ); sb.append( "AccessTypeInformation" );
sb.append( "{fqcn='" ).append( fqcn ).append( '\'' ); sb.append( "{fqcn='" ).append(fullyQualifiedName).append( '\'' );
sb.append( ", explicitAccessType=" ).append( explicitAccessType ); sb.append( ", explicitAccessType=" ).append( explicitAccessType );
sb.append( ", defaultAccessType=" ).append( defaultAccessType ); sb.append( ", defaultAccessType=" ).append( defaultAccessType );
sb.append( '}' ); sb.append( '}' );