View Javadoc

    1   /*
    2    * InvokeMethodAdapter.java created on 14.09.2006
    3    *
    4    * To change this generated comment go to
    5    * Window>Preferences>Java>Code Generation>Code and Comments
    6    */
    7   package org.codehaus.groovy.classgen;
    8
    9   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, String baseName, boolean createNArgs, boolean skipSpreadSafeAndSafe) {
    18          MethodCallerMultiAdapter mcma = new MethodCallerMultiAdapter();
    19          mcma.skipSpreadSafeAndSafe = skipSpreadSafeAndSafe;
    20          if (createNArgs) {
    21              int numberOfBaseMethods = mcma.numberOfBaseMethods();
    22              mcma.methods = new MethodCaller[(maxArgs+2)*numberOfBaseMethods];
    23              for (int i=0; i<=maxArgs; i++) {
    24                  mcma.methods[i*numberOfBaseMethods] = MethodCaller.newStatic(theClass,baseName+i);
    25                  if (skipSpreadSafeAndSafe) continue;
    26                  mcma.methods[i*numberOfBaseMethods+1] = MethodCaller.newStatic(theClass,baseName+i+"Safe");
    27                  mcma.methods[i*numberOfBaseMethods+2] = MethodCaller.newStatic(theClass,baseName+i+"SpreadSafe");
    28              }
    29              mcma.methods[(maxArgs+1)*numberOfBaseMethods] = MethodCaller.newStatic(theClass,baseName+"N");
    30              if (!skipSpreadSafeAndSafe) {
    31                  mcma.methods[(maxArgs+1)*numberOfBaseMethods+1] = MethodCaller.newStatic(theClass,baseName+"N"+"Safe");
    32                  mcma.methods[(maxArgs+1)*numberOfBaseMethods+2] = MethodCaller.newStatic(theClass,baseName+"N"+"SpreadSafe");
    33              }
    34
    35          } else if (!skipSpreadSafeAndSafe) {
    36              mcma.methods = new MethodCaller[]{
    37                      MethodCaller.newStatic(theClass,baseName),
    38                      MethodCaller.newStatic(theClass,baseName+"Safe"),
    39                      MethodCaller.newStatic(theClass,baseName+"SpreadSafe")
    40              };
    41          } else {
    42              mcma.methods = new MethodCaller[]{
    43                      MethodCaller.newStatic(theClass,baseName)
    44              };
    45          }
    46          return mcma;
    47      }
    48
    49      /***
    50       *
    51       * @param methodVisitor
    52       * @param numberOfArguments a value >0 describing how many arguments are additionally used for the method call
    53       * @param safe
    54       * @param spreadSafe
    55       */
    56      public void call(MethodVisitor methodVisitor, int numberOfArguments, boolean safe, boolean spreadSafe) {
    57          int offset = 0;
    58          if (safe && !skipSpreadSafeAndSafe) offset = 1;
    59          if (spreadSafe && !skipSpreadSafeAndSafe) offset = 2;
    60          if (numberOfArguments>maxArgs || numberOfArguments<0){
    61              offset += (maxArgs+1)*numberOfBaseMethods();
    62          } else {
    63              offset += numberOfArguments*numberOfBaseMethods();
    64          }
    65          methods[offset].call(methodVisitor);
    66      }
    67
    68      private int numberOfBaseMethods(){
    69          if (skipSpreadSafeAndSafe) return 1;
    70          return 3;
    71      }
    72  }