All Downloads are FREE. Search and download functionalities are using the official Maven repository.

patterntesting.runtime.monitor.ClassWalker Maven / Gradle / Ivy

Go to download

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.

The newest version!
/*
 * $Id: ClassWalker.java,v 1.5 2009/12/28 10:07:23 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.5 $
 */
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(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")
    @Override
    protected void handleFile(File file, int depth, Collection results)
            throws IOException {
        String absFilename = FilenameUtils.normalize(file.getAbsolutePath());
        String relFilename = absFilename.substring(startDirnameLength+1);
        String classname = Converter.resourceToClass(relFilename);
        results.add(classname);
    }

}

/**
 * $Log: ClassWalker.java,v $
 * Revision 1.5  2009/12/28 10:07:23  oboehm
 * missing Javadocs completed
 *
 * Revision 1.4  2009/12/19 22:34:09  oboehm
 * trailing spaces removed
 *
 * Revision 1.3  2009/09/25 14:49:43  oboehm
 * javadocs completed with the help of JAutodoc
 *
 * Revision 1.2  2009/09/18 13:54:52  oboehm
 * javadoc warnings fixed
 *
 * Revision 1.1  2009/04/15 19:25:14  oboehm
 * a new attribute "classpathClasses" added for JMX
 *
 * $Source: /cvsroot/patterntesting/PatternTesting08/patterntesting-rt/src/main/java/patterntesting/runtime/monitor/ClassWalker.java,v $
 */




© 2015 - 2024 Weber Informatics LLC | Privacy Policy