Skip to content

Commit e8994e2

Browse files
author
Steve Powell
committed
Isolate classId x methodId -> Method algorithm in AMQImpl: getMethodClass(int, int);
use reflection to invoke constructor in readMethodFrom().
1 parent 0e968fb commit e8994e2

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

codegen.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ def printHeader():
416416
print
417417
print "import java.io.IOException;"
418418
print "import java.io.DataInputStream;"
419+
print "import java.lang.reflect.Constructor;"
419420
print "import java.util.Collections;"
420421
print "import java.util.HashMap;"
421422
print "import java.util.Map;"
@@ -428,7 +429,6 @@ def printHeader():
428429
def printClassMethods(spec, c):
429430
print
430431
print " public static class %s {" % (java_class_name(c.name))
431-
print " public static final int INDEX = %s;" % (c.index)
432432
for m in c.allMethods():
433433

434434
def getters():
@@ -464,9 +464,9 @@ def constructors():
464464

465465
def others():
466466
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)
467+
print " public int protocolClassId() { return PROTOCOL_CLASS_ID; }"
468+
print " public int protocolMethodId() { return PROTOCOL_METHOD_ID; }"
469+
print " public String protocolMethodName() { return PROTOCOL_METHOD_NAME; }"
470470
print
471471
print " public boolean hasContent() { return %s; }" % (trueOrFalse(m.hasContent))
472472
print
@@ -503,8 +503,9 @@ def write_arguments():
503503
print " extends Method"
504504
print " implements com.rabbitmq.client.AMQP.%s.%s" % (java_class_name(c.name), java_class_name(m.name))
505505
print " {"
506-
print " public static final int INDEX = %s;" % (m.index)
507-
print
506+
print " private static final int PROTOCOL_CLASS_ID = %s;" % (c.index)
507+
print " private static final int PROTOCOL_METHOD_ID = %s;" % (m.index)
508+
print " public static final String PROTOCOL_METHOD_NAME = \"%s.%s\";" % (c.name, m.name)
508509
for a in m.arguments:
509510
print " private final %s %s;" % (java_field_type(spec, a.domain), java_field_name(a.name))
510511

@@ -534,24 +535,37 @@ def printMethodVisitor():
534535
print " public Object visit(%s.%s x) throws IOException { throw new UnexpectedMethodError(x); }" % (java_class_name(c.name), java_class_name(m.name))
535536
print " }"
536537

537-
def printMethodArgumentReader():
538+
def printGetMethodClassMethod():
538539
print
539-
print " public static Method readMethodFrom(DataInputStream in) throws IOException {"
540-
print " int classId = in.readShort();"
541-
print " int methodId = in.readShort();"
540+
print " public static Class<? extends Method> getMethodClass(int classId, int methodId) {"
542541
print " switch (classId) {"
543542
for c in spec.allClasses():
544543
print " case %s:" % (c.index)
545544
print " switch (methodId) {"
546545
for m in c.allMethods():
547546
fq_name = java_class_name(c.name) + '.' + java_class_name(m.name)
548547
print " case %s: {" % (m.index)
549-
print " return new %s(new MethodArgumentReader(in));" % (fq_name)
548+
print " return %s.class;" % (fq_name)
550549
print " }"
551550
print " default: break;"
552551
print " } break;"
553552
print " }"
554553
print
554+
print " return null;"
555+
print " }"
556+
557+
def printMethodArgumentReader():
558+
print
559+
print " public static Method readMethodFrom(DataInputStream in) throws IOException {"
560+
print " int classId = in.readShort();"
561+
print " int methodId = in.readShort();"
562+
print " Class<? extends Method> clazz = getMethodClass(classId, methodId);"
563+
print " if (clazz != null) {"
564+
print " try {"
565+
print " Constructor<? extends Method> cons = clazz.getConstructor(MethodArgumentReader.class);"
566+
print " return cons.newInstance(new MethodArgumentReader(in));"
567+
print " } catch (Exception e) { /*ignore*/ }"
568+
print " }"
555569
print " throw new UnknownClassOrMethodId(classId, methodId);"
556570
print " }"
557571

@@ -576,6 +590,7 @@ def printContentHeaderReader():
576590
for c in spec.allClasses(): printClassMethods(spec,c)
577591

578592
printMethodVisitor()
593+
printGetMethodClassMethod()
579594
printMethodArgumentReader()
580595
printContentHeaderReader()
581596

0 commit comments

Comments
 (0)