/**
   * @return a reference to the public static serializableInstance() method of clazz, if there is
   *     one; otherwise, returns null.
   */
  private Method serializableInstanceMethod(Class clazz) {
    Method[] methods = clazz.getMethods();

    for (Method method : methods) {
      if ("serializableInstance".equals(method.getName())) {
        Class[] parameterTypes = method.getParameterTypes();

        if (!(parameterTypes.length == 0)) {
          continue;
        }

        if (!(Modifier.isStatic(method.getModifiers()))) {
          continue;
        }

        if (Modifier.isAbstract(method.getModifiers())) {
          continue;
        }

        return method;
      }
    }

    return null;
  }
Exemplo n.º 2
0
 /**
  * Returns all public methods declared by the specified class. This excludes inherited methods.
  *
  * @param clazz the class from which to pull public declared methods
  * @return the public methods declared in the specified class
  * @see Class#getDeclaredMethods()
  */
 static Method[] getMethodList(Class clazz) {
   Method[] methods = null;
   try {
     // getDeclaredMethods may be rejected by the security manager
     // but getMethods is more expensive
     if (!sawSecurityException) {
       methods = clazz.getDeclaredMethods();
     }
   } catch (SecurityException e) {
     // If we get an exception once, give up on getDeclaredMethods
     sawSecurityException = true;
   }
   if (methods == null) {
     methods = clazz.getMethods();
   }
   int count = 0;
   for (int i = 0; i < methods.length; i++) {
     if (sawSecurityException
         ? methods[i].getDeclaringClass() != clazz
         : !Modifier.isPublic(methods[i].getModifiers())) {
       methods[i] = null;
     } else {
       count++;
     }
   }
   Method[] result = new Method[count];
   int j = 0;
   for (int i = 0; i < methods.length; i++) {
     if (methods[i] != null) {
       result[j++] = methods[i];
     }
   }
   return result;
 }
Exemplo n.º 3
0
  static <T> T set(Class<T> interf, Object value) {
    Properties p = new Properties();
    Method ms[] = interf.getMethods();

    for (Method m : ms) {
      p.put(m.getName(), value);
    }
    return Configurable.createConfigurable(interf, (Map<Object, Object>) p);
  }
    /**
     * This function returns the check_modified method from the class type provided.
     *
     * @param theClass the class in which to find the check_modified method
     * @return the check_modified method if it exists
     * @throws RuntimeException if check_modified does not exist.
     */
    private static Method getCheckModified(Class<? extends Invariant> theClass) {
      Method[] methods = theClass.getMethods();

      Method currentMethod;
      for (int i = 0; i < methods.length; i++) {
        currentMethod = methods[i];
        if (currentMethod.getName().lastIndexOf("check_modified")
            != -1) { // Method should be called check_modified
          return currentMethod;
        }
      }
      throw new RuntimeException("Cannot find check_modified method");
    }
    /**
     * @return the method of invariant named by theClass that produces a String representation of
     *     the invariant.
     */
    private static Method getOutputProducer(Class<? extends Invariant> theClass) {
      Method[] methods = theClass.getMethods();

      Method currentMethod;
      for (int i = 0; i < methods.length; i++) {
        currentMethod = methods[i];

        // Method should be called format_using
        if (currentMethod.getName().lastIndexOf("format_using") != -1) {
          return currentMethod;
        }
      }
      throw new RuntimeException("Cannot find format_using method");
    }
 public AnnotatedClass(Class clazz) {
   this.clazz = clazz;
   this.className = clazz.getName();
   this.initialize();
   try {
     Method[] methods = clazz.getMethods();
     for (Method method : methods) {
       if (method.isAnnotationPresent(RequestMapping.class)) {
         this.listOfMethods.add(new AnnotatedMethod(method, this.path));
       }
     }
   } catch (NoClassDefFoundError error) {
     System.err.println("No Class Def Found Error: " + className);
   }
 }
Exemplo n.º 7
0
  public static void invokeSetMethodCaseInsensitive(Object obj, String prop, String value)
      throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    String alternateMethodName = null;
    Class cl = obj.getClass();

    String setMeth = "set" + prop;

    Method[] methodsList = cl.getMethods();
    boolean methodFound = false;
    int i = 0;
    for (i = 0; i < methodsList.length; ++i) {
      if (methodsList[i].getName().equalsIgnoreCase(setMeth) == true) {
        Class[] parameterTypes = methodsList[i].getParameterTypes();
        if (parameterTypes.length == 1) {
          if (parameterTypes[0].getName().equals("java.lang.String")) {
            methodFound = true;
            break;
          } else alternateMethodName = methodsList[i].getName();
        }
      }
    }
    if (methodFound == true) {
      Object[] params = {value};
      methodsList[i].invoke(obj, params);
      return;
    }
    if (alternateMethodName != null) {
      try {
        // try int method
        Class[] cldef = {Integer.TYPE};
        Method meth = cl.getMethod(alternateMethodName, cldef);
        Object[] params = {Integer.valueOf(value)};
        meth.invoke(obj, params);
        return;
      } catch (NoSuchMethodException nsmex) {
        // try boolean method
        Class[] cldef = {Boolean.TYPE};
        Method meth = cl.getMethod(alternateMethodName, cldef);
        Object[] params = {Boolean.valueOf(value)};
        meth.invoke(obj, params);
        return;
      }

    } else throw new NoSuchMethodException(setMeth);
  }
  //
  // Return the methods of an interface in a deterministic
  // order. Class.getMethods() does not do us this favor.
  //
  private Method[] sortMethods(Class iface) throws Exception {
    Method[] raw = iface.getMethods();
    int count = raw.length;
    Method[] cooked = new Method[count];
    MethodSortable[] sortables = new MethodSortable[count];

    for (int i = 0; i < count; i++) {
      sortables[i] = new MethodSortable(raw[i]);
    }

    Arrays.sort(sortables);

    for (int i = 0; i < count; i++) {
      cooked[i] = sortables[i].getMethod();
    }

    return cooked;
  }
Exemplo n.º 9
0
  private DependencyAnnotations createDependencyAnnotations(Class ruleClass) {
    DependencyAnnotations da = new DependencyAnnotations();

    for (Method m : ruleClass.getMethods()) {
      Consumes consumes = m.getAnnotation(Consumes.class);
      if (consumes != null) {
        // System.out.println("Adding consumes "+m.getName());
        da.addConsumesMethod(consumes.value(), m);
      } else {
        Provides provides = m.getAnnotation(Provides.class);
        if (provides != null) {
          // System.out.println("Adding provides "+m.getName());
          da.addProvidesMethod(provides.value(), m);
        }
      }
    }

    return da;
  }
Exemplo n.º 10
0
  /**
   * Enumerate the methods of the Clob interface and get the list of methods present in the
   * interface
   *
   * @param LOB an instance of the Clob interface implementation
   */
  void buildMethodList(Object LOB) throws IllegalAccessException, InvocationTargetException {
    // If the given method throws the correct exception
    // set this to true and add it to the
    boolean valid = true;

    // create a list of the methods that fail the test
    Vector<Method> methodList = new Vector<Method>();

    // The class whose methods are to be verified
    Class clazz = Clob.class;

    // The list of the methods in the class that need to be invoked
    // and verified
    Method[] methods = clazz.getMethods();

    // Check each of the methods to ensure that
    // they throw the required exception
    for (int i = 0; i < methods.length; i++) {
      if (!checkIfExempted(methods[i])) {
        valid = checkIfMethodThrowsSQLException(LOB, methods[i]);

        // add the method to the list if the method does
        // not throw the required exception
        if (valid == false) methodList.add(methods[i]);

        // reset valid
        valid = true;
      }
    }

    if (!methodList.isEmpty()) {
      int c = 0;
      String failureMessage = "The Following methods don't throw " + "required exception - ";
      for (Method m : methodList) {
        c = c + 1;
        if (c == methodList.size() && c != 1) failureMessage += " & ";
        else if (c != 1) failureMessage += " , ";
        failureMessage += m.getName();
      }
      fail(failureMessage);
    }
  }
Exemplo n.º 11
0
  public boolean handleCommand(String line, PrintWriter out) {
    String[] split = line.split("[\t ]");
    String commandName = split[0];

    try {
      Method[] methods = exportedInterface.getMethods();
      for (Method method : methods) {
        if (method.getName().equals(commandName) && method.getReturnType() == void.class) {
          return invokeMethod(line, out, method, split);
        }
      }
      throw new NoSuchMethodException();
    } catch (NoSuchMethodException nsmex) {
      out.println(fullName + ": didn't understand request \"" + line + "\".");
    } catch (Exception ex) {
      Log.warn(fullName + ": exception thrown while handling command \"" + line + "\".", ex);
      out.println(fullName + ": request denied \"" + line + "\" (" + ex.toString() + ").");
    } finally {
      out.flush();
      out.close();
    }
    return false;
  }
Exemplo n.º 12
0
  /**
   * Construct a client-side proxy that implements the named protocol, talking to a server at the
   * named address.
   *
   * @param protocol protocol
   * @param clientVersion client's version
   * @param addr server address
   * @param ticket security ticket
   * @param conf configuration
   * @param factory socket factory
   * @param rpcTimeout max time for each rpc; 0 means no timeout
   * @return the proxy
   * @throws IOException if any error occurs
   */
  @SuppressWarnings("unchecked")
  public static <T extends VersionedProtocol> ProtocolProxy<T> getProtocolProxy(
      Class<T> protocol,
      long clientVersion,
      InetSocketAddress addr,
      UserGroupInformation ticket,
      Configuration conf,
      SocketFactory factory,
      int rpcTimeout)
      throws IOException {
    T proxy =
        (T)
            Proxy.newProxyInstance(
                protocol.getClassLoader(),
                new Class[] {protocol},
                new Invoker(addr, ticket, conf, factory, rpcTimeout, protocol));
    String protocolName = protocol.getName();

    try {
      ProtocolSignature serverInfo =
          proxy.getProtocolSignature(
              protocolName, clientVersion, ProtocolSignature.getFingerprint(protocol.getMethods()));
      return new ProtocolProxy<T>(protocol, proxy, serverInfo.getMethods());
    } catch (RemoteException re) {
      IOException ioe = re.unwrapRemoteException(IOException.class);
      if (ioe.getMessage()
          .startsWith(IOException.class.getName() + ": " + NoSuchMethodException.class.getName())) {
        // Method getProtocolSignature not supported
        long serverVersion = proxy.getProtocolVersion(protocol.getName(), clientVersion);
        if (serverVersion == clientVersion) {
          return new ProtocolProxy<T>(protocol, proxy, null);
        }
        throw new VersionMismatch(protocolName, clientVersion, serverVersion, proxy);
      }
      throw re;
    }
  }
Exemplo n.º 13
0
  /**
   * Given class of the interface for a fluid engine, build a concrete instance of that interface
   * that calls back into a Basic Engine for all methods in the interface.
   */
  private static BasicEngine buildCallbackEngine(String engineInterface, Class cEngine) {
    // build up list of callbacks for <engineInterface>
    StringBuffer decls = new StringBuffer();
    Method[] engineMethods = cEngine.getMethods();
    for (int i = 0; i < engineMethods.length; i++) {
      Method meth = engineMethods[i];
      // skip methods that aren't declared in the interface
      if (meth.getDeclaringClass() != cEngine) {
        continue;
      }
      decls.append(
          "    public " + Utils.asTypeDecl(meth.getReturnType()) + " " + meth.getName() + "(");
      Class[] params = meth.getParameterTypes();
      for (int j = 0; j < params.length; j++) {
        decls.append(Utils.asTypeDecl(params[j]) + " p" + j);
        if (j != params.length - 1) {
          decls.append(", ");
        }
      }
      decls.append(") {\n");
      decls.append(
          "        return ("
              + Utils.asTypeDecl(meth.getReturnType())
              + ")super.callNative(\""
              + meth.getName()
              + "\", new Object[] {");
      for (int j = 0; j < params.length; j++) {
        decls.append("p" + j);
        if (j != params.length - 1) {
          decls.append(", ");
        }
      }
      decls.append("} );\n");
      decls.append("    }\n\n");
    }

    // write template file
    String engineName = "BasicEngine_" + engineInterface;
    String fileName = engineName + ".java";
    try {
      String contents = Utils.readFile("lava/engine/basic/BasicEngineTemplate.java").toString();
      // remove package name
      contents = Utils.replaceAll(contents, "package lava.engine.basic;", "");
      // for class decl
      contents =
          Utils.replaceAll(
              contents, "extends BasicEngine", "extends BasicEngine implements " + engineInterface);
      // for constructor
      contents = Utils.replaceAll(contents, "BasicEngineTemplate", engineName);
      // for methods
      contents = Utils.replaceAll(contents, "// INSERT METHODS HERE", decls.toString());
      Utils.writeFile(fileName, contents);
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(1);
    }

    // compile the file
    try {
      Process jProcess = Runtime.getRuntime().exec("jikes " + fileName, null, null);
      jProcess.waitFor();
    } catch (Exception e) {
      System.err.println("Error compiling auto-generated file " + fileName);
      e.printStackTrace();
      System.exit(1);
    }

    // instantiate the class
    BasicEngine result = null;
    try {
      result = (BasicEngine) Class.forName(engineName).newInstance();
      // cleanup the files we made
      new File(engineName + ".java").delete();
      new File(engineName + ".class").delete();
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }

    return result;
  }
  /* methods that take an EventListener Class Type to create an EventAdapterClass */
  public static Class makeEventAdapterClass(Class listenerType, boolean writeClassFile) {
    DebugLog.stdoutPrintln("EventAdapterGenerator", DebugLog.BSF_LOG_L3);

    if (EVENTLISTENER.isAssignableFrom(listenerType)) {
      boolean exceptionable = false;
      boolean nonExceptionable = false;
      byte constantPool[] = null;
      short cpBaseIndex;
      short cpCount = 0;
      short cpExceptionBaseIndex;
      short exceptionableCount;
      short nonExceptionableCount;

      /* Derive Names */
      String listenerTypeName = listenerType.getName();
      DebugLog.stdoutPrintln("  ListenerTypeName: " + listenerTypeName, DebugLog.BSF_LOG_L3);
      String adapterClassName =
          CLASSPACKAGE
              + (listenerTypeName.endsWith("Listener")
                      ? listenerTypeName.substring(0, listenerTypeName.length() - 8)
                      : listenerTypeName)
                  .replace('.', '_')
              + "Adapter";
      String finalAdapterClassName = adapterClassName;
      Class cached = null;
      int suffixIndex = 0;

      do {
        if (null != (cached = ldr.getLoadedClass(finalAdapterClassName))) {
          DebugLog.stdoutPrintln("cached:  " + cached, DebugLog.BSF_LOG_L3);
          try {
            if (!listenerType.isAssignableFrom(cached))
              finalAdapterClassName = adapterClassName + "_" + suffixIndex++;
            else return cached;
          } catch (VerifyError ex) {
            System.err.println(ex.getMessage());
            ex.printStackTrace();
            return cached;
          }
        }
      } while (cached != null);

      String eventListenerName = listenerTypeName.replace('.', '/');

      /* method stuff */
      java.lang.reflect.Method lms[] = listenerType.getMethods();

      /* ****************************************************************************************** */
      // Listener interface
      // Class name
      cpCount += 4;

      // cp item 17
      constantPool = Bytecode.addUtf8(constantPool, eventListenerName);

      // cp item 18
      constantPool = Bytecode.addUtf8(constantPool, finalAdapterClassName);

      // cp item 19
      constantPool = Bytecode.addClass(constantPool, (short) 17);

      // cp item 20
      constantPool = Bytecode.addClass(constantPool, (short) 18);

      // do we have nonExceptionalble event, exceptionable or both
      for (int i = 0; i < lms.length; ++i) {
        Class exceptionTypes[] = lms[i].getExceptionTypes();
        if (0 < exceptionTypes.length) {
          exceptionable = true;
        } else {
          nonExceptionable = true;
        }
      } /* End for*/

      /* ****************************************************************************************** */
      // optional inclusion of nonexceptional events affects exceptional events indices

      nonExceptionableCount = 0;
      if (nonExceptionable) {
        nonExceptionableCount = 3;
        cpCount += nonExceptionableCount;

        // cp item 21
        constantPool = Bytecode.addUtf8(constantPool, "processEvent");

        // cp item 22
        constantPool = Bytecode.addNameAndType(constantPool, (short) 21, (short) 8);

        // cp item 23
        constantPool = Bytecode.addInterfaceMethodRef(constantPool, (short) 12, (short) 22);
      }

      /* ****************************************************************************************** */
      // optional inclusion of exceptional events affects CP Items which follow for specific methods

      exceptionableCount = 0;
      if (exceptionable) {
        int classIndex = BASECPCOUNT + cpCount + 1;
        int nameIndex = BASECPCOUNT + cpCount + 0;
        int natIndex = BASECPCOUNT + cpCount + 3;

        exceptionableCount = 5;
        cpCount += exceptionableCount;

        // cp item 24 or 21
        constantPool = Bytecode.addUtf8(constantPool, "processExceptionableEvent");

        // cp item 25 or 22
        constantPool = Bytecode.addUtf8(constantPool, "java/lang/Exception");

        // cp item 26 or 23
        constantPool = Bytecode.addClass(constantPool, (short) classIndex);

        // cp item 27 or 24
        constantPool = Bytecode.addNameAndType(constantPool, (short) nameIndex, (short) 8);

        // cp item 28 or 25
        constantPool = Bytecode.addInterfaceMethodRef(constantPool, (short) 12, (short) natIndex);
      }

      // base index for method cp references
      cpBaseIndex = (short) (BASECPCOUNT + cpCount);
      DebugLog.stderrPrintln("cpBaseIndex: " + cpBaseIndex, DebugLog.BSF_LOG_L3);

      for (int i = 0; i < lms.length; ++i) {
        String eventMethodName = lms[i].getName();
        String eventName = lms[i].getParameterTypes()[0].getName().replace('.', '/');
        cpCount += 3;
        // cp items for event methods
        constantPool = Bytecode.addUtf8(constantPool, eventMethodName);
        constantPool = Bytecode.addUtf8(constantPool, ("(L" + eventName + ";)V"));
        constantPool = Bytecode.addString(constantPool, (short) (BASECPCOUNT + cpCount - 3));
      } /* End for*/

      boolean propertyChangeFlag[] = new boolean[lms.length];
      int cpIndexPCE = 0;
      for (int i = 0; i < lms.length; ++i) {
        String eventName = lms[i].getParameterTypes()[0].getName().replace('.', '/');
        // cp items for PropertyChangeEvent special handling
        if (eventName.equalsIgnoreCase("java/beans/PropertyChangeEvent")) {
          propertyChangeFlag[i] = true;
          if (0 == cpIndexPCE) {
            constantPool = Bytecode.addUtf8(constantPool, eventName);
            constantPool = Bytecode.addUtf8(constantPool, "getPropertyName");
            constantPool = Bytecode.addUtf8(constantPool, "()Ljava/lang/String;");
            constantPool = Bytecode.addClass(constantPool, (short) (BASECPCOUNT + cpCount));
            constantPool =
                Bytecode.addNameAndType(
                    constantPool,
                    (short) (BASECPCOUNT + cpCount + 1),
                    (short) (BASECPCOUNT + cpCount + 2));
            constantPool =
                Bytecode.addMethodRef(
                    constantPool,
                    (short) (BASECPCOUNT + cpCount + 3),
                    (short) (BASECPCOUNT + cpCount + 4));
            cpCount += 6;
            cpIndexPCE = BASECPCOUNT + cpCount - 1;
          }
        } else {
          propertyChangeFlag[i] = false;
        }
      } /* End for*/

      cpExceptionBaseIndex = (short) (BASECPCOUNT + cpCount);
      DebugLog.stderrPrintln("cpExceptionBaseIndex: " + cpExceptionBaseIndex, DebugLog.BSF_LOG_L3);

      int excpIndex[][] = new int[lms.length][];
      for (int i = 0; i < lms.length; ++i) {
        Class exceptionTypes[] = lms[i].getExceptionTypes();
        excpIndex[i] = new int[exceptionTypes.length];
        for (int j = 0; j < exceptionTypes.length; j++) {
          constantPool =
              Bytecode.addUtf8(constantPool, exceptionTypes[j].getName().replace('.', '/'));
          constantPool = Bytecode.addClass(constantPool, (short) (BASECPCOUNT + cpCount));
          excpIndex[i][j] = BASECPCOUNT + cpCount + 1;
          cpCount += 2;
        }
      } /* End for*/
      /* end constant pool */

      /* ************************************************************************************************ */
      // put the Class byte array together

      /* start */
      byte newClass[] = CLASSHEADER; // magic, version      (fixed)
      short count = (short) (BASECPCOUNT + cpCount);
      newClass = ByteUtility.addBytes(newClass, count); // constant_pool_count (variable)
      newClass = ByteUtility.addBytes(newClass, BASECP); // constant_pool       (fixed)
      newClass = ByteUtility.addBytes(newClass, constantPool); // constant_pool       (variable)
      newClass = ByteUtility.addBytes(newClass, FIXEDCLASSBYTES); // see FIXEDCLASSBYTES (fixed)
      newClass =
          ByteUtility.addBytes(
              newClass, (short) (lms.length + 1)); // method_count        (variable)
      newClass = ByteUtility.addBytes(newClass, INITMETHOD); // constructor <init>  (fixed)
      // methods

      /* ****************************************************************************************** */
      /* loop over listener methods from listenerType */
      for (int i = 0; i < lms.length; ++i) {
        newClass = ByteUtility.addBytes(newClass, (short) 1); // access_flags             (fixed)
        newClass =
            ByteUtility.addBytes(
                newClass, (short) (cpBaseIndex + 3 * i + 0)); // name_index               (variable)
        newClass =
            ByteUtility.addBytes(
                newClass, (short) (cpBaseIndex + 3 * i + 1)); // descriptor_index         (variable)
        newClass = ByteUtility.addBytes(newClass, (short) 1); // attribute_count          (fixed)
        newClass = ByteUtility.addBytes(newClass, (short) 3); // attribute_name_index code(fixed)

        // Code Attribute Length
        int length = 32;
        if (0 < excpIndex[i].length) {
          length += 5 + 8 * (1 + excpIndex[i].length);
        }
        if (propertyChangeFlag[i]) {
          length += 2;
        }
        newClass =
            ByteUtility.addBytes(newClass, (long) length); // attribute_length         (variable)

        // start code attribute
        newClass = ByteUtility.addBytes(newClass, (short) 6); // max_stack                (fixed)
        newClass = ByteUtility.addBytes(newClass, (short) 3); // max_locals               (fixed)

        // Code Length
        length = 20;
        if (exceptionable && 0 < excpIndex[i].length) {
          length += 5;
        }
        if (propertyChangeFlag[i]) {
          length += 2;
        }
        newClass =
            ByteUtility.addBytes(newClass, (long) length); // code_length              (variable)

        // start code
        newClass = ByteUtility.addBytes(newClass, (byte) 0x2A); // aload_0                  (fixed)
        newClass = ByteUtility.addBytes(newClass, (byte) 0xB4); // getfield                 (fixed)
        newClass = ByteUtility.addBytes(newClass, (short) 15); // index                    (fixed)

        if (propertyChangeFlag[i]) { // the propertyName is passed as the first parameter
          newClass =
              ByteUtility.addBytes(newClass, (byte) 0x2B); // aload_1                  (fixed)
          newClass =
              ByteUtility.addBytes(newClass, (byte) 0xB6); // invokevirtual            (fixed)
          newClass =
              ByteUtility.addBytes(
                  newClass, (short) cpIndexPCE); // methodref                (variable)
        } else { // the eventMethodName is passed as the first parameter
          // Target for method invocation.
          newClass = ByteUtility.addBytes(newClass, (byte) 0x12); // ldc                    (fixed)
          newClass =
              ByteUtility.addBytes(
                  newClass, (byte) (cpBaseIndex + 3 * i + 2)); // index (byte)           (variable)
        }

        newClass = ByteUtility.addBytes(newClass, (byte) 0x04); // iconst_1                 (fixed)
        newClass = ByteUtility.addBytes(newClass, (byte) 0xBD); // anewarray                (fixed)
        newClass = ByteUtility.addBytes(newClass, (short) 10); // Class java/lang/Object   (fixed)
        newClass = ByteUtility.addBytes(newClass, (byte) 0x59); // dup                      (fixed)
        newClass = ByteUtility.addBytes(newClass, (byte) 0x03); // iconst_0                 (fixed)
        newClass = ByteUtility.addBytes(newClass, (byte) 0x2B); // aload_1                  (fixed)
        newClass = ByteUtility.addBytes(newClass, (byte) 0x53); // aastore                  (fixed)
        newClass = ByteUtility.addBytes(newClass, (byte) 0xB9); // invokeinterface          (fixed)

        // index to processEvent or processExceptionableEvent method
        length = 23; // actually an index into cp
        if (exceptionable && nonExceptionable) { // interface method index
          if (0 < lms[i].getExceptionTypes().length) {
            length += 5;
          }
        } else if (exceptionable) {
          length += 2;
        }
        newClass =
            ByteUtility.addBytes(newClass, (short) length); // index (process??????...) (variable)

        newClass = ByteUtility.addBytes(newClass, (byte) 0x03); // iconst_0                 (fixed)
        newClass = ByteUtility.addBytes(newClass, (byte) 0x00); // noop                     (fixed)
        newClass = ByteUtility.addBytes(newClass, (byte) 0xB1); // return                   (fixed)

        if (exceptionable && 0 < excpIndex[i].length) { // exception code
          newClass =
              ByteUtility.addBytes(newClass, (byte) 0x4D); // astore_2                 (fixed)
          newClass =
              ByteUtility.addBytes(newClass, (byte) 0x2C); // aload_2                  (fixed)
          newClass =
              ByteUtility.addBytes(newClass, (byte) 0xBF); // athrow                   (fixed)
          newClass =
              ByteUtility.addBytes(newClass, (byte) 0x57); // pop                      (fixed)
          newClass =
              ByteUtility.addBytes(newClass, (byte) 0xB1); // return                   (fixed)
          // end code

          // exception table
          length = excpIndex[i].length;
          newClass =
              ByteUtility.addBytes(
                  newClass, (short) (1 + length)); // exception_table_length   (variable)
          for (int j = 0; j < length; j++) { // catch exception types and rethrow
            newClass =
                ByteUtility.addBytes(newClass, (short) 0); // start_pc                 (fixed)
            if (propertyChangeFlag[i]) {
              newClass =
                  ByteUtility.addBytes(newClass, (short) 21); // end_pc                   (fixed)
              newClass =
                  ByteUtility.addBytes(newClass, (short) 22); // handler_pc               (fixed)
            } else {
              newClass =
                  ByteUtility.addBytes(newClass, (short) 19); // end_pc                   (fixed)
              newClass =
                  ByteUtility.addBytes(newClass, (short) 20); // handler_pc               (fixed)
            }
            newClass =
                ByteUtility.addBytes(
                    newClass, (short) excpIndex[i][j]); // catch_type               (variable)
          }
          // catch "exception" and trap it
          newClass = ByteUtility.addBytes(newClass, (short) 0); // start_pc                 (fixed)
          if (propertyChangeFlag[i]) {
            newClass =
                ByteUtility.addBytes(newClass, (short) 21); // end_pc                   (fixed)
            newClass =
                ByteUtility.addBytes(newClass, (short) 25); // handler_pc               (fixed)
          } else {
            newClass =
                ByteUtility.addBytes(newClass, (short) 19); // end_pc                   (fixed)
            newClass =
                ByteUtility.addBytes(newClass, (short) 23); // handler_pc               (fixed)
          }
          if (nonExceptionable) {
            newClass = ByteUtility.addBytes(newClass, (short) 26);
          } // catch_type               (fixed)
          else // or
          {
            newClass = ByteUtility.addBytes(newClass, (short) 23);
          } // catch_type               (fixed)
        } else {
          newClass = ByteUtility.addBytes(newClass, (short) 0);
        } // exception_table_length   (fixed)
        // attributes on the code attribute (none)
        newClass = ByteUtility.addBytes(newClass, (short) 0); // attribute_count          (fixed)
        // end code attribute

      } /* End for*/
      // Class Attributes (none for this)
      newClass = ByteUtility.addBytes(newClass, (short) 0); // attribute_count          (fixed)
      /* done */

      DebugLog.stdoutPrintln("adapterName: " + finalAdapterClassName, DebugLog.BSF_LOG_L3);
      DebugLog.stdoutPrintln(
          "cpCount: " + count + " = " + BASECPCOUNT + " + " + cpCount, DebugLog.BSF_LOG_L3);
      DebugLog.stdoutPrintln("methodCount: " + (lms.length + 1), DebugLog.BSF_LOG_L3);
      // output to disk class file
      /* ****************************************************************************************** */

      // now create the class and load it
      // return the Class.

      if (writeClassFile) {
        try {
          FileOutputStream fos =
              new FileOutputStream(WRITEDIRECTORY + finalAdapterClassName + ".class");
          fos.write(newClass);
          fos.close();
        } catch (IOException ex) {
          System.err.println(ex.getMessage());
          ex.printStackTrace();
        }

        try {
          Class ret = ldr.loadClass(finalAdapterClassName);
          DebugLog.stdoutPrintln(
              "EventAdapterGenerator: " + ret.getName() + " dynamically generated",
              DebugLog.BSF_LOG_L3);
          return ret;
        } catch (ClassNotFoundException ex) {
          System.err.println(ex.getMessage());
          ex.printStackTrace();
        }
      }

      try {
        Class ret = ldr.defineClass(finalAdapterClassName, newClass);
        DebugLog.stdoutPrintln(
            "EventAdapterGenerator: " + ret.getName() + " dynamically generated",
            DebugLog.BSF_LOG_L3);
        return ret;
      } catch (Exception ex) {
        System.err.println(ex.getMessage());
        ex.printStackTrace();
      }
    } else {
      System.err.println(
          "EventAdapterGenerator ListenerType invalid: listenerType = " + listenerType);
    }
    return null;
  }
  /**
   * Derive a ranking based on how "natural" the conversion is. The special value CONVERSION_NONE
   * means no conversion is possible, and CONVERSION_NONTRIVIAL signals that more type conformance
   * testing is required. Based on <a
   * href="http://www.mozilla.org/js/liveconnect/lc3_method_overloading.html">"preferred method
   * conversions" from Live Connect 3</a>
   */
  static int getConversionWeight(Object fromObj, Class<?> to) {
    int fromCode = getJSTypeCode(fromObj);

    switch (fromCode) {
      case JSTYPE_UNDEFINED:
        if (to == ScriptRuntime.StringClass || to == ScriptRuntime.ObjectClass) {
          return 1;
        }
        break;

      case JSTYPE_NULL:
        if (!to.isPrimitive()) {
          return 1;
        }
        break;

      case JSTYPE_BOOLEAN:
        // "boolean" is #1
        if (to == Boolean.TYPE) {
          return 1;
        } else if (to == ScriptRuntime.BooleanClass) {
          return 2;
        } else if (to == ScriptRuntime.ObjectClass) {
          return 3;
        } else if (to == ScriptRuntime.StringClass) {
          return 4;
        }
        break;

      case JSTYPE_NUMBER:
        if (to.isPrimitive()) {
          if (to == Double.TYPE) {
            return 1;
          } else if (to != Boolean.TYPE) {
            return 1 + getSizeRank(to);
          }
        } else {
          if (to == ScriptRuntime.StringClass) {
            // native numbers are #1-8
            return 9;
          } else if (to == ScriptRuntime.ObjectClass) {
            return 10;
          } else if (ScriptRuntime.NumberClass.isAssignableFrom(to)) {
            // "double" is #1
            return 2;
          }
        }
        break;

      case JSTYPE_STRING:
        if (to == ScriptRuntime.StringClass) {
          return 1;
        } else if (to.isInstance(fromObj)) {
          return 2;
        } else if (to.isPrimitive()) {
          if (to == Character.TYPE) {
            return 3;
          } else if (to != Boolean.TYPE) {
            return 4;
          }
        }
        break;

      case JSTYPE_JAVA_CLASS:
        if (to == ScriptRuntime.ClassClass) {
          return 1;
        } else if (to == ScriptRuntime.ObjectClass) {
          return 3;
        } else if (to == ScriptRuntime.StringClass) {
          return 4;
        }
        break;

      case JSTYPE_JAVA_OBJECT:
      case JSTYPE_JAVA_ARRAY:
        Object javaObj = fromObj;
        if (javaObj instanceof Wrapper) {
          javaObj = ((Wrapper) javaObj).unwrap();
        }
        if (to.isInstance(javaObj)) {
          return CONVERSION_NONTRIVIAL;
        }
        if (to == ScriptRuntime.StringClass) {
          return 2;
        } else if (to.isPrimitive() && to != Boolean.TYPE) {
          return (fromCode == JSTYPE_JAVA_ARRAY) ? CONVERSION_NONE : 2 + getSizeRank(to);
        }
        break;

      case JSTYPE_OBJECT:
        // Other objects takes #1-#3 spots
        if (to == fromObj.getClass()) {
          // No conversion required
          return 1;
        }
        if (to.isArray()) {
          if (fromObj instanceof NativeArray) {
            // This is a native array conversion to a java array
            // Array conversions are all equal, and preferable to object
            // and string conversion, per LC3.
            return 1;
          }
        } else if (to == ScriptRuntime.ObjectClass) {
          return 2;
        } else if (to == ScriptRuntime.StringClass) {
          return 3;
        } else if (to == ScriptRuntime.DateClass) {
          if (fromObj instanceof NativeDate) {
            // This is a native date to java date conversion
            return 1;
          }
        } else if (to.isInterface()) {
          if (fromObj instanceof Function) {
            // See comments in coerceType
            if (to.getMethods().length == 1) {
              return 1;
            }
          }
          return 11;
        } else if (to.isPrimitive() && to != Boolean.TYPE) {
          return 3 + getSizeRank(to);
        }
        break;
    }

    return CONVERSION_NONE;
  }