Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 213   Methods: 4
NCLOC: 101   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CompilerAdapterFactory.java 29.2% 31% 50% 31.4%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 
 55   
 package org.apache.tools.ant.taskdefs.compilers;
 56   
 
 57   
 import org.apache.tools.ant.BuildException;
 58   
 import org.apache.tools.ant.Project;
 59   
 import org.apache.tools.ant.Task;
 60   
 import org.apache.tools.ant.util.JavaEnvUtils;
 61   
 
 62   
 /**
 63   
  * Creates the necessary compiler adapter, given basic criteria.
 64   
  *
 65   
  * @author <a href="mailto:jayglanville@home.com">J D Glanville</a>
 66   
  * @since Ant 1.3
 67   
  */
 68   
 public class CompilerAdapterFactory {
 69   
 
 70   
     /** This is a singleton -- can't create instances!! */
 71  0
     private CompilerAdapterFactory() {
 72   
     }
 73   
 
 74   
     /**
 75   
      * Based on the parameter passed in, this method creates the necessary
 76   
      * factory desired.
 77   
      *
 78   
      * The current mapping for compiler names are as follows:
 79   
      * <ul><li>jikes = jikes compiler
 80   
      * <li>classic, javac1.1, javac1.2 = the standard compiler from JDK
 81   
      * 1.1/1.2
 82   
      * <li>modern, javac1.3, javac1.4 = the compiler of JDK 1.3+
 83   
      * <li>jvc, microsoft = the command line compiler from Microsoft's SDK
 84   
      * for Java / Visual J++
 85   
      * <li>kjc = the kopi compiler</li>
 86   
      * <li>gcj = the gcj compiler from gcc</li>
 87   
      * <li>sj, symantec = the Symantec Java compiler</li>
 88   
      * <li><i>a fully quallified classname</i> = the name of a compiler
 89   
      * adapter
 90   
      * </ul>
 91   
      *
 92   
      * @param compilerType either the name of the desired compiler, or the
 93   
      * full classname of the compiler's adapter.
 94   
      * @param task a task to log through.
 95   
      * @throws BuildException if the compiler type could not be resolved into
 96   
      * a compiler adapter.
 97   
      */
 98  19
     public static CompilerAdapter getCompiler(String compilerType, Task task) 
 99   
         throws BuildException {
 100  19
             boolean isClassicCompilerSupported = true;
 101   
             //as new versions of java come out, add them to this test
 102  19
             if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_4)) {
 103  19
                 isClassicCompilerSupported = false;
 104   
             }
 105   
 
 106  19
             if (compilerType.equalsIgnoreCase("jikes")) {
 107  0
                 return new Jikes();
 108   
             }
 109  19
             if (compilerType.equalsIgnoreCase("extJavac")) {
 110  1
                 return new JavacExternal();
 111   
             }       
 112  18
             if (compilerType.equalsIgnoreCase("classic") ||
 113   
                 compilerType.equalsIgnoreCase("javac1.1") ||
 114   
                 compilerType.equalsIgnoreCase("javac1.2")) {
 115  0
                 if (isClassicCompilerSupported) {
 116  0
                     return new Javac12();
 117   
                 } else {
 118  0
                     task.log("This version of java does "
 119   
                                              + "not support the classic "
 120   
                                              + "compiler; upgrading to modern",
 121   
                                              Project.MSG_WARN);
 122  0
                     compilerType="modern";
 123   
                 }
 124   
             }
 125   
             //on java<=1.3 the modern falls back to classic if it is not found
 126   
             //but on java>=1.4 we just bail out early
 127  18
             if (compilerType.equalsIgnoreCase("modern") ||
 128   
                 compilerType.equalsIgnoreCase("javac1.3") ||
 129   
                 compilerType.equalsIgnoreCase("javac1.4")) {
 130   
                 // does the modern compiler exist?
 131  18
                 if (doesModernCompilerExist()) {
 132  18
                     return new Javac13();
 133   
                 } else {
 134  0
                     if (isClassicCompilerSupported) {
 135  0
                         task.log("Modern compiler not found - looking for "
 136   
                                  + "classic compiler", Project.MSG_WARN);
 137  0
                         return new Javac12();
 138   
                     } else {
 139  0
                         throw new BuildException("Unable to find a javac " 
 140   
                                                  + "compiler;\n"
 141   
                                                  + "com.sun.tools.javac.Main "
 142   
                                                  + "is not on the " 
 143   
                                                  + "classpath.\n"
 144   
                                                  + "Perhaps JAVA_HOME does not"
 145   
                                                  + " point to the JDK");
 146   
                     }
 147   
                 }
 148   
             }
 149   
 
 150  0
             if (compilerType.equalsIgnoreCase("jvc") ||
 151   
                 compilerType.equalsIgnoreCase("microsoft")) {
 152  0
                 return new Jvc();
 153   
             }
 154  0
             if (compilerType.equalsIgnoreCase("kjc")) {
 155  0
                 return new Kjc();
 156   
             }
 157  0
             if (compilerType.equalsIgnoreCase("gcj")) {
 158  0
                 return new Gcj();
 159   
             }
 160  0
             if (compilerType.equalsIgnoreCase("sj") ||
 161   
                 compilerType.equalsIgnoreCase("symantec")) {
 162  0
                 return new Sj();
 163   
             }
 164  0
             return resolveClassName(compilerType);
 165   
         }
 166   
 
 167   
     /**
 168   
      * query for the Modern compiler existing
 169   
      * @return true iff classic os on the classpath
 170   
      */ 
 171  18
     private static boolean doesModernCompilerExist() {
 172  18
         try {
 173  18
             Class.forName("com.sun.tools.javac.Main");
 174  18
             return true;
 175   
         } catch (ClassNotFoundException cnfe) {
 176  0
             try {
 177  0
                 CompilerAdapterFactory.class.getClassLoader().loadClass( "com.sun.tools.javac.Main" );
 178  0
                 return true;
 179   
             } catch (ClassNotFoundException cnfe2) {
 180   
             }
 181   
         }
 182  0
         return false;
 183   
     }
 184   
     
 185   
     /**
 186   
      * Tries to resolve the given classname into a compiler adapter.
 187   
      * Throws a fit if it can't.
 188   
      *
 189   
      * @param className The fully qualified classname to be created.
 190   
      * @throws BuildException This is the fit that is thrown if className
 191   
      * isn't an instance of CompilerAdapter.
 192   
      */
 193  0
     private static CompilerAdapter resolveClassName(String className)
 194   
         throws BuildException {
 195  0
         try {
 196  0
             Class c = Class.forName(className);
 197  0
             Object o = c.newInstance();
 198  0
             return (CompilerAdapter) o;
 199   
         } catch (ClassNotFoundException cnfe) {
 200  0
             throw new BuildException("Compiler Adapter '"+className 
 201   
                     + "' can\'t be found.", cnfe);
 202   
         } catch (ClassCastException cce) {
 203  0
             throw new BuildException(className + " isn\'t the classname of "
 204   
                     + "a compiler adapter.", cce);
 205   
         } catch (Throwable t) {
 206   
             // for all other possibilities
 207  0
             throw new BuildException("Compiler Adapter "+className 
 208   
                     + " caused an interesting exception.", t);
 209   
         }
 210   
     }
 211   
 
 212   
 }
 213