Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4127257

forName doesn't provide static initializing if user's loader is used

    XMLWordPrintable

Details

    • 1.2beta4
    • sparc
    • solaris_2.5.1
    • Not verified

    Description



      Name: szC45993 Date: 04/09/98



      The JCK
      vm/concepts/execution/execution054/execution05402/execution05402.html
      test shows that Class.forName does not provide the static initializing of
      the loaded class if USER-defined class loader is used for jdk1.2b3N jvm.

      Short test sources are represented below.

      For the reproducing of the bug need to do the following:

      setenv CLASSPATH .
      novo7% /export/ld14/java/dest/jdk1.2b3N/solaris/bin/javac -d . *.java
      Note: execution05402g.java uses or overrides a deprecated API. Recompile with "-deprecation" for details.
      1 warning
      novo7% mkdir 1; cd 1; mkdir 2; cd 2; mkdir 3; cd ../..
      novo7% cp execution05402.class 1/2/3;cp execution05402z.class 1/2/3;rm execution05402.class execution05402z.class
      novo7% /export/ld14/java/dest/jdk1.2b3N/solaris/bin/java -verify execution05402g
      TEST before forName
      TEST after forName,1
      iii= 3
      ERROR: Class.forName and USER-defined class loader do not provide the static initializing of the loaded class!!!
      novo7%
      ----------------------------------------------------------------------------



      SOURCES:


      --------------------- execution05402g.java
      import java.io.PrintStream;
      import java.io.*;
      import java.util.Hashtable;

      class Loader extends ClassLoader
          {
      Loader(File dir) {
      loadDir = dir;
      }


      //----------ClassLoader methods--------------------------------------------------

      /**
      * Attempt to load a class if it is not already loaded, and optionally
      * resolve any imports it might have.
      * @exception ClassNotFoundException The class was not found.
      */
      protected Class loadClass(String name, boolean resolve)
      throws ClassNotFoundException {

      /// System.out.println("### pusk ###: "+name+", "+resolve);
      Class cl = (Class)classes.get(name);

      if (cl == null) {
      try {
      /// System.out.println("### findSystemClass ###: "+name);
      return findSystemClass(name);
      }
      catch (Throwable e) {
      /// System.out.println("### findSystemClass ###: "+e);
      }

      /// System.out.println("### loadClass ###: "+name);
      cl = findClass(name);
      }

      if (cl == null)
      throw new ClassNotFoundException(name);

      if (resolve)
      resolveClass(cl);

      return cl;
      }

      //----------Internal methods-----------------------------------------------------

      synchronized Class findClass(String name) {
      Class cl = (Class)classes.get(name);
      if (cl != null)
      return cl;

      String cname = name.replace('.', '/') + ".class";

      File file = new File(loadDir, cname);
      /// System.out.println("***************** file name: "+cname);
      /// System.out.println("***************** file: "+file);
      cl = loadClass(file);
      /// System.out.println("***************** cl: "+cl);
      if (cl != null) {
      if (!name.equals(cl.getName()))
      throw new ClassFormatError(name + " != " + cl.getName());
      classes.put(name, cl);
      }

      return cl;
      }

      synchronized Class loadClass(File file) {
      try {
      InputStream in = new FileInputStream(file);
      try {
      int len = in.available();
      byte data[] = new byte[len];
      for (int total = 0; total < data.length; ) {
      total += in.read(data, total, data.length - total);
      }
      return defineClass(data, 0, data.length);
      }
      finally {
      if (in != null)
      in.close();
      }
      } catch (Throwable e) {
      return null;
      }
      }

      //----------Data members---------------------------------------------------------

      private File loadDir;
      private Hashtable classes = new Hashtable();
          }

      public class execution05402g {
        public static PrintStream o;

        public static int run(String argv[], PrintStream out) {
      Loader ldr = new Loader(new File("1/2/3"));

      o = out;
      try{
      (ldr.loadClass("execution05402z")).newInstance();
      } catch (java.lang.Throwable e) {
      out.println(e);
      return 0;
      };

      return 0;
        }

        public static void main(String argv[]) {
      System.exit(run(argv, System.out) + 95/*STATUS_TEMP*/);
        }
      }



      --------------------- execution05402z.java
      import java.io.PrintStream;

      class execution05402 implements Runnable {

        static {

      execution05402z.o.println("INITIALIZATION of execution05402");
      execution05402z.iii = 6;
        }

        public void run() {
        }
      }

      public class execution05402z {
        public static int iii = 3;
        public static PrintStream o;

        static {

      o = execution05402g.o;
      o.println("TEST before forName");
      try{
      Class cl = Class.forName("execution05402");
      // cl.newInstance();

      o.println("TEST after forName,1");

      } catch (java.lang.Throwable e) {
      o.println(e);
      };

      o.println("iii= "+iii);
      if (iii != 6) o.println("ERROR: Class.forName and USER-defined class loader do not provide the static initializing of the loaded class!!!");
        }

      }
      ======================================================================

      Hook 5(hook5): test

      ======================================================================

      Attachments

        Activity

          People

            sliangsunw Sheng Liang (Inactive)
            zsssunw Zss Zss (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: