1917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul/* 2917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Copyright (C) 2007 The Android Open Source Project 3917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 4917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Licensed under the Apache License, Version 2.0 (the "License"); 5917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * you may not use this file except in compliance with the License. 6917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * You may obtain a copy of the License at 7917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 8917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * http://www.apache.org/licenses/LICENSE-2.0 9917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 10917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Unless required by applicable law or agreed to in writing, software 11917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * distributed under the License is distributed on an "AS IS" BASIS, 12917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * See the License for the specific language governing permissions and 14917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * limitations under the License. 15917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 16917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 17917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulpackage com.android.dexgen.util; 18917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 19917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport java.io.PrintStream; 20917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport java.io.PrintWriter; 21917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 22917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul/** 23917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Exception which carries around structured context. 24917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 25917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulpublic class ExceptionWithContext 26917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul extends RuntimeException { 27917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@code non-null;} human-oriented context of the exception */ 28917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul private StringBuffer context; 29917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 30917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 31917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Augments the given exception with the given context, and return the 32917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * result. The result is either the given exception if it was an 33917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * {@link ExceptionWithContext}, or a newly-constructed exception if it 34917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * was not. 35917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 36917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param ex {@code non-null;} the exception to augment 37917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param str {@code non-null;} context to add 38917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @return {@code non-null;} an appropriate instance 39917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 40917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public static ExceptionWithContext withContext(Throwable ex, String str) { 41917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul ExceptionWithContext ewc; 42917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 43917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (ex instanceof ExceptionWithContext) { 44917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul ewc = (ExceptionWithContext) ex; 45917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } else { 46917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul ewc = new ExceptionWithContext(ex); 47917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 48917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 49917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul ewc.addContext(str); 50917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return ewc; 51917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 52917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 53917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 54917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Constructs an instance. 55917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 56917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param message human-oriented message 57917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 58917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public ExceptionWithContext(String message) { 59917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul this(message, null); 60917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 61917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 62917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 63917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Constructs an instance. 64917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 65917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param cause {@code null-ok;} exception that caused this one 66917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 67917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public ExceptionWithContext(Throwable cause) { 68917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul this(null, cause); 69917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 70917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 71917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 72917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Constructs an instance. 73917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 74917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param message human-oriented message 75917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param cause {@code null-ok;} exception that caused this one 76917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 77917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public ExceptionWithContext(String message, Throwable cause) { 78917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul super((message != null) ? message : 79917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul (cause != null) ? cause.getMessage() : null, 80917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul cause); 81917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 82917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (cause instanceof ExceptionWithContext) { 83917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul String ctx = ((ExceptionWithContext) cause).context.toString(); 84917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul context = new StringBuffer(ctx.length() + 200); 85917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul context.append(ctx); 86917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } else { 87917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul context = new StringBuffer(200); 88917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 89917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 90917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 91917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 92917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 93917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public void printStackTrace(PrintStream out) { 94917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul super.printStackTrace(out); 95917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.println(context); 96917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 97917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 98917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 99917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 100917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public void printStackTrace(PrintWriter out) { 101917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul super.printStackTrace(out); 102917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.println(context); 103917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 104917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 105917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 106917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Adds a line of context to this instance. 107917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 108917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param str {@code non-null;} new context 109917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 110917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public void addContext(String str) { 111917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (str == null) { 112917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul throw new NullPointerException("str == null"); 113917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 114917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 115917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul context.append(str); 116917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (!str.endsWith("\n")) { 117917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul context.append('\n'); 118917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 119917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 120917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 121917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 122917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Gets the context. 123917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 124917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @return {@code non-null;} the context 125917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 126917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public String getContext() { 127917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return context.toString(); 128917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 129917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 130917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 131917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Prints the message and context. 132917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 133917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param out {@code non-null;} where to print to 134917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 135917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public void printContext(PrintStream out) { 136917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.println(getMessage()); 137917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.print(context); 138917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 139917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 140917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 141917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Prints the message and context. 142917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 143917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param out {@code non-null;} where to print to 144917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 145917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public void printContext(PrintWriter out) { 146917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.println(getMessage()); 147917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.print(context); 148917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 149917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul} 150