Skip to content

Commit

Permalink
HHH-18917 Follow all of the JavaBeans rules in enhance/internal/byteb…
Browse files Browse the repository at this point in the history
…uddy/EnhancerImpl when checking if a class can be enhanced

Signed-off-by: Scott Marlow <[email protected]>
  • Loading branch information
scottmarlow authored and mbladel committed Dec 9, 2024
1 parent 06758f0 commit 611fb77
Showing 1 changed file with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")))) {
Expand All @@ -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;
Expand Down Expand Up @@ -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<EnhancementInfo> existingInfo = managedCtClass
.getDeclaredAnnotations()
Expand Down

0 comments on commit 611fb77

Please sign in to comment.