示例#1
0
  /**
   * Implementation of getClass()
   *
   * <p>Load a class through this namespace taking into account imports.
   *
   * <p>Check the cache first. If an unqualified name look for imported class or package. Else try
   * to load absolute name.
   *
   * <p>This method implements caching of unqualified names (normally imports). Qualified names are
   * cached by the BshClassManager. Unqualified absolute class names (e.g. unpackaged Foo) are
   * cached too so that we don't go searching through the imports for them each time.
   *
   * @return null if not found.
   */
  private Class getClassImpl(String name) throws UtilEvalError {
    Class c = null;

    // Check the cache
    if (classCache != null) {
      c = classCache.get(name);

      if (c != null) return c;
    }

    // Unqualified (simple, non-compound) name
    boolean unqualifiedName = !Name.isCompound(name);

    // Unqualified name check imported
    if (unqualifiedName) {
      // Try imported class
      if (c == null) c = getImportedClassImpl(name);

      // if found as imported also cache it
      if (c != null) {
        cacheClass(name, c);
        return c;
      }
    }

    // Try absolute
    c = classForName(name);
    if (c != null) {
      // Cache unqualified names to prevent import check again
      if (unqualifiedName) cacheClass(name, c);
      return c;
    }

    // Not found
    if (Interpreter.DEBUG) Interpreter.debug("getClass(): " + name + " not	found in " + this);
    return null;
  }
示例#2
0
  /**
   * Try to make the name into an imported class. This method takes into account only imports (class
   * or package) found directly in this NameSpace (no parent chain).
   */
  private Class getImportedClassImpl(String name) throws UtilEvalError {
    // Try explicitly imported class, e.g. import foo.Bar;
    String fullname = null;
    if (importedClasses != null) fullname = importedClasses.get(name);

    // not sure if we should really recurse here for explicitly imported
    // class in parent...

    if (fullname != null) {
      /*
      	Found the full name in imported classes.
      */
      // Try to make the full imported name
      Class clas = classForName(fullname);

      // Handle imported inner class case
      if (clas == null) {
        // Imported full name wasn't found as an absolute class
        // If it is compound, try to resolve to an inner class.
        // (maybe this should happen in the BshClassManager?)

        if (Name.isCompound(fullname))
          try {
            clas = getNameResolver(fullname).toClass();
          } catch (ClassNotFoundException e) {
            /* not a class */
          }
        else if (Interpreter.DEBUG)
          Interpreter.debug("imported unpackaged name not found:" + fullname);

        // If found cache the full name in the BshClassManager
        if (clas != null) {
          // (should we cache info in not a class case too?)
          getClassManager().cacheClassInfo(fullname, clas);
          return clas;
        }
      } else return clas;

      // It was explicitly imported, but we don't know what it is.
      // should we throw an error here??
      return null;
    }

    /*
    	Try imported packages, e.g. "import foo.bar.*;"
    	in reverse order of import...
    	(give later imports precedence...)
    */
    if (importedPackages != null)
      for (int i = importedPackages.size() - 1; i >= 0; i--) {
        String s = importedPackages.get(i) + "." + name;
        Class c = classForName(s);
        if (c != null) return c;
      }

    BshClassManager bcm = getClassManager();
    /*
    	Try super import if available
    	Note: we do this last to allow explicitly imported classes
    	and packages to take priority.  This method will also throw an
    	error indicating ambiguity if it exists...
    */
    if (bcm.hasSuperImport()) {
      String s = bcm.getClassNameByUnqName(name);
      if (s != null) return classForName(s);
    }

    return null;
  }