Coverage Report - com.sun.tools.javafx.api.JavafxcTool
 
Classes in this File Line Coverage Branch Coverage Complexity
JavafxcTool
89%
63/71
88%
42/48
0
JavafxcTool$1
50%
1/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.api;
 27  
 
 28  
 import com.sun.javafx.api.*;
 29  
 import java.io.InputStream;
 30  
 import java.io.OutputStream;
 31  
 import java.io.PrintWriter;
 32  
 import java.util.Collections;
 33  
 import java.util.EnumSet;
 34  
 import java.util.Set;
 35  
 import javax.lang.model.SourceVersion;
 36  
 import javax.tools.*;
 37  
 
 38  
 import com.sun.tools.javac.util.Context;
 39  
 import com.sun.tools.javac.util.JavacFileManager;
 40  
 import com.sun.tools.javac.util.Log;
 41  
 import com.sun.tools.javac.util.Options;
 42  
 import com.sun.tools.javafx.main.JavafxOption;
 43  
 import com.sun.tools.javafx.main.Main;
 44  
 import com.sun.tools.javafx.main.RecognizedOptions.GrumpyHelper;
 45  
 import com.sun.tools.javafx.main.RecognizedOptions;
 46  
 import com.sun.tools.javafx.util.JavafxFileManager;
 47  
 import com.sun.tools.javafx.util.MsgSym;
 48  
 import java.io.Writer;
 49  
 import java.nio.charset.Charset;
 50  
 import java.util.Iterator;
 51  
 import java.util.Locale;
 52  
 
 53  
 /**
 54  
  * The Tool API implementation for the JavaFX Script compiler, based on the
 55  
  * javac compiler tool implementation.
 56  
  *
 57  
  * @author Tom Ball
 58  
  */
 59  56
 public final class JavafxcTool implements JavafxCompiler {
 60  56
     private final Context dummyContext = new Context();
 61  
 
 62  56
     private final PrintWriter silent = new PrintWriter(new OutputStream(){
 63  
         @Override
 64  0
         public void write(int b) {}
 65  
     });
 66  
 
 67  56
     private final Main sharedCompiler = new Main("javafxc", silent);
 68  
     {
 69  56
         sharedCompiler.setOptions(Options.instance(dummyContext));
 70  56
     }
 71  
 
 72  
     /**
 73  
      * Static factory method for creating new instances of this tool.
 74  
      * @return new instance of this tool
 75  
      */
 76  
     public static JavafxcTool create() {
 77  41
         return new JavafxcTool();
 78  
     }
 79  
 
 80  
     @Override
 81  
     public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
 82  351
         if (err == null)
 83  0
             err = System.err;
 84  2818
         for (String argument : arguments)
 85  2467
             argument.getClass(); // null check
 86  351
         return com.sun.tools.javafx.Main.compile(arguments, new PrintWriter(err, true));
 87  
     }
 88  
 
 89  
     @Override
 90  
     public Set<SourceVersion> getSourceVersions() {
 91  1
         return Collections.unmodifiableSet(EnumSet.range(SourceVersion.RELEASE_3,
 92  
                                                          SourceVersion.latest()));
 93  
     }
 94  
 
 95  
     @Override
 96  
     public JavacFileManager getStandardFileManager(
 97  
         DiagnosticListener<? super JavaFileObject> diagnosticListener,
 98  
         Locale locale,
 99  
         Charset charset) {
 100  44
         Context context = new Context();
 101  44
         if (diagnosticListener != null)
 102  35
             context.put(DiagnosticListener.class, diagnosticListener);
 103  44
         context.put(Log.outKey, new PrintWriter(System.err, true)); // FIXME
 104  44
         return new JavafxFileManager(context, true, charset);
 105  
     }
 106  
 
 107  
     @Override
 108  
     public JavafxcTask getTask(Writer out,
 109  
                              JavaFileManager fileManager,
 110  
                              DiagnosticListener<? super JavaFileObject> diagnosticListener,
 111  
                              Iterable<String> options,
 112  
                              Iterable<? extends JavaFileObject> compilationUnits)
 113  
     {
 114  52
         final String kindMsg = "All compilation units must be of SOURCE kind";
 115  52
         if (options != null)
 116  29
             for (String option : options)
 117  232
                 option.getClass(); // null check
 118  52
         if (compilationUnits != null) {
 119  44
             for (JavaFileObject cu : compilationUnits) {
 120  44
                 if (cu.getKind() != JavaFileObject.Kind.SOURCE) // implicit null check
 121  0
                     throw new IllegalArgumentException(kindMsg);
 122  
             }
 123  
         }
 124  
 
 125  52
         Context context = new Context();
 126  
 
 127  52
         if (diagnosticListener != null)
 128  44
             context.put(DiagnosticListener.class, diagnosticListener);
 129  
 
 130  52
         if (out == null)
 131  23
             context.put(Log.outKey, new PrintWriter(System.err, true));
 132  
         else
 133  29
             context.put(Log.outKey, new PrintWriter(out, true));
 134  
 
 135  52
         if (fileManager == null)
 136  8
             fileManager = getStandardFileManager(diagnosticListener, null, null);
 137  52
         context.put(JavaFileManager.class, fileManager);
 138  52
         processOptions(context, fileManager, options);
 139  52
         Main compiler = new Main("javacTask", context.get(Log.outKey));
 140  52
         return new JavafxcTaskImpl(this, compiler, options, context, compilationUnits);
 141  
     }
 142  
 
 143  
     private static void processOptions(Context context,
 144  
                                        JavaFileManager fileManager,
 145  
                                        Iterable<String> options)
 146  
     {
 147  52
         if (options == null)
 148  23
             return;
 149  
 
 150  29
         Options optionTable = Options.instance(context);
 151  
 
 152  29
         JavafxOption[] recognizedOptions =
 153  
             RecognizedOptions.getJavacToolOptions(new GrumpyHelper());
 154  29
         Iterator<String> flags = options.iterator();
 155  203
         while (flags.hasNext()) {
 156  174
             String flag = flags.next();
 157  
             int j;
 158  2436
             for (j=0; j<recognizedOptions.length; j++)
 159  2407
                 if (recognizedOptions[j].matches(flag))
 160  145
                     break;
 161  
 
 162  174
             if (j == recognizedOptions.length) {
 163  29
                 if (fileManager.handleOption(flag, flags)) {
 164  29
                     continue;
 165  
                 } else {
 166  0
                     String msg = Main.getLocalizedString(MsgSym.MESSAGE_ERR_INVALID_FLAG, flag);
 167  0
                     throw new IllegalArgumentException(msg);
 168  
                 }
 169  
             }
 170  
 
 171  145
             JavafxOption option = recognizedOptions[j];
 172  145
             if (option.hasArg()) {
 173  29
                 if (!flags.hasNext()) {
 174  0
                     String msg = Main.getLocalizedString(MsgSym.MESSAGE_ERR_REQ_ARG, flag);
 175  0
                     throw new IllegalArgumentException(msg);
 176  
                 }
 177  29
                 String operand = flags.next();
 178  29
                 if (option.process(optionTable, flag, operand))
 179  
                     // should not happen as the GrumpyHelper will throw exceptions
 180  
                     // in case of errors
 181  0
                     throw new IllegalArgumentException(flag + " " + operand);
 182  29
             } else {
 183  116
                 if (option.process(optionTable, flag))
 184  
                     // should not happen as the GrumpyHelper will throw exceptions
 185  
                     // in case of errors
 186  0
                     throw new IllegalArgumentException(flag);
 187  
             }
 188  145
         }
 189  29
     }
 190  
 
 191  
     @Override
 192  
     public int isSupportedOption(String option) {
 193  4
         JavafxOption[] recognizedOptions =
 194  
             RecognizedOptions.getJavacToolOptions(new GrumpyHelper());
 195  76
         for (JavafxOption o : recognizedOptions) {
 196  74
             if (o.matches(option))
 197  2
                 return o.hasArg() ? 1 : 0;
 198  
         }
 199  2
         return -1;
 200  
     }
 201  
 }