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 { 18 public static void main(String[] args) { 19 checkExceptions(); 20 checkTiming(); 21 } 22 23 public static void sleep(int msec) { 24 try { 25 Thread.sleep(msec); 26 } catch (InterruptedException ie) { 27 System.err.println("sleep interrupted"); 28 } 29 } 30 31 static void checkExceptions() { 32 try { 33 System.out.println(PartialInit.FIELD0); 34 System.err.println("Construction of PartialInit succeeded unexpectedly"); 35 } catch (ExceptionInInitializerError eiie) { 36 System.out.println("Got expected EIIE for FIELD0"); 37 } 38 39 try { 40 System.out.println(PartialInit.FIELD0); 41 System.err.println("Load of FIELD0 succeeded unexpectedly"); 42 } catch (NoClassDefFoundError ncdfe) { 43 System.out.println("Got expected NCDFE for FIELD0"); 44 } 45 try { 46 System.out.println(PartialInit.FIELD1); 47 System.err.println("Load of FIELD1 succeeded unexpectedly"); 48 } catch (NoClassDefFoundError ncdfe) { 49 System.out.println("Got expected NCDFE for FIELD1"); 50 } 51 52 try { 53 System.out.println(Exploder.FIELD); 54 System.err.println("Load of FIELD succeeded unexpectedly"); 55 } catch (AssertionError expected) { 56 System.out.println("Got expected '" + expected.getMessage() + "' from Exploder"); 57 } 58 } 59 60 static void checkTiming() { 61 FieldThread fieldThread = new FieldThread(); 62 MethodThread methodThread = new MethodThread(); 63 64 fieldThread.start(); 65 methodThread.start(); 66 67 /* start class init */ 68 IntHolder zero = SlowInit.FIELD0; 69 70 /* wait for children to complete */ 71 try { 72 fieldThread.join(); 73 methodThread.join(); 74 } catch (InterruptedException ie) { 75 System.err.println(ie); 76 } 77 78 /* print all values */ 79 System.out.println("Fields (main thread): " + 80 SlowInit.FIELD0.getValue() + SlowInit.FIELD1.getValue() + 81 SlowInit.FIELD2.getValue() + SlowInit.FIELD3.getValue()); 82 } 83 84 static class FieldThread extends Thread { 85 public void run() { 86 /* allow SlowInit's <clinit> to start */ 87 Main.sleep(1000); 88 89 /* collect fields; should delay until class init completes */ 90 int field0, field1, field2, field3; 91 field0 = SlowInit.FIELD0.getValue(); 92 field1 = SlowInit.FIELD1.getValue(); 93 field2 = SlowInit.FIELD2.getValue(); 94 field3 = SlowInit.FIELD3.getValue(); 95 96 /* let MethodThread print first */ 97 Main.sleep(5000); 98 System.out.println("Fields (child thread): " + 99 field0 + field1 + field2 + field3); 100 } 101 } 102 103 static class MethodThread extends Thread { 104 public void run() { 105 /* allow SlowInit's <clinit> to start */ 106 Main.sleep(1000); 107 108 /* use a method that shouldn't be accessible yet */ 109 SlowInit.printMsg("MethodThread message"); 110 } 111 } 112 } 113