source: trunk/abcl/src/org/armedbear/lisp/Condition.java @ 12450

Last change on this file since 12450 was 12298, checked in by ehuelsmann, 15 years ago

Full source scan of "catch (Throwable";

remove lots of instances, or make the catch statement more
specific, e.g. replace Throwable by IOException.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.9 KB
Line 
1/*
2 * Condition.java
3 *
4 * Copyright (C) 2003-2007 Peter Graves
5 * $Id: Condition.java 12298 2009-12-18 21:50:54Z ehuelsmann $
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 * As a special exception, the copyright holders of this library give you
22 * permission to link this library with independent modules to produce an
23 * executable, regardless of the license terms of these independent
24 * modules, and to copy and distribute the resulting executable under
25 * terms of your choice, provided that you also meet, for each linked
26 * independent module, the terms and conditions of the license of that
27 * module.  An independent module is a module which is not derived from
28 * or based on this library.  If you modify this library, you may extend
29 * this exception to your version of the library, but you are not
30 * obligated to do so.  If you do not wish to do so, delete this
31 * exception statement from your version.
32 */
33
34package org.armedbear.lisp;
35
36import static org.armedbear.lisp.Lisp.*;
37
38public class Condition extends StandardObject
39{
40  protected String message;
41
42  public Condition()
43  {
44    super(StandardClass.CONDITION);
45    Debug.assertTrue(slots.length == 2);
46    setFormatArguments(NIL);
47  }
48
49  protected Condition(LispClass cls)
50  {
51    super(cls);
52    Debug.assertTrue(slots.length >= 2);
53    setFormatArguments(NIL);
54  }
55
56  public Condition(LispClass cls, int length)
57  {
58    super(cls, length);
59  }
60
61  public Condition(LispObject initArgs)
62  {
63    super(StandardClass.CONDITION);
64    Debug.assertTrue(slots.length == 2);
65    initialize(initArgs);
66  }
67
68  protected void initialize(LispObject initArgs)
69  {
70    LispObject control = null;
71    LispObject arguments = null;
72    LispObject first, second;
73    while (initArgs instanceof Cons)
74      {
75        first = initArgs.car();
76        initArgs = initArgs.cdr();
77        second = initArgs.car();
78        initArgs = initArgs.cdr();
79        if (first == Keyword.FORMAT_CONTROL)
80          {
81            if (control == null)
82              control = second;
83          }
84        else if (first == Keyword.FORMAT_ARGUMENTS)
85          {
86            if (arguments == null)
87              arguments = second;
88          }
89      }
90    if (control != null)
91      setFormatControl(control);
92    if (arguments == null)
93      arguments = NIL;
94    setFormatArguments(arguments);
95  }
96
97  public Condition(String message)
98  {
99    super(StandardClass.CONDITION);
100    Debug.assertTrue(slots.length == 2);
101    setFormatControl(message);
102    setFormatArguments(NIL);
103  }
104
105  public final LispObject getFormatControl()
106  {
107    return getInstanceSlotValue(Symbol.FORMAT_CONTROL);
108  }
109
110  public final void setFormatControl(LispObject formatControl)
111
112  {
113    setInstanceSlotValue(Symbol.FORMAT_CONTROL, formatControl);
114  }
115
116  public final void setFormatControl(String s)
117  {
118    setFormatControl(new SimpleString(s));
119  }
120
121  public final LispObject getFormatArguments()
122  {
123    return getInstanceSlotValue(Symbol.FORMAT_ARGUMENTS);
124  }
125
126  public final void setFormatArguments(LispObject formatArguments)
127
128  {
129    setInstanceSlotValue(Symbol.FORMAT_ARGUMENTS, formatArguments);
130  }
131
132  public String getMessage()
133  {
134    return message;
135  }
136
137  @Override
138  public LispObject typeOf()
139  {
140    LispClass c = getLispClass();
141    if (c != null)
142      return c.getSymbol();
143    return Symbol.CONDITION;
144  }
145
146  @Override
147  public LispObject classOf()
148  {
149    LispClass c = getLispClass();
150    if (c != null)
151      return c;
152    return StandardClass.CONDITION;
153  }
154
155  @Override
156  public LispObject typep(LispObject type)
157  {
158    if (type == Symbol.CONDITION)
159      return T;
160    if (type == StandardClass.CONDITION)
161      return T;
162    return super.typep(type);
163  }
164
165  public String getConditionReport()
166  {
167    String s = getMessage();
168    if (s != null)
169      return s;
170    LispObject formatControl = getFormatControl();
171    if (formatControl != NIL)
172      {
173        return format(formatControl, getFormatArguments());
174      }
175    return unreadableString(typeOf().writeToString());
176  }
177
178  @Override
179  public String writeToString()
180  {
181    final LispThread thread = LispThread.currentThread();
182    if (Symbol.PRINT_ESCAPE.symbolValue(thread) == NIL)
183      {
184        String s = getMessage();
185        if (s != null)
186          return s;
187        LispObject formatControl = getFormatControl();
188        if (formatControl instanceof Function)
189          {
190            StringOutputStream stream = new StringOutputStream();
191            Symbol.APPLY.execute(formatControl, stream, getFormatArguments());
192            return stream.getString().getStringValue();
193          }
194        if (formatControl instanceof AbstractString)
195          {
196            LispObject f = Symbol.FORMAT.getSymbolFunction();
197            if (f == null || f instanceof Autoload)
198              return format(formatControl, getFormatArguments());
199            return Symbol.APPLY.execute(f, NIL, formatControl, getFormatArguments()).getStringValue();
200          }
201      }
202    final int maxLevel;
203    LispObject printLevel = Symbol.PRINT_LEVEL.symbolValue(thread);
204    if (printLevel instanceof Fixnum)
205      maxLevel = ((Fixnum)printLevel).value;
206    else
207      maxLevel = Integer.MAX_VALUE;
208    LispObject currentPrintLevel =
209      _CURRENT_PRINT_LEVEL_.symbolValue(thread);
210    int currentLevel = ((Fixnum)currentPrintLevel).value;
211    if (currentLevel >= maxLevel)
212      return "#";
213    return unreadableString(typeOf().writeToString());
214  }
215}
Note: See TracBrowser for help on using the repository browser.