jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
| 17 | public class Main { |
Elliott Hughes | a58ff17 | 2012-02-29 16:33:23 -0800 | [diff] [blame] | 18 | static { |
Elliott Hughes | 79686a0 | 2012-03-01 10:16:45 -0800 | [diff] [blame] | 19 | staticMethodCalledByClinit(); |
Elliott Hughes | a58ff17 | 2012-02-29 16:33:23 -0800 | [diff] [blame] | 20 | } |
| 21 | |
Elliott Hughes | 79686a0 | 2012-03-01 10:16:45 -0800 | [diff] [blame] | 22 | private static void staticMethodCalledByClinit() { |
| 23 | // Test that DeliverException works when we need to unwind to a handler -- this method -- |
| 24 | // that is currently a resolution stub because it's running on behalf of <clinit>. |
Elliott Hughes | a58ff17 | 2012-02-29 16:33:23 -0800 | [diff] [blame] | 25 | try { |
| 26 | throwDuringClinit(); |
Kevin Brodsky | f6c66c3 | 2015-12-17 14:13:00 +0000 | [diff] [blame] | 27 | System.out.println("didn't throw!"); |
Elliott Hughes | a58ff17 | 2012-02-29 16:33:23 -0800 | [diff] [blame] | 28 | } catch (NullPointerException ex) { |
| 29 | System.out.println("caught exception thrown during clinit"); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | private static void throwDuringClinit() { |
| 34 | throw new NullPointerException(); |
| 35 | } |
| 36 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 37 | public static void main(String[] args) { |
| 38 | checkExceptions(); |
| 39 | checkTiming(); |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 40 | checkStaticMethodInvokeAfterFailedClinit(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | public static void sleep(int msec) { |
| 44 | try { |
| 45 | Thread.sleep(msec); |
| 46 | } catch (InterruptedException ie) { |
Kevin Brodsky | f6c66c3 | 2015-12-17 14:13:00 +0000 | [diff] [blame] | 47 | System.out.println("sleep interrupted"); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
| 51 | static void checkExceptions() { |
| 52 | try { |
| 53 | System.out.println(PartialInit.FIELD0); |
Kevin Brodsky | f6c66c3 | 2015-12-17 14:13:00 +0000 | [diff] [blame] | 54 | System.out.println("Construction of PartialInit succeeded unexpectedly"); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 55 | } catch (ExceptionInInitializerError eiie) { |
| 56 | System.out.println("Got expected EIIE for FIELD0"); |
| 57 | } |
| 58 | |
| 59 | try { |
| 60 | System.out.println(PartialInit.FIELD0); |
Kevin Brodsky | f6c66c3 | 2015-12-17 14:13:00 +0000 | [diff] [blame] | 61 | System.out.println("Load of FIELD0 succeeded unexpectedly"); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 62 | } catch (NoClassDefFoundError ncdfe) { |
| 63 | System.out.println("Got expected NCDFE for FIELD0"); |
| 64 | } |
| 65 | try { |
| 66 | System.out.println(PartialInit.FIELD1); |
Kevin Brodsky | f6c66c3 | 2015-12-17 14:13:00 +0000 | [diff] [blame] | 67 | System.out.println("Load of FIELD1 succeeded unexpectedly"); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 68 | } catch (NoClassDefFoundError ncdfe) { |
| 69 | System.out.println("Got expected NCDFE for FIELD1"); |
| 70 | } |
Elliott Hughes | 4f3d94b | 2012-12-10 15:47:02 -0800 | [diff] [blame] | 71 | |
| 72 | try { |
| 73 | System.out.println(Exploder.FIELD); |
Kevin Brodsky | f6c66c3 | 2015-12-17 14:13:00 +0000 | [diff] [blame] | 74 | System.out.println("Load of FIELD succeeded unexpectedly"); |
Elliott Hughes | 4f3d94b | 2012-12-10 15:47:02 -0800 | [diff] [blame] | 75 | } catch (AssertionError expected) { |
| 76 | System.out.println("Got expected '" + expected.getMessage() + "' from Exploder"); |
| 77 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static void checkTiming() { |
| 81 | FieldThread fieldThread = new FieldThread(); |
| 82 | MethodThread methodThread = new MethodThread(); |
| 83 | |
| 84 | fieldThread.start(); |
| 85 | methodThread.start(); |
| 86 | |
| 87 | /* start class init */ |
| 88 | IntHolder zero = SlowInit.FIELD0; |
| 89 | |
| 90 | /* wait for children to complete */ |
| 91 | try { |
| 92 | fieldThread.join(); |
| 93 | methodThread.join(); |
| 94 | } catch (InterruptedException ie) { |
Kevin Brodsky | f6c66c3 | 2015-12-17 14:13:00 +0000 | [diff] [blame] | 95 | System.out.println(ie); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /* print all values */ |
| 99 | System.out.println("Fields (main thread): " + |
| 100 | SlowInit.FIELD0.getValue() + SlowInit.FIELD1.getValue() + |
| 101 | SlowInit.FIELD2.getValue() + SlowInit.FIELD3.getValue()); |
| 102 | } |
| 103 | |
| 104 | static class FieldThread extends Thread { |
| 105 | public void run() { |
Elliott Hughes | 741b5b7 | 2012-01-31 19:18:51 -0800 | [diff] [blame] | 106 | /* allow SlowInit's <clinit> to start */ |
jeffhao | 94d6df4 | 2012-11-26 16:02:12 -0800 | [diff] [blame] | 107 | Main.sleep(5000); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 108 | |
| 109 | /* collect fields; should delay until class init completes */ |
| 110 | int field0, field1, field2, field3; |
| 111 | field0 = SlowInit.FIELD0.getValue(); |
| 112 | field1 = SlowInit.FIELD1.getValue(); |
| 113 | field2 = SlowInit.FIELD2.getValue(); |
| 114 | field3 = SlowInit.FIELD3.getValue(); |
| 115 | |
| 116 | /* let MethodThread print first */ |
Elliott Hughes | 741b5b7 | 2012-01-31 19:18:51 -0800 | [diff] [blame] | 117 | Main.sleep(5000); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 118 | System.out.println("Fields (child thread): " + |
| 119 | field0 + field1 + field2 + field3); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | static class MethodThread extends Thread { |
| 124 | public void run() { |
Elliott Hughes | 741b5b7 | 2012-01-31 19:18:51 -0800 | [diff] [blame] | 125 | /* allow SlowInit's <clinit> to start */ |
jeffhao | 94d6df4 | 2012-11-26 16:02:12 -0800 | [diff] [blame] | 126 | Main.sleep(5000); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 127 | |
| 128 | /* use a method that shouldn't be accessible yet */ |
| 129 | SlowInit.printMsg("MethodThread message"); |
| 130 | } |
| 131 | } |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 132 | |
| 133 | static void checkStaticMethodInvokeAfterFailedClinit() { |
| 134 | System.out.println("checkStaticMethodInvokeAfterFailedClinit START"); |
| 135 | |
| 136 | // Call static method to cause implicit clinit. |
| 137 | try { |
| 138 | ClassWithThrowingClinit.staticMethod(); |
| 139 | System.out.println("checkStaticMethodInvokeAfterFailedClinit FAILED" |
| 140 | + " due to missing ExceptionInInitializerError"); |
| 141 | } catch (ExceptionInInitializerError expected) { |
| 142 | } |
| 143 | |
| 144 | // Call again to make sure we still get the expected error. |
| 145 | try { |
| 146 | ClassWithThrowingClinit.staticMethod(); |
| 147 | System.out.println("checkStaticMethodInvokeAfterFailedClinit FAILED" |
| 148 | + " due to missing NoClassDefFoundError"); |
| 149 | } catch (NoClassDefFoundError expected) { |
| 150 | } |
| 151 | System.out.println("checkStaticMethodInvokeAfterFailedClinit PASSED"); |
| 152 | } |
| 153 | |
| 154 | static class ClassWithThrowingClinit { |
| 155 | static { |
| 156 | throwDuringClinit(); |
| 157 | } |
| 158 | static void staticMethod() { |
| 159 | } |
| 160 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 161 | } |