< prev index next >

test/hotspot/jtreg/runtime/Nestmates/privateConstructors/TestConstructorHierarchy.java

Print this page




  34 
  35 public class TestConstructorHierarchy {
  36 
  37     static class NestedA extends ExternalSuper {
  38         private NestedA() {}
  39         protected NestedA(int i) {} // for compile-time only
  40     }
  41 
  42     // Access to private members of classes outside the nest is
  43     // not permitted. These tests should throw IllegalAccessError
  44     // at runtime. To allow them to compile the classes below are
  45     // defined with public members. We then replace those class files
  46     // with jcod variants that make the member private again.
  47 
  48     public static void main(String[] args) throws Throwable {
  49         try {
  50             new ExternalSuper();
  51             throw new Error("Unexpected construction of ExternalSuper");
  52         }
  53         catch (IllegalAccessError iae) {
  54             if (iae.getMessage().contains("class TestConstructorHierarchy tried to access private method 'void ExternalSuper.<init>()'")) {
  55                 System.out.println("Got expected exception constructing ExternalSuper: " + iae);
  56             }
  57             else throw new Error("Unexpected IllegalAccessError: " + iae);
  58         }
  59         try {
  60             new NestedA();
  61             throw new Error("Unexpected construction of NestedA and supers");
  62         }
  63         catch (IllegalAccessError iae) {
  64             if (iae.getMessage().contains("class TestConstructorHierarchy$NestedA tried to access private method 'void ExternalSuper.<init>()'")) {
  65                 System.out.println("Got expected exception constructing NestedA: " + iae);
  66             }
  67             else throw new Error("Unexpected IllegalAccessError: " + iae);
  68         }
  69         try {
  70             new ExternalSub();
  71             throw new Error("Unexpected construction of ExternalSub");
  72         }
  73         catch (IllegalAccessError iae) {
  74             if (iae.getMessage().contains("class ExternalSub tried to access private method 'void TestConstructorHierarchy$NestedA.<init>()'")) {
  75                 System.out.println("Got expected exception constructing ExternalSub: " + iae);
  76             }
  77             else throw new Error("Unexpected IllegalAccessError: " + iae);
  78         }
  79     }
  80 }
  81 
  82 // Classes that are not part of the nest.
  83 // Being non-public allows us to declare them in this file.
  84 // The constructor is public to allow this file to compile, but
  85 // the jcod files change it back to private.
  86 
  87 class ExternalSuper {
  88     public ExternalSuper() { }
  89 }
  90 
  91 
  92 class ExternalSub extends TestConstructorHierarchy.NestedA {
  93     public ExternalSub() {
  94         super(0); // this is changed to super() in jcod file


  34 
  35 public class TestConstructorHierarchy {
  36 
  37     static class NestedA extends ExternalSuper {
  38         private NestedA() {}
  39         protected NestedA(int i) {} // for compile-time only
  40     }
  41 
  42     // Access to private members of classes outside the nest is
  43     // not permitted. These tests should throw IllegalAccessError
  44     // at runtime. To allow them to compile the classes below are
  45     // defined with public members. We then replace those class files
  46     // with jcod variants that make the member private again.
  47 
  48     public static void main(String[] args) throws Throwable {
  49         try {
  50             new ExternalSuper();
  51             throw new Error("Unexpected construction of ExternalSuper");
  52         }
  53         catch (IllegalAccessError iae) {
  54             if (iae.getMessage().contains("class TestConstructorHierarchy tried to access private method ExternalSuper.<init>()V")) {
  55                 System.out.println("Got expected exception constructing ExternalSuper: " + iae);
  56             }
  57             else throw new Error("Unexpected IllegalAccessError: " + iae);
  58         }
  59         try {
  60             new NestedA();
  61             throw new Error("Unexpected construction of NestedA and supers");
  62         }
  63         catch (IllegalAccessError iae) {
  64             if (iae.getMessage().contains("class TestConstructorHierarchy$NestedA tried to access private method ExternalSuper.<init>()V")) {
  65                 System.out.println("Got expected exception constructing NestedA: " + iae);
  66             }
  67             else throw new Error("Unexpected IllegalAccessError: " + iae);
  68         }
  69         try {
  70             new ExternalSub();
  71             throw new Error("Unexpected construction of ExternalSub");
  72         }
  73         catch (IllegalAccessError iae) {
  74             if (iae.getMessage().contains("class ExternalSub tried to access private method TestConstructorHierarchy$NestedA.<init>()V")) {
  75                 System.out.println("Got expected exception constructing ExternalSub: " + iae);
  76             }
  77             else throw new Error("Unexpected IllegalAccessError: " + iae);
  78         }
  79     }
  80 }
  81 
  82 // Classes that are not part of the nest.
  83 // Being non-public allows us to declare them in this file.
  84 // The constructor is public to allow this file to compile, but
  85 // the jcod files change it back to private.
  86 
  87 class ExternalSuper {
  88     public ExternalSuper() { }
  89 }
  90 
  91 
  92 class ExternalSub extends TestConstructorHierarchy.NestedA {
  93     public ExternalSub() {
  94         super(0); // this is changed to super() in jcod file
< prev index next >