Clover coverage report - DrJava Test Coverage (drjava-20120304-r5456)
Coverage timestamp: Sun Mar 4 2012 03:13:23 CST
file stats: LOC: 334   Methods: 29
NCLOC: 176   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DrJavaFileUtils.java 9.1% 32.1% 48.3% 31.8%
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.io.File;
 40    import java.util.Set;
 41    import java.util.HashSet;
 42    import edu.rice.cs.drjava.DrJava;
 43    import edu.rice.cs.drjava.config.OptionConstants;
 44    import edu.rice.cs.plt.io.IOUtil;
 45   
 46    /** Some common methods for determining Java source files, language level files, etc.
 47    * @version $Id: DrJavaFileUtils.java 5395 2010-09-21 15:26:15Z mgricken $
 48    */
 49    public class DrJavaFileUtils {
 50    /** Return the set of source file extensions that this compiler supports.
 51    * @return the set of source file extensions that this compiler supports. */
 52  0 public static Set<String> getSourceFileExtensions() {
 53  0 HashSet<String> extensions = new HashSet<String>();
 54  0 extensions.add(OptionConstants.JAVA_FILE_EXTENSION);
 55  0 extensions.add(OptionConstants.DJ_FILE_EXTENSION);
 56  0 extensions.add(OptionConstants.OLD_DJ0_FILE_EXTENSION);
 57  0 extensions.add(OptionConstants.OLD_DJ1_FILE_EXTENSION);
 58  0 extensions.add(OptionConstants.OLD_DJ2_FILE_EXTENSION);
 59  0 return extensions;
 60    }
 61   
 62    /** Return the suggested file extension that will be appended to a file without extension.
 63    * @return the suggested file extension */
 64  0 public static String getSuggestedFileExtension() {
 65  0 return OptionConstants.LANGUAGE_LEVEL_EXTENSIONS[DrJava.getConfig().getSetting(OptionConstants.LANGUAGE_LEVEL)];
 66    }
 67   
 68   
 69    /** .java --> true
 70    * .dj --> true
 71    * .dj0 --> true
 72    * .dj1 --> true
 73    * .dj2 --> true
 74    * otherwise false
 75    * @return true if the file is a Java or language level file. */
 76  923 public static boolean isSourceFile(String fileName) {
 77  923 return fileName.endsWith(OptionConstants.JAVA_FILE_EXTENSION)
 78    || fileName.endsWith(OptionConstants.DJ_FILE_EXTENSION)
 79    || fileName.endsWith(OptionConstants.OLD_DJ0_FILE_EXTENSION)
 80    || fileName.endsWith(OptionConstants.OLD_DJ1_FILE_EXTENSION)
 81    || fileName.endsWith(OptionConstants.OLD_DJ2_FILE_EXTENSION);
 82    }
 83   
 84    /** @return true if the file is a Java or language level file. */
 85  103 public static boolean isSourceFile(File f) {
 86  103 File canonicalFile = IOUtil.attemptCanonicalFile(f);
 87  103 String fileName = canonicalFile.getPath();
 88  103 return isSourceFile(fileName);
 89    }
 90   
 91    /** .dj --> true
 92    * .dj0 --> true
 93    * .dj1 --> true
 94    * .dj2 --> true
 95    * otherwise false
 96    * @return true if the file is a language level file. */
 97  5996 public static boolean isLLFile(String fileName) {
 98  5999 return fileName.endsWith(OptionConstants.DJ_FILE_EXTENSION)
 99    || fileName.endsWith(OptionConstants.OLD_DJ0_FILE_EXTENSION)
 100    || fileName.endsWith(OptionConstants.OLD_DJ1_FILE_EXTENSION)
 101    || fileName.endsWith(OptionConstants.OLD_DJ2_FILE_EXTENSION);
 102    }
 103   
 104    /** @return true if the file is a language level file. */
 105  13 public static boolean isLLFile(File f) {
 106  13 File canonicalFile = IOUtil.attemptCanonicalFile(f);
 107  13 String fileName = canonicalFile.getPath();
 108  13 return isLLFile(fileName);
 109    }
 110   
 111    /** .dj0 --> true
 112    * .dj1 --> true
 113    * .dj2 --> true
 114    * otherwise false
 115    * @return true if the file is an old language level file. */
 116  9 public static boolean isOldLLFile(String fileName) {
 117  9 return fileName.endsWith(OptionConstants.OLD_DJ0_FILE_EXTENSION)
 118    || fileName.endsWith(OptionConstants.OLD_DJ1_FILE_EXTENSION)
 119    || fileName.endsWith(OptionConstants.OLD_DJ2_FILE_EXTENSION);
 120    }
 121   
 122    /** @return true if the file is an old language level file. */
 123  9 public static boolean isOldLLFile(File f) {
 124  9 File canonicalFile = IOUtil.attemptCanonicalFile(f);
 125  9 String fileName = canonicalFile.getPath();
 126  9 return isOldLLFile(fileName);
 127    }
 128   
 129    /** .pjt --> true
 130    * otherwise false
 131    * @return true if the file is an old project file. */
 132  0 public static boolean isOldProjectFile(String fileName) {
 133  0 return fileName.endsWith(OptionConstants.OLD_PROJECT_FILE_EXTENSION);
 134    }
 135   
 136    /** @return true if the file is an old project file. */
 137  0 public static boolean isOldProjectFile(File f) {
 138  0 File canonicalFile = IOUtil.attemptCanonicalFile(f);
 139  0 String fileName = canonicalFile.getPath();
 140  0 return isOldProjectFile(fileName);
 141    }
 142   
 143    /** .pjt --> true
 144    * .drjava --> true
 145    * .xml --> true
 146    * otherwise false
 147    * @return true if the file is a project file. */
 148  0 public static boolean isProjectFile(String fileName) {
 149  0 return fileName.endsWith(OptionConstants.PROJECT_FILE_EXTENSION)
 150    || fileName.endsWith(OptionConstants.PROJECT_FILE_EXTENSION2)
 151    || fileName.endsWith(OptionConstants.OLD_PROJECT_FILE_EXTENSION);
 152    }
 153   
 154    /** @return true if the file is a project file. */
 155  0 public static boolean isProjectFile(File f) {
 156  0 File canonicalFile = IOUtil.attemptCanonicalFile(f);
 157  0 String fileName = canonicalFile.getPath();
 158  0 return isProjectFile(fileName);
 159    }
 160   
 161    /** A.dj --> A.java
 162    * A.dj0 --> A.java
 163    * A.dj1 --> A.java
 164    * A.dj2 --> A.java
 165    * otherwise unchanged
 166    * @return matching Java file for a language level file. */
 167  0 public static String getJavaForLLFile(String fileName) {
 168  0 if (fileName.endsWith(OptionConstants.DJ_FILE_EXTENSION)) {
 169  0 return fileName.substring(0, fileName.lastIndexOf(OptionConstants.DJ_FILE_EXTENSION))
 170    + OptionConstants.JAVA_FILE_EXTENSION;
 171    }
 172  0 else if (fileName.endsWith(OptionConstants.OLD_DJ0_FILE_EXTENSION)) {
 173  0 return fileName.substring(0, fileName.lastIndexOf(OptionConstants.OLD_DJ0_FILE_EXTENSION))
 174    + OptionConstants.JAVA_FILE_EXTENSION;
 175    }
 176  0 else if (fileName.endsWith(OptionConstants.OLD_DJ1_FILE_EXTENSION)) {
 177  0 return fileName.substring(0, fileName.lastIndexOf(OptionConstants.OLD_DJ1_FILE_EXTENSION))
 178    + OptionConstants.JAVA_FILE_EXTENSION;
 179    }
 180  0 else if (fileName.endsWith(OptionConstants.OLD_DJ2_FILE_EXTENSION)) {
 181  0 return fileName.substring(0, fileName.lastIndexOf(OptionConstants.OLD_DJ2_FILE_EXTENSION))
 182    + OptionConstants.JAVA_FILE_EXTENSION;
 183    }
 184  0 else return fileName;
 185    }
 186   
 187    /** @return matching Java file for a language level file. */
 188  0 public static File getJavaForLLFile(File f) {
 189  0 File canonicalFile = IOUtil.attemptCanonicalFile(f);
 190  0 String fileName = canonicalFile.getPath();
 191  0 return new File(getJavaForLLFile(fileName));
 192    }
 193   
 194    /** A.java --> A.dj
 195    * otherwise unchanged
 196    * @return matching .dj file for a .java file. */
 197  0 public static File getDJForJavaFile(File f) {
 198  0 return getFileWithDifferentExt(f, OptionConstants.JAVA_FILE_EXTENSION, OptionConstants.DJ_FILE_EXTENSION);
 199    }
 200   
 201    /** A.java --> A.dj0
 202    * otherwise unchanged
 203    * @return matching .dj0 file for a .java file. */
 204  0 public static File getDJ0ForJavaFile(File f) {
 205  0 return getFileWithDifferentExt(f, OptionConstants.JAVA_FILE_EXTENSION, OptionConstants.OLD_DJ0_FILE_EXTENSION);
 206    }
 207   
 208    /** A.java --> A.dj1
 209    * otherwise unchanged
 210    * @return matching .dj1 file for a .java file. */
 211  0 public static File getDJ1ForJavaFile(File f) {
 212  0 return getFileWithDifferentExt(f, OptionConstants.JAVA_FILE_EXTENSION, OptionConstants.OLD_DJ1_FILE_EXTENSION);
 213    }
 214   
 215    /** A.java --> A.dj2
 216    * otherwise unchanged
 217    * @return matching .dj2 file for a .java file. */
 218  1 public static File getDJ2ForJavaFile(File f) {
 219  1 return getFileWithDifferentExt(f, OptionConstants.JAVA_FILE_EXTENSION, OptionConstants.OLD_DJ2_FILE_EXTENSION);
 220    }
 221   
 222    /** A.java --> A.dj
 223    * otherwise unchanged
 224    * @return matching .dj file for a .java file. */
 225  312 public static String getDJForJavaFile(String f) {
 226  312 return getFileWithDifferentExt(f, OptionConstants.JAVA_FILE_EXTENSION, OptionConstants.DJ_FILE_EXTENSION);
 227    }
 228   
 229    /** A.java --> A.dj0
 230    * otherwise unchanged
 231    * @return matching .dj0 file for a .java file. */
 232  312 public static String getDJ0ForJavaFile(String f) {
 233  312 return getFileWithDifferentExt(f, OptionConstants.JAVA_FILE_EXTENSION, OptionConstants.OLD_DJ0_FILE_EXTENSION);
 234    }
 235   
 236    /** A.java --> A.dj1
 237    * otherwise unchanged
 238    * @return matching .dj1 file for a .java file. */
 239  312 public static String getDJ1ForJavaFile(String f) {
 240  312 return getFileWithDifferentExt(f, OptionConstants.JAVA_FILE_EXTENSION, OptionConstants.OLD_DJ1_FILE_EXTENSION);
 241    }
 242   
 243    /** A.java --> A.dj2
 244    * otherwise unchanged
 245    * @return matching .dj2 file for a .java file. */
 246  312 public static String getDJ2ForJavaFile(String f) {
 247  312 return getFileWithDifferentExt(f, OptionConstants.JAVA_FILE_EXTENSION, OptionConstants.OLD_DJ2_FILE_EXTENSION);
 248    }
 249   
 250    /** A.dj0 -> A.dj
 251    * A.dj1 -> A.dj
 252    * A.dj2 -> A.java
 253    * otherwise unchanged
 254    * @return new language level file matching an old language level file. */
 255  0 public static String getNewLLForOldLLFile(String fileName) {
 256  0 if (fileName.endsWith(OptionConstants.OLD_DJ0_FILE_EXTENSION)) {
 257  0 return fileName.substring(0, fileName.lastIndexOf(OptionConstants.OLD_DJ0_FILE_EXTENSION))
 258    + OptionConstants.DJ_FILE_EXTENSION;
 259    }
 260  0 else if (fileName.endsWith(OptionConstants.OLD_DJ1_FILE_EXTENSION)) {
 261  0 return fileName.substring(0, fileName.lastIndexOf(OptionConstants.OLD_DJ1_FILE_EXTENSION))
 262    + OptionConstants.DJ_FILE_EXTENSION;
 263    }
 264  0 else if (fileName.endsWith(OptionConstants.OLD_DJ2_FILE_EXTENSION)) {
 265  0 return fileName.substring(0, fileName.lastIndexOf(OptionConstants.OLD_DJ2_FILE_EXTENSION))
 266    + OptionConstants.JAVA_FILE_EXTENSION;
 267    }
 268  0 else return fileName;
 269    }
 270   
 271    /** @return new language level file matching an old language level file. */
 272  0 public static File getNewLLForOldLLFile(File f) {
 273  0 File canonicalFile = IOUtil.attemptCanonicalFile(f);
 274  0 String fileName = canonicalFile.getPath();
 275  0 return new File(getNewLLForOldLLFile(fileName));
 276    }
 277   
 278    /** getFileWithDifferentExt("A.java", ".java", ".dj") --> "A.dj"
 279    * @return matching file with extension dest for a file with extension source. */
 280  1249 public static String getFileWithDifferentExt(String fileName, String source, String dest) {
 281  1249 if (fileName.endsWith(source)) {
 282  1249 return fileName.substring(0, fileName.lastIndexOf(source)) + dest;
 283    }
 284  0 else return fileName;
 285    }
 286   
 287    /** getFileWithDifferentExt(new File("A.java"), ".java", ".dj") ~= new File("A.dj")
 288    * @return matching file with extension dest for a file with extension source. */
 289  1 public static File getFileWithDifferentExt(File f, String source, String dest) {
 290  1 File canonicalFile = IOUtil.attemptCanonicalFile(f);
 291  1 String fileName = canonicalFile.getPath();
 292  1 return new File(getFileWithDifferentExt(fileName, source, dest));
 293    }
 294   
 295   
 296    /** Returns the relative directory (from the source root) that the source file with this qualifed name will be in,
 297    * given its package. Returns the empty string for classes without packages.
 298    * @param className The fully qualified class name
 299    */
 300  0 public static String getPackageDir(String className) {
 301    // Only keep up to the last dot
 302  0 int lastDotIndex = className.lastIndexOf(".");
 303  0 if (lastDotIndex == -1) {
 304    // No dots, so no package
 305  0 return "";
 306    }
 307    else {
 308  0 String packageName = className.substring(0, lastDotIndex);
 309  0 packageName = packageName.replace('.', File.separatorChar);
 310  0 return packageName + File.separatorChar;
 311    }
 312    }
 313   
 314    /** @return the file without the extension; the dot is removed too. */
 315  0 public static String removeExtension(String fileName) {
 316  0 int lastDotIndex = fileName.lastIndexOf(".");
 317  0 if (lastDotIndex == -1) {
 318    // No dots, so no package
 319  0 return fileName;
 320    }
 321  0 return fileName.substring(0, lastDotIndex);
 322    }
 323   
 324    /** @return the extension, including the dot. */
 325  1 public static String getExtension(String fileName) {
 326  1 int lastDotIndex = fileName.lastIndexOf(".");
 327  1 if (lastDotIndex == -1) {
 328    // No dots, so no package
 329  0 return "";
 330    }
 331  1 return fileName.substring(lastDotIndex);
 332    }
 333   
 334    }