Skip to content

Commit 5809821

Browse files
author
Steve Powell
committed
Copy map parameters and protect with unmodifiableMap() for subsequent getters;
small changes in JSONWriter.java to reduce warnings and improve performance.
1 parent 29aa76a commit 5809821

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

codegen.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ def printHeader():
150150
print "package com.rabbitmq.client;"
151151
print
152152
print "import java.io.IOException;"
153+
print "import java.util.Collections;"
154+
print "import java.util.HashMap;"
153155
print "import java.util.Map;"
154156
print "import java.util.Date;"
155157
print
@@ -188,8 +190,14 @@ def genFields(spec, m):
188190
def genArgMethods(spec, m):
189191
for a in m.arguments:
190192
(jfType, jfName, jfDefault) = typeNameDefault(spec, a)
191-
print " public Builder %s(%s %s)" % (jfName, jfType, jfName)
192-
print " { this.%s = %s; return this; }" % (jfName, jfName)
193+
194+
if jfType == "Map<String,Object>":
195+
print " public Builder %s(%s %s)" % (jfName, jfType, jfName)
196+
print " { this.%s = %s==null ? null : Collections.unmodifiableMap(new HashMap<String,Object>(%s)); return this; }" % (jfName, jfName, jfName)
197+
else:
198+
print " public Builder %s(%s %s)" % (jfName, jfType, jfName)
199+
print " { this.%s = %s; return this; }" % (jfName, jfName)
200+
193201
if jfType == "boolean":
194202
print " public Builder %s()" % (jfName)
195203
print " { return this.%s(true); }" % (jfName)

src/com/rabbitmq/tools/json/JSONWriter.java

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
public class JSONWriter {
4242
private boolean indentMode = false;
4343
private int indentLevel = 0;
44-
private StringBuffer buf = new StringBuffer();
44+
private StringBuilder buf = new StringBuilder();
4545

4646
public JSONWriter() {}
4747

@@ -86,6 +86,7 @@ public String write(boolean b) {
8686
return write(Boolean.valueOf(b));
8787
}
8888

89+
@SuppressWarnings("unchecked")
8990
private void value(Object object) {
9091
if (object == null) add("null");
9192
else if (object instanceof JSONSerializable) {
@@ -97,7 +98,7 @@ else if (object instanceof JSONSerializable) {
9798
else if (object instanceof Character) string(object);
9899
else if (object instanceof Map) map((Map<String, Object>) object);
99100
else if (object.getClass().isArray()) array(object);
100-
else if (object instanceof Collection) array(((Collection) object).iterator());
101+
else if (object instanceof Collection) array(((Collection<?>) object).iterator());
101102
else bean(object);
102103
}
103104

@@ -111,7 +112,7 @@ private void bean(Object object) {
111112
* @param object the object
112113
* @param properties explicit list of property/field names to include - may be null for "all"
113114
*/
114-
public void writeLimited(Class klass, Object object, String[] properties) {
115+
public void writeLimited(Class<?> klass, Object object, String[] properties) {
115116
Set<String> propertiesSet = null;
116117
if (properties != null) {
117118
propertiesSet = new HashSet<String>();
@@ -120,7 +121,7 @@ public void writeLimited(Class klass, Object object, String[] properties) {
120121
}
121122
}
122123

123-
add("{"); indentLevel += 2; newline();
124+
add('{'); indentLevel += 2; newline();
124125
boolean needComma = false;
125126

126127
BeanInfo info;
@@ -174,7 +175,7 @@ public void writeLimited(Class klass, Object object, String[] properties) {
174175
}
175176
}
176177

177-
indentLevel -= 2; newline(); add("}");
178+
indentLevel -= 2; newline(); add('}');
178179
}
179180

180181
private void add(String name, Object value) {
@@ -185,35 +186,45 @@ private void add(String name, Object value) {
185186
}
186187

187188
private void map(Map<String, Object> map) {
188-
add("{"); indentLevel += 2; newline();
189+
add('{'); indentLevel += 2; newline();
189190
Iterator<String> it = map.keySet().iterator();
191+
if (it.hasNext()) {
192+
mapEntry(it.next(), map);
193+
}
190194
while (it.hasNext()) {
195+
add(','); newline();
191196
Object key = it.next();
192197
value(key);
193-
add(":");
198+
add(':');
194199
value(map.get(key));
195-
if (it.hasNext()) { add(","); newline(); }
196200
}
197-
indentLevel -= 2; newline(); add("}");
201+
indentLevel -= 2; newline(); add('}');
202+
}
203+
private void mapEntry(Object key, Map<String, Object> map) {
204+
value(key);
205+
add(':');
206+
value(map.get(key));
198207
}
199208

200-
private void array(Iterator it) {
201-
add("[");
209+
private void array(Iterator<?> it) {
210+
add('[');
211+
if (it.hasNext()) value(it.next());
202212
while (it.hasNext()) {
213+
add(',');
203214
value(it.next());
204-
if (it.hasNext()) add(",");
205215
}
206-
add("]");
216+
add(']');
207217
}
208218

209219
private void array(Object object) {
210-
add("[");
220+
add('[');
211221
int length = Array.getLength(object);
212-
for (int i = 0; i < length; ++i) {
222+
if (length > 0) value(Array.get(object, 0));
223+
for (int i = 1; i < length; ++i) {
224+
add(',');
213225
value(Array.get(object, i));
214-
if (i < length - 1) add(',');
215226
}
216-
add("]");
227+
add(']');
217228
}
218229

219230
private void bool(boolean b) {

0 commit comments

Comments
 (0)