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(); |
| 27 | System.err.println("didn't throw!"); |
| 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(); |
| 40 | } |
| 41 | |
| 42 | public static void sleep(int msec) { |
| 43 | try { |
| 44 | Thread.sleep(msec); |
| 45 | } catch (InterruptedException ie) { |
| 46 | System.err.println("sleep interrupted"); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | static void checkExceptions() { |
| 51 | try { |
| 52 | System.out.println(PartialInit.FIELD0); |
| 53 | System.err.println("Construction of PartialInit succeeded unexpectedly"); |
| 54 | } catch (ExceptionInInitializerError eiie) { |
| 55 | System.out.println("Got expected EIIE for FIELD0"); |
| 56 | } |
| 57 | |
| 58 | try { |
| 59 | System.out.println(PartialInit.FIELD0); |
| 60 | System.err.println("Load of FIELD0 succeeded unexpectedly"); |
| 61 | } catch (NoClassDefFoundError ncdfe) { |
| 62 | System.out.println("Got expected NCDFE for FIELD0"); |
| 63 | } |
| 64 | try { |
| 65 | System.out.println(PartialInit.FIELD1); |
| 66 | System.err.println("Load of FIELD1 succeeded unexpectedly"); |
| 67 | } catch (NoClassDefFoundError ncdfe) { |
| 68 | System.out.println("Got expected NCDFE for FIELD1"); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | static void checkTiming() { |
| 73 | FieldThread fieldThread = new FieldThread(); |
| 74 | MethodThread methodThread = new MethodThread(); |
| 75 | |
| 76 | fieldThread.start(); |
| 77 | methodThread.start(); |
| 78 | |
| 79 | /* start class init */ |
| 80 | IntHolder zero = SlowInit.FIELD0; |
| 81 | |
| 82 | /* wait for children to complete */ |
| 83 | try { |
| 84 | fieldThread.join(); |
| 85 | methodThread.join(); |
| 86 | } catch (InterruptedException ie) { |
| 87 | System.err.println(ie); |
| 88 | } |
| 89 | |
| 90 | /* print all values */ |
| 91 | System.out.println("Fields (main thread): " + |
| 92 | SlowInit.FIELD0.getValue() + SlowInit.FIELD1.getValue() + |
| 93 | SlowInit.FIELD2.getValue() + SlowInit.FIELD3.getValue()); |
| 94 | } |
| 95 | |
| 96 | static class FieldThread extends Thread { |
| 97 | public void run() { |
Elliott Hughes | 741b5b7 | 2012-01-31 19:18:51 -0800 | [diff] [blame] | 98 | /* allow SlowInit's <clinit> to start */ |
jeffhao | 94d6df4 | 2012-11-26 16:02:12 -0800 | [diff] [blame^] | 99 | Main.sleep(5000); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 100 | |
| 101 | /* collect fields; should delay until class init completes */ |
| 102 | int field0, field1, field2, field3; |
| 103 | field0 = SlowInit.FIELD0.getValue(); |
| 104 | field1 = SlowInit.FIELD1.getValue(); |
| 105 | field2 = SlowInit.FIELD2.getValue(); |
| 106 | field3 = SlowInit.FIELD3.getValue(); |
| 107 | |
| 108 | /* let MethodThread print first */ |
Elliott Hughes | 741b5b7 | 2012-01-31 19:18:51 -0800 | [diff] [blame] | 109 | Main.sleep(5000); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 110 | System.out.println("Fields (child thread): " + |
| 111 | field0 + field1 + field2 + field3); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | static class MethodThread extends Thread { |
| 116 | public void run() { |
Elliott Hughes | 741b5b7 | 2012-01-31 19:18:51 -0800 | [diff] [blame] | 117 | /* allow SlowInit's <clinit> to start */ |
jeffhao | 94d6df4 | 2012-11-26 16:02:12 -0800 | [diff] [blame^] | 118 | Main.sleep(5000); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 119 | |
| 120 | /* use a method that shouldn't be accessible yet */ |
| 121 | SlowInit.printMsg("MethodThread message"); |
| 122 | } |
| 123 | } |
| 124 | } |