@RubyLevelMethod(name = "__load_with_reflection__", module = true)
  public static RubyValue loadWithReflection(RubyValue receiver, RubyValue arg, RubyBlock block) {
    String required_file = arg.toStr();
    String name = NameFactory.createMainClassName(required_file);
    try {
      Class c = Class.forName(name);
      Object o = c.newInstance();
      RubyProgram p = (RubyProgram) o;

      // $".push(file_name) unless $".include?(file_name)
      RubyArray a = (RubyArray) GlobalVariables.get("$\"");
      if (a.include(arg) == RubyConstant.QFALSE) {
        a.push(arg);
      }

      p.invoke();
      return RubyConstant.QTRUE;
    } catch (ClassNotFoundException e) {
      return RubyConstant.QFALSE;
    } catch (InstantiationException e) {
      return RubyConstant.QFALSE;
    } catch (IllegalAccessException e) {
      return RubyConstant.QFALSE;
    }
  }
 private static RubyValue eval(String evalText) {
   RubyCompiler compiler = new RubyCompiler();
   try {
     CompilationResults codes = compiler.compileString(evalText);
     RubyProgram p = codes.getRubyProgram();
     return p.invoke();
   } catch (RecognitionException e) {
     throw new RubyException(RubyRuntime.SyntaxErrorClass, e.toString());
   } catch (TokenStreamException e) {
     throw new RubyException(RubyRuntime.SyntaxErrorClass, e.toString());
   } catch (InstantiationException e) {
     throw new RubyException(e.toString());
   } catch (IllegalAccessException e) {
     throw new RubyException(e.toString());
   }
 }
 public static RubyValue eval(String evalText, RubyBinding binding) {
   RubyCompiler compiler = new RubyCompiler();
   try {
     CompilationResults codes = compiler.compileString(evalText);
     RubyProgram p = codes.getRubyProgram();
     return p.invoke(
         binding.getSelf(), binding.getVariables(), binding.getBlock(), binding.getScope());
   } catch (RecognitionException e) {
     throw new RubyException(RubyRuntime.SyntaxErrorClass, e.toString());
   } catch (TokenStreamException e) {
     throw new RubyException(RubyRuntime.SyntaxErrorClass, e.toString());
   } catch (InstantiationException e) {
     throw new RubyException(e.toString());
   } catch (IllegalAccessException e) {
     throw new RubyException(e.toString());
   }
 }
 private static RubyValue eval(String evalText, RubyBinding binding, String file_name) {
   RubyCompiler compiler = new RubyCompiler(binding, false);
   try {
     CompilationResults codes = compiler.compileString(file_name, evalText);
     RubyProgram p = codes.getRubyProgram();
     if (null != binding) {
       return p.invoke(
           binding.getSelf(), binding.getVariables(), binding.getBlock(), binding.getScope());
     } else {
       return p.invoke();
     }
   } catch (RecognitionException e) {
     throw new RubyException(RubyRuntime.SyntaxErrorClass, file_name + " " + e.toString());
   } catch (TokenStreamException e) {
     throw new RubyException(RubyRuntime.SyntaxErrorClass, file_name + " " + e.toString());
   } catch (InstantiationException e) {
     throw new RubyException(e.toString());
   } catch (IllegalAccessException e) {
     throw new RubyException(e.toString());
   }
 }
Ejemplo n.º 5
0
  public static void main(String[] args) throws Exception {

    String program_text =
        ""
            + "import 'java.util.*'\n"
            + "r = Random.new"
            + "\n"
            +
            // call overloaded methods
            "puts r.nextInt\n"
            + "puts r.nextInt(3)\n"
            +
            // call methods inherited superclass(for example:Object)
            "puts r.toString\n"
            +

            // call static methods and fields
            "require_java 'java.lang.Math'\n"
            + "puts JMath.PI\n"
            + "puts JMath.cos(0)\n"
            +

            // access Java constant variables
            "require_java 'java.lang.System'\n"
            + "out = System::out\n"
            + "out.println('ok!')\n"
            +

            // escape from name collision
            "require_java 'java.lang.Object'\n"
            + "o = JObject.new\n"
            + "puts o.toString\n"
            +

            // implement a Java interface
            "require_java 'java.lang.Runnable'\n"
            + "require_java 'java.lang.Thread'\n"
            + "class MyRunnable < Runnable\n"
            + "   def run\n"
            + "       puts 'Thread is running!'\n"
            + "   end\n"
            + "end\n"
            + "r = MyRunnable.new\n"
            + "thread = JThread.new(r)\n"
            + "thread.start()\n"
            +

            // Creating Java Arrays and accessing Java Arrays
            "require_java 'com.xruby.runtime.javasupport.AssistantTester'\n"
            + "require_java 'java.lang.reflect.Array'\n"
            + "require_java 'java.lang.String'\n"
            + "s = JArray.newInstance(JString, 2)\n"
            + "s[0]='Hello '\n"
            + "s[1]='World!'\n"
            + "AssistantTester.echo(s)\n"
            +

            // Handling Java exceptions
            "require_java 'java.io.FileInputStream'\n"
            + "require_java 'java.io.FileNotFoundException'\n"
            + "require_java 'java.io.IOException'\n"
            + "begin\n"
            + "   f = FileInputStream.new('myfile')\n"
            + "rescue FileNotFoundException=>fnfe\n"
            + "   puts fnfe.getMessage\n"
            + "rescue IOException=>ioe\n"
            + "   puts ioe\n"
            + "end\n"
            + "array = ArrayList.new\n"
            + "array.add 1\n"
            + "array.add 2\n"
            + "array.add 3\n"
            + "for i in 1 ... 3\n"
            + "    puts array.get(i)\n"
            + "end\n"
            +

            // JavaBean support
            "require_java 'javax.swing.JFrame'\n"
            + "f = JFrame.new('hello')\n"
            +
            // calls setVisible
            "f.visible= true\n"
            +
            // calls getTitle
            "puts f.title\n"
            + "\n";

    RubyCompiler compiler = new RubyCompiler();
    CompilationResults codes = compiler.compileString(program_text);
    RubyProgram p = codes.getRubyProgram();

    RubyRuntime.init(args);
    p.invoke();
    RubyRuntime.fini();
  }