Clover coverage report - DrJava Test Coverage (drjava-20120304-r5456)
Coverage timestamp: Sun Mar 4 2012 03:13:23 CST
file stats: LOC: 208   Methods: 13
NCLOC: 127   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
JDKToolsLibrary.java 50% 88.5% 100% 84.1%
coverage coverage
 1    /*BEGIN_COPYRIGHT_BLOCK
 2    *
 3    * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu)
 4    * All rights reserved.
 5    *
 6    * Redistribution and use in source and binary forms, with or without
 7    * modification, are permitted provided that the following conditions are met:
 8    * * Redistributions of source code must retain the above copyright
 9    * notice, this list of conditions and the following disclaimer.
 10    * * Redistributions in binary form must reproduce the above copyright
 11    * notice, this list of conditions and the following disclaimer in the
 12    * documentation and/or other materials provided with the distribution.
 13    * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
 14    * names of its contributors may be used to endorse or promote products
 15    * derived from this software without specific prior written permission.
 16    *
 17    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18    * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19    * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20    * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 21    * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 22    * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 23    * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 24    * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 25    * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 26    * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27    * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28    *
 29    * This software is Open Source Initiative approved Open Source Software.
 30    * Open Source Initative Approved is a trademark of the Open Source Initiative.
 31    *
 32    * This file is part of DrJava. Download the current version of this project
 33    * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
 34    *
 35    * END_COPYRIGHT_BLOCK*/
 36   
 37    package edu.rice.cs.drjava.model;
 38   
 39    import java.util.List;
 40    import java.util.ArrayList;
 41    import java.io.File;
 42   
 43    import edu.rice.cs.plt.reflect.ReflectUtil;
 44    import edu.rice.cs.plt.reflect.JavaVersion;
 45    import edu.rice.cs.plt.reflect.JavaVersion.FullVersion;
 46    import edu.rice.cs.plt.reflect.ReflectException;
 47    import edu.rice.cs.plt.io.IOUtil;
 48    import edu.rice.cs.plt.collect.CollectUtil;
 49    import edu.rice.cs.plt.object.ObjectUtil;
 50   
 51    import edu.rice.cs.drjava.model.compiler.CompilerInterface;
 52    import edu.rice.cs.drjava.model.compiler.NoCompilerAvailable;
 53    import edu.rice.cs.drjava.model.debug.Debugger;
 54    import edu.rice.cs.drjava.model.debug.NoDebuggerAvailable;
 55    import edu.rice.cs.drjava.model.javadoc.JavadocModel;
 56    import edu.rice.cs.drjava.model.javadoc.NoJavadocAvailable;
 57    import edu.rice.cs.drjava.model.javadoc.DefaultJavadocModel;
 58    import edu.rice.cs.drjava.model.JDKDescriptor;
 59   
 60    /**
 61    * Provides dynamic access to the interface of a JDK's tools.jar classes. This level of indirection
 62    * eliminates the need to have specific tools.jar classes available statically (and the resulting need
 63    * to reset the JVM if they are not), and makes it possible to interface with multiple tools.jar
 64    * libraries simultaneously.
 65    */
 66    public class JDKToolsLibrary {
 67   
 68    private final FullVersion _version;
 69    private final CompilerInterface _compiler;
 70    private final Debugger _debugger;
 71    private final JavadocModel _javadoc;
 72    private final JDKDescriptor _jdkDescriptor; // JDKDescriptor.NONE if none
 73   
 74  2983 protected JDKToolsLibrary(FullVersion version, JDKDescriptor jdkDescriptor,
 75    CompilerInterface compiler, Debugger debugger,
 76    JavadocModel javadoc) {
 77  2983 assert jdkDescriptor != null;
 78  2983 _version = version;
 79  2983 _compiler = compiler;
 80  2983 _debugger = debugger;
 81  2983 _javadoc = javadoc;
 82  2983 _jdkDescriptor = jdkDescriptor;
 83    }
 84   
 85  8949 public FullVersion version() { return _version; }
 86   
 87  3454 public JDKDescriptor jdkDescriptor() { return _jdkDescriptor; }
 88   
 89  1570 public CompilerInterface compiler() { return _compiler; }
 90   
 91  628 public Debugger debugger() { return _debugger; }
 92   
 93  628 public JavadocModel javadoc() { return _javadoc; }
 94   
 95  2983 public boolean isValid() {
 96  2983 return _compiler.isAvailable() || _debugger.isAvailable() || _javadoc.isAvailable();
 97    }
 98   
 99  5181 public String toString() { return _jdkDescriptor.getDescription(_version); }
 100   
 101  2983 public static String adapterForCompiler(JavaVersion.FullVersion version) {
 102  2983 switch (version.majorVersion()) {
 103  314 case JAVA_7: return "edu.rice.cs.drjava.model.compiler.Javac170Compiler";
 104  1413 case JAVA_6: {
 105  1413 switch (version.vendor()) {
 106  0 case OPENJDK: return "edu.rice.cs.drjava.model.compiler.Javac160OpenJDKCompiler";
 107  0 case UNKNOWN: return null;
 108  1413 default: return "edu.rice.cs.drjava.model.compiler.Javac160Compiler";
 109    }
 110    }
 111  785 case JAVA_5: return "edu.rice.cs.drjava.model.compiler.Javac150Compiler";
 112  471 default: return null;
 113    }
 114    }
 115   
 116  2983 public static String adapterForDebugger(JavaVersion.FullVersion version) {
 117  2983 switch (version.majorVersion()) {
 118  314 case JAVA_7:
 119  1413 case JAVA_6:
 120  2512 case JAVA_5: return "edu.rice.cs.drjava.model.debug.jpda.JPDADebugger";
 121  471 default: return null;
 122    }
 123    }
 124   
 125  157 protected static CompilerInterface getCompilerInterface(String className, FullVersion version) {
 126  157 if (className != null) {
 127  157 List<File> bootClassPath = null;
 128  157 String bootProp = System.getProperty("sun.boot.class.path");
 129  157 if (bootProp != null) { bootClassPath = CollectUtil.makeList(IOUtil.parsePath(bootProp)); }
 130  157 File toolsJar = edu.rice.cs.drjava.DrJava.getConfig().getSetting(edu.rice.cs.drjava.config.OptionConstants.JAVAC_LOCATION);
 131  157 try {
 132  157 Class<?>[] sig = { FullVersion.class, String.class, List.class };
 133  157 Object[] args = { version, "the runtime class path", bootClassPath };
 134  157 CompilerInterface attempt = (CompilerInterface) ReflectUtil.loadObject(className, sig, args);
 135  157 msg(" attempt = "+attempt+", isAvailable() = "+attempt.isAvailable());
 136  157 if (attempt.isAvailable()) { return attempt; }
 137    }
 138    catch (ReflectException e) { /* can't load */ }
 139    catch (LinkageError e) { /* can't load */ }
 140    }
 141  0 return NoCompilerAvailable.ONLY;
 142    }
 143   
 144    /** Create a JDKToolsLibrary from the runtime class path (or, more accurately, from the class
 145    * loader that loaded this class.
 146    */
 147  157 public static Iterable<JDKToolsLibrary> makeFromRuntime(GlobalModel model) {
 148  157 FullVersion version = JavaVersion.CURRENT_FULL;
 149   
 150  157 String compilerAdapter = adapterForCompiler(version);
 151  157 msg("makeFromRuntime: compilerAdapter="+compilerAdapter);
 152  157 CompilerInterface compiler = getCompilerInterface(compilerAdapter, version);
 153  157 msg(" compiler="+compiler.getClass().getName());
 154   
 155  157 Debugger debugger = NoDebuggerAvailable.ONLY;
 156  157 String debuggerAdapter = adapterForDebugger(version);
 157  157 if (debuggerAdapter != null) {
 158  157 try {
 159  157 msg(" loading debugger: "+debuggerAdapter);
 160  157 Debugger attempt = (Debugger) ReflectUtil.loadObject(debuggerAdapter, new Class<?>[]{GlobalModel.class}, model);
 161  157 msg(" debugger="+attempt.getClass().getName());
 162  157 if (attempt.isAvailable()) { debugger = attempt; }
 163    }
 164  0 catch (ReflectException e) { msg(" no debugger, ReflectException "+e); /* can't load */ }
 165  0 catch (LinkageError e) { msg(" no debugger, LinkageError "+e); /* can't load */ }
 166    }
 167   
 168  157 JavadocModel javadoc = new NoJavadocAvailable(model);
 169  157 try {
 170  157 Class.forName("com.sun.tools.javadoc.Main");
 171  157 javadoc = new DefaultJavadocModel(model, null, ReflectUtil.SYSTEM_CLASS_PATH);
 172    }
 173    catch (ClassNotFoundException e) { /* can't load */ }
 174    catch (LinkageError e) { /* can't load (probably not necessary, but might as well catch it) */ }
 175   
 176  157 List<JDKToolsLibrary> list = new ArrayList<JDKToolsLibrary>();
 177   
 178  157 if (compiler!=NoCompilerAvailable.ONLY) {
 179    // if we have found a compiler, add it
 180  157 msg(" compiler found");
 181  157 list.add(new JDKToolsLibrary(version, JDKDescriptor.NONE, compiler, debugger, javadoc));
 182    }
 183   
 184  157 msg(" compilers found: "+list.size());
 185   
 186  157 if (list.size()==0) {
 187    // no compiler found, i.e. compiler == NoCompilerAvailable.ONLY
 188  0 msg(" no compilers found, adding NoCompilerAvailable library");
 189  0 list.add(new JDKToolsLibrary(version, JDKDescriptor.NONE, NoCompilerAvailable.ONLY, debugger, javadoc));
 190    }
 191   
 192  157 return list;
 193    }
 194   
 195    public static final java.io.StringWriter LOG_STRINGWRITER = new java.io.StringWriter();
 196    protected static final java.io.PrintWriter LOG_PW = new java.io.PrintWriter(LOG_STRINGWRITER);
 197   
 198  44117 public static void msg(String s) {
 199    // try {
 200    // java.io.PrintWriter pw = new java.io.PrintWriter(new java.io.FileWriter(new File(new File(System.getProperty("user.home")),
 201    // "mintcompiler.txt").getAbsolutePath(),true));
 202    // pw.println(s);
 203  44117 LOG_PW.println(s);
 204    // pw.close();
 205    // }
 206    // catch(java.io.IOException ioe) { }
 207    }
 208    }