コード例 #1
0
  /**
   * Caches a block of code, returning a handle that can be passed to runCachedBlock().
   *
   * @param id An identifier. If null, a unique identifier is generated
   * @param code The code
   * @param namespace If true, the namespace will be set
   * @return Description of the Return Value
   * @exception Exception instances are thrown when various BeanShell errors occur
   * @since jEdit 4.1pre1
   */
  public BshMethod cacheBlock(String id, String code, boolean namespace) throws Exception {
    String name = "__internal_" + id;

    // evaluate a method declaration
    if (namespace) {
      _eval(global, name + "(ns) {\nthis.callstack.set(0,ns);\n" + code + "\n}");
      return global.getMethod(name, new Class[] {NameSpace.class});
    } else {
      _eval(global, name + "() {\n" + code + "\n}");
      return global.getMethod(name, new Class[0]);
    }
  } // }}}
コード例 #2
0
 /**
  * Description of the Method
  *
  * @param namespace Description of the Parameter
  * @exception UtilEvalError Description of the Exception
  */
 private void resetDefaultVariables(NameSpace namespace)
       // throws UtilEvalError {
     {
   try {
     namespace.setVariable("reg", factory, false);
   } catch (UtilEvalError ex) {
     log.error(ex, ex);
   }
 } // }}}
コード例 #3
0
  /** Description of the Method */
  public void init() {

    global = new NameSpace(new BshClassManager(), "Embedded BeanShell interpreter");
    // global.importPackage("org.gjt.sp.util");
    global.importPackage("org.twdata.TW1606U.gui");
    global.importPackage("org.twdata.TW1606U.signal");
    global.importPackage("org.twdata.TW1606U.action");
    global.importPackage("org.twdata.TW1606U.tw");
    global.importPackage("org.twdata.TW1606U.tw.data");
    global.importPackage("org.twdata.TW1606U.data");
    global.importPackage("org.twdata.TW1606U.tw.model");
    global.importPackage("org.twdata.TW1606U.tw.signal");
    global.importPackage("org.werx.framework.bus");
    interpForMethods = createInterpreter(global);
  } // }}}
コード例 #4
0
  /**
   * Parse the BSHBlock for for the class definition and generate the class using ClassGenerator.
   */
  public static Class generateClassImpl(
      String name,
      Modifiers modifiers,
      Class[] interfaces,
      Class superClass,
      BSHBlock block,
      boolean isInterface,
      CallStack callstack,
      Interpreter interpreter)
      throws EvalError {
    // Scripting classes currently requires accessibility
    // This can be eliminated with a bit more work.
    try {
      Capabilities.setAccessibility(true);
    } catch (Capabilities.Unavailable e) {
      throw new EvalError(
          "Defining classes currently requires reflective Accessibility.", block, callstack);
    }

    NameSpace enclosingNameSpace = callstack.top();
    String packageName = enclosingNameSpace.getPackage();
    String className =
        enclosingNameSpace.isClass ? (enclosingNameSpace.getName() + "$" + name) : name;
    String fqClassName = packageName == null ? className : packageName + "." + className;

    BshClassManager bcm = interpreter.getClassManager();
    // Race condition here...
    bcm.definingClass(fqClassName);

    // Create the class static namespace
    NameSpace classStaticNameSpace = new NameSpace(enclosingNameSpace, className);
    classStaticNameSpace.isClass = true;

    callstack.push(classStaticNameSpace);

    // Evaluate any inner class class definitions in the block
    // effectively recursively call this method for contained classes first
    block.evalBlock(callstack, interpreter, true /*override*/, ClassNodeFilter.CLASSCLASSES);

    // Generate the type for our class
    Variable[] variables = getDeclaredVariables(block, callstack, interpreter, packageName);
    DelayedEvalBshMethod[] methods = getDeclaredMethods(block, callstack, interpreter, packageName);

    ClassGeneratorUtil classGenerator =
        new ClassGeneratorUtil(
            modifiers,
            className,
            packageName,
            superClass,
            interfaces,
            variables,
            methods,
            classStaticNameSpace,
            isInterface);
    byte[] code = classGenerator.generateClass();

    // if debug, write out the class file to debugClasses directory
    if (DEBUG_DIR != null)
      try {
        FileOutputStream out = new FileOutputStream(DEBUG_DIR + '/' + className + ".class");
        out.write(code);
        out.close();
      } catch (IOException e) {
        throw new IllegalStateException(
            "cannot create file " + DEBUG_DIR + '/' + className + ".class", e);
      }

    // Define the new class in the classloader
    Class genClass = bcm.defineClass(fqClassName, code);

    // import the unq name into parent
    enclosingNameSpace.importClass(fqClassName.replace('$', '.'));

    try {
      classStaticNameSpace.setLocalVariable(
          ClassGeneratorUtil.BSHINIT, block, false /*strictJava*/);
    } catch (UtilEvalError e) {
      throw new InterpreterError("unable to init static: " + e);
    }

    // Give the static space its class static import
    // important to do this after all classes are defined
    classStaticNameSpace.setClassStatic(genClass);

    // evaluate the static portion of the block in the static space
    block.evalBlock(callstack, interpreter, true /*override*/, ClassNodeFilter.CLASSSTATIC);

    callstack.pop();

    if (!genClass.isInterface()) {
      // Set the static bsh This callback
      String bshStaticFieldName = ClassGeneratorUtil.BSHSTATIC + className;
      try {
        LHS lhs = Reflect.getLHSStaticField(genClass, bshStaticFieldName);
        lhs.assign(classStaticNameSpace.getThis(interpreter), false /*strict*/);
      } catch (Exception e) {
        throw new InterpreterError("Error in class gen setup: " + e);
      }
    }

    bcm.doneDefiningClass(fqClassName);
    return genClass;
  }