Bump version number for next cycle
[jpcrr.git] / exceptiondefs / ExceptionDefProcessor.java
blobbaf512d6d607d538020ea711ead4b05b9d747eb4
1 package exceptiondefs;
3 import java.util.*;
4 import java.io.*;
5 import java.nio.*;
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)
23 try {
24 ByteBuffer buf;
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;
29 stream.write(buf2);
30 } catch(Exception e) {
31 e.printStackTrace();
35 void close()
37 try {
38 stream.close();
39 } catch(Exception e) {
40 e.printStackTrace();
45 private static char identity(char x)
47 return 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 + ".");
55 return 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();
61 while(true) {
62 try {
63 if(git.waitFor() != 0)
64 throw new IOException("Git subprocess failed");
65 break;
66 } catch(InterruptedException e) {
69 BufferedReader r = new BufferedReader(new InputStreamReader(output));
70 x = r.readLine();
71 r.close();
73 System.err.println("Detected revision: " + x + ".");
74 return x;
77 private static String escapeString(String s)
79 StringBuffer r = new StringBuffer();;
80 for(int i = 0; i < s.length(); i++) {
81 char x = s.charAt(i);
82 if(x == '\"')
83 r.append("\"");
84 else if(x == '\\')
85 r.append("\\");
86 else
87 r.append(x);
89 return r.toString();
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);
97 Class<?> classObject;
99 try {
100 classObject = Class.forName(clazz);
101 } catch(Exception e) {
102 System.err.println("Warning: Can't find class \"" + clazz + "\", dropping.");
103 return;
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>");
114 return;
117 String autoexec = args[0];
118 try {
119 BufferedReader kbd2 = new BufferedReader(new InputStreamReader(
120 new FileInputStream(autoexec), "UTF-8"));
121 while(true) {
122 String cmd = kbd2.readLine();
123 if(cmd == null)
124 break;
125 if(!cmd.equals(""))
126 doClass(cmd);
128 } catch (Exception e) {
129 System.err.println("Failed to load exception defintions: " + e.getMessage());
132 Class<?> failingClass = null;
133 do {
134 if(failingClass != null)
135 classes.put(failingClass, failingClass.getName());
136 failingClass = null;
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"))
141 continue;
142 if(!classes.containsKey(superclass)) {
143 System.err.println("Warning: Missing superclass \"" + superclass.getName() + "\" for \"" +
144 x.getKey().getName() + "\".");
145 failingClass = superclass;
146 break;
149 } while(failingClass != null);
151 UTFStream stream = null;
152 try {
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());
156 return;
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>();");
165 Class<?> out = null;
166 String desc = null;
167 do {
168 if(out != null) {
169 classes.remove(out);
170 stream.println("classes.put(\"" + out.getName() + "\", \"" + desc + "\");");
172 out = null;
173 for(Map.Entry<Class<?>, String> x : classes.entrySet()) {
174 Class<?> superclass = x.getKey().getSuperclass();
175 if(!classes.containsKey(superclass)) {
176 out = x.getKey();
177 desc = x.getValue();
178 break;
181 } while(out != null);
182 stream.println("}}");
183 stream.close();
185 try {
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());
189 return;
191 stream.println("package org.jpc;");
192 stream.println("public class Revision {");
193 stream.println("public static String getRevision() {");
194 try {
195 stream.println("return \"" + escapeString(getRevision()) + "\";");
196 } catch(Exception e) {
197 System.err.println("Can't get revision: " + e.getMessage());
198 return;
200 stream.println("}}");
201 stream.close();