6 import java
.nio
.charset
.*;
8 class ExceptionDefProcessor
10 private static Map
<Class
<?
>, String
> classes
;
12 static class UTFStream
14 FileOutputStream stream
;
16 UTFStream(String name
) throws IOException
18 stream
= new FileOutputStream(name
);
21 void println(String str
)
25 buf
= Charset
.forName("UTF-8").newEncoder().encode(CharBuffer
.wrap(str
));
26 byte[] buf2
= new byte[buf
.remaining() + 1];
27 buf
.get(buf2
, 0, buf
.remaining());
28 buf2
[buf2
.length
- 1] = 10;
30 } catch(Exception e
) {
39 } catch(Exception e
) {
45 private static char identity(char x
)
50 private static String
getRevision() throws IOException
52 String x
= "$Format:%h by %cn on %ci$";
53 if(identity(x
.charAt(0)) != '$') {
54 System
.err
.println("Detected revision: " + x
+ ".");
57 ProcessBuilder gitproc
= new ProcessBuilder();
58 gitproc
.command("git", "log", "--pretty=format:%h by %cn on %ci", "-1");
59 Process git
= gitproc
.start();
60 InputStream output
= git
.getInputStream();
63 if(git
.waitFor() != 0)
64 throw new IOException("Git subprocess failed");
66 } catch(InterruptedException e
) {
69 BufferedReader r
= new BufferedReader(new InputStreamReader(output
));
73 System
.err
.println("Detected revision: " + x
+ ".");
77 private static String
escapeString(String s
)
79 StringBuffer r
= new StringBuffer();;
80 for(int i
= 0; i
< s
.length(); i
++) {
92 private static void doClass(String line
)
94 int split
= line
.indexOf(32);
95 String clazz
= line
.substring(0, split
);
96 String desc
= line
.substring(split
+ 1);
100 classObject
= Class
.forName(clazz
);
101 } catch(Exception e
) {
102 System
.err
.println("Warning: Can't find class \"" + clazz
+ "\", dropping.");
105 classes
.put(classObject
, desc
);
108 public static void main(String
[] args
)
110 classes
= new HashMap
<Class
<?
>, String
>();
112 if(args
== null || args
.length
< 1) {
113 System
.err
.println("Syntax: java ExceptionDefProcessor <inputfile>");
117 String autoexec
= args
[0];
119 BufferedReader kbd2
= new BufferedReader(new InputStreamReader(
120 new FileInputStream(autoexec
), "UTF-8"));
122 String cmd
= kbd2
.readLine();
128 } catch (Exception e
) {
129 System
.err
.println("Failed to load exception defintions: " + e
.getMessage());
132 Class
<?
> failingClass
= null;
134 if(failingClass
!= null)
135 classes
.put(failingClass
, failingClass
.getName());
137 for(Map
.Entry
<Class
<?
>, String
> x
: classes
.entrySet()) {
138 Class
<?
> superclass
= x
.getKey().getSuperclass();
139 if(x
.getKey().getName().equals("java.lang.Error") ||
140 x
.getKey().getName().equals("java.lang.RuntimeException"))
142 if(!classes
.containsKey(superclass
)) {
143 System
.err
.println("Warning: Missing superclass \"" + superclass
.getName() + "\" for \"" +
144 x
.getKey().getName() + "\".");
145 failingClass
= superclass
;
149 } while(failingClass
!= null);
151 UTFStream stream
= null;
153 stream
= new UTFStream("org/jpc/Exceptions.java");
154 } catch(Exception e
) {
155 System
.err
.println("Can't open org/jpc/Exceptions.java: " + e
.getMessage());
159 stream
.println("package org.jpc;");
160 stream
.println("import java.util.*;");
161 stream
.println("class Exceptions {");
162 stream
.println("public static Map<String,String> classes;");
163 stream
.println("static {");
164 stream
.println("classes = new HashMap<String,String>();");
170 stream
.println("classes.put(\"" + out
.getName() + "\", \"" + desc
+ "\");");
173 for(Map
.Entry
<Class
<?
>, String
> x
: classes
.entrySet()) {
174 Class
<?
> superclass
= x
.getKey().getSuperclass();
175 if(!classes
.containsKey(superclass
)) {
181 } while(out
!= null);
182 stream
.println("}}");
186 stream
= new UTFStream("org/jpc/Revision.java");
187 } catch(Exception e
) {
188 System
.err
.println("Can't open org/jpc/Revision.java: " + e
.getMessage());
191 stream
.println("package org.jpc;");
192 stream
.println("public class Revision {");
193 stream
.println("public static String getRevision() {");
195 stream
.println("return \"" + escapeString(getRevision()) + "\";");
196 } catch(Exception e
) {
197 System
.err
.println("Can't get revision: " + e
.getMessage());
200 stream
.println("}}");