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.

There is a newer version: 2.5.1
Show newest version
/*
 * $Id: ClassWalker.java,v 1.7 2016/02/17 19:31:51 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
 * @version $Revision: 1.7 $
 * @since 14.04.2009
 */
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
     */
    private static FileFilter getFileFilter() {
        IOFileFilter dirFilter = FileFilterUtils.and(FileFilterUtils.directoryFileFilter(), HiddenFileFilter.VISIBLE);
        IOFileFilter fileFilter = FileFilterUtils.and(FileFilterUtils.fileFileFilter(),
                FileFilterUtils.suffixFileFilter(".class"));
        return FileFilterUtils.or(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".
     * 

* See also * DirectoryWalker. *

* * @return a collection of classnames * @throws IOException Signals that an I/O exception has occurred. */ public Collection getClasses() throws IOException { Collection classes = new TreeSet(); walk(startDir, classes); return classes; } /** * Handle file. * * @param file the file * @param depth the depth * @param results the results * @throws IOException Signals that an I/O exception has occurred. * @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); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy