Skip to content

Commit 42928d9

Browse files
committed
Refactor: Extract isRecordStyleAccessor helper method
1 parent 1faa8bd commit 42928d9

File tree

1 file changed

+36
-19
lines changed

1 file changed

+36
-19
lines changed

src/main/java/org/json/JSONObject.java

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,25 +1912,7 @@ private static String getKeyNameFromMethod(Method method) {
19121912
// They must start with a lowercase letter and should be declared in the class itself
19131913
// (not inherited from Object, Enum, Number, or any java.* class)
19141914
// Also exclude common Object/bean method names
1915-
Class<?> declaringClass = method.getDeclaringClass();
1916-
if (name.length() > 0 && Character.isLowerCase(name.charAt(0))
1917-
&& !"get".equals(name)
1918-
&& !"is".equals(name)
1919-
&& !"set".equals(name)
1920-
&& !"toString".equals(name)
1921-
&& !"hashCode".equals(name)
1922-
&& !"equals".equals(name)
1923-
&& !"clone".equals(name)
1924-
&& !"notify".equals(name)
1925-
&& !"notifyAll".equals(name)
1926-
&& !"wait".equals(name)
1927-
&& declaringClass != null
1928-
&& declaringClass != Object.class
1929-
&& !Enum.class.isAssignableFrom(declaringClass)
1930-
&& !Number.class.isAssignableFrom(declaringClass)
1931-
&& !declaringClass.getName().startsWith("java.")
1932-
&& !declaringClass.getName().startsWith("javax.")) {
1933-
// This is a record-style accessor - return the method name as-is
1915+
if (isRecordStyleAccessor(name, method)) {
19341916
return name;
19351917
}
19361918
return null;
@@ -1949,6 +1931,41 @@ private static String getKeyNameFromMethod(Method method) {
19491931
return key;
19501932
}
19511933

1934+
/**
1935+
* Checks if a method is a record-style accessor.
1936+
* Record accessors have lowercase names without get/is prefixes and are not inherited from standard Java classes.
1937+
*
1938+
* @param methodName the name of the method
1939+
* @param method the method to check
1940+
* @return true if this is a record-style accessor, false otherwise
1941+
*/
1942+
private static boolean isRecordStyleAccessor(String methodName, Method method) {
1943+
if (methodName.isEmpty() || !Character.isLowerCase(methodName.charAt(0))) {
1944+
return false;
1945+
}
1946+
1947+
// Exclude common bean/Object method names
1948+
if ("get".equals(methodName) || "is".equals(methodName) || "set".equals(methodName)
1949+
|| "toString".equals(methodName) || "hashCode".equals(methodName)
1950+
|| "equals".equals(methodName) || "clone".equals(methodName)
1951+
|| "notify".equals(methodName) || "notifyAll".equals(methodName)
1952+
|| "wait".equals(methodName)) {
1953+
return false;
1954+
}
1955+
1956+
Class<?> declaringClass = method.getDeclaringClass();
1957+
if (declaringClass == null || declaringClass == Object.class) {
1958+
return false;
1959+
}
1960+
1961+
if (Enum.class.isAssignableFrom(declaringClass) || Number.class.isAssignableFrom(declaringClass)) {
1962+
return false;
1963+
}
1964+
1965+
String className = declaringClass.getName();
1966+
return !className.startsWith("java.") && !className.startsWith("javax.");
1967+
}
1968+
19521969
/**
19531970
* checks if the annotation is not null and the {@link JSONPropertyName#value()} is not null and is not empty.
19541971
* @param annotation the annotation to check

0 commit comments

Comments
 (0)