src/main/java/xelfi/compiler/XelfiCompilerMain.java
author Jaroslav Tulach <jaroslav.tulach@xelfi.cz>
Tue, 24 Jan 2017 04:30:13 +0100
branchDirtyFix
changeset 15 cdb47e644581
parent 10 fe294d0f1297
parent 14 3ff7a893a7ce
permissions -rw-r--r--
Merging in usage of modern Java compiler
     1 /** XelfiCompilerMain class
     2 * @author: Filip Dvorak
     3 * @version: 1.3
     4 */
     5 
     6 package xelfi.compiler;
     7 
     8 import java.io.*;
     9 import java.util.Arrays;
    10 import javax.tools.JavaCompiler;
    11 import javax.tools.StandardJavaFileManager;
    12 import javax.tools.ToolProvider;
    13 
    14 
    15 class XelfiCompilerMain extends Object implements MainCompiler
    16 {
    17     /** Output from compiler is send here. */
    18   protected XelfiCompilerOutput out;
    19 
    20   XelfiCompilerMain(XelfiCompilerOutput o)
    21   {
    22     out = o;
    23   }
    24 
    25   public void compileClass(File file)
    26   {
    27     String args[] = null;
    28     if(CompilerOptions.getUseGparam())
    29     {
    30       args = new String[2];
    31       args[0] = "-g";
    32       args[1] = file.toString();
    33       output("Compiling: " + args[0] + " " + args[1]);
    34     }
    35     else
    36     {
    37       args = new String[1];
    38       args[0] = file.toString();
    39       output("Compiling: " + args[0]);
    40     }
    41     JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    42     StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
    43     try {
    44       compiler.getTask(new OutputStreamWriter(out), fm, null, Arrays.asList(args).subList(0, args.length - 1), null, fm.getJavaFileObjects(args[args.length - 1])).call();
    45     } finally {
    46       try {
    47         fm.close();
    48       } catch (IOException ex) {
    49          ex.printStackTrace();
    50       }
    51     }
    52     output("\n");
    53   }
    54 
    55   public void error(String err)
    56   {
    57     out.topLevelError(err);
    58   }
    59 
    60   public void output(String msg)
    61   {
    62     out.topLevelOutput(msg);
    63   }
    64 }