Skip to content

Commit 2550c69

Browse files
committed
Refactor: Extract isRecordStyleAccessor helper method
1 parent 20f5200 commit 2550c69

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
@@ -1915,25 +1915,7 @@ private static String getKeyNameFromMethod(Method method) {
19151915
// They must start with a lowercase letter and should be declared in the class itself
19161916
// (not inherited from Object, Enum, Number, or any java.* class)
19171917
// Also exclude common Object/bean method names
1918-
Class<?> declaringClass = method.getDeclaringClass();
1919-
if (name.length() > 0 && Character.isLowerCase(name.charAt(0))
1920-
&& !"get".equals(name)
1921-
&& !"is".equals(name)
1922-
&& !"set".equals(name)
1923-
&& !"toString".equals(name)
1924-
&& !"hashCode".equals(name)
1925-
&& !"equals".equals(name)
1926-
&& !"clone".equals(name)
1927-
&& !"notify".equals(name)
1928-
&& !"notifyAll".equals(name)
1929-
&& !"wait".equals(name)
1930-
&& declaringClass != null
1931-
&& declaringClass != Object.class
1932-
&& !Enum.class.isAssignableFrom(declaringClass)
1933-
&& !Number.class.isAssignableFrom(declaringClass)
1934-
&& !declaringClass.getName().startsWith("java.")
1935-
&& !declaringClass.getName().startsWith("javax.")) {
1936-
// This is a record-style accessor - return the method name as-is
1918+
if (isRecordStyleAccessor(name, method)) {
19371919
return name;
19381920
}
19391921
return null;
@@ -1952,6 +1934,41 @@ private static String getKeyNameFromMethod(Method method) {
19521934
return key;
19531935
}
19541936

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

0 commit comments

Comments
 (0)