blob: 4346152f6cc00128b3c80a77196edfd1632f78b7 [file] [log] [blame]
Vladimir Marko51b8aaf2017-06-09 15:17:05 +01001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17public class Main {
18 public static void main(String args[]) {
19 simpleTest();
20 hierarchyTest();
21 }
22
23 public static void simpleTest() {
24 // Partial initialization of Bad; ignoring the error.
25 Error badClinit = null;
26 try {
27 new Bad(11);
28 } catch (Error e) {
29 badClinit = e;
30 }
31 // Call foo() on the escaped instance of Bad.
32 try {
33 bad.foo();
34 } catch (NoClassDefFoundError ncdfe) {
35 // On RI, the NCDFE has no cause. On ART, the badClinit is the cause.
36 if (ncdfe.getCause() == badClinit || ncdfe.getCause() == null) {
37 System.out.println("Caught NoClassDefFoundError.");
38 } else {
39 ncdfe.printStackTrace();
40 }
41 }
42 }
43
44 public static void hierarchyTest() {
45 // Partial initialization of BadSuper; ignoring the error. Fully initializes BadSub.
46 Error badClinit = null;
47 try {
48 new BadSuper(0);
49 } catch (Error e) {
50 badClinit = e;
51 }
52 // Call BadSuper.foo() on the escaped instance of BadSuper.
53 try {
54 badSuper.foo();
55 } catch (NoClassDefFoundError ncdfe) {
56 // On RI, the NCDFE has no cause. On ART, the badClinit is the cause.
57 if (ncdfe.getCause() == badClinit || ncdfe.getCause() == null) {
58 System.out.println("Caught NoClassDefFoundError.");
59 } else {
60 ncdfe.printStackTrace();
61 }
62 }
63
64 // Call BadSub.bar() on the escaped instance of BadSub.
65 try {
66 badSub.bar();
67 } catch (NoClassDefFoundError ncdfe) {
68 // On RI, the NCDFE has no cause. On ART, the badClinit is the cause.
69 if (ncdfe.getCause() == badClinit || ncdfe.getCause() == null) {
70 System.out.println("Caught NoClassDefFoundError.");
71 } else {
72 ncdfe.printStackTrace();
73 }
74 }
75
76 // Test that we can even create instances of BadSub with erroneous superclass BadSuper.
77 try {
78 new BadSub(-1, -2).bar();
79 } catch (NoClassDefFoundError ncdfe) {
80 // On RI, the NCDFE has no cause. On ART, the badClinit is the cause.
81 if (ncdfe.getCause() == badClinit || ncdfe.getCause() == null) {
82 System.out.println("Caught NoClassDefFoundError.");
83 } else {
84 ncdfe.printStackTrace();
85 }
86 }
87
88 // Test that we cannot create instances of BadSuper from BadSub.
89 try {
90 badSub.allocSuper(11111); // Should throw.
91 System.out.println("Allocated BadSuper!");
92 } catch (NoClassDefFoundError ncdfe) {
93 // On RI, the NCDFE has no cause. On ART, the badClinit is the cause.
94 if (ncdfe.getCause() == badClinit || ncdfe.getCause() == null) {
95 System.out.println("Caught NoClassDefFoundError.");
96 } else {
97 ncdfe.printStackTrace();
98 }
99 }
100 }
101
102 public static Bad bad;
103
104 public static BadSuper badSuper;
105 public static BadSub badSub;
106}
107
108class Bad {
109 static {
110 // Create an instance of Bad and let it escape in Main.bad.
111 Main.bad = new Bad(33);
112 staticValue = 42;
113 if (true) { throw new Error("Bad <clinit>"); }
114 }
115 public void foo() {
116 System.out.println("Bad.foo()");
117 System.out.println("Bad.instanceValue = " + instanceValue);
118 System.out.println("Bad.staticValue = " + staticValue);
119 }
120 public Bad(int iv) { instanceValue = iv; }
121 public int instanceValue;
122 public static int staticValue;
123}
124
125class BadSuper {
126 static {
127 Main.badSuper = new BadSuper(1);
128 Main.badSub = new BadSub(11, 111); // Fully initializes BadSub.
129 BadSuper.superStaticValue = 42;
130 BadSub.subStaticValue = 4242;
131 if (true) { throw new Error("Bad <clinit>"); }
132 }
133 public void foo() {
134 System.out.println("BadSuper.foo()");
135 System.out.println("BadSuper.superInstanceValue = " + superInstanceValue);
136 System.out.println("BadSuper.superStaticValue = " + superStaticValue);
137 }
138 public BadSuper(int superiv) { superInstanceValue = superiv; }
139 public int superInstanceValue;
140 public static int superStaticValue;
141}
142
143// Note: If we tried to initialize BadSub before BadSuper, it would end up erroneous
144// because the superclass fails initialization. However, since we start initializing the
145// BadSuper first, BadSub is initialized successfully while BadSuper is "initializing"
146// and remains initialized after the BadSuper's class initializer throws.
147class BadSub extends BadSuper {
148 public void bar() {
149 System.out.println("BadSub.bar()");
150 System.out.println("BadSub.subInstanceValue = " + subInstanceValue);
151 System.out.println("BadSub.subStaticValue = " + subStaticValue);
152 System.out.println("BadSuper.superInstanceValue = " + superInstanceValue);
153 System.out.println("BadSuper.superStaticValue = " + superStaticValue);
154 }
155 public BadSuper allocSuper(int superiv) {
156 System.out.println("BadSub.allocSuper(.)");
157 return new BadSuper(superiv);
158 }
159 public BadSub(int subiv, int superiv) {
160 super(superiv);
161 subInstanceValue = subiv;
162 }
163 public int subInstanceValue;
164 public static int subStaticValue;
165}