Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

127
LINES

< > BotCompany Repo | #564 // isCompilable (JavaX)

JavaX source code - run with: x30.jar

1  
import java.io.*;
2  
import java.lang.reflect.InvocationTargetException;
3  
import java.lang.reflect.Method;
4  
import java.net.MalformedURLException;
5  
import java.net.URL;
6  
import java.net.URLClassLoader;
7  
import java.util.ArrayList;
8  
import java.util.List;
9  
10  
/** isCompilable */
11  
public class main {
12  
  public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
13  
    File srcDir = new File("input");
14  
    List<File> sources = new ArrayList<File>();
15  
    System.out.println("Scanning for sources in " + srcDir.getPath());
16  
    scanForSources(srcDir, sources);
17  
    File optionsFile = File.createTempFile("javax", "");
18  
    File classesDir = new File(System.getProperty("user.home"), ".javax/" + System.currentTimeMillis());
19  
    System.out.println("Compiling " + sources.size() + " source(s) to " + classesDir.getPath());
20  
    String options = "-d " + bashQuote(classesDir.getPath());
21  
    writeOptions(sources, optionsFile, options);
22  
    classesDir.mkdirs();
23  
    invokeJavac(optionsFile);
24  
    boolean isCompilable = new File(classesDir, "main.class").exists();
25  
    System.out.println("Compilable: " + isCompilable);
26  
    saveTextFile(isCompilable ? "output/is-compilable" : "output/is-not-compilable", "x");
27  
  }
28  
29  
  private static void invokeJavac(File optionsFile) throws IOException {
30  
    String javacOutput = backtick("javac " + bashQuote("@" + optionsFile.getPath()));
31  
    System.out.println(javacOutput);
32  
  }
33  
34  
  private static void writeOptions(List<File> sources, File sourcesFile, String moreOptions) throws IOException {
35  
    FileWriter writer = new FileWriter(sourcesFile);
36  
    for (File source : sources)
37  
      writer.write(bashQuote(source.getPath()) + " ");
38  
    writer.write(moreOptions);
39  
    writer.close();
40  
  }
41  
42  
  private static void scanForSources(File source, List<File> sources) {
43  
    if (source.isFile() && source.getName().endsWith(".java"))
44  
      sources.add(source);
45  
    else if (source.isDirectory()) {
46  
      File[] files = source.listFiles();
47  
      for (File file : files)
48  
        scanForSources(file, sources);
49  
    }
50  
  }
51  
52  
  public static String backtick(String cmd) throws IOException {
53  
    File outFile = File.createTempFile("_backtick", "");
54  
    File scriptFile = File.createTempFile("_backtick", "");
55  
56  
    String command = cmd + ">" + bashQuote(outFile.getPath()) + " 2>&1";
57  
    //Log.info("[Backtick] " + command);
58  
    try {
59  
      saveTextFile(scriptFile.getPath(), command);
60  
      String[] command2 = {"/bin/bash", scriptFile.getPath() };
61  
      Process process = Runtime.getRuntime().exec(command2);
62  
      try {
63  
        process.waitFor();
64  
      } catch (InterruptedException e) {
65  
        throw new RuntimeException(e);
66  
      }
67  
      int value = process.exitValue();
68  
      //Log.info("exit value: " + value);
69  
      return loadTextFile(outFile.getPath(), "");
70  
    } finally {
71  
      scriptFile.delete();
72  
    }
73  
  }
74  
75  
  /** possoibly improvable */
76  
  public static String bashQuote(String text) {
77  
    if (text == null) return null;
78  
    return "\"" + text
79  
      .replace("\\", "\\\\")
80  
      .replace("\"", "\\\"")
81  
      .replace("\n", "\\n")
82  
      .replace("\r", "\\r") + "\"";
83  
  }
84  
85  
  public final static String charsetForTextFiles = "UTF8";
86  
87  
  /** writes safely (to temp file, then rename) */
88  
  public static void saveTextFile(String fileName, String contents) throws IOException {
89  
    File file = new File(fileName);
90  
    File parentFile = file.getParentFile();
91  
    if (parentFile != null)
92  
      parentFile.mkdirs();
93  
    String tempFileName = fileName + "_temp";
94  
    FileOutputStream fileOutputStream = new FileOutputStream(tempFileName);
95  
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, charsetForTextFiles);
96  
    PrintWriter printWriter = new PrintWriter(outputStreamWriter);
97  
    printWriter.print(contents);
98  
    printWriter.close();
99  
    if (file.exists() && !file.delete())
100  
      throw new IOException("Can't delete " + fileName);
101  
102  
    if (!new File(tempFileName).renameTo(file))
103  
      throw new IOException("Can't rename " + tempFileName + " to " + fileName);
104  
  }
105  
106  
  public static String loadTextFile(String fileName, String defaultContents) throws IOException {
107  
    if (!new File(fileName).exists())
108  
      return defaultContents;
109  
110  
    FileInputStream fileInputStream = new FileInputStream(fileName);
111  
    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, charsetForTextFiles);
112  
    return loadTextFile(inputStreamReader);
113  
  }
114  
115  
  public static String loadTextFile(Reader reader) throws IOException {
116  
    StringBuilder builder = new StringBuilder();
117  
    try {
118  
      BufferedReader bufferedReader = new BufferedReader(reader);
119  
      String line;
120  
      while ((line = bufferedReader.readLine()) != null)
121  
        builder.append(line).append('\n');
122  
    } finally {
123  
      reader.close();
124  
    }
125  
    return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1);
126  
  }
127  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #564
Snippet name: isCompilable (JavaX)
Eternal ID of this version: #564/1
Text MD5: 7c44c0023a32bd252d038c3a90b5c9e5
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-04-10 21:47:27
Source code size: 5062 bytes / 127 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 643 / 601
Referenced in: [show references]