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

Revision 3835, 6.2 KB (checked in by sukyoungryu, 6 months ago)

[type checker] Fixed small things in the Scala type checker. Matched some error messages from the Java type checker and the Scala type checker. Do not type check programs with syntax errors. Moved two tests from not_working_static_tests to compiler_tests.

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 edu.rice.cs.plt.tuple.Option;
26import com.sun.fortress.interpreter.evaluator.Environment;
27import com.sun.fortress.nodes_util.ErrorMsgMaker;
28import com.sun.fortress.useful.HasAt;
29
30
31public abstract class FortressException extends RuntimeException {
32
33    /**
34     * Make Eclipse happy
35     */
36    private static final long serialVersionUID = 6117319678737763137L;
37    private static final boolean dumpEnv = false;
38
39    private ArrayList<HasAt> where = new ArrayList<HasAt>();
40    private HasAt where2;
41    private Environment within;
42    private Iterable<? extends StaticError> staticErrors;
43
44    public static String errorMsg(Object... messages) {
45        return ErrorMsgMaker.errorMsg(messages);
46    }
47
48    public FortressException setWhere(HasAt where) {
49        /* The null test here is because the unit tests sometimes
50           fling around null location information. */
51        if (where!=null) this.where.add(where);
52        return this;
53    }
54
55    public FortressException setContext(HasAt where, Environment within) {
56        setWithin(within);
57        return setWhere(where);
58    }
59
60    public FortressException setWithin(Environment within) {
61        if (this.within != null) this.within = within;
62        return this;
63    }
64
65    public FortressException() {
66        super();
67    }
68
69    public FortressException(HasAt loc, Environment env, String arg0) {
70        super(arg0);
71        setWhere(loc);
72        within = env;
73    }
74
75    public FortressException(HasAt loc, String arg0) {
76        super(arg0);
77        setWhere(loc);
78
79    }
80
81    public FortressException(HasAt loc1, HasAt loc2, Environment env, String arg0) {
82        super(arg0);
83        setWhere(loc1); where2 = loc2; within = env;
84
85    }
86
87    public FortressException(String arg0) {
88        super(arg0);
89
90    }
91
92    public FortressException(String arg0, Throwable arg1) {
93        super(arg0, arg1);
94
95    }
96
97    public FortressException(HasAt loc, Environment env, String arg0, Throwable arg1) {
98        super(arg0, arg1);
99        setWhere(loc); within = env;
100
101    }
102
103    public FortressException(Throwable arg0) {
104        super(arg0);
105
106    }
107
108    public FortressException(HasAt loc, String string, Throwable ex) {
109        this(loc, null, string, ex);
110    }
111
112    public FortressException(Iterable<? extends StaticError> errors) {
113        this.staticErrors = errors;
114    }
115
116    public Option<HasAt> getLoc() {
117        if ( this.where.isEmpty() )
118            return Option.none();
119        else return Option.wrap(this.where.get(0));
120    }
121
122    public String getOriginalMessage() {
123        return super.getMessage();
124    }
125
126    /* (non-Javadoc)
127     * @see java.lang.Throwable#getMessage()
128     */
129    @Override
130    public String getMessage() {
131        String msg = super.getMessage();
132        if (msg == null)
133            msg = "";
134        if (staticErrors != null) {
135            for (StaticError se : staticErrors) {
136                if (msg.length() > 0) {
137                    msg = msg + "\n";
138                }
139                msg = msg + se.getMessage();
140            }
141        }
142        if (where.size() > 0) {
143            StringBuffer res = new StringBuffer();
144            res.append(where.get(0).at());
145            if (where2 != null) {
146                res.append(": and\n");
147                res.append(where2.at());
148            }
149            res.append(":\n");
150            res.append(msg);
151            if (where.size() > 1) {
152                /* If additional location information was provided while
153                   unwinding from an error, print it. */
154                res.append("\nContext:\n");
155                for (HasAt loc : where) {
156                    res.append(loc.at());
157                    res.append(":\n");
158                }
159            }
160            return res.toString();
161        } else {
162            return msg;
163        }
164    }
165
166    /* (non-Javadoc)
167     * @see java.lang.Throwable#printStackTrace()
168     */
169    @Override
170    public void printStackTrace() {
171        // TODO Auto-generated method stub
172        super.printStackTrace();
173    }
174
175    /**
176     *
177     */
178    private void printInterpreterStackTrace(PrintWriter app) {
179        if (dumpEnv && within != null) {
180            try {
181            within.dump(app);
182            } catch (IOException ex) {
183                app.println("Error dumping interpreter environment");
184                ex.printStackTrace(app);
185            }
186        }
187    }
188
189    /**
190     *
191     */
192    public void printInterpreterStackTrace(PrintStream app) {
193        if (dumpEnv && within != null) {
194            try {
195            within.dump(app);
196            } catch (IOException ex) {
197                app.println("Error dumping interpreter environment");
198                ex.printStackTrace(app);
199            }
200        }
201    }
202
203    /* (non-Javadoc)
204     * @see java.lang.Throwable#printStackTrace(java.io.PrintStream)
205     */
206    @Override
207    public void printStackTrace(PrintStream arg0) {
208        // TODO Auto-generated method stub
209        super.printStackTrace(arg0);
210        printInterpreterStackTrace(arg0);
211    }
212
213    /* (non-Javadoc)
214     * @see java.lang.Throwable#printStackTrace(java.io.PrintWriter)
215     */
216    @Override
217    public void printStackTrace(PrintWriter arg0) {
218        // TODO Auto-generated method stub
219        super.printStackTrace(arg0);
220        printInterpreterStackTrace(arg0);
221    }
222
223    public Iterable<? extends StaticError> getStaticErrors() {
224        return staticErrors;
225    }
226
227}
Note: See TracBrowser for help on using the browser.