Skip to content

Commit 7c30bbe

Browse files
committed
very minor code cleanups
Signed-off-by: Gavin King <[email protected]>
1 parent dfe6a09 commit 7c30bbe

File tree

4 files changed

+41
-54
lines changed

4 files changed

+41
-54
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/naming/NamingHelper.java

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.security.MessageDigest;
1212
import java.security.NoSuchAlgorithmException;
1313
import java.util.Arrays;
14-
import java.util.Comparator;
1514
import java.util.List;
1615

1716
import org.hibernate.HibernateException;
@@ -28,7 +27,7 @@ public class NamingHelper {
2827
public static final NamingHelper INSTANCE = new NamingHelper();
2928

3029
public static NamingHelper withCharset(String charset) {
31-
return new NamingHelper(charset);
30+
return new NamingHelper( charset );
3231
}
3332

3433
private final String charset;
@@ -69,28 +68,17 @@ public String generateHashedFkName(
6968
Identifier tableName,
7069
Identifier referencedTableName,
7170
Identifier... columnNames) {
72-
// Use a concatenation that guarantees uniqueness, even if identical names
73-
// exist between all table and column identifiers.
74-
75-
StringBuilder sb = new StringBuilder()
71+
// Use a concatenation that guarantees uniqueness, even if identical
72+
// names exist between all table and column identifiers.
73+
final StringBuilder sb = new StringBuilder()
7674
.append( "table`" ).append( tableName ).append( "`" )
7775
.append( "references`" ).append( referencedTableName ).append( "`" );
78-
7976
// Ensure a consistent ordering of columns, regardless of the order
8077
// they were bound.
8178
// Clone the list, as sometimes a set of order-dependent Column
8279
// bindings are given.
83-
Identifier[] alphabeticalColumns = columnNames.clone();
84-
Arrays.sort(
85-
alphabeticalColumns,
86-
new Comparator<Identifier>() {
87-
@Override
88-
public int compare(Identifier o1, Identifier o2) {
89-
return o1.getCanonicalName().compareTo( o2.getCanonicalName() );
90-
}
91-
}
92-
);
93-
80+
final Identifier[] alphabeticalColumns = columnNames.clone();
81+
Arrays.sort( alphabeticalColumns, comparing( Identifier::getCanonicalName ) );
9482
for ( Identifier columnName : alphabeticalColumns ) {
9583
sb.append( "column`" ).append( columnName ).append( "`" );
9684
}
@@ -103,17 +91,16 @@ public int compare(Identifier o1, Identifier o2) {
10391
*
10492
* @return String The generated name
10593
*/
106-
public String generateHashedConstraintName(String prefix, Identifier tableName, Identifier... columnNames ) {
107-
// Use a concatenation that guarantees uniqueness, even if identical names
108-
// exist between all table and column identifiers.
109-
110-
StringBuilder sb = new StringBuilder( "table`" + tableName + "`" );
111-
94+
public String generateHashedConstraintName(
95+
String prefix, Identifier tableName, Identifier... columnNames ) {
96+
// Use a concatenation that guarantees uniqueness, even if identical
97+
// names exist between all table and column identifiers.
98+
final StringBuilder sb = new StringBuilder( "table`" + tableName + "`" );
11299
// Ensure a consistent ordering of columns, regardless of the order
113100
// they were bound.
114101
// Clone the list, as sometimes a set of order-dependent Column
115102
// bindings are given.
116-
Identifier[] alphabeticalColumns = columnNames.clone();
103+
final Identifier[] alphabeticalColumns = columnNames.clone();
117104
Arrays.sort( alphabeticalColumns, comparing(Identifier::getCanonicalName) );
118105
for ( Identifier columnName : alphabeticalColumns ) {
119106
sb.append( "column`" ).append( columnName ).append( "`" );
@@ -127,8 +114,9 @@ public String generateHashedConstraintName(String prefix, Identifier tableName,
127114
*
128115
* @return String The generated name
129116
*/
130-
public String generateHashedConstraintName(String prefix, Identifier tableName, List<Identifier> columnNames) {
131-
Identifier[] columnNamesArray = new Identifier[columnNames.size()];
117+
public String generateHashedConstraintName(
118+
String prefix, Identifier tableName, List<Identifier> columnNames) {
119+
final Identifier[] columnNamesArray = new Identifier[columnNames.size()];
132120
for ( int i = 0; i < columnNames.size(); i++ ) {
133121
columnNamesArray[i] = columnNames.get( i );
134122
}
@@ -141,23 +129,22 @@ public String generateHashedConstraintName(String prefix, Identifier tableName,
141129
* that the length of the name will always be smaller than the 30
142130
* character identifier restriction enforced by a few dialects.
143131
*
144-
* @param s The name to be hashed.
132+
* @param name The name to be hashed.
145133
*
146134
* @return String The hashed name.
147135
*/
148-
public String hashedName(String s) {
136+
public String hashedName(String name) {
149137
try {
150-
MessageDigest md = MessageDigest.getInstance( "MD5" );
151-
md.reset();
152-
md.update( charset != null ? s.getBytes( charset ) : s.getBytes() );
153-
byte[] digest = md.digest();
154-
BigInteger bigInt = new BigInteger( 1, digest );
138+
final MessageDigest md5 = MessageDigest.getInstance( "MD5" );
139+
md5.reset();
140+
md5.update( charset != null ? name.getBytes( charset ) : name.getBytes() );
141+
final BigInteger bigInt = new BigInteger( 1, md5.digest() );
155142
// By converting to base 35 (full alphanumeric), we guarantee
156143
// that the length of the name will always be smaller than the 30
157144
// character identifier restriction enforced by a few dialects.
158145
return bigInt.toString( 35 );
159146
}
160-
catch ( NoSuchAlgorithmException|UnsupportedEncodingException e ) {
147+
catch ( NoSuchAlgorithmException | UnsupportedEncodingException e ) {
161148
throw new HibernateException( "Unable to generate a hashed name", e );
162149
}
163150
}

tooling/metamodel-generator/src/main/java/org/hibernate/processor/HibernateProcessor.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,9 +589,7 @@ && hasAnnotation( element, ENTITY, MAPPED_SUPERCLASS )
589589
&& alreadyExistingMetaEntity == null
590590
// let a handwritten metamodel "override" the generated one
591591
// (this is used in the Jakarta Data TCK)
592-
&& element.getEnclosingElement().getEnclosedElements()
593-
.stream().noneMatch(e -> e.getSimpleName()
594-
.contentEquals('_' + element.getSimpleName().toString()))) {
592+
&& !hasHandwrittenMetamodel(element) ) {
595593
final AnnotationMetaEntity dataMetaEntity =
596594
AnnotationMetaEntity.create( typeElement, context,
597595
requiresLazyMemberInitialization,
@@ -608,6 +606,12 @@ && hasAnnotation( element, ENTITY, MAPPED_SUPERCLASS )
608606
}
609607
}
610608

609+
private static boolean hasHandwrittenMetamodel(Element element) {
610+
return element.getEnclosingElement().getEnclosedElements()
611+
.stream().anyMatch(e -> e.getSimpleName()
612+
.contentEquals('_' + element.getSimpleName().toString()));
613+
}
614+
611615
private void indexEntityName(TypeElement typeElement) {
612616
final AnnotationMirror mirror = getAnnotationMirror( typeElement, ENTITY );
613617
if ( mirror != null ) {

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,7 @@ public final String getQualifiedName() {
250250

251251
@Override
252252
public @Nullable String getSupertypeName() {
253-
if ( repository ) {
254-
return null;
255-
}
256-
else {
257-
return findMappedSuperClass( this, context );
258-
}
253+
return repository ? null : findMappedSuperClass( this, context );
259254
}
260255

261256
@Override
@@ -275,7 +270,6 @@ public List<MetaAttribute> getMembers() {
275270
mergeInMembers( entityToMerge.getMembers() );
276271
}
277272
}
278-
279273
return new ArrayList<>( members.values() );
280274
}
281275

@@ -289,7 +283,6 @@ private void mergeInMembers(Collection<MetaAttribute> attributes) {
289283
// propagate types to be imported
290284
importType( attribute.getMetaType() );
291285
importType( attribute.getTypeDeclaration() );
292-
293286
members.put( attribute.getPropertyName(), attribute );
294287
}
295288
}
@@ -298,7 +291,7 @@ public void mergeInMembers(Metamodel other) {
298291
// store the entity in order do the merge lazily in case of
299292
// an uninitialized embeddedable or mapped superclass
300293
if ( !initialized ) {
301-
this.entityToMerge = other;
294+
entityToMerge = other;
302295
}
303296
else {
304297
mergeInMembers( other.getMembers() );
@@ -1133,7 +1126,7 @@ private void addQueryMethod(ExecutableElement method) {
11331126
addQueryMethod( method, returnType, null );
11341127
}
11351128
else if ( kind == TypeKind.DECLARED ) {
1136-
final DeclaredType declaredType = (DeclaredType) ununiIfPossible(method, returnType);
1129+
final DeclaredType declaredType = (DeclaredType) unUniIfPossible(method, returnType);
11371130
final TypeElement typeElement = (TypeElement) declaredType.asElement();
11381131
final List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
11391132
switch ( typeArguments.size() ) {
@@ -1165,7 +1158,7 @@ else if ( kind == TypeKind.DECLARED ) {
11651158
}
11661159
}
11671160

1168-
private TypeMirror ununiIfPossible(ExecutableElement method, TypeMirror returnType) {
1161+
private TypeMirror unUniIfPossible(ExecutableElement method, TypeMirror returnType) {
11691162
final TypeMirror result = ununi( returnType );
11701163
if ( repository ) {
11711164
if ( usingReactiveSession( sessionType ) ) {
@@ -1324,7 +1317,7 @@ private void addDeleteMethod(ExecutableElement method, @Nullable TypeMirror retu
13241317
}
13251318

13261319
private void addLifecycleMethod(ExecutableElement method) {
1327-
final TypeMirror returnType = ununiIfPossible( method, method.getReturnType() );
1320+
final TypeMirror returnType = unUniIfPossible( method, method.getReturnType() );
13281321
if ( method.getParameters().size() != 1 ) {
13291322
message( method,
13301323
"must have exactly one parameter",

tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/AccessTypeInformation.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* @author Hardy Ferentschik
1616
*/
1717
public class AccessTypeInformation {
18-
private final String fqcn;
18+
private final String fullyQualifiedName;
1919

2020
/**
2121
* Access type explicitly specified in xml or on an entity.
@@ -30,8 +30,11 @@ public class AccessTypeInformation {
3030

3131
static final AccessType DEFAULT_ACCESS_TYPE = AccessType.FIELD;
3232

33-
public AccessTypeInformation(String fqcn, @Nullable AccessType explicitAccessType, @Nullable AccessType defaultAccessType) {
34-
this.fqcn = fqcn;
33+
public AccessTypeInformation(
34+
String fullyQualifiedName,
35+
@Nullable AccessType explicitAccessType,
36+
@Nullable AccessType defaultAccessType) {
37+
this.fullyQualifiedName = fullyQualifiedName;
3538
this.explicitAccessType = explicitAccessType;
3639
this.defaultAccessType = defaultAccessType;
3740
}
@@ -69,7 +72,7 @@ public void setExplicitAccessType(AccessType explicitAccessType) {
6972
public String toString() {
7073
final StringBuilder sb = new StringBuilder();
7174
sb.append( "AccessTypeInformation" );
72-
sb.append( "{fqcn='" ).append( fqcn ).append( '\'' );
75+
sb.append( "{fqcn='" ).append(fullyQualifiedName).append( '\'' );
7376
sb.append( ", explicitAccessType=" ).append( explicitAccessType );
7477
sb.append( ", defaultAccessType=" ).append( defaultAccessType );
7578
sb.append( '}' );

0 commit comments

Comments
 (0)