source: branches/0.17.x/abcl/src/org/armedbear/lisp/Condition.java

Last change on this file was 12254, checked in by ehuelsmann, 16 years ago

Remove 'throws ConditionThrowable?' method annotations:

it's an unchecked exception now, so no need to declare it thrown.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.0 KB
Line 
1/*
2 * Condition.java
3 *
4 * Copyright (C) 2003-2007 Peter Graves
5 * $Id: Condition.java 12254 2009-11-06 20:07: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
36public class Condition extends StandardObject
37{
38  protected String message;
39
40  public Condition()
41  {
42    super(StandardClass.CONDITION);
43    Debug.assertTrue(slots.length == 2);
44    setFormatArguments(NIL);
45  }
46
47  protected Condition(LispClass cls)
48  {
49    super(cls);
50    Debug.assertTrue(slots.length >= 2);
51    setFormatArguments(NIL);
52  }
53
54  public Condition(LispClass cls, int length)
55  {
56    super(cls, length);
57  }
58
59  public Condition(LispObject initArgs)
60  {
61    super(StandardClass.CONDITION);
62    Debug.assertTrue(slots.length == 2);
63    initialize(initArgs);
64  }
65
66  protected void initialize(LispObject initArgs)
67  {
68    LispObject control = null;
69    LispObject arguments = null;
70    LispObject first, second;
71    while (initArgs instanceof Cons)
72      {
73        first = initArgs.car();
74        initArgs = initArgs.cdr();
75        second = initArgs.car();
76        initArgs = initArgs.cdr();
77        if (first == Keyword.FORMAT_CONTROL)
78          {
79            if (control == null)
80              control = second;
81          }
82        else if (first == Keyword.FORMAT_ARGUMENTS)
83          {
84            if (arguments == null)
85              arguments = second;
86          }
87      }
88    if (control != null)
89      setFormatControl(control);
90    if (arguments == null)
91      arguments = NIL;
92    setFormatArguments(arguments);
93  }
94
95  public Condition(String message)
96  {
97    super(StandardClass.CONDITION);
98    Debug.assertTrue(slots.length == 2);
99    try
100      {
101        setFormatControl(message);
102        setFormatArguments(NIL);
103      }
104    catch (Throwable t)
105      {
106        Debug.trace(t);
107      }
108  }
109
110  public final LispObject getFormatControl()
111  {
112    return getInstanceSlotValue(Symbol.FORMAT_CONTROL);
113  }
114
115  public final void setFormatControl(LispObject formatControl)
116
117  {
118    setInstanceSlotValue(Symbol.FORMAT_CONTROL, formatControl);
119  }
120
121  public final void setFormatControl(String s)
122  {
123    setFormatControl(new SimpleString(s));
124  }
125
126  public final LispObject getFormatArguments()
127  {
128    return getInstanceSlotValue(Symbol.FORMAT_ARGUMENTS);
129  }
130
131  public final void setFormatArguments(LispObject formatArguments)
132
133  {
134    setInstanceSlotValue(Symbol.FORMAT_ARGUMENTS, formatArguments);
135  }
136
137  public String getMessage()
138  {
139    return message;
140  }
141
142  @Override
143  public LispObject typeOf()
144  {
145    LispClass c = getLispClass();
146    if (c != null)
147      return c.getSymbol();
148    return Symbol.CONDITION;
149  }
150
151  @Override
152  public LispObject classOf()
153  {
154    LispClass c = getLispClass();
155    if (c != null)
156      return c;
157    return StandardClass.CONDITION;
158  }
159
160  @Override
161  public LispObject typep(LispObject type)
162  {
163    if (type == Symbol.CONDITION)
164      return T;
165    if (type == StandardClass.CONDITION)
166      return T;
167    return super.typep(type);
168  }
169
170  public String getConditionReport()
171  {
172    String s = getMessage();
173    if (s != null)
174      return s;
175    LispObject formatControl = getFormatControl();
176    if (formatControl != NIL)
177      {
178        try
179          {
180            return format(formatControl, getFormatArguments());
181          }
182        catch (Throwable t) {}
183      }
184    return unreadableString(typeOf().writeToString());
185  }
186
187  @Override
188  public String writeToString()
189  {
190    final LispThread thread = LispThread.currentThread();
191    if (Symbol.PRINT_ESCAPE.symbolValue(thread) == NIL)
192      {
193        String s = getMessage();
194        if (s != null)
195          return s;
196        LispObject formatControl = getFormatControl();
197        if (formatControl instanceof Function)
198          {
199            StringOutputStream stream = new StringOutputStream();
200            Symbol.APPLY.execute(formatControl, stream, getFormatArguments());
201            return stream.getString().getStringValue();
202          }
203        if (formatControl instanceof AbstractString)
204          {
205            LispObject f = Symbol.FORMAT.getSymbolFunction();
206            if (f == null || f instanceof Autoload)
207              return format(formatControl, getFormatArguments());
208            return Symbol.APPLY.execute(f, NIL, formatControl, getFormatArguments()).getStringValue();
209          }
210      }
211    final int maxLevel;
212    LispObject printLevel = Symbol.PRINT_LEVEL.symbolValue(thread);
213    if (printLevel instanceof Fixnum)
214      maxLevel = ((Fixnum)printLevel).value;
215    else
216      maxLevel = Integer.MAX_VALUE;
217    LispObject currentPrintLevel =
218      _CURRENT_PRINT_LEVEL_.symbolValue(thread);
219    int currentLevel = ((Fixnum)currentPrintLevel).value;
220    if (currentLevel >= maxLevel)
221      return "#";
222    return unreadableString(typeOf().writeToString());
223  }
224}
Note: See TracBrowser for help on using the repository browser.