Clover coverage report - DrJava Test Coverage (drjava-20110828-r5448)
Coverage timestamp: Sun Aug 28 2011 03:13:33 CDT
file stats: LOC: 167   Methods: 10
NCLOC: 70   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ClassAndInterfaceFinder.java 66.7% 74% 90% 73.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.*;
 40   
 41    /** Class with getClassName method for finding the name of the first class or
 42    * interface defined in a file */
 43   
 44    public class ClassAndInterfaceFinder {
 45   
 46    private StreamTokenizer tokenizer;
 47   
 48    /* constructor to support unit testing */
 49  4 public ClassAndInterfaceFinder(Reader r) {
 50  4 initialize(r);
 51    }
 52   
 53    /* normal constructor */
 54  0 public ClassAndInterfaceFinder(File f) {
 55  0 Reader r = null;
 56  0 try {
 57  0 try { r = new FileReader(f); }
 58    catch(FileNotFoundException e) { /* create a Reader for an "empty" file */
 59  0 r = new StringReader("");
 60    }
 61  0 initialize(r);
 62    }
 63    finally {
 64  0 try {
 65  0 if (r != null) r.close();
 66    }
 67    catch(IOException ioe) { /* ignore exception on close */ }
 68    }
 69    }
 70   
 71  4 private void initialize(Reader r) {
 72   
 73  4 tokenizer = new StreamTokenizer(r);
 74  4 tokenizer.slashSlashComments(true);
 75  4 tokenizer.slashStarComments(true);
 76  4 tokenizer.lowerCaseMode(false);
 77  4 tokenizer.wordChars('_','_');
 78  4 tokenizer.wordChars('.','.');
 79    }
 80   
 81    /** Finds the the name of the first class or interface defined in this file.
 82    * @return the String containing this name or "" if no such class or interface
 83    * is found.
 84    */
 85  3 public String getClassOrInterfaceName() { return getName(true); }
 86   
 87    /** Finds the the name of the first class (excluding interfaces) defined in this file.
 88    * @return the String containing this name or "" if no such class or interface
 89    * is found.
 90    */
 91  1 public String getClassName() { return getName(false); }
 92   
 93    /** Finds the the name of the first class or interface in this file, respecting the
 94    * value of the interfaceOK flag.
 95    * I hate flags but did not see a simpler way to avoid duplicated code.
 96    * This method has package (rather than private) visibility for testing purposes.
 97    */
 98  4 String getName(boolean interfaceOK) {
 99  4 try {
 100  4 String package_name = "";
 101  4 int tokenType;
 102   
 103    // find the "class"/"interface" or "package" keyword (may encounter EOF)
 104  4 do {
 105  17 tokenType = tokenizer.nextToken();
 106  17 } while(! isClassOrInterfaceWord(tokenType,interfaceOK) && ! isPackageWord(tokenType));
 107   
 108  1 if (isEOF(tokenType)) return "";
 109   
 110    /* Save opening keyword */
 111  3 String keyword = tokenizer.sval;
 112   
 113    // find the name of the class or package (may encounter EOF)
 114  3 do {
 115  3 tokenType = tokenizer.nextToken();
 116  3 } while (! isWord(tokenType));
 117   
 118  0 if (isEOF(tokenType)) return "";
 119   
 120  1 if (keyword.equals("class")) return tokenizer.sval; // a class defined without a package
 121   
 122  1 if (interfaceOK && keyword.equals("interface")) return tokenizer.sval; // an interface without a package
 123   
 124  1 if (keyword.equals("package")) package_name = tokenizer.sval;
 125   
 126    // find the "class" keyword
 127  2 do { tokenType = tokenizer.nextToken(); } while (! isClassOrInterfaceWord(tokenType, interfaceOK));
 128   
 129    // find the name of the class or interface
 130  1 do { tokenType = tokenizer.nextToken(); } while (! isWord(tokenType));
 131   
 132  0 if (tokenType == StreamTokenizer.TT_EOF) return "";
 133   
 134  1 if (package_name.length() > 0) return package_name + "." + tokenizer.sval;
 135   
 136  0 return tokenizer.sval;
 137   
 138    } catch(IOException e) {
 139  0 return "";
 140    }
 141    }
 142   
 143    /** returns true iff the token is a word (as defined by StreamTokenizer)
 144    */
 145  4 private static boolean isWord(int tt) { return tt == StreamTokenizer.TT_WORD || isEOF(tt); }
 146   
 147  26 private static boolean isEOF(int tt) { return tt == StreamTokenizer.TT_EOF; }
 148   
 149   
 150    /** returns true iff the token is "class" or we're at the end of the file
 151    */
 152  19 private boolean isClassOrInterfaceWord(int tt, boolean interfaceOK) {
 153  19 return isEOF(tt) ||
 154    (tt == StreamTokenizer.TT_WORD && tokenizer.sval.equals("class")) ||
 155    (tt == StreamTokenizer.TT_WORD && interfaceOK && tokenizer.sval.equals("interface"));
 156    }
 157   
 158    /** returns true iff the token is "package" or we're at the end of the file
 159    */
 160  14 private boolean isPackageWord(int tt) {
 161  14 return (tt == StreamTokenizer.TT_WORD && tokenizer.sval.equals("package") ||
 162    tt == StreamTokenizer.TT_EOF);
 163    }
 164    }
 165   
 166   
 167