From 611fb779453dfbad51e2aa639876f02ee7465420 Mon Sep 17 00:00:00 2001 From: Scott Marlow Date: Thu, 5 Dec 2024 15:54:45 -0500 Subject: [PATCH] HHH-18917 Follow all of the JavaBeans rules in enhance/internal/bytebuddy/EnhancerImpl when checking if a class can be enhanced Signed-off-by: Scott Marlow --- .../internal/bytebuddy/EnhancerImpl.java | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/EnhancerImpl.java b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/EnhancerImpl.java index 27d92325d230..c79f3e4b2350 100644 --- a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/EnhancerImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/EnhancerImpl.java @@ -461,8 +461,8 @@ else if (methodName.startsWith("get") || continue; } boolean propertyNameMatchesFieldName = false; - // convert field letter to lower case - methodFieldName = methodFieldName.substring(0, 1).toLowerCase() + methodFieldName.substring(1); + // extract the property name from method name + methodFieldName = getJavaBeansFieldName(methodFieldName); TypeList typeList = methodDescription.getDeclaredAnnotations().asTypeList(); if (typeList.stream().anyMatch(typeDefinitions -> (typeDefinitions.getName().equals("jakarta.persistence.Transient")))) { @@ -476,7 +476,6 @@ else if (methodName.startsWith("get") || for (FieldDescription ctField : methodDescription.getDeclaringType().getDeclaredFields()) { if (!Modifier.isStatic(ctField.getModifiers())) { AnnotatedFieldDescription annotatedField = new AnnotatedFieldDescription(enhancementContext, ctField); - boolean containsPropertyAccessorMethods = false; if (enhancementContext.isPersistentField(annotatedField)) { if (methodFieldName.equals(ctField.getActualName())) { propertyNameMatchesFieldName = true; @@ -507,6 +506,22 @@ else if (methodName.startsWith("get") || return false; } + /** + * If the first two characters are upper case, assume all characters are upper case to be returned as is. + * Otherwise, return the name with the first character converted to lower case and the remaining part returned as is. + * @param fieldName is the property accessor name to be updated following Persistence property name rules. + * @return name that follows JavaBeans rules. + */ + private static String getJavaBeansFieldName(String fieldName) { + + if (fieldName.length() == 0 || + (fieldName.length() > 1 && Character.isUpperCase(fieldName.charAt(0)) && Character.isUpperCase(fieldName.charAt(1))) + ) { + return fieldName; + } + return Character.toLowerCase(fieldName.charAt(0)) + fieldName.substring(1); + } + private static void verifyVersions(TypeDescription managedCtClass, ByteBuddyEnhancementContext enhancementContext) { final AnnotationDescription.Loadable existingInfo = managedCtClass .getDeclaredAnnotations()