4 import java
.nio
.charset
.*;
6 class ExceptionDefProcessor
8 private static Map
<Class
<?
>, String
> classes
;
10 static class UTFStream
12 FileOutputStream stream
;
14 UTFStream(String name
) throws IOException
16 stream
= new FileOutputStream(name
);
19 void println(String str
)
23 buf
= Charset
.forName("UTF-8").newEncoder().encode(CharBuffer
.wrap(str
));
24 byte[] buf2
= new byte[buf
.remaining() + 1];
25 buf
.get(buf2
, 0, buf
.remaining());
26 buf2
[buf2
.length
- 1] = 10;
28 } catch(Exception e
) {
37 } catch(Exception e
) {
43 private static char identity(char x
)
48 private static String
getRevision() throws IOException
50 String x
= "$Format:%h by %cn on %ci$";
51 if(identity(x
.charAt(0)) != '$') {
52 System
.err
.println("Detected revision: " + x
+ ".");
55 ProcessBuilder gitproc
= new ProcessBuilder();
56 gitproc
.command("git", "log", "--pretty=format:%h by %cn on %ci", "-1");
57 Process git
= gitproc
.start();
58 InputStream output
= git
.getInputStream();
61 if(git
.waitFor() != 0)
62 throw new IOException("Git subprocess failed");
64 } catch(InterruptedException e
) {
67 BufferedReader r
= new BufferedReader(new InputStreamReader(output
));
71 System
.err
.println("Detected revision: " + x
+ ".");
75 private static String
escapeString(String s
)
77 StringBuffer r
= new StringBuffer();;
78 for(int i
= 0; i
< s
.length(); i
++) {
90 private static void doClass(String line
)
92 int split
= line
.indexOf(32);
93 String clazz
= line
.substring(0, split
);
94 String desc
= line
.substring(split
+ 1);
98 classObject
= Class
.forName(clazz
);
99 } catch(Exception e
) {
100 System
.err
.println("Warning: Can't find class \"" + clazz
+ "\", dropping.");
103 classes
.put(classObject
, desc
);
106 public static void main(String
[] args
)
108 classes
= new HashMap
<Class
<?
>, String
>();
110 if(args
== null || args
.length
< 1) {
111 System
.err
.println("Syntax: java ExceptionDefProcessor <inputfile>");
115 String autoexec
= args
[0];
117 BufferedReader kbd2
= new BufferedReader(new InputStreamReader(
118 new FileInputStream(autoexec
), "UTF-8"));
120 String cmd
= kbd2
.readLine();
126 } catch (Exception e
) {
127 System
.err
.println("Failed to load exception defintions: " + e
.getMessage());
130 Class
<?
> failingClass
= null;
132 if(failingClass
!= null)
133 classes
.put(failingClass
, failingClass
.getName());
135 for(Map
.Entry
<Class
<?
>, String
> x
: classes
.entrySet()) {
136 Class
<?
> superclass
= x
.getKey().getSuperclass();
137 if(x
.getKey().getName().equals("java.lang.Error") ||
138 x
.getKey().getName().equals("java.lang.RuntimeException"))
140 if(!classes
.containsKey(superclass
)) {
141 System
.err
.println("Warning: Missing superclass \"" + superclass
.getName() + "\" for \"" +
142 x
.getKey().getName() + "\".");
143 failingClass
= superclass
;
147 } while(failingClass
!= null);
149 UTFStream stream
= null;
151 stream
= new UTFStream("org/jpc/Exceptions.java");
152 } catch(Exception e
) {
153 System
.err
.println("Can't open org/jpc/Exceptions.java: " + e
.getMessage());
157 stream
.println("package org.jpc;");
158 stream
.println("import java.util.*;");
159 stream
.println("class Exceptions {");
160 stream
.println("public static Map<String,String> classes;");
161 stream
.println("static {");
162 stream
.println("classes = new HashMap<String,String>();");
168 stream
.println("classes.put(\"" + out
.getName() + "\", \"" + desc
+ "\");");
171 for(Map
.Entry
<Class
<?
>, String
> x
: classes
.entrySet()) {
172 Class
<?
> superclass
= x
.getKey().getSuperclass();
173 if(!classes
.containsKey(superclass
)) {
179 } while(out
!= null);
180 stream
.println("}}");
184 stream
= new UTFStream("org/jpc/Revision.java");
185 } catch(Exception e
) {
186 System
.err
.println("Can't open org/jpc/Revision.java: " + e
.getMessage());
189 stream
.println("package org.jpc;");
190 stream
.println("public class Revision {");
191 stream
.println("public static String getRevision() {");
193 stream
.println("return \"" + escapeString(getRevision()) + "\";");
194 } catch(Exception e
) {
195 System
.err
.println("Can't get revision: " + e
.getMessage());
198 stream
.println("}}");