Package com.example.test.reflect2

Source Code of com.example.test.reflect2.Shell

package com.example.test.reflect2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import com.example.test.reflect1.ThingFactory;

public class Shell {
  final private ScriptEngine engine;
 
  public Shell(String engineName) {
    ScriptEngineManager engineMgr = new ScriptEngineManager();
    this.engine = engineMgr.getEngineByName(engineName);
  }

  public static void main(String[] args) {
    new Shell(args[0]).run();
  }

  private void run() {
    Bindings bindings = this.engine.getBindings(ScriptContext.ENGINE_SCOPE);
    ThingFactory factory = new ThingFactory();
    bindings.put("factory", factory);
    int N = factory.getThingCount();
    for (int i = 1; i <= N; ++i)
    {
      bindings.put("thing"+i, factory.getThing(i));
    }
    bindings.put("invoker", ReflectInvocations.invoker());
   
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try
    {
      while (true)
      {
        System.out.print(">> ");
        String line = br.readLine();
        if (line == null)
          break;
        try
        {
          Object result = this.engine.eval(line);
          System.out.println(result);
        }
        catch (ScriptException e) {
          e.printStackTrace();
        }
      }
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }
}
TOP

Related Classes of com.example.test.reflect2.Shell

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.