source: trunk/j/src/org/armedbear/lisp/print-object.lisp @ 11390

Last change on this file since 11390 was 11390, checked in by ehuelsmann, 13 years ago

Make JAVA-EXCEPTION objects print more human readable.

Inspired by: Mark Evenson

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.2 KB
Line 
1;;; print-object.lisp
2;;;
3;;; Copyright (C) 2003-2006 Peter Graves
4;;; $Id: print-object.lisp 11390 2008-11-15 22:12:31Z ehuelsmann $
5;;;
6;;; This program is free software; you can redistribute it and/or
7;;; modify it under the terms of the GNU General Public License
8;;; as published by the Free Software Foundation; either version 2
9;;; of the License, or (at your option) any later version.
10;;;
11;;; This program is distributed in the hope that it will be useful,
12;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with this program; if not, write to the Free Software
18;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20(in-package #:system)
21
22(require 'clos)
23(require 'java)
24
25(when (autoloadp 'print-object)
26  (fmakunbound 'print-object))
27
28(defgeneric print-object (object stream))
29
30(defmethod print-object ((object t) stream)
31  (print-unreadable-object (object stream :type t :identity t)))
32
33(defmethod print-object ((object structure-object) stream)
34  (write-string (%write-to-string object) stream))
35
36(defmethod print-object ((object standard-object) stream)
37  (print-unreadable-object (object stream :identity t)
38    (format stream "~S" (class-name (class-of object))))
39  object)
40
41(defmethod print-object ((class class) stream)
42  (print-unreadable-object (class stream :identity t)
43    (format stream "~S ~S"
44            (class-name (class-of class))
45            (class-name class)))
46  class)
47
48(defmethod print-object ((gf standard-generic-function) stream)
49  (print-unreadable-object (gf stream :identity t)
50    (format stream "~S ~S"
51            (class-name (class-of gf))
52            (%generic-function-name gf)))
53  gf)
54
55(defmethod print-object ((method standard-method) stream)
56  (print-unreadable-object (method stream :identity t)
57    (format stream "~S ~S~{ ~S~} ~S"
58            (class-name (class-of method))
59            (%generic-function-name
60             (%method-generic-function method))
61            (method-qualifiers method)
62            (mapcar #'(lambda (c)
63                        (if (typep c 'mop::eql-specializer)
64                            `(eql ,(mop::eql-specializer-object c))
65                          (class-name c)))
66                    (%method-specializers method))))
67  method)
68
69(defmethod print-object ((restart restart) stream)
70  (if *print-escape*
71      (print-unreadable-object (restart stream :type t :identity t)
72        (prin1 (restart-name restart) stream))
73      (restart-report restart stream)))
74
75(defmethod print-object ((c condition) stream)
76  (if *print-escape*
77      (call-next-method)
78      (if (slot-boundp c 'format-control)
79          (apply #'format stream
80                 (simple-condition-format-control c)
81                 (simple-condition-format-arguments c))
82          (call-next-method))))
83
84(defmethod print-object ((c type-error) stream)
85  (if *print-escape*
86      (call-next-method)
87      (if (slot-boundp c 'format-control)
88          (apply 'format stream
89                 (simple-condition-format-control c)
90                 (simple-condition-format-arguments c))
91          (format stream "The value ~S is not of type ~S."
92                  (type-error-datum c)
93                  (type-error-expected-type c)))))
94
95(defmethod print-object ((x undefined-function) stream)
96  (if *print-escape*
97      (call-next-method)
98      (format stream "The function ~S is undefined." (cell-error-name x))))
99
100(defmethod print-object ((x unbound-variable) stream)
101  (if *print-escape*
102      (print-unreadable-object (x stream :identity t)
103        (format stream "~S ~S"
104                (type-of x)
105                (cell-error-name x)))
106      (format stream "The variable ~S is unbound." (cell-error-name x))))
107
108(defmethod print-object ((e java:java-exception) stream)
109  (if *print-escape*
110      (print-unreadable-object (e stream :type t :identity t)
111        (format stream "~A"
112                (java:jcall (java:jmethod "java.lang.Object" "toString")
113                            (java:java-exception-cause e))))
114      (format stream "Java exception '~A'."
115              (java:jcall (java:jmethod "java.lang.Object" "toString")
116                          (java:java-exception-cause e)))))
117
118(provide 'print-object)
Note: See TracBrowser for help on using the repository browser.