@@ -416,6 +416,8 @@ def printHeader():
416416 print
417417 print "import java.io.IOException;"
418418 print "import java.io.DataInputStream;"
419+ print "import java.lang.reflect.Constructor;"
420+ print "import java.lang.reflect.Field;"
419421 print "import java.util.Collections;"
420422 print "import java.util.HashMap;"
421423 print "import java.util.Map;"
@@ -427,8 +429,7 @@ def printHeader():
427429
428430 def printClassMethods (spec , c ):
429431 print
430- print " public static class %s {" % (java_class_name (c .name ))
431- print " public static final int INDEX = %s;" % (c .index )
432+ print " public final static class %s {" % (java_class_name (c .name ))
432433 for m in c .allMethods ():
433434
434435 def getters ():
@@ -464,9 +465,9 @@ def constructors():
464465
465466 def others ():
466467 print
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 )
468+ print " public int protocolClassId() { return PROTOCOL_CLASS_ID ; }"
469+ print " public int protocolMethodId() { return PROTOCOL_METHOD_ID ; }"
470+ print " public String protocolMethodName() { return PROTOCOL_METHOD_NAME; }"
470471 print
471472 print " public boolean hasContent() { return %s; }" % (trueOrFalse (m .hasContent ))
472473 print
@@ -480,9 +481,12 @@ def trueOrFalse(truthVal):
480481 return "false"
481482
482483 def argument_debug_string ():
484+ anames = [a .name for a in m .arguments ]
483485 appendList = [ "%s=\" )\n .append(this.%s)\n .append(\" "
484- % (a .name , java_field_name (a .name ))
485- for a in m .arguments ]
486+ % (name , java_field_name (name ))
487+ for name in anames ]
488+ if "class-id" in anames and "method-id" in anames :
489+ appendList .append ("protocol-name='\" )\n .append(getProtocolMethodName(this.classId, this.methodId))\n .append(\" '" )
486490 print
487491 print " public void appendArgumentDebugStringTo(StringBuilder acc) {"
488492 print " acc.append(\" (%s)\" );" % ", " .join (appendList )
@@ -499,12 +503,13 @@ def write_arguments():
499503
500504 #start
501505 print
502- print " public static class %s" % (java_class_name (m .name ),)
506+ print " public final static class %s" % (java_class_name (m .name ),)
503507 print " extends Method"
504508 print " implements com.rabbitmq.client.AMQP.%s.%s" % (java_class_name (c .name ), java_class_name (m .name ))
505509 print " {"
506- print " public static final int INDEX = %s;" % (m .index )
507- print
510+ print " private static final int PROTOCOL_CLASS_ID = %s;" % (c .index )
511+ print " private static final int PROTOCOL_METHOD_ID = %s;" % (m .index )
512+ print " public static final String PROTOCOL_METHOD_NAME = \" %s.%s\" ;" % (c .name , m .name )
508513 for a in m .arguments :
509514 print " private final %s %s;" % (java_field_type (spec , a .domain ), java_field_name (a .name ))
510515
@@ -534,30 +539,56 @@ def printMethodVisitor():
534539 print " public Object visit(%s.%s x) throws IOException { throw new UnexpectedMethodError(x); }" % (java_class_name (c .name ), java_class_name (m .name ))
535540 print " }"
536541
537- def printMethodArgumentReader ():
542+ def printGetProtocolMethodNameMethod ():
538543 print
539- print " public static Method readMethodFrom(DataInputStream in) throws IOException {"
540- print " int classId = in.readShort();"
541- print " int methodId = in.readShort();"
544+ print " public final static String getProtocolMethodName(int classId, int methodId) {"
545+ print " Class<? extends Method> clazz = getMethodClass(classId, methodId);"
546+ print " if (clazz != null) {"
547+ print " try {"
548+ print " Field pmnField = clazz.getField(\" PROTOCOL_METHOD_NAME\" );"
549+ print " return (String)pmnField.get(null);"
550+ print " } catch (Exception e) { /*ignore*/ }"
551+ print " }"
552+ print " return null;"
553+ print " }"
554+
555+ def printGetMethodClassMethod ():
556+ print
557+ print " private final static Class<? extends Method> getMethodClass(int classId, int methodId) {"
542558 print " switch (classId) {"
543559 for c in spec .allClasses ():
544560 print " case %s:" % (c .index )
545561 print " switch (methodId) {"
546562 for m in c .allMethods ():
547563 fq_name = java_class_name (c .name ) + '.' + java_class_name (m .name )
548564 print " case %s: {" % (m .index )
549- print " return new %s(new MethodArgumentReader(in)) ;" % (fq_name )
565+ print " return %s.class ;" % (fq_name )
550566 print " }"
551567 print " default: break;"
552568 print " } break;"
553569 print " }"
554570 print
571+ print " return null;"
572+ print " }"
573+
574+ def printMethodArgumentReader ():
575+ print
576+ print " public final static Method readMethodFrom(DataInputStream in) throws IOException {"
577+ print " int classId = in.readShort();"
578+ print " int methodId = in.readShort();"
579+ print " Class<? extends Method> clazz = getMethodClass(classId, methodId);"
580+ print " if (clazz != null) {"
581+ print " try {"
582+ print " Constructor<? extends Method> cons = clazz.getConstructor(MethodArgumentReader.class);"
583+ print " return cons.newInstance(new MethodArgumentReader(in));"
584+ print " } catch (Exception e) { /*ignore*/ }"
585+ print " }"
555586 print " throw new UnknownClassOrMethodId(classId, methodId);"
556587 print " }"
557588
558589 def printContentHeaderReader ():
559590 print
560- print " public static AMQContentHeader readContentHeaderFrom(DataInputStream in) throws IOException {"
591+ print " public final static AMQContentHeader readContentHeaderFrom(DataInputStream in) throws IOException {"
561592 print " int classId = in.readShort();"
562593 print " switch (classId) {"
563594 for c in spec .allClasses ():
@@ -571,11 +602,13 @@ def printContentHeaderReader():
571602
572603 printHeader ()
573604 print
574- print "public class AMQImpl implements AMQP {"
605+ print "public final class AMQImpl implements AMQP {"
575606
576607 for c in spec .allClasses (): printClassMethods (spec ,c )
577608
578609 printMethodVisitor ()
610+ printGetProtocolMethodNameMethod ()
611+ printGetMethodClassMethod ()
579612 printMethodArgumentReader ()
580613 printContentHeaderReader ()
581614
0 commit comments