Libraryless. Click here for Pure Java version (117L/2K/5K).
// See #593 for an example! import java.util.*; import java.io.*; import java.util.regex.*; import java.net.*; public class main { // My standard packages :) static String[] standardClasses = { "java.util.*", "java.util.regex.*", "javax.swing.*", "java.io.*", "java.net.*" }; public static void main(String[] args) throws IOException { String s = loadMainJava(); List<String> imports = findImports(s); for (String c : standardClasses) if (!(imports.contains(c))) s = "import " + c + ";\n" + s; saveMainJava(s); } static List<String> findImports(String src) { List<String> imports = new ArrayList<String>(); for (String line : toLines(src)) { Matcher matcher = Pattern.compile("^\\s*import\\s+(.*?)\\s*;").matcher(line); if (matcher.find()) imports.add(matcher.group(1)); } return imports; } public static List<String> toLines(String s) { List<String> lines = new ArrayList<String>(); int start = 0; while (true) { int i = toLines_nextLineBreak(s, start); if (i < 0) { if (s.length() > start) lines.add(s.substring(start)); break; } lines.add(s.substring(start, i)); if (s.charAt(i) == '\r' && i+1 < s.length() && s.charAt(i+1) == '\n') i += 2; else ++i; start = i; } return lines; } private static int toLines_nextLineBreak(String s, int start) { for (int i = start; i < s.length(); i++) { char c = s.charAt(i); if (c == '\r' || c == '\n') return i; } return -1; } static String loadMainJava() throws IOException { return loadTextFile("input/main.java", ""); } static void saveMainJava(String s) throws IOException { saveTextFile("output/main.java", s); } static String charsetForTextFiles = "UTF-8"; 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, charsetForTextFiles); 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); } /** 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, charsetForTextFiles); 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); } }
download show line numbers debug dex old transpilations
Travelled to 23 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gkwdfizivqfm, gwrvuhgaqvyk, hxnwyiuffukg, ishqpsrjomds, jtubtzbbkimh, kajysqoxcvfe, kmhbujppghqa, lpdgvwnxivlt, mqqgnosmbjvj, nrtiiiyxqhmw, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, qbtsjoyahagl, teubizvjbppd, tslmcundralx, tvejysmllsmz, ugbnzuvxksoj, vouqrxazstgt
ID | Author/Program | Comment | Date |
---|---|---|---|
750 | #1000610 (pitcher) | 2015-08-18 00:07:07 | |
749 | #1000604 (pitcher) | 2015-08-18 00:07:22 |
Snippet ID: | #592 |
Snippet name: | Stefan's Auto-Importer |
Eternal ID of this version: | #592/1 |
Text MD5: | d075b1c332cc0a39077e304abac30859 |
Transpilation MD5: | d075b1c332cc0a39077e304abac30859 |
Author: | stefan |
Category: | javax |
Type: | JavaX translator |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-06-20 21:24:58 |
Source code size: | 3695 bytes / 117 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 1268 / 6897 |
Referenced in: | [show references] |