| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | package com.sun.fortress.exceptions; |
|---|
| 19 | |
|---|
| 20 | import java.io.IOException; |
|---|
| 21 | import java.io.PrintStream; |
|---|
| 22 | import java.io.PrintWriter; |
|---|
| 23 | import java.util.ArrayList; |
|---|
| 24 | |
|---|
| 25 | import com.sun.fortress.interpreter.evaluator.Environment; |
|---|
| 26 | import com.sun.fortress.nodes_util.ErrorMsgMaker; |
|---|
| 27 | import com.sun.fortress.useful.HasAt; |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | public abstract class FortressException extends RuntimeException { |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | private static final long serialVersionUID = 6117319678737763137L; |
|---|
| 36 | private static final boolean dumpEnv = false; |
|---|
| 37 | |
|---|
| 38 | private ArrayList<HasAt> where = new ArrayList<HasAt>(); |
|---|
| 39 | private HasAt where2; |
|---|
| 40 | private Environment within; |
|---|
| 41 | private Iterable<? extends StaticError> staticErrors; |
|---|
| 42 | |
|---|
| 43 | public static String errorMsg(Object... messages) { |
|---|
| 44 | return ErrorMsgMaker.errorMsg(messages); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | public FortressException setWhere(HasAt where) { |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | if (where!=null) this.where.add(where); |
|---|
| 51 | return this; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | public FortressException setContext(HasAt where, Environment within) { |
|---|
| 55 | setWithin(within); |
|---|
| 56 | return setWhere(where); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | public FortressException setWithin(Environment within) { |
|---|
| 60 | if (this.within != null) this.within = within; |
|---|
| 61 | return this; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | public FortressException() { |
|---|
| 65 | super(); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | public FortressException(HasAt loc, Environment env, String arg0) { |
|---|
| 69 | super(arg0); |
|---|
| 70 | setWhere(loc); |
|---|
| 71 | within = env; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | public FortressException(HasAt loc, String arg0) { |
|---|
| 75 | super(arg0); |
|---|
| 76 | setWhere(loc); |
|---|
| 77 | |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | public FortressException(HasAt loc1, HasAt loc2, Environment env, String arg0) { |
|---|
| 81 | super(arg0); |
|---|
| 82 | setWhere(loc1); where2 = loc2; within = env; |
|---|
| 83 | |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | public FortressException(String arg0) { |
|---|
| 87 | super(arg0); |
|---|
| 88 | |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | public FortressException(String arg0, Throwable arg1) { |
|---|
| 92 | super(arg0, arg1); |
|---|
| 93 | |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | public FortressException(HasAt loc, Environment env, String arg0, Throwable arg1) { |
|---|
| 97 | super(arg0, arg1); |
|---|
| 98 | setWhere(loc); within = env; |
|---|
| 99 | |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | public FortressException(Throwable arg0) { |
|---|
| 103 | super(arg0); |
|---|
| 104 | |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | public FortressException(HasAt loc, String string, Throwable ex) { |
|---|
| 108 | this(loc, null, string, ex); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | public FortressException(Iterable<? extends StaticError> errors) { |
|---|
| 112 | this.staticErrors = errors; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | @Override |
|---|
| 119 | public String getMessage() { |
|---|
| 120 | String msg = super.getMessage(); |
|---|
| 121 | if (msg == null) |
|---|
| 122 | msg = ""; |
|---|
| 123 | if (staticErrors != null) { |
|---|
| 124 | for (StaticError se : staticErrors) { |
|---|
| 125 | if (msg.length() > 0) { |
|---|
| 126 | msg = msg + "\n"; |
|---|
| 127 | } |
|---|
| 128 | msg = msg + se.getMessage(); |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | if (where.size() > 0) { |
|---|
| 132 | StringBuffer res = new StringBuffer(); |
|---|
| 133 | res.append(where.get(0).at()); |
|---|
| 134 | if (where2 != null) { |
|---|
| 135 | res.append(": and\n"); |
|---|
| 136 | res.append(where2.at()); |
|---|
| 137 | } |
|---|
| 138 | res.append(":\n"); |
|---|
| 139 | res.append(msg); |
|---|
| 140 | if (where.size() > 1) { |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | res.append("\nContext:\n"); |
|---|
| 144 | for (HasAt loc : where) { |
|---|
| 145 | res.append(loc.at()); |
|---|
| 146 | res.append(":\n"); |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | return res.toString(); |
|---|
| 150 | } else { |
|---|
| 151 | return msg; |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | @Override |
|---|
| 159 | public void printStackTrace() { |
|---|
| 160 | |
|---|
| 161 | super.printStackTrace(); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | private void printInterpreterStackTrace(PrintWriter app) { |
|---|
| 168 | if (dumpEnv && within != null) { |
|---|
| 169 | try { |
|---|
| 170 | within.dump(app); |
|---|
| 171 | } catch (IOException ex) { |
|---|
| 172 | app.println("Error dumping interpreter environment"); |
|---|
| 173 | ex.printStackTrace(app); |
|---|
| 174 | } |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | public void printInterpreterStackTrace(PrintStream app) { |
|---|
| 182 | if (dumpEnv && within != null) { |
|---|
| 183 | try { |
|---|
| 184 | within.dump(app); |
|---|
| 185 | } catch (IOException ex) { |
|---|
| 186 | app.println("Error dumping interpreter environment"); |
|---|
| 187 | ex.printStackTrace(app); |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | @Override |
|---|
| 196 | public void printStackTrace(PrintStream arg0) { |
|---|
| 197 | |
|---|
| 198 | super.printStackTrace(arg0); |
|---|
| 199 | printInterpreterStackTrace(arg0); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | @Override |
|---|
| 206 | public void printStackTrace(PrintWriter arg0) { |
|---|
| 207 | |
|---|
| 208 | super.printStackTrace(arg0); |
|---|
| 209 | printInterpreterStackTrace(arg0); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | public Iterable<? extends StaticError> getStaticErrors() { |
|---|
| 213 | return staticErrors; |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | } |
|---|