Skip to content

Commit 8af90ca

Browse files
scottmarlowmbellade
authored andcommitted
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 <[email protected]>
1 parent 4ebbf5b commit 8af90ca

File tree

1 file changed

+18
-3
lines changed
  • hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy

1 file changed

+18
-3
lines changed

hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/EnhancerImpl.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,8 @@ else if (methodName.startsWith("get") ||
459459
continue;
460460
}
461461
boolean propertyNameMatchesFieldName = false;
462-
// convert field letter to lower case
463-
methodFieldName = methodFieldName.substring(0, 1).toLowerCase() + methodFieldName.substring(1);
462+
// extract the property name from method name
463+
methodFieldName = getJavaBeansFieldName(methodFieldName);
464464
TypeList typeList = methodDescription.getDeclaredAnnotations().asTypeList();
465465
if (typeList.stream().anyMatch(typeDefinitions ->
466466
(typeDefinitions.getName().equals("jakarta.persistence.Transient")))) {
@@ -474,7 +474,6 @@ else if (methodName.startsWith("get") ||
474474
for (FieldDescription ctField : methodDescription.getDeclaringType().getDeclaredFields()) {
475475
if (!Modifier.isStatic(ctField.getModifiers())) {
476476
AnnotatedFieldDescription annotatedField = new AnnotatedFieldDescription(enhancementContext, ctField);
477-
boolean containsPropertyAccessorMethods = false;
478477
if (enhancementContext.isPersistentField(annotatedField)) {
479478
if (methodFieldName.equals(ctField.getActualName())) {
480479
propertyNameMatchesFieldName = true;
@@ -505,6 +504,22 @@ else if (methodName.startsWith("get") ||
505504
return false;
506505
}
507506

507+
/**
508+
* If the first two characters are upper case, assume all characters are upper case to be returned as is.
509+
* Otherwise, return the name with the first character converted to lower case and the remaining part returned as is.
510+
* @param fieldName is the property accessor name to be updated following Persistence property name rules.
511+
* @return name that follows JavaBeans rules.
512+
*/
513+
private static String getJavaBeansFieldName(String fieldName) {
514+
515+
if (fieldName.length() == 0 ||
516+
(fieldName.length() > 1 && Character.isUpperCase(fieldName.charAt(0)) && Character.isUpperCase(fieldName.charAt(1)))
517+
) {
518+
return fieldName;
519+
}
520+
return Character.toLowerCase(fieldName.charAt(0)) + fieldName.substring(1);
521+
}
522+
508523
private static void verifyVersions(TypeDescription managedCtClass, ByteBuddyEnhancementContext enhancementContext) {
509524
final AnnotationDescription.Loadable<EnhancementInfo> existingInfo = managedCtClass
510525
.getDeclaredAnnotations()

0 commit comments

Comments
 (0)