import java.net.*; import java.io.*; import javax.swing.*; import java.util.regex.*; import java.util.*; // Syntax: thread { doSomething(); } // Short for: new Thread() { public void run() { doSomething(); }}.start(); // similar for daemon {} public class main { public static void main(String[] args) throws Exception { String s = loadMainJava(); Pattern regex = Pattern.compile("(thread|daemon)\\s*\\{"); for (int i = 0; i < 100; i++) { if (i == 99) System.out.println("Warning: 100 iterations!"); Matcher matcher = regex.matcher(s); if (!matcher.find()) break; int start = matcher.start(), end = matcher.end(); int endOfBlock = findEndOfBlock(s, end); boolean daemon = matcher.group().startsWith("d"); String var = "_t_" + i; String code = "Thread " + var + " = new Thread() {\n" + "public void run() {\n" + "try {\n" + s.substring(end, endOfBlock) + "} catch (Exception _e) {\n" + " throw _e instanceof RuntimeException ? (RuntimeException) _e : new RuntimeException(_e); } }\n};\n" + (daemon ? var + ".setDaemon(true);\n" : "") + var + ".start();"; s = s.substring(0, start) + code + s.substring(endOfBlock+1); } saveMainJava(s); } // start is the index AFTER the opening bracket // returns index OF closing bracket static int findEndOfBlock(String s, int start) { int level = 1; for (int i = start; i < s.length(); i++) { if (s.charAt(i) == '{') ++level; else if (s.charAt(i) == '}') --level; if (level == 0) return i; } return s.length(); } static String mainJava; static String loadMainJava() throws IOException { if (mainJava != null) return mainJava; return loadTextFile("input/main.java", ""); } static void saveMainJava(String s) throws IOException { if (mainJava != null) mainJava = s; else saveTextFile("output/main.java", s); } static void saveMainJava(List tok) throws IOException { saveMainJava(join(tok)); } public static String join(String glue, Iterable strings) { StringBuilder buf = new StringBuilder(); Iterator i = strings.iterator(); if (i.hasNext()) { buf.append(i.next()); while (i.hasNext()) buf.append(glue).append(i.next()); } return buf.toString(); } public static String join(String glue, String[] strings) { return join(glue, Arrays.asList(strings)); } public static String join(Iterable strings) { return join("", strings); } public static String join(String[] strings) { return join("", strings); } /** writes safely (to temp file, then rename) */ public static void saveTextFile(String fileName, String contents) throws IOException { File file = new File(fileName); File parentFile = file.getParentFile(); if (parentFile != null) parentFile.mkdirs(); String tempFileName = fileName + "_temp"; FileOutputStream fileOutputStream = new FileOutputStream(tempFileName); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8"); PrintWriter printWriter = new PrintWriter(outputStreamWriter); printWriter.print(contents); printWriter.close(); if (file.exists() && !file.delete()) throw new IOException("Can't delete " + fileName); if (!new File(tempFileName).renameTo(file)) throw new IOException("Can't rename " + tempFileName + " to " + fileName); } public static String loadTextFile(String fileName, String defaultContents) throws IOException { if (!new File(fileName).exists()) return defaultContents; FileInputStream fileInputStream = new FileInputStream(fileName); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); return loadTextFile(inputStreamReader); } public static String loadTextFile(Reader reader) throws IOException { StringBuilder builder = new StringBuilder(); try { BufferedReader bufferedReader = new BufferedReader(reader); String line; while ((line = bufferedReader.readLine()) != null) builder.append(line).append('\n'); } finally { reader.close(); } return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1); } }