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
deriveLogFormat(String x
)
52 //This shouldn't be interpretted as keyword!
54 return x
.replaceFirst("\\" + dollar
+ "Format:([^" + dollar
+ "]*)\\" + dollar
+ ".*", "--pretty=format:$1");
57 private static String
getRevision() throws IOException
59 String x
= "$Format:%h by %cn on %ci$";
60 if(identity(x
.charAt(0)) != '$') {
61 System
.err
.println("Detected revision: " + x
+ ".");
64 ProcessBuilder gitproc
= new ProcessBuilder();
65 gitproc
.command("git", "log", deriveLogFormat(x
), "-1");
66 Process git
= gitproc
.start();
67 InputStream output
= git
.getInputStream();
70 if(git
.waitFor() != 0)
71 throw new IOException("Git subprocess failed");
73 } catch(InterruptedException e
) {
76 BufferedReader r
= new BufferedReader(new InputStreamReader(output
));
80 System
.err
.println("Detected revision: " + x
+ ".");
84 private static String
getRelease() throws IOException
86 BufferedReader kbd2
= new BufferedReader(new InputStreamReader(
87 new FileInputStream("VERSIONINFO"), "UTF-8"));
88 String cmd
= kbd2
.readLine();
90 System
.err
.println("Detected release: " + cmd
+ ".");
94 private static String
escapeString(String s
)
96 StringBuffer r
= new StringBuffer();;
97 for(int i
= 0; i
< s
.length(); i
++) {
109 private static void doClass(String line
)
111 int split
= line
.indexOf(32);
112 String clazz
= line
.substring(0, split
);
113 String desc
= line
.substring(split
+ 1);
114 Class
<?
> classObject
;
117 classObject
= Class
.forName(clazz
);
118 } catch(Exception e
) {
119 System
.err
.println("Warning: Can't find class \"" + clazz
+ "\", dropping.");
122 classes
.put(classObject
, desc
);
125 public static void main(String
[] args
)
127 classes
= new HashMap
<Class
<?
>, String
>();
129 if(args
== null || args
.length
< 1) {
130 System
.err
.println("Syntax: java ExceptionDefProcessor <inputfile>");
134 String autoexec
= args
[0];
136 BufferedReader kbd2
= new BufferedReader(new InputStreamReader(
137 new FileInputStream(autoexec
), "UTF-8"));
139 String cmd
= kbd2
.readLine();
145 } catch (Exception e
) {
146 System
.err
.println("Failed to load exception defintions: " + e
.getMessage());
149 Class
<?
> failingClass
= null;
151 if(failingClass
!= null)
152 classes
.put(failingClass
, failingClass
.getName());
154 for(Map
.Entry
<Class
<?
>, String
> x
: classes
.entrySet()) {
155 Class
<?
> superclass
= x
.getKey().getSuperclass();
156 if(x
.getKey().getName().equals("java.lang.Error") ||
157 x
.getKey().getName().equals("java.lang.RuntimeException"))
159 if(!classes
.containsKey(superclass
)) {
160 System
.err
.println("Warning: Missing superclass \"" + superclass
.getName() + "\" for \"" +
161 x
.getKey().getName() + "\".");
162 failingClass
= superclass
;
166 } while(failingClass
!= null);
168 UTFStream stream
= null;
170 stream
= new UTFStream("org/jpc/Exceptions.java");
171 } catch(Exception e
) {
172 System
.err
.println("Can't open org/jpc/Exceptions.java: " + e
.getMessage());
176 stream
.println("package org.jpc;");
177 stream
.println("import java.util.*;");
178 stream
.println("class Exceptions {");
179 stream
.println("public static Map<String,String> classes;");
180 stream
.println("static {");
181 stream
.println("classes = new HashMap<String,String>();");
187 stream
.println("classes.put(\"" + out
.getName() + "\", \"" + desc
+ "\");");
190 for(Map
.Entry
<Class
<?
>, String
> x
: classes
.entrySet()) {
191 Class
<?
> superclass
= x
.getKey().getSuperclass();
192 if(!classes
.containsKey(superclass
)) {
198 } while(out
!= null);
199 stream
.println("}}");
203 stream
= new UTFStream("org/jpc/Revision.java");
204 } catch(Exception e
) {
205 System
.err
.println("Can't open org/jpc/Revision.java: " + e
.getMessage());
208 stream
.println("package org.jpc;");
209 stream
.println("public class Revision {");
210 stream
.println("public static String getRevision() {");
212 stream
.println("return \"" + escapeString(getRevision()) + "\";");
213 stream
.println("}\npublic static String getRelease() {");
214 stream
.println("return \"" + escapeString(getRelease()) + "\";");
215 } catch(Exception e
) {
216 System
.err
.println("Can't get revision: " + e
.getMessage());
219 stream
.println("}}");