//
// ReflectedUniverse.java
//

/*
VisAD system for interactive analysis and visualization of numerical
data.  Copyright (C) 1996 - 2017 Bill Hibbard, Curtis Rueden, Tom
Rink, Dave Glowacki, Steve Emmerson, Tom Whittaker, Don Murray, and
Tommy Jasmin.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
*/

package visad.util;

import visad.VisADException;
import java.lang.reflect.*;
import java.net.*;
import java.util.*;

/**
 * A general-purpose reflection wrapper class.
 * See visad.data.jai.JAIForm for examples of usage.
 */
public class ReflectedUniverse {

  // -- Fields --

  /** Hashtable containing all variables present in the universe. */
  protected Hashtable variables;

  /** Class loader for imported classes. */
  protected ClassLoader loader;

  /** Debugging flag. */
  protected boolean debug;


  // -- Constructors --

  /** Constructs a new reflected universe. */
  public ReflectedUniverse() {
    this((ClassLoader) null);
  }

  /**
   * Constructs a new reflected universe, with the given URLs
   * representing additional search paths for imported classes
   * (in addition to the CLASSPATH).
   */
  public ReflectedUniverse(URL[] urls) {
    this(urls == null ? null : new URLClassLoader(urls));
  }

  /** Constructs a new reflected universe that uses the given class loader. */
  public ReflectedUniverse(ClassLoader loader) {
    variables = new Hashtable();
    this.loader = loader == null ? getClass().getClassLoader() : loader;
    debug = false;
  }


  // -- Utility methods --

  /**
   * Returns whether the given object is compatible with the
   * specified class for the purposes of reflection.
   */
  public static boolean isInstance(Class c, Object o) {
    return (o == null || c.isInstance(o) ||
      (c == byte.class && o instanceof Byte) ||
      (c == short.class && o instanceof Short) ||
      (c == int.class && o instanceof Integer) ||
      (c == long.class && o instanceof Long) ||
      (c == float.class && o instanceof Float) ||
      (c == double.class && o instanceof Double) ||
      (c == boolean.class && o instanceof Boolean) ||
      (c == char.class && o instanceof Character));
  }


  // -- API methods --

  /**
   * Executes a command in the universe. The following syntaxes are valid:
   * 
  • import fully.qualified.package.ClassName *
  • var = new ClassName(param1, ..., paramN) *
  • var.method(param1, ..., paramN) *
  • var2 = var1.method(param1, ..., paramN) *
  • ClassName.method(param1, ..., paramN) *

    * Important guidelines: *

  • Any referenced class must be imported first using "import". *
  • Variables can be exported from the universe with getVar(). *
  • Variables can be imported to the universe with setVar(). *
  • Each parameter must be either a variable in the universe * or a static or instance field (i.e., no nested methods). */ public Object exec(String command) throws VisADException { command = command.trim(); if (command.startsWith("import ")) { // command is an import statement command = command.substring(7).trim(); int dot = command.lastIndexOf("."); String varName = dot < 0 ? command : command.substring(dot + 1); Class c; try { c = Class.forName(command, true, loader); } catch (ClassNotFoundException exc) { if (debug) exc.printStackTrace(); throw new VisADException("No such class: " + command); } setVar(varName, c); return null; } // get variable where results of command should be stored int eqIndex = command.indexOf("="); String target = null; if (eqIndex >= 0) { target = command.substring(0, eqIndex).trim(); command = command.substring(eqIndex + 1).trim(); } // parse parentheses int leftParen = command.indexOf("("); if (leftParen < 0 || leftParen != command.lastIndexOf("(") || command.indexOf(")") != command.length() - 1) { throw new VisADException("invalid parentheses"); } // parse arguments String arglist = command.substring(leftParen + 1); StringTokenizer st = new StringTokenizer(arglist, "(,)"); int len = st.countTokens(); Object[] args = new Object[len]; for (int i=0; i= 0) { // get field value of variable Object var = variables.get(varName.substring(0, dot).trim()); Class varClass = var instanceof Class ? (Class) var : var.getClass(); String fieldName = varName.substring(dot + 1).trim(); Field field; try { field = varClass.getField(fieldName); } catch (NoSuchFieldException exc) { if (debug) exc.printStackTrace(); throw new VisADException("No such field: " + varName); } Object fieldVal; try { fieldVal = field.get(var); } catch (Exception exc) { if (debug) exc.printStackTrace(); throw new VisADException("Cannot get field value: " + varName); } return fieldVal; } else { // get variable Object var = variables.get(varName); return var; } } /** Enables or disables extended debugging output. */ public void setDebug(boolean debug) { this.debug = debug; } /** Gets whether extended debugging output is enabled. */ public boolean isDebug() { return debug; } }
  • Ads help maintain this website.