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

setClassAssertionStatus does not affect nested classes

    XMLWordPrintable

Details

    • beta2
    • generic
    • generic
    • Not verified

    Description



      Name: skR10005 Date: 06/07/2001


      The spec for ClassLoader.setClassAssertionStatus() reads:
        "Sets the assertion status for the named top-level class in this class
        loader and any nested classes contained therein."

      The jdk1.4.0beta-b66 behaviour does not conform to the spec.
      The following example reproduces this failure:
      ---------------------------A.java--------------------------
      package A;

      import java.net.URL;
      import java.io.InputStream;
      import java.io.IOException;

      public class A {
          public static void main(String[] argv) {
              URL url = null;
              try {
                  url = new URL("file://" + argv[0]);
              } catch (java.net.MalformedURLException e) {
                  System.out.println("Failed: " + e);
              }
              MyClassLoader loader = new MyClassLoader(url);
              loader.setDefaultAssertionStatus(true);
              loader.setClassAssertionStatus("B.B", false);
              try {
                  Class cl = Class.forName("B.B$NestedB", true, loader);
                  I obj = (I)cl.newInstance();
                  System.out.println(cl + " - " + obj.check());
                  cl = Class.forName("B.B", true, loader);
                  obj = (I)cl.newInstance();
                  System.out.println(cl + " - " + obj.check());
              } catch (ClassNotFoundException e) {
                  System.out.println("Failed: " + e);
              } catch (InstantiationException e) {
                  System.out.println("Failed: " + e);
              } catch (IllegalAccessException e) {
                  System.out.println("Failed: " + e);
              }
          }
      }

      class MyClassLoader extends ClassLoader {
          URL url = null;

          public MyClassLoader(URL url) {
              this.url = url;
          }

          public Class findClass(String name) throws ClassNotFoundException {
              byte[] b = loadClassData(name);
              return defineClass(name, b, 0, b.length);
          }

          private byte[] loadClassData(String name) throws ClassNotFoundException {
              String path = name.replace('.', '/') + ".clazz";
              InputStream s = null;
              URL u = null;
              byte[] buffer = new byte[0];
              byte[] chunk = new byte[4096];
              int count;

              try {
                  u = new URL(url, path);
                  s = u.openStream();
                  while ((count = s.read(chunk)) >= 0) {
                     byte[] t = new byte[buffer.length + count];
                     System.arraycopy(buffer, 0, t, 0, buffer.length);
                     System.arraycopy(chunk, 0, t, buffer.length, count);
                     buffer = t;
                  }
              }
              catch (Exception e) {
                  throw new ClassNotFoundException(name + " : " + u);
              } finally {
                  try {
                      if(s != null) {
                          s.close();
                      }
                  }
                  catch (IOException e) {
                      throw new ClassNotFoundException(name);
                  }
              }
              return buffer;
          }
      }
      ---------------------------I.java--------------------------
      package A;

      public interface I {
          boolean check();
      }
      ---------------------------B.java--------------------------
      package B;
      import A.I;

      public class B implements I {
          public boolean check() {
              return getClass().desiredAssertionStatus();
          }
          public static class NestedB implements I {
              public boolean check() {
                  return getClass().desiredAssertionStatus();
              }
          }
      }
      -----------------------------------------------------------
      $javac -source 1.4 -d . *.java
      $mv B/B.class B/B.clazz
      $mv B/B$NestedB.class B/B$NetedB.clazz
      $java -version
      java version "1.4.0-beta_refresh"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta_refresh-b66)
      Java HotSpot(TM) Client VM (build 1.4.0-beta_refresh-b66, mixed mode)
      $java -cp . A.A `pwd`/
      class B.B$NestedB - true
      class B.B - false

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

      Attachments

        Activity

          People

            jjb Josh Bloch
            serjsunw Serj Serj (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: