1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2007 The Android Open Source Project 3*795d594fSAndroid Build Coastguard Worker * 4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*795d594fSAndroid Build Coastguard Worker * 8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*795d594fSAndroid Build Coastguard Worker * 10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*795d594fSAndroid Build Coastguard Worker * limitations under the License. 15*795d594fSAndroid Build Coastguard Worker */ 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker // An error class. 18*795d594fSAndroid Build Coastguard Worker class BadError extends Error { BadError(String s)19*795d594fSAndroid Build Coastguard Worker public BadError(String s) { 20*795d594fSAndroid Build Coastguard Worker super("This is bad by convention: " + s); 21*795d594fSAndroid Build Coastguard Worker } 22*795d594fSAndroid Build Coastguard Worker } 23*795d594fSAndroid Build Coastguard Worker 24*795d594fSAndroid Build Coastguard Worker // A class that throws BadError during static initialization. 25*795d594fSAndroid Build Coastguard Worker class BadInit { 26*795d594fSAndroid Build Coastguard Worker static int intField; 27*795d594fSAndroid Build Coastguard Worker static { 28*795d594fSAndroid Build Coastguard Worker System.out.println("Static Init"); 29*795d594fSAndroid Build Coastguard Worker if (true) { 30*795d594fSAndroid Build Coastguard Worker throw new BadError("BadInit"); 31*795d594fSAndroid Build Coastguard Worker } 32*795d594fSAndroid Build Coastguard Worker } 33*795d594fSAndroid Build Coastguard Worker } 34*795d594fSAndroid Build Coastguard Worker 35*795d594fSAndroid Build Coastguard Worker // An error that doesn't have a <init>(String) method. 36*795d594fSAndroid Build Coastguard Worker class BadErrorNoStringInit extends Error { BadErrorNoStringInit()37*795d594fSAndroid Build Coastguard Worker public BadErrorNoStringInit() { 38*795d594fSAndroid Build Coastguard Worker super("This is bad by convention"); 39*795d594fSAndroid Build Coastguard Worker } 40*795d594fSAndroid Build Coastguard Worker } 41*795d594fSAndroid Build Coastguard Worker 42*795d594fSAndroid Build Coastguard Worker // A class that throws BadErrorNoStringInit during static initialization. 43*795d594fSAndroid Build Coastguard Worker class BadInitNoStringInit { 44*795d594fSAndroid Build Coastguard Worker static int intField; 45*795d594fSAndroid Build Coastguard Worker static { 46*795d594fSAndroid Build Coastguard Worker System.out.println("Static BadInitNoStringInit"); 47*795d594fSAndroid Build Coastguard Worker if (true) { BadErrorNoStringInit()48*795d594fSAndroid Build Coastguard Worker throw new BadErrorNoStringInit(); 49*795d594fSAndroid Build Coastguard Worker } 50*795d594fSAndroid Build Coastguard Worker } 51*795d594fSAndroid Build Coastguard Worker } 52*795d594fSAndroid Build Coastguard Worker 53*795d594fSAndroid Build Coastguard Worker // A class that throws BadError during static initialization, serving as a super class. 54*795d594fSAndroid Build Coastguard Worker class BadSuperClass { 55*795d594fSAndroid Build Coastguard Worker static int intField; 56*795d594fSAndroid Build Coastguard Worker static { 57*795d594fSAndroid Build Coastguard Worker System.out.println("BadSuperClass Static Init"); 58*795d594fSAndroid Build Coastguard Worker if (true) { 59*795d594fSAndroid Build Coastguard Worker throw new BadError("BadInit"); 60*795d594fSAndroid Build Coastguard Worker } 61*795d594fSAndroid Build Coastguard Worker } 62*795d594fSAndroid Build Coastguard Worker } 63*795d594fSAndroid Build Coastguard Worker 64*795d594fSAndroid Build Coastguard Worker // A class that derives from BadSuperClass. 65*795d594fSAndroid Build Coastguard Worker class DerivedFromBadSuperClass extends BadSuperClass { 66*795d594fSAndroid Build Coastguard Worker } 67*795d594fSAndroid Build Coastguard Worker 68*795d594fSAndroid Build Coastguard Worker /** 69*795d594fSAndroid Build Coastguard Worker * Exceptions across method calls 70*795d594fSAndroid Build Coastguard Worker */ 71*795d594fSAndroid Build Coastguard Worker public class Main { exceptions_007()72*795d594fSAndroid Build Coastguard Worker public static void exceptions_007() { 73*795d594fSAndroid Build Coastguard Worker try { 74*795d594fSAndroid Build Coastguard Worker catchAndRethrow(); 75*795d594fSAndroid Build Coastguard Worker } catch (NullPointerException npe) { 76*795d594fSAndroid Build Coastguard Worker System.out.print("Got an NPE: "); 77*795d594fSAndroid Build Coastguard Worker System.out.println(npe.getMessage()); 78*795d594fSAndroid Build Coastguard Worker npe.printStackTrace(System.out); 79*795d594fSAndroid Build Coastguard Worker } 80*795d594fSAndroid Build Coastguard Worker } main(String args[])81*795d594fSAndroid Build Coastguard Worker public static void main(String args[]) { 82*795d594fSAndroid Build Coastguard Worker exceptions_007(); 83*795d594fSAndroid Build Coastguard Worker exceptionsRethrowClassInitFailure(); 84*795d594fSAndroid Build Coastguard Worker exceptionsRethrowClassInitFailureNoStringInit(); 85*795d594fSAndroid Build Coastguard Worker exceptionsForSuperClassInitFailure(); 86*795d594fSAndroid Build Coastguard Worker exceptionsInMultiDex(); 87*795d594fSAndroid Build Coastguard Worker } 88*795d594fSAndroid Build Coastguard Worker catchAndRethrow()89*795d594fSAndroid Build Coastguard Worker private static void catchAndRethrow() { 90*795d594fSAndroid Build Coastguard Worker try { 91*795d594fSAndroid Build Coastguard Worker throwNullPointerException(); 92*795d594fSAndroid Build Coastguard Worker } catch (NullPointerException npe) { 93*795d594fSAndroid Build Coastguard Worker NullPointerException npe2; 94*795d594fSAndroid Build Coastguard Worker npe2 = new NullPointerException("second throw"); 95*795d594fSAndroid Build Coastguard Worker npe2.initCause(npe); 96*795d594fSAndroid Build Coastguard Worker throw npe2; 97*795d594fSAndroid Build Coastguard Worker } 98*795d594fSAndroid Build Coastguard Worker } 99*795d594fSAndroid Build Coastguard Worker throwNullPointerException()100*795d594fSAndroid Build Coastguard Worker private static void throwNullPointerException() { 101*795d594fSAndroid Build Coastguard Worker throw new NullPointerException("first throw"); 102*795d594fSAndroid Build Coastguard Worker } 103*795d594fSAndroid Build Coastguard Worker exceptionsRethrowClassInitFailure()104*795d594fSAndroid Build Coastguard Worker private static void exceptionsRethrowClassInitFailure() { 105*795d594fSAndroid Build Coastguard Worker try { 106*795d594fSAndroid Build Coastguard Worker try { 107*795d594fSAndroid Build Coastguard Worker BadInit.intField = 1; 108*795d594fSAndroid Build Coastguard Worker throw new IllegalStateException("Should not reach here."); 109*795d594fSAndroid Build Coastguard Worker } catch (BadError e) { 110*795d594fSAndroid Build Coastguard Worker System.out.println(e); 111*795d594fSAndroid Build Coastguard Worker } 112*795d594fSAndroid Build Coastguard Worker 113*795d594fSAndroid Build Coastguard Worker // Check if it works a second time. 114*795d594fSAndroid Build Coastguard Worker 115*795d594fSAndroid Build Coastguard Worker try { 116*795d594fSAndroid Build Coastguard Worker BadInit.intField = 1; 117*795d594fSAndroid Build Coastguard Worker throw new IllegalStateException("Should not reach here."); 118*795d594fSAndroid Build Coastguard Worker } catch (NoClassDefFoundError e) { 119*795d594fSAndroid Build Coastguard Worker System.out.println(e); 120*795d594fSAndroid Build Coastguard Worker System.out.println(e.getCause()); 121*795d594fSAndroid Build Coastguard Worker } 122*795d594fSAndroid Build Coastguard Worker } catch (Exception error) { 123*795d594fSAndroid Build Coastguard Worker error.printStackTrace(System.out); 124*795d594fSAndroid Build Coastguard Worker } 125*795d594fSAndroid Build Coastguard Worker } 126*795d594fSAndroid Build Coastguard Worker exceptionsRethrowClassInitFailureNoStringInit()127*795d594fSAndroid Build Coastguard Worker private static void exceptionsRethrowClassInitFailureNoStringInit() { 128*795d594fSAndroid Build Coastguard Worker try { 129*795d594fSAndroid Build Coastguard Worker try { 130*795d594fSAndroid Build Coastguard Worker BadInitNoStringInit.intField = 1; 131*795d594fSAndroid Build Coastguard Worker throw new IllegalStateException("Should not reach here."); 132*795d594fSAndroid Build Coastguard Worker } catch (BadErrorNoStringInit e) { 133*795d594fSAndroid Build Coastguard Worker System.out.println(e); 134*795d594fSAndroid Build Coastguard Worker } 135*795d594fSAndroid Build Coastguard Worker 136*795d594fSAndroid Build Coastguard Worker // Check if it works a second time. 137*795d594fSAndroid Build Coastguard Worker 138*795d594fSAndroid Build Coastguard Worker try { 139*795d594fSAndroid Build Coastguard Worker BadInitNoStringInit.intField = 1; 140*795d594fSAndroid Build Coastguard Worker throw new IllegalStateException("Should not reach here."); 141*795d594fSAndroid Build Coastguard Worker } catch (NoClassDefFoundError e) { 142*795d594fSAndroid Build Coastguard Worker System.out.println(e); 143*795d594fSAndroid Build Coastguard Worker System.out.println(e.getCause()); 144*795d594fSAndroid Build Coastguard Worker } 145*795d594fSAndroid Build Coastguard Worker } catch (Exception error) { 146*795d594fSAndroid Build Coastguard Worker error.printStackTrace(System.out); 147*795d594fSAndroid Build Coastguard Worker } 148*795d594fSAndroid Build Coastguard Worker } 149*795d594fSAndroid Build Coastguard Worker exceptionsForSuperClassInitFailure()150*795d594fSAndroid Build Coastguard Worker private static void exceptionsForSuperClassInitFailure() { 151*795d594fSAndroid Build Coastguard Worker try { 152*795d594fSAndroid Build Coastguard Worker // Resolve DerivedFromBadSuperClass. 153*795d594fSAndroid Build Coastguard Worker BadSuperClass.intField = 1; 154*795d594fSAndroid Build Coastguard Worker throw new IllegalStateException("Should not reach here."); 155*795d594fSAndroid Build Coastguard Worker } catch (BadError e) { 156*795d594fSAndroid Build Coastguard Worker System.out.println(e); 157*795d594fSAndroid Build Coastguard Worker } catch (Throwable t) { 158*795d594fSAndroid Build Coastguard Worker t.printStackTrace(System.out); 159*795d594fSAndroid Build Coastguard Worker } 160*795d594fSAndroid Build Coastguard Worker try { 161*795d594fSAndroid Build Coastguard Worker // Before splitting ClassStatus::kError into 162*795d594fSAndroid Build Coastguard Worker // ClassStatus::kErrorUnresolved and ClassStatus::kErrorResolved, 163*795d594fSAndroid Build Coastguard Worker // this would trigger a 164*795d594fSAndroid Build Coastguard Worker // CHECK(super_class->IsResolved()) 165*795d594fSAndroid Build Coastguard Worker // failure in 166*795d594fSAndroid Build Coastguard Worker // ClassLinker::LoadSuperAndInterfaces(). 167*795d594fSAndroid Build Coastguard Worker // After the change we're getting either VerifyError 168*795d594fSAndroid Build Coastguard Worker // (for Optimizing) or NoClassDefFoundError wrapping 169*795d594fSAndroid Build Coastguard Worker // BadError (for interpreter or JIT). 170*795d594fSAndroid Build Coastguard Worker new DerivedFromBadSuperClass(); 171*795d594fSAndroid Build Coastguard Worker throw new IllegalStateException("Should not reach here."); 172*795d594fSAndroid Build Coastguard Worker } catch (NoClassDefFoundError ncdfe) { 173*795d594fSAndroid Build Coastguard Worker if (!(ncdfe.getCause() instanceof BadError)) { 174*795d594fSAndroid Build Coastguard Worker ncdfe.getCause().printStackTrace(System.out); 175*795d594fSAndroid Build Coastguard Worker } 176*795d594fSAndroid Build Coastguard Worker } catch (VerifyError e) { 177*795d594fSAndroid Build Coastguard Worker } catch (Throwable t) { 178*795d594fSAndroid Build Coastguard Worker t.printStackTrace(System.out); 179*795d594fSAndroid Build Coastguard Worker } 180*795d594fSAndroid Build Coastguard Worker } 181*795d594fSAndroid Build Coastguard Worker exceptionsInMultiDex()182*795d594fSAndroid Build Coastguard Worker private static void exceptionsInMultiDex() { 183*795d594fSAndroid Build Coastguard Worker try { 184*795d594fSAndroid Build Coastguard Worker MultiDexBadInit.intField = 1; 185*795d594fSAndroid Build Coastguard Worker throw new IllegalStateException("Should not reach here."); 186*795d594fSAndroid Build Coastguard Worker } catch (Error e) { 187*795d594fSAndroid Build Coastguard Worker System.out.println(e); 188*795d594fSAndroid Build Coastguard Worker } catch (Throwable t) { 189*795d594fSAndroid Build Coastguard Worker t.printStackTrace(System.out); 190*795d594fSAndroid Build Coastguard Worker } 191*795d594fSAndroid Build Coastguard Worker // Before splitting ClassStatus::kError into 192*795d594fSAndroid Build Coastguard Worker // ClassStatus::kErrorUnresolved and ClassStatus::kErrorResolved, 193*795d594fSAndroid Build Coastguard Worker // the exception from wrapper 1 would have been 194*795d594fSAndroid Build Coastguard Worker // wrapped in NoClassDefFoundError but the exception 195*795d594fSAndroid Build Coastguard Worker // from wrapper 2 would have been unwrapped. 196*795d594fSAndroid Build Coastguard Worker try { 197*795d594fSAndroid Build Coastguard Worker MultiDexBadInitWrapper1.setIntField(1); 198*795d594fSAndroid Build Coastguard Worker throw new IllegalStateException("Should not reach here."); 199*795d594fSAndroid Build Coastguard Worker } catch (NoClassDefFoundError ncdfe) { 200*795d594fSAndroid Build Coastguard Worker System.out.println(ncdfe); 201*795d594fSAndroid Build Coastguard Worker System.out.println(" cause: " + ncdfe.getCause()); 202*795d594fSAndroid Build Coastguard Worker } catch (Throwable t) { 203*795d594fSAndroid Build Coastguard Worker t.printStackTrace(System.out); 204*795d594fSAndroid Build Coastguard Worker } 205*795d594fSAndroid Build Coastguard Worker try { 206*795d594fSAndroid Build Coastguard Worker MultiDexBadInitWrapper2.setIntField(1); 207*795d594fSAndroid Build Coastguard Worker throw new IllegalStateException("Should not reach here."); 208*795d594fSAndroid Build Coastguard Worker } catch (NoClassDefFoundError ncdfe) { 209*795d594fSAndroid Build Coastguard Worker System.out.println(ncdfe); 210*795d594fSAndroid Build Coastguard Worker System.out.println(" cause: " + ncdfe.getCause()); 211*795d594fSAndroid Build Coastguard Worker } catch (Throwable t) { 212*795d594fSAndroid Build Coastguard Worker t.printStackTrace(System.out); 213*795d594fSAndroid Build Coastguard Worker } 214*795d594fSAndroid Build Coastguard Worker } 215*795d594fSAndroid Build Coastguard Worker } 216