From 2550c692cfe32d840431434f531a7735d438c17a Mon Sep 17 00:00:00 2001 From: AbhineshJha Date: Sat, 25 Oct 2025 14:30:25 +0530 Subject: [PATCH] Refactor: Extract isRecordStyleAccessor helper method --- src/main/java/org/json/JSONObject.java | 55 +++++++++++++++++--------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 72c8453..6b5c7b0 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -1915,25 +1915,7 @@ public class JSONObject { // They must start with a lowercase letter and should be declared in the class itself // (not inherited from Object, Enum, Number, or any java.* class) // Also exclude common Object/bean method names - Class declaringClass = method.getDeclaringClass(); - if (name.length() > 0 && Character.isLowerCase(name.charAt(0)) - && !"get".equals(name) - && !"is".equals(name) - && !"set".equals(name) - && !"toString".equals(name) - && !"hashCode".equals(name) - && !"equals".equals(name) - && !"clone".equals(name) - && !"notify".equals(name) - && !"notifyAll".equals(name) - && !"wait".equals(name) - && declaringClass != null - && declaringClass != Object.class - && !Enum.class.isAssignableFrom(declaringClass) - && !Number.class.isAssignableFrom(declaringClass) - && !declaringClass.getName().startsWith("java.") - && !declaringClass.getName().startsWith("javax.")) { - // This is a record-style accessor - return the method name as-is + if (isRecordStyleAccessor(name, method)) { return name; } return null; @@ -1952,6 +1934,41 @@ public class JSONObject { return key; } + /** + * Checks if a method is a record-style accessor. + * Record accessors have lowercase names without get/is prefixes and are not inherited from standard Java classes. + * + * @param methodName the name of the method + * @param method the method to check + * @return true if this is a record-style accessor, false otherwise + */ + private static boolean isRecordStyleAccessor(String methodName, Method method) { + if (methodName.isEmpty() || !Character.isLowerCase(methodName.charAt(0))) { + return false; + } + + // Exclude common bean/Object method names + if ("get".equals(methodName) || "is".equals(methodName) || "set".equals(methodName) + || "toString".equals(methodName) || "hashCode".equals(methodName) + || "equals".equals(methodName) || "clone".equals(methodName) + || "notify".equals(methodName) || "notifyAll".equals(methodName) + || "wait".equals(methodName)) { + return false; + } + + Class declaringClass = method.getDeclaringClass(); + if (declaringClass == null || declaringClass == Object.class) { + return false; + } + + if (Enum.class.isAssignableFrom(declaringClass) || Number.class.isAssignableFrom(declaringClass)) { + return false; + } + + String className = declaringClass.getName(); + return !className.startsWith("java.") && !className.startsWith("javax."); + } + /** * checks if the annotation is not null and the {@link JSONPropertyName#value()} is not null and is not empty. * @param annotation the annotation to check