Changeset 3049


Ignore:
Timestamp:
08/06/07 15:50:57 (12 years ago)
Author:
curtis
Message:

Some improvements to reflected universe functionality:

  • direct variable assignment ('var2 = var')
  • support for boolean, double, int, long and String literals, and for null
  • main method with interactive interpreter test environment
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/formats/ReflectedUniverse.java

    r3017 r3049  
    2626 
    2727import java.lang.reflect.*; 
     28import java.io.*; 
    2829import java.net.*; 
    2930import java.util.*; 
     
    9596   * <li>var = new ClassName(param1, ..., paramN) 
    9697   * <li>var.method(param1, ..., paramN) 
    97    * <li>var2 = var1.method(param1, ..., paramN) 
     98   * <li>var2 = var.method(param1, ..., paramN) 
    9899   * <li>ClassName.method(param1, ..., paramN) 
     100   * <li>var2 = var 
    99101   * <p> 
    100102   * Important guidelines: 
     
    102104   * <li>Variables can be exported from the universe with getVar(). 
    103105   * <li>Variables can be imported to the universe with setVar(). 
    104    * <li>Each parameter must be either a variable in the universe 
    105    *     or a static or instance field (i.e., no nested methods). 
     106   * <li>Each parameter must be either: 
     107   *     1) a variable in the universe; 
     108   *     2) a static or instance field (i.e., no nested methods); 
     109   *     3) a string literal (remember to escape the double quotes); 
     110   *     4) an integer literal; 
     111   *     5) a long literal (ending in L); 
     112   *     or 6) a double literal (containing a decimal point). 
    106113   */ 
    107114  public Object exec(String command) throws ReflectException { 
     
    143150    } 
    144151 
     152    Object result; 
     153 
    145154    // parse parentheses 
    146155    int leftParen = command.indexOf("("); 
    147     if (leftParen < 0 || leftParen != command.lastIndexOf("(") || 
     156    if (leftParen < 0) { 
     157      // command is a simple assignment 
     158      result = getVar(command); 
     159      if (target != null) setVar(target, result); 
     160      return result; 
     161    } 
     162    else if (leftParen != command.lastIndexOf("(") || 
    148163      command.indexOf(")") != command.length() - 1) 
    149164    { 
    150       throw new ReflectException("invalid parentheses"); 
     165      throw new ReflectException("Invalid parentheses"); 
    151166    } 
    152167 
     
    162177    command = command.substring(0, leftParen); 
    163178 
    164     Object result; 
    165179    if (command.startsWith("new ")) { 
    166180      // command is a constructor call 
     
    168182      Object var = getVar(className); 
    169183      if (var == null) { 
    170         throw new ReflectException("class not found: " + className); 
     184        throw new ReflectException("Class not found: " + className); 
    171185      } 
    172186      else if (!(var instanceof Class)) { 
    173         throw new ReflectException("not a class: " + className); 
     187        throw new ReflectException("Not a class: " + className); 
    174188      } 
    175189      Class cl = (Class) var; 
     
    220234      // command is a method call 
    221235      int dot = command.indexOf("."); 
    222       if (dot < 0) throw new ReflectException("syntax error"); 
     236      if (dot < 0) throw new ReflectException("Syntax error"); 
    223237      String varName = command.substring(0, dot).trim(); 
    224238      String methodName = command.substring(dot + 1).trim(); 
     
    325339   */ 
    326340  public Object getVar(String varName) throws ReflectException { 
     341    if (varName.equals("null")) { 
     342      // variable is a null value 
     343      return null; 
     344    } 
     345    else if (varName.equals("true")) { 
     346      // variable is a boolean literal 
     347      return new Boolean(true); 
     348    } 
     349    else if (varName.equals("false")) { 
     350      // variable is a boolean literal 
     351      return new Boolean(false); 
     352    } 
     353    else if (varName.startsWith("\"") && varName.endsWith("\"")) { 
     354      // variable is a string literal 
     355      return varName.substring(1, varName.length() - 1); 
     356    } 
     357    try { 
     358      if (varName.matches("-?\\d+")) { 
     359        // variable is an int literal 
     360        return new Integer(varName); 
     361      } 
     362      else if (varName.matches("-?\\d+L")) { 
     363        // variable is a long literal 
     364        return new Long(varName); 
     365      } 
     366      else if (varName.matches("-?\\d*\\.\\d*")) { 
     367        // variable is a double literal 
     368        return new Double(varName); 
     369      } 
     370    } 
     371    catch (NumberFormatException exc) { 
     372      throw new ReflectException("Invalid literal: " + varName, exc); 
     373    } 
    327374    int dot = varName.indexOf("."); 
    328375    if (dot >= 0) { 
     
    366413  public boolean isDebug() { return debug; } 
    367414 
     415  // -- Main method -- 
     416 
     417  /** 
     418   * Allows exploration of a reflected universe in an interactive environment. 
     419   */ 
     420  public static void main(String[] args) throws IOException { 
     421    ReflectedUniverse r = new ReflectedUniverse(); 
     422    LogTools.println("Reflected universe test environment. " + 
     423      "Type commands, or press ^D to quit."); 
     424    BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
     425    while (true) { 
     426      LogTools.print("> "); 
     427      String line = in.readLine(); 
     428      if (line == null) break; 
     429      try { r.exec(line); } 
     430      catch (ReflectException exc) { LogTools.trace(exc); } 
     431    } 
     432    LogTools.println(); 
     433  } 
     434 
    368435} 
Note: See TracChangeset for help on using the changeset viewer.