root/trunk/ProjectFortress/src/com/sun/fortress/exceptions/FortressException.java @ 3586

Revision 3586, 5.9 KB (checked in by sukyoungryu, 8 months ago)

[preparser] Reorganized the parser family.

Line 
1/*******************************************************************************
2    Copyright 2009 Sun Microsystems, Inc.,
3    4150 Network Circle, Santa Clara, California 95054, U.S.A.
4    All rights reserved.
5
6    U.S. Government Rights - Commercial software.
7    Government users are subject to the Sun Microsystems, Inc. standard
8    license agreement and applicable provisions of the FAR and its supplements.
9
10    Use is subject to license terms.
11
12    This distribution may include materials developed by third parties.
13
14    Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
15    trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
16 ******************************************************************************/
17
18package com.sun.fortress.exceptions;
19
20import java.io.IOException;
21import java.io.PrintStream;
22import java.io.PrintWriter;
23import java.util.ArrayList;
24
25import com.sun.fortress.interpreter.evaluator.Environment;
26import com.sun.fortress.nodes_util.ErrorMsgMaker;
27import com.sun.fortress.useful.HasAt;
28
29
30public abstract class FortressException extends RuntimeException {
31
32    /**
33     * Make Eclipse happy
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        /* The null test here is because the unit tests sometimes
49           fling around null location information. */
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    /* (non-Javadoc)
116     * @see java.lang.Throwable#getMessage()
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                /* If additional location information was provided while
142                   unwinding from an error, print it. */
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    /* (non-Javadoc)
156     * @see java.lang.Throwable#printStackTrace()
157     */
158    @Override
159    public void printStackTrace() {
160        // TODO Auto-generated method stub
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    /* (non-Javadoc)
193     * @see java.lang.Throwable#printStackTrace(java.io.PrintStream)
194     */
195    @Override
196    public void printStackTrace(PrintStream arg0) {
197        // TODO Auto-generated method stub
198        super.printStackTrace(arg0);
199        printInterpreterStackTrace(arg0);
200    }
201
202    /* (non-Javadoc)
203     * @see java.lang.Throwable#printStackTrace(java.io.PrintWriter)
204     */
205    @Override
206    public void printStackTrace(PrintWriter arg0) {
207        // TODO Auto-generated method stub
208        super.printStackTrace(arg0);
209        printInterpreterStackTrace(arg0);
210    }
211
212    public Iterable<? extends StaticError> getStaticErrors() {
213        return staticErrors;
214    }
215
216}
Note: See TracBrowser for help on using the browser.