@@ -72,13 +72,6 @@ def java_boxed_type(jtype):
7272def java_type (spec , domain ):
7373 return javaTypeMap [spec .resolveDomain (domain )]
7474
75- def java_const_name (nm ):
76- out = ''
77- for c in nm :
78- if c .isalnum (): out += c .upper ()
79- else : out += '_'
80- return out
81-
8275def java_name (upperNext , name ):
8376 out = ''
8477 for c in name :
@@ -320,11 +313,11 @@ def printBuilderSetter(fieldType, fieldName):
320313 def printPropertiesBuilder (c ):
321314 print
322315 print " public Builder builder() {"
323- print " Builder builder = new Builder(); "
316+ print " Builder builder = new Builder()"
324317 setFieldList = [ "%s(%s)" % (fn , fn )
325318 for fn in [ java_field_name (f .name ) for f in c .fields ]
326319 ]
327- print " builder .%s;" % ("\n ." .join (setFieldList ))
320+ print " .%s;" % ("\n ." .join (setFieldList ))
328321 print " return builder;"
329322 print " }"
330323
@@ -432,71 +425,12 @@ def printHeader():
432425 print "import com.rabbitmq.client.UnknownClassOrMethodId;"
433426 print "import com.rabbitmq.client.UnexpectedMethodError;"
434427
435- def printMethodsEnum ():
436- print " public enum MethodCodex {"
437- first = True
438- for c in spec .allClasses ():
439- for m in c .allMethods ():
440- fullConstName = java_const_name (c .name + '-' + m .name )
441- fullClassName = java_class_name (c .name ) + '.' + java_class_name (m .name )
442- consArgs = [ "rdr.read%s()" % (java_class_name (spec .resolveDomain (a .domain ))) for a in m .arguments ]
443- if first :
444- first = False
445- else :
446- print " },"
447- print " %s(%s, %s, \" %s.%s\" ) {" % (fullConstName , c .index , m .index , c .name , m .name )
448- print " Method readArgs(MethodArgumentReader rdr) throws IOException {"
449- print " return new %s(%s);" % (fullClassName , ", " .join (consArgs ))
450- print " }"
451- print " void writeArgs(MethodArgumentWriter writer, Method m) throws IOException {"
452- if m .arguments :
453- print " %s method = (%s) m;" % (fullClassName , fullClassName )
454- for a in m .arguments :
455- print " writer.write%s(method.%s);" % (java_class_name (spec .resolveDomain (a .domain )), java_field_name (a .name ))
456- print " }"
457- print " };"
458- print
459- print " private final int classId; // must be >=0 and <256"
460- print " private final int methodId; // must be >=0 and <256"
461- print " private final String methodName;"
462- print " MethodCodex(int classId, int methodId, String methodName) {"
463- print " this.classId = classId;"
464- print " this.methodId = methodId;"
465- print " this.methodName = methodName;"
466- print " }"
467- print " abstract Method readArgs(MethodArgumentReader rdr) throws IOException;"
468- print " abstract void writeArgs(MethodArgumentWriter writer, Method m) throws IOException;"
469- print " int getClassId() { return this.classId; }"
470- print " int getMethodId() { return this.methodId; }"
471- print " String getMethodName() { return this.methodName; }"
472- print " private static final Map<Integer, MethodCodex> codexMap = new HashMap<Integer, MethodCodex>();"
473- print " static { // build index of MethodCodices"
474- print " for (MethodCodex m : values()) {"
475- print " int key = pack(m.classId, m.methodId);"
476- print " if (codexMap.containsKey(key))"
477- print " throw new IllegalStateException(\" Protocol defines <classId,methodId>=<\" + m.classId + \" ,\" + m.methodId + \" > more than once.\" );"
478- print " codexMap.put(pack(m.classId, m.methodId), m);"
479- print " }"
480- print " }"
481- print " private static final int pack(int cid, int mid) {"
482- print " return (cid<<8) | (0xff & mid);"
483- print " }"
484- print " public static final MethodCodex lookupCodex(int classId, int methodId) {"
485- print " return codexMap.get(pack(classId, methodId));"
486- print " }"
487- print " }"
488-
489428 def printClassMethods (spec , c ):
490429 print
491- print " public final static class %s {" % (java_class_name (c .name ))
430+ print " public static class %s {" % (java_class_name (c .name ))
431+ print " public static final int INDEX = %s;" % (c .index )
492432 for m in c .allMethods ():
493433
494- def fields ():
495- if m .arguments :
496- print
497- for a in m .arguments :
498- print " private final %s %s;" % (java_field_type (spec , a .domain ), java_field_name (a .name ))
499-
500434 def getters ():
501435 if m .arguments :
502436 print
@@ -523,48 +457,65 @@ def constructors():
523457
524458 print " }"
525459
460+ consArgs = [ "rdr.read%s()" % (java_class_name (spec .resolveDomain (a .domain ))) for a in m .arguments ]
461+ print " public %s(MethodArgumentReader rdr) throws IOException {" % (java_class_name (m .name ))
462+ print " this(%s);" % (", " .join (consArgs ))
463+ print " }"
464+
526465 def others ():
527466 print
528- print " protected MethodCodex getCodex() { return %s.codex; }" % (java_class_name (m .name ))
467+ print " public int protocolClassId() { return %s; }" % (c .index )
468+ print " public int protocolMethodId() { return %s; }" % (m .index )
469+ print " public String protocolMethodName() { return \" %s.%s\" ;}" % (c .name , m .name )
529470 print
530471 print " public boolean hasContent() { return %s; }" % (trueOrFalse (m .hasContent ))
531472 print
532473 print " public Object visit(MethodVisitor visitor) throws IOException"
533474 print " { return visitor.visit(this); }"
534475
476+ def trueOrFalse (truthVal ):
477+ if truthVal :
478+ return "true"
479+ else :
480+ return "false"
481+
535482 def argument_debug_string ():
536- anames = [a .name for a in m .arguments ]
537483 appendList = [ "%s=\" )\n .append(this.%s)\n .append(\" "
538- % (name , java_field_name (name ))
539- for name in anames ]
540- if "class-id" in anames and "method-id" in anames :
541- appendList .append ("protocol-name='\" )\n .append(getProtocolMethodName(this.classId, this.methodId))\n .append(\" '" )
484+ % (a .name , java_field_name (a .name ))
485+ for a in m .arguments ]
542486 print
543487 print " public void appendArgumentDebugStringTo(StringBuilder acc) {"
544488 print " acc.append(\" (%s)\" );" % ", " .join (appendList )
545489 print " }"
546490
547- def trueOrFalse (truthVal ):
548- if truthVal :
549- return "true"
550- else :
551- return "false"
491+ def write_arguments ():
492+ print
493+ print " public void writeArgumentsTo(MethodArgumentWriter writer)"
494+ print " throws IOException"
495+ print " {"
496+ for a in m .arguments :
497+ print " writer.write%s(this.%s);" % (java_class_name (spec .resolveDomain (a .domain )), java_field_name (a .name ))
498+ print " }"
552499
553500 #start
554501 print
555- print " public final static class %s" % (java_class_name (m .name ),)
502+ print " public static class %s" % (java_class_name (m .name ),)
556503 print " extends Method"
557504 print " implements com.rabbitmq.client.AMQP.%s.%s" % (java_class_name (c .name ), java_class_name (m .name ))
558505 print " {"
559- print " private static final MethodCodex codex = MethodCodex.%s;" % (java_const_name (c .name + '-' + m .name ))
506+ print " public static final int INDEX = %s;" % (m .index )
507+ print
508+ for a in m .arguments :
509+ print " private final %s %s;" % (java_field_type (spec , a .domain ), java_field_name (a .name ))
560510
561- fields ()
562511 getters ()
563512 constructors ()
564513 others ()
514+
565515 argument_debug_string ()
566- print " }"
516+ write_arguments ()
567517
518+ print " }"
568519 print " }"
569520
570521 def printMethodVisitor ():
@@ -583,31 +534,30 @@ def printMethodVisitor():
583534 print " public Object visit(%s.%s x) throws IOException { throw new UnexpectedMethodError(x); }" % (java_class_name (c .name ), java_class_name (m .name ))
584535 print " }"
585536
586- def printGetProtocolMethodNameMethod ():
587- print
588- print " public final static String getProtocolMethodName(int classId, int methodId) {"
589- print " MethodCodex codex = MethodCodex.lookupCodex(classId, methodId);"
590- print " if (codex != null) {"
591- print " return codex.getMethodName();"
592- print " }"
593- print " return \" UNKNOWN_CLASSID_METHODID\" ;"
594- print " }"
595-
596537 def printMethodArgumentReader ():
597538 print
598- print " public final static Method readMethodFrom(DataInputStream in) throws IOException {"
539+ print " public static Method readMethodFrom(DataInputStream in) throws IOException {"
599540 print " int classId = in.readShort();"
600541 print " int methodId = in.readShort();"
601- print " MethodCodex codex = MethodCodex.lookupCodex(classId, methodId);"
602- print " if (codex != null) {"
603- print " return codex.readArgs(new MethodArgumentReader(in));"
542+ print " switch (classId) {"
543+ for c in spec .allClasses ():
544+ print " case %s:" % (c .index )
545+ print " switch (methodId) {"
546+ for m in c .allMethods ():
547+ fq_name = java_class_name (c .name ) + '.' + java_class_name (m .name )
548+ print " case %s: {" % (m .index )
549+ print " return new %s(new MethodArgumentReader(new ValueReader(in)));" % (fq_name )
550+ print " }"
551+ print " default: break;"
552+ print " } break;"
604553 print " }"
554+ print
605555 print " throw new UnknownClassOrMethodId(classId, methodId);"
606556 print " }"
607557
608558 def printContentHeaderReader ():
609559 print
610- print " public final static AMQContentHeader readContentHeaderFrom(DataInputStream in) throws IOException {"
560+ print " public static AMQContentHeader readContentHeaderFrom(DataInputStream in) throws IOException {"
611561 print " int classId = in.readShort();"
612562 print " switch (classId) {"
613563 for c in spec .allClasses ():
@@ -619,17 +569,13 @@ def printContentHeaderReader():
619569 print " throw new UnknownClassOrMethodId(classId);"
620570 print " }"
621571
622- #start
623572 printHeader ()
624573 print
625- print "public final class AMQImpl implements AMQP {"
626-
627- printMethodsEnum ()
574+ print "public class AMQImpl implements AMQP {"
628575
629576 for c in spec .allClasses (): printClassMethods (spec ,c )
630-
577+
631578 printMethodVisitor ()
632- printGetProtocolMethodNameMethod ()
633579 printMethodArgumentReader ()
634580 printContentHeaderReader ()
635581
0 commit comments