Coverage Report - com.sun.tools.javafx.ant.JavaFxAntTask
 
Classes in this File Line Coverage Branch Coverage Complexity
JavaFxAntTask
0%
0/43
0%
0/22
0
JavaFxAntTask$JavaFxCompilerAdapter
0%
0/34
0%
0/16
0
JavaFxAntTask$JavaFxCompilerAdapter$1
0%
0/14
0%
0/10
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.ant;
 27  
 
 28  
 import org.apache.tools.ant.BuildException;
 29  
 import org.apache.tools.ant.Project;
 30  
 import org.apache.tools.ant.taskdefs.Javac;
 31  
 import org.apache.tools.ant.taskdefs.compilers.CompilerAdapter;
 32  
 import org.apache.tools.ant.taskdefs.compilers.DefaultCompilerAdapter;
 33  
 import org.apache.tools.ant.types.Commandline;
 34  
 import org.apache.tools.ant.types.Path;
 35  
 import org.apache.tools.ant.types.Reference;
 36  
 import org.apache.tools.ant.util.GlobPatternMapper;
 37  
 import org.apache.tools.ant.util.JavaEnvUtils;
 38  
 import org.apache.tools.ant.util.SourceFileScanner;
 39  
 
 40  
 import java.io.File;
 41  
 import java.lang.reflect.Method;
 42  
 import java.net.URL;
 43  
 import java.net.URLClassLoader;
 44  
 
 45  
 /**
 46  
  * JavaFxAntTask
 47  
  *
 48  
  * @author Brian Goetz
 49  
  */
 50  0
 public class JavaFxAntTask extends Javac {
 51  
     public Path compilerClassPath;
 52  
 
 53  
     private static final String FAIL_MSG
 54  
             = "JavaFX compile failed; see the compiler error output for details.";
 55  
 
 56  
     public static final String FX_ENTRY_POINT = "com.sun.tools.javafx.Main";
 57  
 
 58  
     public JavaFxAntTask() {
 59  0
         super();
 60  0
         super.setCompiler(JavaFxCompilerAdapter.class.getName());
 61  0
         super.setIncludeantruntime(true);
 62  0
     }
 63  
 
 64  
     @Override
 65  
     protected void scanDir(File srcDir, File destDir, String[] files) {
 66  0
         GlobPatternMapper m = new GlobPatternMapper();
 67  0
         m.setFrom("*.fx");
 68  0
         m.setTo("*.class");
 69  0
         SourceFileScanner sfs = new SourceFileScanner(this);
 70  0
         File[] newFiles = sfs.restrictAsFiles(files, srcDir, destDir, m);
 71  
 
 72  0
         if (newFiles.length > 0) {
 73  0
             File[] newCompileList
 74  
                     = new File[compileList.length + newFiles.length];
 75  0
             System.arraycopy(compileList, 0, newCompileList, 0, compileList.length);
 76  0
             System.arraycopy(newFiles, 0, newCompileList, compileList.length, newFiles.length);
 77  0
             compileList = newCompileList;
 78  
         }
 79  0
     }
 80  
 
 81  
     /**
 82  
      * Workaround for classloader bug in CompilerAdapterFactory
 83  
      */
 84  
     @Override
 85  
     protected void compile() {
 86  0
         if (compileList.length > 0) {
 87  0
             log("Compiling " + compileList.length + " source file"
 88  
                     + (compileList.length == 1 ? "" : "s")
 89  
                     + (getDestdir() != null ? " to " + getDestdir() : ""));
 90  
 
 91  0
             if (listFiles) {
 92  0
                 for (int i = 0; i < compileList.length; i++) {
 93  0
                     String filename = compileList[i].getAbsolutePath();
 94  0
                     log(filename);
 95  
                 }
 96  
             }
 97  
 
 98  0
             CompilerAdapter adapter = new JavaFxCompilerAdapter();
 99  
 
 100  
             // now we need to populate the compiler adapter
 101  0
             adapter.setJavac(this);
 102  
 
 103  
             // finally, lets execute the compiler!!
 104  0
             if (!adapter.execute()) {
 105  0
                 if (failOnError) {
 106  0
                     throw new BuildException(FAIL_MSG, getLocation());
 107  
                 } else {
 108  0
                     log(FAIL_MSG, Project.MSG_ERR);
 109  
                 }
 110  
             }
 111  
         }
 112  0
     }
 113  
 
 114  
     public void setCompilerClassPath(Path p) {
 115  0
         compilerClassPath = p;
 116  0
     }
 117  
 
 118  
     public void setCompilerClassPathRef(Reference r) {
 119  0
         setCompilerClassPath((Path) r.getReferencedObject());
 120  0
     }
 121  
 
 122  
     private URL[] pathAsURLs() throws java.net.MalformedURLException {
 123  0
         Path p = compilerClassPath != null ? compilerClassPath : new Path(getProject());
 124  0
         java.util.ArrayList<URL> urls = new java.util.ArrayList<URL>();
 125  0
         for (String s : p.list()) {
 126  0
             urls.add(new File(s).toURI().toURL());
 127  
         }
 128  0
         return urls.toArray(new URL[0]);
 129  
     }
 130  
 
 131  
     @Override
 132  
     public String getCompiler() {
 133  0
         return "extJavac";
 134  
     }
 135  
 
 136  
     @Override
 137  
     protected void checkParameters() throws BuildException {
 138  0
         super.checkParameters();
 139  0
             if (compilerClassPath == null) {
 140  0
                 throw new BuildException("javafxc: compilerclasspath must be set", getLocation());
 141  
             }
 142  0
     }
 143  
     
 144  0
     public static class JavaFxCompilerAdapter extends DefaultCompilerAdapter {
 145  
 
 146  
         public boolean execute() throws BuildException {
 147  
             try {
 148  0
                 if (getJavac().isForkedJavac())
 149  0
                     return forkeExecute();
 150  
                 else {
 151  0
                     Commandline cmd = setupModernJavacCommand();
 152  0
                     URL[] jars = ((JavaFxAntTask) getJavac()).pathAsURLs();
 153  0
                     URLClassLoader loader = new URLClassLoader(jars) {
 154  
                         @Override
 155  
                         protected Class loadClass(String n, boolean r) throws ClassNotFoundException {
 156  0
                             if (n.indexOf("sun.tools") >= 0 || n.startsWith("com.sun.source")) {
 157  0
                                 Class c = findLoadedClass(n);
 158  0
                                 if (c != null) {
 159  0
                                     getJavac().log("found loaded class: " + n);
 160  0
                                     return c;
 161  
                                 }
 162  0
                                 c = findClass(n);
 163  0
                                 if (c == null) {
 164  0
                                     getJavac().log("didn't find class:  " + n);
 165  0
                                     return super.loadClass(n, r);
 166  
                                 }
 167  0
                                 if (r)
 168  0
                                     resolveClass(c);
 169  0
                                 return c;
 170  
                             }
 171  0
                             return super.loadClass(n, r);
 172  
                         }
 173  
                     };
 174  0
                     Class c = Class.forName(FX_ENTRY_POINT, true, loader);
 175  0
                     Object compiler = c.newInstance();
 176  0
                     Method compile = c.getMethod("compile", String[].class);
 177  0
                     Object[] args = cmd.getArguments();
 178  0
                     int result = (Integer) compile.invoke(compiler, new Object[]{args});
 179  0
                     return (result == 0);  // zero errors
 180  
                 }
 181  0
             } catch (Exception ex) {
 182  0
                 getJavac().log(ex.toString());
 183  0
                 if (ex instanceof ClassNotFoundException ||
 184  
                         ex instanceof java.lang.reflect.InvocationTargetException) {
 185  0
                     throw new BuildException(ex);
 186  
                 }
 187  0
                 if (ex instanceof BuildException) {
 188  0
                     throw (BuildException) ex;
 189  
                 } else {
 190  0
                     throw new BuildException("Error starting JavaFX compiler",
 191  
                             ex, location);
 192  
                 }
 193  
             }
 194  
         }
 195  
 
 196  
         public boolean forkeExecute() throws Exception {
 197  0
             Commandline cmd = new Commandline();
 198  0
             cmd.setExecutable(JavaEnvUtils.getJdkExecutable("java"));
 199  0
             if (memoryInitialSize != null) {
 200  0
                 cmd.createArgument().setValue("-Xms" + memoryInitialSize);
 201  0
                 memoryInitialSize = null; // don't include it in setupJavacCommandlineSwitches()
 202  
             }
 203  0
             if (memoryMaximumSize != null) {
 204  0
                 cmd.createArgument().setValue("-Xmx" + memoryMaximumSize);
 205  0
                 memoryMaximumSize = null; // don't include it in setupJavacCommandlineSwitches()
 206  
             }
 207  0
             String cp = "-Xbootclasspath/p:" +
 208  
                     ((JavaFxAntTask) getJavac()).compilerClassPath.toString();
 209  0
             cmd.createArgument().setValue(cp);
 210  0
             cmd.createArgument().setValue(FX_ENTRY_POINT);
 211  0
             setupJavacCommandlineSwitches(cmd, true);
 212  0
             int firstFileName = cmd.size();
 213  0
             logAndAddFilesToCompile(cmd);
 214  0
             return executeExternalCompile(cmd.getCommandline(), firstFileName, true) == 0;
 215  
         }
 216  
     }
 217  
 }