Skip to content

Commit ca6d804

Browse files
author
Steve Powell
committed
Changed codegen to use scalar types for *Properties:
introduce java scalar types functions; update writePropertiesTo and readPropertiesFrom; preserve boxed scalars on constructor to indicate 'unset' by null; adjusted one test to expect a scalar.
1 parent 1108d15 commit ca6d804

File tree

5 files changed

+113
-54
lines changed

5 files changed

+113
-54
lines changed

codegen.py

Lines changed: 99 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,36 @@ def java_constant_name(c):
5151
'Date'
5252
])
5353

54-
javaPropertyTypeMap = {
55-
'octet': 'Integer',
56-
'shortstr': 'String',
57-
'longstr': 'LongString',
58-
'short': 'Integer',
59-
'long': 'Integer',
60-
'longlong': 'Long',
61-
'bit': 'Boolean',
62-
'table': 'Map<String,Object>',
63-
'timestamp': 'Date'
54+
# the types in the range of javaTypeMap must be in java_scala_types
55+
java_scalar_types = set([
56+
'int',
57+
'long',
58+
'boolean'
59+
])
60+
61+
# the java_scalar_types must be in the domain of javaBoxedTypeMap
62+
javaBoxedTypeMap = {
63+
'int': 'Integer',
64+
'long': 'Long',
65+
'boolean': 'Boolean'
6466
}
67+
def java_boxed_type(jtype):
68+
if jtype in java_scalar_types:
69+
return javaBoxedTypeMap[jtype]
70+
else:
71+
return jtype
6572

6673
def java_type(spec, domain):
6774
return javaTypeMap[spec.resolveDomain(domain)]
6875

69-
def java_name(upper, name):
76+
def java_name(upperNext, name):
7077
out = ''
7178
for c in name:
7279
if not c.isalnum():
73-
upper = True
74-
elif upper:
80+
upperNext = True
81+
elif upperNext:
7582
out += c.upper()
76-
upper = False
83+
upperNext = False
7784
else:
7885
out += c
7986
return out
@@ -84,8 +91,6 @@ def java_class_name(name):
8491
def java_getter_name(name):
8592
return java_name(False, 'get-' + name)
8693

87-
def java_property_type(spec, type):
88-
return javaPropertyTypeMap[spec.resolveDomain(type)]
8994
def java_field_name(name):
9095
return java_name(False, name)
9196
def java_field_type(spec, domain):
@@ -108,10 +113,9 @@ def java_field_default_value(type, value):
108113
raise BogusDefaultValue("JSON provided default value {0} for suspicious type {1}".format(value, type))
109114

110115
def typeNameDefault(spec, a):
111-
return (java_field_type(spec, a.domain),
112-
java_field_name(a.name),
113-
java_field_default_value(java_field_type(spec, a.domain),
114-
a.defaultvalue))
116+
fieldType = java_field_type(spec, a.domain)
117+
defaultVal = java_field_default_value(fieldType, a.defaultvalue)
118+
return (fieldType, java_field_name(a.name), defaultVal)
115119

116120
def nullCheckedFields(spec, m):
117121
fieldsToNullCheck = set([])
@@ -236,59 +240,111 @@ def printClassInterfaces():
236240

237241
def printReadPropertiesFrom(c):
238242
print
239-
print """ public void readPropertiesFrom(ContentHeaderPropertyReader reader)
240-
throws IOException
241-
{"""
243+
print " public void readPropertiesFrom(ContentHeaderPropertyReader reader)"
244+
print " throws IOException"
245+
print " {"
246+
242247
for f in c.fields:
243-
print " boolean %s_present = reader.readPresence();" % (java_field_name(f.name))
248+
(jfName, jfType) = (java_field_name(f.name), java_field_type(spec, f.domain))
249+
if jfType in java_scalar_types:
250+
print " this.%sIsSet = reader.readPresence();" % (jfName)
251+
else:
252+
print " boolean %s_present = reader.readPresence();" % (jfName)
244253
print " reader.finishPresence();"
245254
for f in c.fields:
246-
print " this.%s = %s_present ? reader.read%s() : null;" % (java_field_name(f.name), java_field_name(f.name), java_class_name(f.domain))
255+
(jfName, jfType, jfClass) = (java_field_name(f.name), java_field_type(spec, f.domain), java_class_name(f.domain))
256+
if jfType in java_scalar_types:
257+
print " if (this.%sIsSet) this.%s = reader.read%s();" % (jfName, jfName, jfClass)
258+
else:
259+
print " this.%s = %s_present ? reader.read%s() : null;" % (jfName, jfName, jfClass)
260+
247261
print " }"
248262

249263
def printWritePropertiesTo(c):
250264
print
251-
print """ public void writePropertiesTo(ContentHeaderPropertyWriter writer)
252-
throws IOException
253-
{"""
265+
print " public void writePropertiesTo(ContentHeaderPropertyWriter writer)"
266+
print " throws IOException"
267+
print " {"
254268
for f in c.fields:
255-
print " writer.writePresence(this.%s != null);" % (java_field_name(f.name))
269+
(jfName, jfType) = (java_field_name(f.name), java_field_type(spec, f.domain))
270+
if jfType in java_scalar_types:
271+
print " writer.writePresence(this.%sIsSet);" % (jfName)
272+
else:
273+
print " writer.writePresence(this.%s != null);" % (jfName)
274+
256275
print " writer.finishPresence();"
276+
257277
for f in c.fields:
258-
print " if (this.%s != null) { writer.write%s(this.%s); }" % (java_field_name(f.name), java_class_name(f.domain), java_field_name(f.name))
278+
(jfName, jfType, jfClass) = (java_field_name(f.name), java_field_type(spec, f.domain), java_class_name(f.domain))
279+
if jfType in java_scalar_types:
280+
print " if (this.%sIsSet) writer.write%s(this.%s);" % (jfName, jfClass, jfName)
281+
else:
282+
print " if (this.%s != null) writer.write%s(this.%s);" % (jfName, jfClass, jfName)
259283
print " }"
260284

261285
def printAppendArgumentDebugStringTo(c):
262-
appendList = [ "%s=\")\n .append(this.%s)\n .append(\""
263-
% (f.name, java_field_name(f.name))
286+
def optionalValueClause(jField, jType):
287+
if jType in java_scalar_types:
288+
return "this.%sIsSet ? String.valueOf(this.%s) : \"unset\"" % (jField, jField)
289+
else:
290+
return "this.%s" % (jField)
291+
292+
appendList = [ "%s=\")\n .append(%s)\n .append(\""
293+
% (f.name, optionalValueClause(java_field_name(f.name), java_field_type(spec, f.domain)))
264294
for f in c.fields ]
265295
print
266296
print " public void appendArgumentDebugStringTo(StringBuffer acc) {"
267-
print " acc.append(\"(%s)\");" % ", ".join(appendList)
297+
print " acc.append(\"(%s)\");" % (", ".join(appendList))
298+
print " }"
299+
300+
def printPropertiesBuilderClass(c):
301+
print " public static final class Builder {"
302+
for f in c.fields:
303+
print " private %s %s;" % (java_field_type(spec, f.domain),java_field_name(f.name))
304+
268305
print " }"
269-
306+
270307
def printPropertiesClass(c):
308+
def printGetterAndSetter(fieldType, fieldName):
309+
capFieldName = fieldName[0].upper() + fieldName[1:]
310+
print " public %s get%s() { return this.%s; }" % (fieldType, capFieldName, fieldName)
311+
if fieldType in java_scalar_types:
312+
print " public void set%s(%s %s)" % (capFieldName, fieldType, fieldName)
313+
print " { this.%s = %s; this.%sIsSet = true; }" % (fieldName, fieldName, fieldName)
314+
else:
315+
print " public void set%s(%s %s) { this.%s = %s; }" % (capFieldName, fieldType, fieldName, fieldName, fieldName)
316+
317+
jClassName = java_class_name(c.name)
318+
271319
print
272-
print " public static class %(className)s extends %(parentClass)s {" % {'className' : java_class_name(c.name) + 'Properties', 'parentClass' : 'com.rabbitmq.client.impl.AMQ' + java_class_name(c.name) + 'Properties'}
320+
print " public static class %sProperties extends com.rabbitmq.client.impl.AMQ%sProperties {" % (jClassName, jClassName)
273321
#property fields
274322
for f in c.fields:
275-
print " private %s %s;" % (java_property_type(spec, f.domain),java_field_name(f.name))
323+
(fType, fName) = (java_field_type(spec, f.domain),java_field_name(f.name))
324+
if fType in java_scalar_types:
325+
print " private boolean %sIsSet = false;" % (fName)
326+
print " private %s %s;" % (fType, fName)
276327

277328
#explicit constructor
278329
if c.fields:
279330
print
280-
consParmList = [ "%s %s" % (java_property_type(spec,f.domain),java_field_name(f.name))
331+
consParmList = [ "%s %s" % (java_boxed_type(java_field_type(spec,f.domain)),java_field_name(f.name))
281332
for f in c.fields ]
282-
print " public %sProperties(" % (java_class_name(c.name))
333+
print " public %sProperties(" % (jClassName)
283334
print " %s)" % (",\n ".join(consParmList))
284335
print " {"
285336
for f in c.fields:
286-
print " this.%s = %s;" % (java_field_name(f.name), java_field_name(f.name))
337+
(fType, fName) = (java_field_type(spec, f.domain),java_field_name(f.name))
338+
if fType in java_scalar_types:
339+
print " if (%s == null) { this.%sIsSet = false; }" % (fName, fName)
340+
print " else { this.%sIsSet = true; this.%s = %s; }" % (fName, fName, fName)
341+
else:
342+
print " this.%s = %s;" % (fName, fName)
287343
print " }"
288344

289345
#default constructor
290346
print
291-
print " public %sProperties() {}" % (java_class_name(c.name))
347+
print " public %sProperties() {}" % (jClassName)
292348

293349
#class properties
294350
print " public int getClassId() { return %i; }" % (c.index)
@@ -297,15 +353,13 @@ def printPropertiesClass(c):
297353
#accessor methods
298354
print
299355
for f in c.fields:
300-
print """ public %(fieldType)s get%(capFieldName)s() { return %(fieldName)s; }
301-
public void set%(capFieldName)s(%(fieldType)s %(fieldName)s) { this.%(fieldName)s = %(fieldName)s; }""" % \
302-
{'fieldType' : java_property_type(spec, f.domain), \
303-
'capFieldName' : (java_field_name(f.name)[0].upper() + java_field_name(f.name)[1:]), \
304-
'fieldName' : java_field_name(f.name)}
356+
printGetterAndSetter(java_field_type(spec, f.domain), java_field_name(f.name))
305357

306358
printReadPropertiesFrom(c)
307359
printWritePropertiesTo(c)
308360
printAppendArgumentDebugStringTo(c)
361+
printPropertiesBuilderClass(c)
362+
309363
print " }"
310364

311365
def printPropertiesClasses():

src/com/rabbitmq/client/BasicProperties.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ public interface BasicProperties {
4444
* Retrieve the value in the deliveryMode field.
4545
* @return deliveryMode field, or null if the field has not been set.
4646
*/
47-
public abstract Integer getDeliveryMode();
47+
public abstract int getDeliveryMode();
4848

4949
/**
5050
* Retrieve the value in the priority field.
5151
* @return priority field, or null if the field has not been set.
5252
*/
53-
public abstract Integer getPriority();
53+
public abstract int getPriority();
5454

5555
/**
5656
* Retrieve the value in the correlationId field.
@@ -122,13 +122,13 @@ public interface BasicProperties {
122122
* Set the deliveryMode field, or null indicating the field is not set
123123
* @param deliveryMode the value to set the field to
124124
*/
125-
public abstract void setDeliveryMode(Integer deliveryMode);
125+
public abstract void setDeliveryMode(int deliveryMode);
126126

127127
/**
128128
* Set the priority field, or null indicating the field is not set
129129
* @param priority the value to set the field to
130130
*/
131-
public abstract void setPriority(Integer priority);
131+
public abstract void setPriority(int priority);
132132

133133
/**
134134
* Set the correlationId field, or null indicating the field is not set

src/com/rabbitmq/client/impl/ContentHeaderPropertyReader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public ContentHeaderPropertyReader(DataInputStream in) throws IOException {
4848
this.bitCount = 15; // forces a flagWord read
4949
}
5050

51-
public boolean isContinuationBitSet() {
51+
private boolean isContinuationBitSet() {
5252
return (flagWord & 1) != 0;
5353
}
5454

@@ -109,7 +109,7 @@ public Map<String, Object> readTable() throws IOException {
109109
}
110110

111111
/** Reads and returns an AMQP octet content header field. */
112-
public Integer readOctet() throws IOException {
112+
public int readOctet() throws IOException {
113113
return in.readOctet();
114114
}
115115

src/com/rabbitmq/client/impl/ContentHeaderPropertyWriter.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
import com.rabbitmq.client.ContentHeader;
2626

2727
/**
28-
* Generates an AMQP wire-protocol packet from a {@link ContentHeader}. Methods on this object are usually called from autogenerated code.
28+
* Generates an AMQP wire-protocol packet from a {@link ContentHeader}.
29+
* Methods on this object are usually called from autogenerated code.
2930
*/
3031
public class ContentHeaderPropertyWriter {
3132
/** Accumulates our output */
@@ -46,7 +47,7 @@ public ContentHeaderPropertyWriter(DataOutputStream out) {
4647
this.bitCount = 0;
4748
}
4849

49-
public void emitFlagWord(boolean continuationBit) throws IOException {
50+
private void emitFlagWord(boolean continuationBit) throws IOException {
5051
out.writeShort(continuationBit ? (flagWord | 1) : flagWord);
5152
flagWord = 0;
5253
bitCount = 0;
@@ -100,6 +101,10 @@ public void writeOctet(Integer octet) throws IOException {
100101
out.writeOctet(octet);
101102
}
102103

104+
public void writeOctet(int octet) throws IOException {
105+
out.writeOctet(octet);
106+
}
107+
103108
public void writeTimestamp(Date timestamp) throws IOException {
104109
out.writeTimestamp(timestamp);
105110
}

test/src/com/rabbitmq/client/test/ClonePropertiesTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void testPropertyClonePreservesValues()
5454
assertEquals(MessageProperties.MINIMAL_PERSISTENT_BASIC.getDeliveryMode(),
5555
((BasicProperties) MessageProperties.MINIMAL_PERSISTENT_BASIC.clone())
5656
.getDeliveryMode());
57-
assertEquals((Integer) 2,
57+
assertEquals(2,
5858
((BasicProperties) MessageProperties.MINIMAL_PERSISTENT_BASIC.clone())
5959
.getDeliveryMode());
6060
}

0 commit comments

Comments
 (0)