Coverage Report - com.sun.tools.javafx.script.JavaFXScriptCompiler
 
Classes in this File Line Coverage Branch Coverage Complexity
JavaFXScriptCompiler
67%
39/58
67%
12/18
0
JavaFXScriptCompiler$RedirectedLog
100%
2/2
N/A
0
 
 1  
 /*
 2  
  * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
 3  
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  
  *
 5  
  * This code is free software; you can redistribute it and/or modify it
 6  
  * under the terms of the GNU General Public License version 2 only, as
 7  
  * published by the Free Software Foundation.  Sun designates this
 8  
  * particular file as subject to the "Classpath" exception as provided
 9  
  * by Sun in the LICENSE file that accompanied this code.
 10  
  *
 11  
  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  
  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  
  * version 2 for more details (a copy is included in the LICENSE file that
 15  
  * accompanied this code).
 16  
  *
 17  
  * You should have received a copy of the GNU General Public License version
 18  
  * 2 along with this work; if not, write to the Free Software Foundation,
 19  
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  
  *
 21  
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 22  
  * CA 95054 USA or visit www.sun.com if you need additional information or
 23  
  * have any questions.
 24  
  */
 25  
 
 26  
 package com.sun.tools.javafx.script;
 27  
 
 28  
 import com.sun.tools.javafx.api.JavafxcTool;
 29  
 import com.sun.javafx.api.*;
 30  
 import java.io.FileReader;
 31  
 import java.io.IOException;
 32  
 import java.io.Writer;
 33  
 import java.io.PrintWriter;
 34  
 import java.io.Reader;
 35  
 import java.util.ArrayList;
 36  
 import java.util.List;
 37  
 import java.util.Map;
 38  
 import javax.tools.*;
 39  
 /**
 40  
  * Simple interface to the JavaFX Script compiler using JSR 199 Compiler API, 
 41  
  * based on https://scripting.dev.java.net's JavaCompiler by A. Sundararajan.
 42  
  */
 43  
 public class JavaFXScriptCompiler {    
 44  
     private JavafxcTool tool;
 45  
     private StandardJavaFileManager stdManager;
 46  
     private ClassLoader parentClassLoader;
 47  
 
 48  26
     public JavaFXScriptCompiler(ClassLoader parent) {
 49  26
         parentClassLoader = parent;
 50  26
         tool = JavafxcTool.create();
 51  26
     }
 52  
 
 53  
     public Map<String, byte[]> compile(String filename, String source) {
 54  0
         PrintWriter err = new PrintWriter(System.err);
 55  0
         return compile(filename, source, err, null, null);
 56  
     }
 57  
 
 58  
     public Map<String, byte[]> compile(String fileName, String source, 
 59  
                                     Writer err) {
 60  0
         return compile(fileName, source, err, null, null);
 61  
     }
 62  
 
 63  
     public Map<String, byte[]> compile(String fileName, String source, 
 64  
                                     Writer err, String sourcePath) {
 65  0
         return compile(fileName, source, err, sourcePath, null);
 66  
     }
 67  
 
 68  
     public Map<String, byte[]> compile(String fileName, String source, 
 69  
                                     Writer err, String sourcePath, 
 70  
                                     String classPath) {
 71  0
         return compile(fileName, source, err, sourcePath, classPath,
 72  
                 new DiagnosticCollector<JavaFileObject>(), true);
 73  
     }
 74  
 
 75  
     public Map<String, byte[]> compile(String filename) throws IOException {
 76  0
         String source = readFully(new FileReader(filename));
 77  0
         PrintWriter err = new PrintWriter(System.err);
 78  0
         return compile(filename, source, err, null, null);
 79  
     }
 80  
 
 81  
     /**
 82  
      * compile given String source and return bytecodes as a Map.
 83  
      *
 84  
      * @param fileName source fileName to be used for error messages etc.
 85  
      * @param source Java source as String
 86  
      * @param err error writer where diagnostic messages are written
 87  
      * @param sourcePath location of additional .java source files
 88  
      * @param classPath location of additional .class files
 89  
      * @param diagnostics error and warning collector
 90  
      * @param printDiagnostics true if diagnostics should be displayed
 91  
      */
 92  
     public Map<String, byte[]> compile(String fileName, String source, 
 93  
                     Writer err, String sourcePath, String classPath,
 94  
                     DiagnosticListener<JavaFileObject> diagnostics,
 95  
                     boolean printDiagnostics) {
 96  
 
 97  
         // create a new memory JavaFileManager
 98  29
         if (stdManager == null) {
 99  19
             stdManager = tool.getStandardFileManager(diagnostics, null, null);
 100  
         }
 101  29
         MemoryFileManager manager = new MemoryFileManager(stdManager, parentClassLoader);
 102  
 
 103  
         // prepare the compilation unit
 104  29
         List<JavaFileObject> compUnits = new ArrayList<JavaFileObject>(1);
 105  29
         compUnits.add(manager.makeStringSource(fileName, source));
 106  
 
 107  
         // javac options
 108  29
         List<String> options = new ArrayList<String>();
 109  29
         options.add("-Xlint:all-unchecked");
 110  29
         options.add("-g");
 111  29
         options.add("-deprecation");
 112  29
         if (sourcePath != null) {
 113  0
             options.add("-sourcepath");
 114  0
             options.add(sourcePath);
 115  
         }
 116  29
         if (classPath != null) {
 117  29
             options.add("-classpath");
 118  29
             options.add(classPath);
 119  
         }
 120  29
         options.add("-target");
 121  29
         options.add(isJava6() ? "1.6" : "1.5");
 122  
         
 123  29
         options.add("-XDdumpfx=/tmp");
 124  
         
 125  
         // create a compilation task
 126  29
         JavafxcTask task = tool.getTask(err, manager, diagnostics, options, compUnits);
 127  
 
 128  29
         if (task.call() == false) {
 129  11
             if (printDiagnostics) {
 130  2
                 if (diagnostics instanceof DiagnosticCollector) {
 131  0
                     printDiagnostics((DiagnosticCollector)diagnostics, err);
 132  
                 }
 133  
             }
 134  11
             return null;
 135  
         }
 136  
 
 137  18
         Map<String, byte[]> classBytes = manager.getClassBytes();
 138  
         try {
 139  18
             manager.close();
 140  0
         } catch (IOException exp) {
 141  18
         }
 142  
 
 143  18
         return classBytes; 
 144  
     }
 145  
     
 146  
     private static boolean isJava6() {
 147  
         try {
 148  29
             Class.forName("java.util.ServiceLoader");
 149  29
             return true;
 150  0
         } catch (ClassNotFoundException e) {
 151  0
             return false;
 152  
         }
 153  
     }
 154  
 
 155  
     static void printDiagnostics(DiagnosticCollector<JavaFileObject> diagnostics, Writer err) {
 156  
         // install customized diagnostic message formats, which don't print script name
 157  1
         com.sun.tools.javac.util.Context context = new com.sun.tools.javac.util.Context();
 158  1
         com.sun.tools.javac.util.Options options =
 159  
                 com.sun.tools.javac.util.Options.instance(context);
 160  1
         options.put("diags", "%l: %t%m|%p%m");
 161  
 
 162  1
         PrintWriter perr = new PrintWriter(err);
 163  1
         RedirectedLog log = new RedirectedLog(context, perr);
 164  1
         for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
 165  1
             log.report((com.sun.tools.javac.util.JCDiagnostic)diagnostic);
 166  
         }
 167  1
     }
 168  
 
 169  
     // read a Reader fully and return the content as string
 170  
     private String readFully(Reader reader) throws IOException {
 171  0
         char[] arr = new char[8*1024]; // 8K at a time
 172  0
         StringBuilder buf = new StringBuilder();
 173  
         int numChars;
 174  0
         while ((numChars = reader.read(arr, 0, arr.length)) > 0) {
 175  0
             buf.append(arr, 0, numChars);
 176  
         }
 177  0
         return buf.toString();
 178  
     }
 179  
     
 180  
     private static class RedirectedLog extends com.sun.tools.javac.util.Log {
 181  
         RedirectedLog(com.sun.tools.javac.util.Context context, PrintWriter writer) {
 182  1
             super(context, writer);
 183  1
         }
 184  
     }
 185  
 }