Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor changes #880

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions src/main/java/org/json/HTTP.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,24 +113,38 @@ public static JSONObject toJSONObject(String string) throws JSONException {
public static String toString(JSONObject jo) throws JSONException {
StringBuilder sb = new StringBuilder();
if (jo.has("Status-Code") && jo.has("Reason-Phrase")) {
sb.append(jo.getString("HTTP-Version"));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure that the refactoring update in this method is a good idea. Although it shortens the method, it hides a more interesting potential refactoring - notice how many times status-code, reason-phrase, method, request-uri, and http-version are referenced in the conditionals. It makes the code look half-finished. Can you come up with a way to reduce the number of conditions being checked without affecting the output ordering?

sb.append(' ');
sb.append(jo.getString("Status-Code"));
sb.append(' ');
sb.append(jo.getString("Reason-Phrase"));
appendResponseHeaderLine(sb, jo);
} else if (jo.has("Method") && jo.has("Request-URI")) {
sb.append(jo.getString("Method"));
sb.append(' ');
sb.append('"');
sb.append(jo.getString("Request-URI"));
sb.append('"');
sb.append(' ');
sb.append(jo.getString("HTTP-Version"));
appendRequestHeaderLine(sb, jo);
} else {
throw new JSONException("Not enough material for an HTTP header.");
}
sb.append(CRLF);
// Don't use the new entrySet API to maintain Android support
appendMainHeaders(sb, jo);
sb.append(CRLF);
return sb.toString();
}

private static void appendResponseHeaderLine(StringBuilder sb, JSONObject jo) throws JSONException {
sb.append(jo.getString("HTTP-Version"));
sb.append(' ');
sb.append(jo.getString("Status-Code"));
sb.append(' ');
sb.append(jo.getString("Reason-Phrase"));
}

private static void appendRequestHeaderLine(StringBuilder sb, JSONObject jo) throws JSONException {
sb.append(jo.getString("Method"));
sb.append(' ');
sb.append('"');
sb.append(jo.getString("Request-URI"));
sb.append('"');
sb.append(' ');
sb.append(jo.getString("HTTP-Version"));
}

private static void appendMainHeaders(StringBuilder sb, JSONObject jo) throws JSONException {
for (final String key : jo.keySet()) {
String value = jo.optString(key);
if (!"HTTP-Version".equals(key) && !"Status-Code".equals(key) &&
Expand All @@ -142,7 +156,8 @@ public static String toString(JSONObject jo) throws JSONException {
sb.append(CRLF);
}
}
sb.append(CRLF);
return sb.toString();
}



}
26 changes: 13 additions & 13 deletions src/main/java/org/json/HTTPTokener.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,31 @@ public HTTPTokener(String string) {
* @throws JSONException if a syntax error occurs
*/
public String nextToken() throws JSONException {
char c;
char q;
char currentChar;
char quoteChar;
StringBuilder sb = new StringBuilder();
do {
c = next();
} while (Character.isWhitespace(c));
if (c == '"' || c == '\'') {
q = c;
currentChar = next();
} while (Character.isWhitespace(currentChar));
if (currentChar == '"' || currentChar == '\'') {
quoteChar = currentChar;
for (;;) {
c = next();
if (c < ' ') {
currentChar = next();
if (currentChar < ' ') {
throw syntaxError("Unterminated string.");
}
if (c == q) {
if (currentChar == quoteChar) {
return sb.toString();
}
sb.append(c);
sb.append(currentChar);
}
}
for (;;) {
if (c == 0 || Character.isWhitespace(c)) {
if (currentChar == 0 || Character.isWhitespace(currentChar)) {
return sb.toString();
}
sb.append(c);
c = next();
sb.append(currentChar);
currentChar = next();
}
}
}
16 changes: 10 additions & 6 deletions src/main/java/org/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -1762,12 +1762,7 @@ private void populateMap(Object bean, Set<Object> objectsRecord) {
Method[] methods = includeSuperClass ? klass.getMethods() : klass.getDeclaredMethods();
for (final Method method : methods) {
final int modifiers = method.getModifiers();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable modifiers is only used with the method isValidMethod. So there is no need for this local variable here.
Instead, it should be a variable inside the method isValidMethod and the parameter should be removed from the method signature.

if (Modifier.isPublic(modifiers)
&& !Modifier.isStatic(modifiers)
&& method.getParameterTypes().length == 0
&& !method.isBridge()
&& method.getReturnType() != Void.TYPE
&& isValidMethodName(method.getName())) {
if (isValidMethod(modifiers, method)) {
final String key = getKeyNameFromMethod(method);
if (key != null && !key.isEmpty()) {
try {
Expand Down Expand Up @@ -1806,6 +1801,15 @@ && isValidMethodName(method.getName())) {
}
}

private boolean isValidMethod(int modifiers,Method method) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a JavaDoc comment to this new method.

return Modifier.isPublic(modifiers)
&& !Modifier.isStatic(modifiers)
&& method.getParameterTypes().length == 0
&& !method.isBridge()
&& method.getReturnType() != Void.TYPE
&& isValidMethodName(method.getName());
}

private static boolean isValidMethodName(String name) {
return !"getClass".equals(name) && !"getDeclaringClass".equals(name);
}
Expand Down
Loading