예제 #1
0
  /**
   * Loads the class with the specified name, optionally linking it after loading. The following
   * steps are performed:
   *
   * <ol>
   *   <li>Call {@link #findLoadedClass(String)} to determine if the requested class has already
   *       been loaded.
   *   <li>If the class has not yet been loaded: Invoke this method on the parent class loader.
   *   <li>If the class has still not been loaded: Call {@link #findClass(String)} to find the
   *       class.
   * </ol>
   *
   * <p><strong>Note:</strong> In the Android reference implementation, the {@code resolve}
   * parameter is ignored; classes are never linked.
   *
   * @return the {@code Class} object.
   * @param className the name of the class to look for.
   * @param resolve Indicates if the class should be resolved after loading. This parameter is
   *     ignored on the Android reference implementation; classes are not resolved.
   * @throws ClassNotFoundException if the class can not be found.
   */
  protected Class<?> loadClass(String className, boolean resolve) throws ClassNotFoundException {
    Class<?> clazz = findLoadedClass(className);

    if (clazz == null) {
      try {
        clazz = parent.loadClass(className, false);
      } catch (ClassNotFoundException e) {
        // Don't want to see this.
      }

      if (clazz == null) {
        clazz = findClass(className);
      }
    }

    return clazz;
  }
예제 #2
0
  protected Class loadClass(String class_name, boolean resolve) throws ClassNotFoundException {
    Class cl = null;

    /* First try: lookup hash table.
     */
    if ((cl = (Class) classes.get(class_name)) == null) {
      /* Second try: Load system class using system class loader. You better
       * don't mess around with them.
       */
      for (int i = 0; i < ignored_packages.length; i++) {
        if (class_name.startsWith(ignored_packages[i])) {
          cl = deferTo.loadClass(class_name);
          break;
        }
      }

      if (cl == null) {
        JavaClass clazz = null;

        /* Third try: Special request?
         */
        if (class_name.indexOf("$$BCEL$$") >= 0) clazz = createClass(class_name);
        else { // Fourth try: Load classes via repository
          if ((clazz = repository.loadClass(class_name)) != null) {
            clazz = modifyClass(clazz);
          } else throw new ClassNotFoundException(class_name);
        }

        if (clazz != null) {
          byte[] bytes = clazz.getBytes();
          cl = defineClass(class_name, bytes, 0, bytes.length);
        } else // Fourth try: Use default class loader
        cl = Class.forName(class_name);
      }

      if (resolve) resolveClass(cl);
    }

    classes.put(class_name, cl);

    return cl;
  }