/**
   * Recursively registers all return types to be accessible, down to the java.lang.Class. By
   * calling this function you ensure a) that all returned objects are accessible from JavaScript,
   * no matter how nested they are and b) memory leaks.
   *
   * @param brdge
   * @param reg
   * @param start
   */
  private static void registerCallableRecursively(
      JSONRPCBridge brdge, Collection<Class<?>> reg, Class<?> start, int levelOfRecursion) {

    if (levelOfRecursion == 0) return;

    try {

      Collection<Class<?>> allRelatedClasses = getAllRelatedClasses(start);
      // = start.getDeclaredClasses();
      // Method[] methods = start.getMethods();
      // for (Method method : methods) {
      for (Class<?> returnType : allRelatedClasses) {
        // Class<?> returnType = method.getReturnType();

        if (reg.contains(returnType)) continue;

        // I think these classes are already serialized by JSON, so don't make them accessible.
        if (returnType.equals(String.class)) continue;
        if (returnType.equals(Void.class)) continue;
        if (returnType.equals(Float.class)) continue;
        if (returnType.equals(Double.class)) continue;
        if (returnType.equals(Integer.class)) continue;
        if (returnType.equals(ArrayList.class)) continue;
        if (returnType.equals(Array.class)) continue;

        reg.add(returnType);
        brdge.registerCallableReference(returnType);

        registerCallableRecursively(brdge, reg, returnType, levelOfRecursion - 1);
      }
    } catch (Exception e1) {
      e1.printStackTrace();
    }
  }
示例#2
0
 protected JSONRPCBridge createBridgeForComponent(
     JSONComponent component,
     String componentName,
     String componentInstance,
     Map<String, JSONRPCBridge> componentBridges)
     throws Exception {
   JSONRPCBridge jsonBridge = JSONBridge.createBridge();
   jsonBridge.registerCallableReference(JSONComponent.class);
   jsonBridge.registerObject("component", component);
   String componentNameAndInstance =
       JSONRequestHandler.componentNameAndInstance(componentName, componentInstance);
   componentBridges.put(componentNameAndInstance, jsonBridge);
   return jsonBridge;
 }