HHH-18949 fix underscores within uppercase names

This commit is contained in:
Gavin King 2024-12-16 20:29:49 +01:00
parent f7e3177dba
commit b712fa0296
3 changed files with 50 additions and 4 deletions

View File

@ -98,18 +98,21 @@ public final class StringUtil {
}
public static String getUpperUnderscoreCaseFromLowerCamelCase(String lowerCamelCaseString) {
if ( lowerCamelCaseString.length() == 1 && isUpperCase( lowerCamelCaseString.charAt( 0 ) ) ) {
return "_" + lowerCamelCaseString;
}
final StringBuilder result = new StringBuilder();
int position = 0;
boolean wasLowerCase = false;
while ( position < lowerCamelCaseString.length() ) {
final int codePoint = lowerCamelCaseString.codePointAt( position );
if ( position>0 && isUpperCase( codePoint ) ) {
final boolean isUpperCase = isUpperCase( codePoint );
if ( wasLowerCase && isUpperCase ) {
result.append('_');
}
result.appendCodePoint( toUpperCase( codePoint ) );
position += charCount( codePoint );
wasLowerCase = !isUpperCase;
}
if ( result.toString().equals( lowerCamelCaseString ) ) {
result.insert(0, '_');
}
return result.toString();
}

View File

@ -0,0 +1,14 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.processor.test.uppercase;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
@Entity
public class Person {
@Id String SSN;
String UserID;
}

View File

@ -0,0 +1,29 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.processor.test.uppercase;
import org.hibernate.processor.test.util.CompilationTest;
import org.hibernate.processor.test.util.WithClasses;
import org.junit.Test;
import static org.hibernate.processor.test.util.TestUtil.assertMetamodelClassGeneratedFor;
import static org.hibernate.processor.test.util.TestUtil.assertPresenceOfFieldInMetamodelFor;
import static org.hibernate.processor.test.util.TestUtil.getMetaModelSourceAsString;
public class UppercaseTest extends CompilationTest {
@Test
@WithClasses(value = Person.class)
public void test() {
System.out.println( getMetaModelSourceAsString( Person.class ) );
assertMetamodelClassGeneratedFor( Person.class );
assertPresenceOfFieldInMetamodelFor( Person.class, "SSN" );
assertPresenceOfFieldInMetamodelFor( Person.class, "_SSN" );
assertPresenceOfFieldInMetamodelFor( Person.class, "UserID" );
assertPresenceOfFieldInMetamodelFor( Person.class, "USER_ID" );
}
}