Coverage Report - com.sun.tools.javafx.api.JavafxcTaskImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
JavafxcTaskImpl
88%
112/127
66%
42/64
0
JavafxcTaskImpl$1
0%
0/11
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 com.sun.source.tree.CompilationUnitTree;
 30  
 import com.sun.source.tree.Tree;
 31  
 import com.sun.source.util.TaskEvent;
 32  
 import com.sun.source.util.TaskListener;
 33  
 import com.sun.tools.javac.model.JavacElements;
 34  
 import com.sun.tools.javac.model.JavacTypes;
 35  
 import com.sun.tools.javac.tree.*;
 36  
 import com.sun.tools.javac.tree.JCTree.*;
 37  
 import com.sun.tools.javac.util.ClientCodeException;
 38  
 import com.sun.tools.javac.util.Context;
 39  
 import com.sun.tools.javac.util.List;
 40  
 import com.sun.tools.javac.util.ListBuffer;
 41  
 import com.sun.tools.javac.util.Options;
 42  
 import com.sun.tools.javafx.main.CommandLine;
 43  
 import com.sun.tools.javafx.main.Main;
 44  
 import java.io.File;
 45  
 import java.io.IOException;
 46  
 import java.util.HashMap;
 47  
 import java.util.Map;
 48  
 import java.util.concurrent.atomic.AtomicBoolean;
 49  
 import javax.lang.model.type.TypeMirror;
 50  
 import javax.tools.JavaFileObject;
 51  
 
 52  
 /**
 53  
  * JavaFX Script implementation of a JavaFX compilation task, based on
 54  
  * JavacTaskImpl.  This class extends JavafxTask to isolate the internal
 55  
  * javafx and javac compiler API from the public API.
 56  
  *
 57  
  * @see com.sun.tools.javac.api.JavacTaskImpl
 58  
  * @author tball
 59  
  */
 60  2
 public class JavafxcTaskImpl extends JavafxcTask {
 61  
 
 62  
     private Main compilerMain;
 63  
     private com.sun.tools.javafx.main.JavafxCompiler compiler;
 64  
     private String[] args;
 65  
     private Context context;
 66  
     private List<JavaFileObject> fileObjects;
 67  
     private Map<JavaFileObject, JCCompilationUnit> notYetEntered;
 68  
     private List<JCCompilationUnit> units;
 69  
     private TaskListener taskListener;
 70  52
     private AtomicBoolean used = new AtomicBoolean();
 71  52
     private Integer result = null;
 72  
 
 73  52
     JavafxcTaskImpl(JavafxcTool tool, Main compilerMain, String[] args, Context context, List<JavaFileObject> fileObjects) {
 74  52
         this.compilerMain = compilerMain;
 75  52
         this.args = args;
 76  52
         this.context = context;
 77  52
         this.fileObjects = fileObjects;
 78  
         // null checks
 79  52
         compilerMain.getClass();
 80  52
         args.getClass();
 81  52
         context.getClass();
 82  52
         fileObjects.getClass();
 83  
 
 84  52
         Options optionTable = Options.instance(context);
 85  52
         optionTable.put("-Xjcov", "-Xjcov");  // generate tree end positions
 86  52
     }
 87  
 
 88  
     JavafxcTaskImpl(JavafxcTool tool,
 89  
                 Main compilerMain,
 90  
                 Iterable<String> flags,
 91  
                 Context context,
 92  
                 Iterable<? extends JavaFileObject> fileObjects) {
 93  52
         this(tool, compilerMain, toArray(flags), context, toList(fileObjects));
 94  52
     }
 95  
 
 96  
     static private String[] toArray(Iterable<String> flags) {
 97  52
         ListBuffer<String> result = new ListBuffer<String>();
 98  52
         if (flags != null)
 99  29
             for (String flag : flags)
 100  232
                 result.append(flag);
 101  52
         return result.toArray(new String[result.length()]);
 102  
     }
 103  
 
 104  
     static private List<JavaFileObject> toList(Iterable<? extends JavaFileObject> fileObjects) {
 105  52
         if (fileObjects == null)
 106  8
             return List.nil();
 107  44
         ListBuffer<JavaFileObject> result = new ListBuffer<JavaFileObject>();
 108  44
         for (JavaFileObject fo : fileObjects)
 109  44
             result.append(fo);
 110  44
         return result.toList();
 111  
     }
 112  
 
 113  
     @Override
 114  
     public Boolean call() {
 115  29
         if (!used.getAndSet(true)) {
 116  29
             beginContext();
 117  
             try {
 118  29
                 result = compilerMain.compile(args, context, fileObjects);
 119  
             } finally {
 120  29
                 endContext();
 121  29
             }
 122  29
             compilerMain = null;
 123  29
             args = null;
 124  29
             context = null;
 125  29
             fileObjects = null;
 126  29
             return result == 0;
 127  
         } else {
 128  0
             throw new IllegalStateException("multiple calls to method 'call'");
 129  
         }
 130  
     }
 131  52
     private boolean compilationInProgress = false;
 132  
 
 133  
     private void prepareCompiler() throws IOException {
 134  22
         if (!used.getAndSet(true)) {
 135  19
             beginContext();
 136  19
             compilerMain.registerServices(context, args);
 137  19
             compilerMain.setOptions(Options.instance(context));
 138  19
             compilerMain.filenames = new ListBuffer<File>();
 139  19
             List<File> filenames = compilerMain.processArgs(CommandLine.parse(args));
 140  19
             if (!filenames.isEmpty())
 141  0
                 throw new IllegalArgumentException("Malformed arguments " + filenames.toString(" "));
 142  19
             compiler = com.sun.tools.javafx.main.JavafxCompiler.instance(context);
 143  19
             compiler.keepComments = true;
 144  19
             notYetEntered = new HashMap<JavaFileObject, JCCompilationUnit>();
 145  19
             for (JavaFileObject file: fileObjects)
 146  15
                 notYetEntered.put(file, null);
 147  19
             args = null;
 148  
         }
 149  22
     }
 150  
 
 151  
     private void beginContext() {
 152  48
         context.put(JavafxcTaskImpl.class, this);
 153  48
         if (context.get(TaskListener.class) != null) {
 154  0
             context.put(TaskListener.class, (TaskListener) null);
 155  
         }
 156  48
         if (taskListener != null) {
 157  0
             context.put(TaskListener.class, wrap(taskListener));
 158  
         }
 159  48
         if (compilationInProgress) {
 160  0
             throw new IllegalStateException("Compilation in progress");
 161  
         }
 162  48
         compilationInProgress = true;
 163  48
     }
 164  
 
 165  
     private TaskListener wrap(final TaskListener tl) {
 166  0
         tl.getClass(); // null check
 167  0
         return new TaskListener() {
 168  
 
 169  
             public void started(TaskEvent e) {
 170  
                 try {
 171  0
                     tl.started(e);
 172  0
                 } catch (Throwable t) {
 173  0
                     throw new ClientCodeException(t);
 174  0
                 }
 175  0
             }
 176  
 
 177  
             public void finished(TaskEvent e) {
 178  
                 try {
 179  0
                     tl.finished(e);
 180  0
                 } catch (Throwable t) {
 181  0
                     throw new ClientCodeException(t);
 182  0
                 }
 183  0
             }
 184  
         };
 185  
     }
 186  
 
 187  
     private void endContext() {
 188  29
         compilationInProgress = false;
 189  29
     }
 190  
 
 191  
     public Iterable<? extends CompilationUnitTree> parse() throws IOException {
 192  
         try {
 193  16
             prepareCompiler();
 194  16
             units = compiler.parseFiles(fileObjects);
 195  16
             for (JCCompilationUnit unit: units) {
 196  15
                 JavaFileObject file = unit.getSourceFile();
 197  15
                 if (notYetEntered.containsKey(file))
 198  15
                     notYetEntered.put(file, unit);
 199  15
             }
 200  16
             return units;
 201  
         }
 202  
         finally {
 203  16
             parsed = true;
 204  16
             if (compiler != null && compiler.log != null)
 205  16
                 compiler.log.flush();
 206  
         }
 207  
     }
 208  
 
 209  52
     private boolean parsed = false;
 210  
 
 211  
     void enter() throws IOException {
 212  6
         prepareCompiler();
 213  
 
 214  6
         ListBuffer<JCCompilationUnit> roots = null;
 215  
 
 216  6
         if (notYetEntered.size() > 0) {
 217  3
             if (!parsed)
 218  3
                 parse();
 219  3
             for (JavaFileObject file: fileObjects) {
 220  3
                 JCCompilationUnit unit = notYetEntered.remove(file);
 221  3
                 if (unit != null) {
 222  3
                     if (roots == null)
 223  3
                         roots = new ListBuffer<JCCompilationUnit>();
 224  3
                     roots.append(unit);
 225  
                 }
 226  3
             }
 227  3
             notYetEntered.clear();
 228  
         }
 229  
 
 230  6
         if (roots != null)
 231  
             try {
 232  3
                 compiler.enterTrees(roots.toList());
 233  
             }
 234  
             finally {
 235  3
                 if (compiler != null && compiler.log != null)
 236  3
                     compiler.log.flush();
 237  
             }
 238  6
     }
 239  
 
 240  
     @Override
 241  
     public Iterable<? extends CompilationUnitTree> analyze() throws IOException {
 242  
         try {
 243  5
             enter();
 244  5
             compiler.attribute();
 245  5
             return units;
 246  
         } finally {
 247  5
             if (compiler != null && compiler.log != null)
 248  5
                 compiler.log.flush();
 249  
         }
 250  
     }
 251  
     
 252  
     @Override
 253  
     public int errorCheck() throws IOException {
 254  
         try {
 255  1
             enter();
 256  1
             compiler.errorCheck();
 257  
         } finally {
 258  1
             if (compiler != null && compiler.log != null)
 259  1
                 compiler.log.flush();
 260  
         }
 261  1
         return compiler.errorCount();
 262  
     }
 263  
 
 264  
     @Override
 265  
     public Iterable<? extends JavaFileObject> generate() throws IOException {
 266  1
         analyze();
 267  1
         final ListBuffer<JavaFileObject> results = new ListBuffer<JavaFileObject>();
 268  1
         compiler.generate(results);
 269  1
         return results;
 270  
     }
 271  
     
 272  
     @Override
 273  
     public void setTaskListener(TaskListener taskListener) {
 274  0
         this.taskListener = taskListener;
 275  0
     }
 276  
 
 277  
     @Override
 278  
     public TypeMirror getTypeMirror(Iterable<? extends Tree> path) {
 279  1
         if (path == null)
 280  1
             return null;
 281  0
         Tree last = null;
 282  0
         for (Tree node : path) {
 283  0
             last = node;
 284  
         }
 285  0
         return ((JCTree) last).type;
 286  
     }
 287  
 
 288  
     @Override
 289  
     public JavacElements getElements() {
 290  1
         if (context == null) {
 291  0
             throw new IllegalStateException();
 292  
         }
 293  1
         return JavacElements.instance(context);
 294  
     }
 295  
 
 296  
     @Override
 297  
     public JavacTypes getTypes() {
 298  1
         if (context == null) {
 299  0
             throw new IllegalStateException();
 300  
         }
 301  1
         return JavacTypes.instance(context);
 302  
     }
 303  
 
 304  
     /**
 305  
      * For internal use by Sun Microsystems only.  This method will be
 306  
      * removed without warning.
 307  
      */
 308  
     public Context getContext() {
 309  4
         return context;
 310  
     }
 311  
 }