Exemplo n.º 1
0
  /** Test a doubled class that extends the base class. */
  static void testExtend(ClassLoader loader) {
    Class doubledExtendClass;
    Object obj;

    /* get the "alternate" version of DoubledExtend */
    try {
      doubledExtendClass = loader.loadClass("DoubledExtend");
      // System.out.println("+++ DoubledExtend is " + doubledExtendClass
      //    + " in " + doubledExtendClass.getClassLoader());
    } catch (ClassNotFoundException cnfe) {
      System.err.println("loadClass failed: " + cnfe);
      return;
    }

    /* instantiate */
    try {
      obj = doubledExtendClass.newInstance();
    } catch (InstantiationException ie) {
      System.err.println("newInstance failed: " + ie);
      return;
    } catch (IllegalAccessException iae) {
      System.err.println("newInstance failed: " + iae);
      return;
    } catch (LinkageError le) {
      System.out.println("Got expected LinkageError on DE");
      return;
    }

    /* use the base class reference to get a CL-specific instance */
    Base baseRef = (Base) obj;
    DoubledExtend de = baseRef.getExtended();

    /* try to call through it */
    try {
      String result;

      result = Base.doStuff(de);
      System.err.println("ERROR: did not get LinkageError on DE");
      System.err.println("(result=" + result + ")");
    } catch (LinkageError le) {
      System.out.println("Got expected LinkageError on DE");
      return;
    }
  }