patterntesting.runtime.monitor.ClassWalker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of patterntesting-rt Show documentation
Show all versions of patterntesting-rt Show documentation
PatternTesting Runtime (patterntesting-rt) is the runtime component for
the PatternTesting framework. It provides the annotations and base classes
for the PatternTesting testing framework (e.g. patterntesting-check,
patterntesting-concurrent or patterntesting-exception) but can be also
used standalone for classpath monitoring or profiling.
It uses AOP and AspectJ to perform this feat.
/*
* $Id: ClassWalker.java,v 1.4 2011/06/29 19:16:43 oboehm Exp $
*
* Copyright (c) 2009 by Oliver Boehm
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* (c)reated 14.04.2009 by oliver ([email protected])
*/
package patterntesting.runtime.monitor;
import java.io.*;
import java.util.*;
import org.apache.commons.io.*;
import org.apache.commons.io.filefilter.*;
import patterntesting.runtime.util.Converter;
/**
* The Class ClassWalker.
*
* @author oliver
* @since 14.04.2009
* @version $Revision: 1.4 $
*/
public final class ClassWalker extends DirectoryWalker {
private final File startDir;
private final int startDirnameLength;
/**
* Instantiates a new class walker.
*
* @param dir the dir
*/
public ClassWalker(final File dir) {
super(getFileFilter(), -1);
this.startDir = dir;
this.startDirnameLength = FilenameUtils
.normalizeNoEndSeparator(dir.getAbsolutePath()).length();
}
/**
* Use all directories which are not hidden and files which end in
* ".class".
*
* @return FileFilter for the walk method
* @{link "http://commons.apache.org/io/api-release/org/apache/commons/io/DirectoryWalker.html"}
*/
private static FileFilter getFileFilter() {
IOFileFilter dirFilter = FileFilterUtils.andFileFilter(FileFilterUtils
.directoryFileFilter(), HiddenFileFilter.VISIBLE);
IOFileFilter fileFilter =
FileFilterUtils.andFileFilter(FileFilterUtils.fileFileFilter(),
FileFilterUtils.suffixFileFilter(".class"));
return FileFilterUtils.orFileFilter(dirFilter, fileFilter);
}
/**
* Walk thru the directories and return all class files as classname,
* e.g. a file java/lang/String.class should be returned as
* "java.lang.String".
*
* @return a collection of classnames
*
* @throws IOException Signals that an I/O exception has occurred.
*
* @{link "http://commons.apache.org/io/api-release/org/apache/commons/io/DirectoryWalker.html"}
*/
public Collection getClasses() throws IOException {
Collection classes = new TreeSet();
walk(startDir, classes);
return classes;
}
/**
* @see org.apache.commons.io.DirectoryWalker#handleFile(java.io.File, int, java.util.Collection)
*/
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
protected void handleFile(final File file, final int depth, final Collection results)
throws IOException {
String absFilename = FilenameUtils.normalize(file.getAbsolutePath());
String relFilename = absFilename.substring(startDirnameLength+1);
String classname = Converter.resourceToClass(relFilename);
results.add(classname);
}
}