From 7c30bbed2b426030c8cfbfae76c680679812cf33 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Wed, 4 Sep 2024 19:32:54 +0200 Subject: [PATCH] very minor code cleanups Signed-off-by: Gavin King --- .../boot/model/naming/NamingHelper.java | 57 +++++++------------ .../processor/HibernateProcessor.java | 10 +++- .../annotation/AnnotationMetaEntity.java | 17 ++---- .../processor/util/AccessTypeInformation.java | 11 ++-- 4 files changed, 41 insertions(+), 54 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/naming/NamingHelper.java b/hibernate-core/src/main/java/org/hibernate/boot/model/naming/NamingHelper.java index 69a194b077..c8e74a2895 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/naming/NamingHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/naming/NamingHelper.java @@ -11,7 +11,6 @@ import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; -import java.util.Comparator; import java.util.List; import org.hibernate.HibernateException; @@ -28,7 +27,7 @@ public class NamingHelper { public static final NamingHelper INSTANCE = new NamingHelper(); public static NamingHelper withCharset(String charset) { - return new NamingHelper(charset); + return new NamingHelper( charset ); } private final String charset; @@ -69,28 +68,17 @@ public class NamingHelper { Identifier tableName, Identifier referencedTableName, Identifier... columnNames) { - // Use a concatenation that guarantees uniqueness, even if identical names - // exist between all table and column identifiers. - - StringBuilder sb = new StringBuilder() + // Use a concatenation that guarantees uniqueness, even if identical + // names exist between all table and column identifiers. + final StringBuilder sb = new StringBuilder() .append( "table`" ).append( tableName ).append( "`" ) .append( "references`" ).append( referencedTableName ).append( "`" ); - // Ensure a consistent ordering of columns, regardless of the order // they were bound. // Clone the list, as sometimes a set of order-dependent Column // bindings are given. - Identifier[] alphabeticalColumns = columnNames.clone(); - Arrays.sort( - alphabeticalColumns, - new Comparator() { - @Override - public int compare(Identifier o1, Identifier o2) { - return o1.getCanonicalName().compareTo( o2.getCanonicalName() ); - } - } - ); - + final Identifier[] alphabeticalColumns = columnNames.clone(); + Arrays.sort( alphabeticalColumns, comparing( Identifier::getCanonicalName ) ); for ( Identifier columnName : alphabeticalColumns ) { sb.append( "column`" ).append( columnName ).append( "`" ); } @@ -103,17 +91,16 @@ public class NamingHelper { * * @return String The generated name */ - public String generateHashedConstraintName(String prefix, Identifier tableName, Identifier... columnNames ) { - // Use a concatenation that guarantees uniqueness, even if identical names - // exist between all table and column identifiers. - - StringBuilder sb = new StringBuilder( "table`" + tableName + "`" ); - + public String generateHashedConstraintName( + String prefix, Identifier tableName, Identifier... columnNames ) { + // Use a concatenation that guarantees uniqueness, even if identical + // names exist between all table and column identifiers. + final StringBuilder sb = new StringBuilder( "table`" + tableName + "`" ); // Ensure a consistent ordering of columns, regardless of the order // they were bound. // Clone the list, as sometimes a set of order-dependent Column // bindings are given. - Identifier[] alphabeticalColumns = columnNames.clone(); + final Identifier[] alphabeticalColumns = columnNames.clone(); Arrays.sort( alphabeticalColumns, comparing(Identifier::getCanonicalName) ); for ( Identifier columnName : alphabeticalColumns ) { sb.append( "column`" ).append( columnName ).append( "`" ); @@ -127,8 +114,9 @@ public class NamingHelper { * * @return String The generated name */ - public String generateHashedConstraintName(String prefix, Identifier tableName, List columnNames) { - Identifier[] columnNamesArray = new Identifier[columnNames.size()]; + public String generateHashedConstraintName( + String prefix, Identifier tableName, List columnNames) { + final Identifier[] columnNamesArray = new Identifier[columnNames.size()]; for ( int i = 0; i < columnNames.size(); 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 * 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. */ - public String hashedName(String s) { + public String hashedName(String name) { try { - MessageDigest md = MessageDigest.getInstance( "MD5" ); - md.reset(); - md.update( charset != null ? s.getBytes( charset ) : s.getBytes() ); - byte[] digest = md.digest(); - BigInteger bigInt = new BigInteger( 1, digest ); + final MessageDigest md5 = MessageDigest.getInstance( "MD5" ); + md5.reset(); + md5.update( charset != null ? name.getBytes( charset ) : name.getBytes() ); + final BigInteger bigInt = new BigInteger( 1, md5.digest() ); // By converting to base 35 (full alphanumeric), we guarantee // that the length of the name will always be smaller than the 30 // character identifier restriction enforced by a few dialects. return bigInt.toString( 35 ); } - catch ( NoSuchAlgorithmException|UnsupportedEncodingException e ) { + catch ( NoSuchAlgorithmException | UnsupportedEncodingException e ) { throw new HibernateException( "Unable to generate a hashed name", e ); } } diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/HibernateProcessor.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/HibernateProcessor.java index 1b5f53ed13..0dc7b23fdd 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/HibernateProcessor.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/HibernateProcessor.java @@ -589,9 +589,7 @@ public class HibernateProcessor extends AbstractProcessor { && alreadyExistingMetaEntity == null // let a handwritten metamodel "override" the generated one // (this is used in the Jakarta Data TCK) - && element.getEnclosingElement().getEnclosedElements() - .stream().noneMatch(e -> e.getSimpleName() - .contentEquals('_' + element.getSimpleName().toString()))) { + && !hasHandwrittenMetamodel(element) ) { final AnnotationMetaEntity dataMetaEntity = AnnotationMetaEntity.create( typeElement, context, 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) { final AnnotationMirror mirror = getAnnotationMirror( typeElement, ENTITY ); if ( mirror != null ) { diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java index 8b5ca60fd6..ab8b7b2499 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java @@ -250,12 +250,7 @@ public class AnnotationMetaEntity extends AnnotationMeta { @Override public @Nullable String getSupertypeName() { - if ( repository ) { - return null; - } - else { - return findMappedSuperClass( this, context ); - } + return repository ? null : findMappedSuperClass( this, context ); } @Override @@ -275,7 +270,6 @@ public class AnnotationMetaEntity extends AnnotationMeta { mergeInMembers( entityToMerge.getMembers() ); } } - return new ArrayList<>( members.values() ); } @@ -289,7 +283,6 @@ public class AnnotationMetaEntity extends AnnotationMeta { // propagate types to be imported importType( attribute.getMetaType() ); importType( attribute.getTypeDeclaration() ); - 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 // an uninitialized embeddedable or mapped superclass if ( !initialized ) { - this.entityToMerge = other; + entityToMerge = other; } else { mergeInMembers( other.getMembers() ); @@ -1133,7 +1126,7 @@ public class AnnotationMetaEntity extends AnnotationMeta { addQueryMethod( method, returnType, null ); } 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 List typeArguments = declaredType.getTypeArguments(); 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 ); if ( repository ) { if ( usingReactiveSession( sessionType ) ) { @@ -1324,7 +1317,7 @@ public class AnnotationMetaEntity extends AnnotationMeta { } private void addLifecycleMethod(ExecutableElement method) { - final TypeMirror returnType = ununiIfPossible( method, method.getReturnType() ); + final TypeMirror returnType = unUniIfPossible( method, method.getReturnType() ); if ( method.getParameters().size() != 1 ) { message( method, "must have exactly one parameter", diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/AccessTypeInformation.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/AccessTypeInformation.java index 3dde49f425..54409b692e 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/AccessTypeInformation.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/AccessTypeInformation.java @@ -15,7 +15,7 @@ import org.checkerframework.checker.nullness.qual.Nullable; * @author Hardy Ferentschik */ public class AccessTypeInformation { - private final String fqcn; + private final String fullyQualifiedName; /** * 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; - public AccessTypeInformation(String fqcn, @Nullable AccessType explicitAccessType, @Nullable AccessType defaultAccessType) { - this.fqcn = fqcn; + public AccessTypeInformation( + String fullyQualifiedName, + @Nullable AccessType explicitAccessType, + @Nullable AccessType defaultAccessType) { + this.fullyQualifiedName = fullyQualifiedName; this.explicitAccessType = explicitAccessType; this.defaultAccessType = defaultAccessType; } @@ -69,7 +72,7 @@ public class AccessTypeInformation { public String toString() { final StringBuilder sb = new StringBuilder(); sb.append( "AccessTypeInformation" ); - sb.append( "{fqcn='" ).append( fqcn ).append( '\'' ); + sb.append( "{fqcn='" ).append(fullyQualifiedName).append( '\'' ); sb.append( ", explicitAccessType=" ).append( explicitAccessType ); sb.append( ", defaultAccessType=" ).append( defaultAccessType ); sb.append( '}' );