comparison src/luan/LuanException.java @ 775:1a68fc55a80c

simplify dir structure
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 26 Aug 2016 14:36:40 -0600
parents core/src/luan/LuanException.java@bffbef96ca6d
children fbbdd369a13a
comparison
equal deleted inserted replaced
774:3e30cf310e56 775:1a68fc55a80c
1 package luan;
2
3 import java.io.StringWriter;
4 import java.io.PrintWriter;
5 import java.util.List;
6 import java.util.ArrayList;
7
8
9 public final class LuanException extends Exception implements DeepCloneable {
10 private LuanTable table;
11
12 public LuanException(String msg,Throwable cause) {
13 super(msg,cause);
14 initTable();
15 }
16
17 public LuanException(String msg) {
18 super(msg);
19 initTable();
20 }
21
22 public LuanException(Throwable cause) {
23 super(cause);
24 initTable();
25 }
26
27 private LuanException(String msg,Throwable cause,int nonsense) {
28 super(msg,cause);
29 }
30
31 @Override public LuanException shallowClone() {
32 return new LuanException(getMessage(),getCause(),99);
33 }
34
35 @Override public void deepenClone(DeepCloneable dc,DeepCloner cloner) {
36 LuanException clone = (LuanException)dc;
37 clone.table = (LuanTable)cloner.get(table);
38 }
39
40 public LuanTable table() {
41 return table;
42 }
43
44 private void initTable() {
45 table = new LuanTable();
46 table.rawPut( "java", this );
47 LuanTable metatable = new LuanTable();
48 table.setMetatable(metatable);
49 try {
50 table.rawPut( "get_message", new LuanJavaFunction(
51 LuanException.class.getMethod( "getMessage" ), this
52 ) );
53 table.rawPut( "throw", new LuanJavaFunction(
54 LuanException.class.getMethod( "throwThis" ), this
55 ) );
56 table.rawPut( "get_java_stack_trace_string", new LuanJavaFunction(
57 LuanException.class.getMethod( "getJavaStackTraceString" ), this
58 ) );
59 metatable.rawPut( "__to_string", new LuanJavaFunction(
60 LuanException.class.getMethod( "getFullMessage" ), this
61 ) );
62 } catch(NoSuchMethodException e) {
63 throw new RuntimeException(e);
64 }
65 }
66
67 public void throwThis() throws LuanException {
68 throw this;
69 }
70
71 public String getFullMessage() {
72 return getLuanStackTraceString();
73 // return getLuanStackTraceString()+"\n"+getJavaStackTraceString();
74 /*
75 StringBuilder buf = new StringBuilder();
76
77 Object msg = table.rawGet("message");
78 String msgStr = (String)table.rawGet("message_string");
79 buf.append( msgStr );
80
81 for( int i = table.rawLength(); i>=1; i-- ) {
82 LuanTable tbl = (LuanTable)table.rawGet(i);
83 buf.append( "\n\t" ).append( tbl.rawGet("source") ).append( " line " ).append( tbl.rawGet("line") );
84 Object callTo = tbl.rawGet("call_to");
85 if( callTo != null )
86 buf.append( " in call to '" ).append( callTo ).append( "'" );
87 }
88
89 if( msg instanceof Throwable ) {
90 buf.append( "\nCaused by: " );
91 Throwable cause = (Throwable)msg;
92 StringWriter sw = new StringWriter();
93 cause.printStackTrace(new PrintWriter(sw));
94 buf.append( sw );
95 }
96
97 return buf.toString();
98 */
99 }
100
101 public String getJavaStackTraceString() {
102 return getJavaStackTraceString(this);
103 }
104
105 private static String getJavaStackTraceString(Throwable th) {
106 StringWriter sw = new StringWriter();
107 th.printStackTrace(new PrintWriter(sw));
108 return sw.toString();
109 }
110
111 public static List<StackTraceElement> justLuan(StackTraceElement[] orig) {
112 List<StackTraceElement> list = new ArrayList<StackTraceElement>();
113 for( int i=0; i<orig.length; i++ ) {
114 StackTraceElement ste = orig[i];
115 if( !ste.getClassName().startsWith("luan.impl.EXP") )
116 continue;
117 list.add(ste);
118 if( !ste.getMethodName().equals("doCall") )
119 i++;
120 }
121 return list;
122 }
123
124 public static String toString(StackTraceElement ste) {
125 StringBuilder sb = new StringBuilder();
126 sb.append( ste.getFileName() ).append( " line " ).append( ste.getLineNumber() );
127 String method = ste.getMethodName();
128 if( !method.equals("doCall") )
129 sb.append( " in function '" ).append( method.substring(1) ).append( "'" );
130 return sb.toString();
131 }
132
133 public String getLuanStackTraceString() {
134 StringBuilder sb = new StringBuilder();
135 sb.append( getMessage() );
136 for( StackTraceElement ste : justLuan(getStackTrace()) ) {
137 sb.append( "\n\t" ).append( toString(ste) );
138 }
139 Throwable cause = getCause();
140 if( cause != null )
141 sb.append( "\nCaused by: " ).append( getJavaStackTraceString(cause) );
142 return sb.toString();
143 }
144
145 public static String currentSource() {
146 LuanException ex = new LuanException("currentSource");
147 List<StackTraceElement> st = ex.justLuan(ex.getStackTrace());
148 return st.isEmpty() ? null : st.get(0).getFileName();
149 }
150
151 }