From b712fa02969bbcee631f5567fa62788664a7d5b4 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Mon, 16 Dec 2024 20:29:49 +0100 Subject: [PATCH] HHH-18949 fix underscores within uppercase names --- .../hibernate/processor/util/StringUtil.java | 11 ++++--- .../processor/test/uppercase/Person.java | 14 +++++++++ .../test/uppercase/UppercaseTest.java | 29 +++++++++++++++++++ 3 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/uppercase/Person.java create mode 100644 tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/uppercase/UppercaseTest.java diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/StringUtil.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/StringUtil.java index 6fd474b51c..5b64904275 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/StringUtil.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/StringUtil.java @@ -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(); } diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/uppercase/Person.java b/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/uppercase/Person.java new file mode 100644 index 0000000000..078e9e4b1b --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/uppercase/Person.java @@ -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; +} diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/uppercase/UppercaseTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/uppercase/UppercaseTest.java new file mode 100644 index 0000000000..d765288a82 --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/uppercase/UppercaseTest.java @@ -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" ); + } +}