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

com.squeakysand.commons.lang.ClassLoaderHelper Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2010-2012 Craig S. Dickson (http://craigsdickson.com)
 *
 * 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 or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.squeakysand.commons.lang;

import java.io.File;
import java.io.IOException;

import java.util.ArrayList;
import java.util.List;

import com.squeakysand.commons.io.FileHelper;

import com.squeakysand.commons.util.CollectionUtils;
import com.squeakysand.commons.util.Predicate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ClassLoaderHelper {

    private static final Logger LOG = LoggerFactory.getLogger(ClassLoaderHelper.class);

    private static final String[] DEFAULT_INCLUDES = new String[] {};

    private static final String[] DEFAULT_EXCLUDES = new String[] {};

    private static class StringListPredicate implements Predicate {

        private String[] includes;
        private String[] excludes;

        public StringListPredicate(String[] includes, String[] excludes) {
            if (includes != null) {
                this.includes = includes;
            } else {
                this.includes = DEFAULT_INCLUDES;
            }
            if (excludes != null) {
                this.excludes = excludes;
            } else {
                this.excludes = DEFAULT_EXCLUDES;
            }
        }

        @Override
        public boolean evaluate(String s) {
            boolean isIncluded = false;
            boolean isNotExcluded = true;
            // if the string is on the include list
            for (String include : includes) {
                if (s.startsWith(include)) {
                    isIncluded = true;
                    break;
                }
            }
            // and is not on the exclude list
            for (String exclude : excludes) {
                if (s.startsWith(exclude)) {
                    isNotExcluded = false;
                    break;
                }
            }
            // then return true
            return isIncluded && isNotExcluded;
        }

    }

    public static List findClassNames(File rootDir) throws IOException {
        LOG.debug("looking for classes under {}", ToStringHelper.toString(rootDir));
        List result = new ArrayList();
        List classFiles = FileHelper.findFilesByExtension(rootDir, "class");
        result = getClassNames(classFiles, rootDir);
        LOG.debug("found classes {}", ToStringHelper.toString(result));
        return result;
    }

    public static List> loadClasses(File rootDir, ClassLoader loader) throws IOException, ClassNotFoundException {
        return loadClasses(rootDir, loader, DEFAULT_INCLUDES, DEFAULT_EXCLUDES);
    }

    public static List> loadClasses(File rootDir, ClassLoader loader, String[] includes, String[] excludes) throws IOException, ClassNotFoundException {
        List classNames = findClassNames(rootDir);
        classNames = CollectionUtils.filter(classNames, new StringListPredicate(includes, excludes));
        return loadClasses(classNames, loader);
    }

    public static List> loadClasses(List classes, ClassLoader loader) throws ClassNotFoundException {
        List> result = new ArrayList>();
        for (String className : classes) {
            LOG.debug("attempting to load class {}", ToStringHelper.toString(className));
            Class klass = loader.loadClass(className);
            result.add(klass);
        }
        return result;
    }

    private static List getClassNames(List files, File baseDirectory) throws IOException {
        List result = new ArrayList();
        for (File file : files) {
            // trim off the base directory
            String classname = file.getPath().substring(baseDirectory.getPath().length() + 1);
            // trim off the .class extension
            classname = classname.substring(0, classname.length() - 6);
            // replace slashes with periods
            classname = classname.replace(SystemProperties.FILE_SEPARATOR, ".");
            result.add(classname);
        }
        return result;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy