Clover coverage report - Maven Clover report
Coverage timestamp: Sun Mar 18 2007 17:42:32 CET
file stats: LOC: 114   Methods: 8
NCLOC: 89   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
FileSystemClassIterator.java 100% 95.5% 100% 97.1%
coverage coverage
 1    package com.agical.rmock.core.find.iterator;
 2   
 3    import java.io.File;
 4    import java.io.FileFilter;
 5    import java.util.Arrays;
 6    import java.util.Iterator;
 7    import java.util.LinkedList;
 8   
 9    public class FileSystemClassIterator implements Iterator {
 10   
 11    private FileFilter directoriesAndClassFileFilter = new FileFilter() {
 12  780 public boolean accept(File file) {
 13  780 return file.isDirectory() || file.getName().endsWith(".class");
 14    }
 15    };
 16   
 17    private LinkedList stack;
 18    private LinkedList pkg;
 19    private Class next;
 20    private ClassBaseDirExtractor classBaseDirExtractor = new ClassBaseDirExtractor();
 21   
 22  8 public FileSystemClassIterator(Class base) {
 23  8 super();
 24  8 File baseDir = classBaseDirExtractor.getDir(base);
 25  8 stack = new LinkedList();
 26  8 pkg = new LinkedList();
 27  8 stack.addLast(getClassesAndDirectories(baseDir));
 28  8 next = null;
 29    }
 30   
 31  134 private Iterator getClassesAndDirectories(File fromDir) {
 32  134 return Arrays.asList(fromDir.listFiles(directoriesAndClassFileFilter)).iterator();
 33    }
 34   
 35   
 36  638 public boolean hasNext() {
 37  638 if(next != null) {
 38  4 return true;
 39    }
 40    else {
 41  634 File file = findNextFile();
 42  634 if (file == null) {
 43  3 return false;
 44    }
 45   
 46  631 String fileName = file.getName();
 47  631 String className = getPackageName()+fileName.substring(0, fileName.lastIndexOf(".class"));
 48  631 try {
 49  631 next = Thread.currentThread().getContextClassLoader().loadClass(className);
 50    } catch (Exception e) {
 51    // Catch any exception and move on
 52  0 e.printStackTrace();
 53    } catch (NoClassDefFoundError e) {
 54    // Catch this error to prevent (I hope...) the problem with loading classes
 55    // that has transitive dependencies not present in the classpath
 56  0 e.printStackTrace();
 57    }
 58    }
 59  631 return true;
 60    }
 61   
 62  631 private String getPackageName() {
 63  631 StringBuffer packageName = new StringBuffer();
 64  631 for (Iterator iter = pkg.iterator(); iter.hasNext();) {
 65  3459 String element = (String) iter.next();
 66  3459 packageName.append(element).append('.');
 67   
 68    }
 69  631 return packageName.toString();
 70    }
 71   
 72   
 73    /**
 74    * Looks ahead for the next available file
 75    * @return
 76    */
 77  634 private File findNextFile() {
 78  634 while (!stack.isEmpty()) {
 79  883 Iterator iterator = (Iterator)stack.getLast();
 80  883 File file = null;
 81  883 while (iterator.hasNext()) {
 82  757 file = (File) iterator.next();
 83  757 if (file.isDirectory()) {
 84  126 stack.addLast(getClassesAndDirectories(file));
 85  126 pkg.add(file.getName());
 86  126 break;
 87    }
 88    else {
 89  631 return file;
 90    }
 91    }
 92  252 if (iterator == stack.getLast()) {
 93  126 stack.removeLast();
 94  126 if(!pkg.isEmpty()) {
 95  123 pkg.removeLast();
 96    }
 97    }
 98    }
 99  3 return null;
 100    }
 101   
 102   
 103  630 public Object next() {
 104  630 Class ret = next;
 105  630 next = null;
 106  630 return ret;
 107    }
 108   
 109  1 public void remove() {
 110  1 throw new UnsupportedOperationException("The remove operation is not supported on the FileSystemClassIterator");
 111   
 112    }
 113   
 114    }