01: /*
02: * InvokeMethodAdapter.java created on 14.09.2006
03: *
04: * To change this generated comment go to
05: * Window>Preferences>Java>Code Generation>Code and Comments
06: */
07: package org.codehaus.groovy.classgen;
08:
09: import org.objectweb.asm.MethodVisitor;
10:
11: public class MethodCallerMultiAdapter {
12: private MethodCaller[] methods;
13: boolean skipSpreadSafeAndSafe;
14:
15: public final static int maxArgs = 0;
16:
17: public static MethodCallerMultiAdapter newStatic(Class theClass,
18: String baseName, boolean createNArgs,
19: boolean skipSpreadSafeAndSafe) {
20: MethodCallerMultiAdapter mcma = new MethodCallerMultiAdapter();
21: mcma.skipSpreadSafeAndSafe = skipSpreadSafeAndSafe;
22: if (createNArgs) {
23: int numberOfBaseMethods = mcma.numberOfBaseMethods();
24: mcma.methods = new MethodCaller[(maxArgs + 2)
25: * numberOfBaseMethods];
26: for (int i = 0; i <= maxArgs; i++) {
27: mcma.methods[i * numberOfBaseMethods] = MethodCaller
28: .newStatic(theClass, baseName + i);
29: if (skipSpreadSafeAndSafe)
30: continue;
31: mcma.methods[i * numberOfBaseMethods + 1] = MethodCaller
32: .newStatic(theClass, baseName + i + "Safe");
33: mcma.methods[i * numberOfBaseMethods + 2] = MethodCaller
34: .newStatic(theClass, baseName + i
35: + "SpreadSafe");
36: }
37: mcma.methods[(maxArgs + 1) * numberOfBaseMethods] = MethodCaller
38: .newStatic(theClass, baseName + "N");
39: if (!skipSpreadSafeAndSafe) {
40: mcma.methods[(maxArgs + 1) * numberOfBaseMethods + 1] = MethodCaller
41: .newStatic(theClass, baseName + "N" + "Safe");
42: mcma.methods[(maxArgs + 1) * numberOfBaseMethods + 2] = MethodCaller
43: .newStatic(theClass, baseName + "N"
44: + "SpreadSafe");
45: }
46:
47: } else if (!skipSpreadSafeAndSafe) {
48: mcma.methods = new MethodCaller[] {
49: MethodCaller.newStatic(theClass, baseName),
50: MethodCaller.newStatic(theClass, baseName + "Safe"),
51: MethodCaller.newStatic(theClass, baseName
52: + "SpreadSafe") };
53: } else {
54: mcma.methods = new MethodCaller[] { MethodCaller.newStatic(
55: theClass, baseName) };
56: }
57: return mcma;
58: }
59:
60: /**
61: *
62: * @param methodVisitor
63: * @param numberOfArguments a value >0 describing how many arguments are additionally used for the method call
64: * @param safe
65: * @param spreadSafe
66: */
67: public void call(MethodVisitor methodVisitor,
68: int numberOfArguments, boolean safe, boolean spreadSafe) {
69: int offset = 0;
70: if (safe && !skipSpreadSafeAndSafe)
71: offset = 1;
72: if (spreadSafe && !skipSpreadSafeAndSafe)
73: offset = 2;
74: if (numberOfArguments > maxArgs || numberOfArguments < 0) {
75: offset += (maxArgs + 1) * numberOfBaseMethods();
76: } else {
77: offset += numberOfArguments * numberOfBaseMethods();
78: }
79: methods[offset].call(methodVisitor);
80: }
81:
82: private int numberOfBaseMethods() {
83: if (skipSpreadSafeAndSafe)
84: return 1;
85: return 3;
86: }
87: }
|