// Syntax: awt { doSomethingInAwtThread(); } !592 1000265 // auto-importer, standard function adder public class main { public static void main(String[] args) throws Exception { String s = loadMainJava(); Pattern regex = Pattern.compile("(awt)\\s*\\{"); for (int i = 0; i < 100; i++) { 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 code = "SwingUtilities.invokeLater(new Runnable() {\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});"; 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(); } }