From 8af90ca92151f3afca3526dedf0488037d29659b 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 d11f27d929..c2630ab3bb 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 @@ -459,8 +459,8 @@ public class EnhancerImpl implements Enhancer { 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")))) { @@ -474,7 +474,6 @@ public class EnhancerImpl implements Enhancer { 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; @@ -505,6 +504,22 @@ public class EnhancerImpl implements Enhancer { 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()