Skip to content

Commit b712fa0

Browse files
committed
HHH-18949 fix underscores within uppercase names
1 parent f7e3177 commit b712fa0

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,21 @@ public static String nameToMethodName(String name) {
9898
}
9999

100100
public static String getUpperUnderscoreCaseFromLowerCamelCase(String lowerCamelCaseString) {
101-
if ( lowerCamelCaseString.length() == 1 && isUpperCase( lowerCamelCaseString.charAt( 0 ) ) ) {
102-
return "_" + lowerCamelCaseString;
103-
}
104101
final StringBuilder result = new StringBuilder();
105102
int position = 0;
103+
boolean wasLowerCase = false;
106104
while ( position < lowerCamelCaseString.length() ) {
107105
final int codePoint = lowerCamelCaseString.codePointAt( position );
108-
if ( position>0 && isUpperCase( codePoint ) ) {
106+
final boolean isUpperCase = isUpperCase( codePoint );
107+
if ( wasLowerCase && isUpperCase ) {
109108
result.append('_');
110109
}
111110
result.appendCodePoint( toUpperCase( codePoint ) );
112111
position += charCount( codePoint );
112+
wasLowerCase = !isUpperCase;
113+
}
114+
if ( result.toString().equals( lowerCamelCaseString ) ) {
115+
result.insert(0, '_');
113116
}
114117
return result.toString();
115118
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.processor.test.uppercase;
6+
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.Id;
9+
10+
@Entity
11+
public class Person {
12+
@Id String SSN;
13+
String UserID;
14+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.processor.test.uppercase;
6+
7+
import org.hibernate.processor.test.util.CompilationTest;
8+
import org.hibernate.processor.test.util.WithClasses;
9+
import org.junit.Test;
10+
11+
import static org.hibernate.processor.test.util.TestUtil.assertMetamodelClassGeneratedFor;
12+
import static org.hibernate.processor.test.util.TestUtil.assertPresenceOfFieldInMetamodelFor;
13+
import static org.hibernate.processor.test.util.TestUtil.getMetaModelSourceAsString;
14+
15+
public class UppercaseTest extends CompilationTest {
16+
17+
@Test
18+
@WithClasses(value = Person.class)
19+
public void test() {
20+
System.out.println( getMetaModelSourceAsString( Person.class ) );
21+
22+
assertMetamodelClassGeneratedFor( Person.class );
23+
24+
assertPresenceOfFieldInMetamodelFor( Person.class, "SSN" );
25+
assertPresenceOfFieldInMetamodelFor( Person.class, "_SSN" );
26+
assertPresenceOfFieldInMetamodelFor( Person.class, "UserID" );
27+
assertPresenceOfFieldInMetamodelFor( Person.class, "USER_ID" );
28+
}
29+
}

0 commit comments

Comments
 (0)