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

patterntesting.runtime.experimental.ClassTester 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.4.0
Show newest version
/*
 * $Id: ClassTester.java,v 1.4 2010/12/31 14:40:14 oboehm Exp $
 *
 * Copyright (c) 2010 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 27.12.2010 by oliver ([email protected])
 */

package patterntesting.runtime.experimental;

import java.security.AccessController;
import java.util.Set;
import java.security.PrivilegedAction;

/**
 * If you want to test some dependencies to a given class you can use this
 * tester here.
 *
 * @author oliver
 * @since 1.1 (27.12.2010)
 */
public final class ClassTester {

    private final Class clazz;
    private final XrayClassLoader classloader = AccessController.doPrivileged(new PrivilegedAction() {
        public XrayClassLoader run() {
            return new XrayClassLoader();
        }
    });

    /**
     * Instantiates a new class tester.
     *
     * @param classname the classname
     * @throws ClassNotFoundException the class not found exception
     */
    public ClassTester(final String classname) throws ClassNotFoundException {
        this.clazz = this.classloader.loadClass(classname);
        if (clazz == null) {
            throw new ClassNotFoundException(classname);
        }
    }

    /**
     * Gets the class name.
     *
     * @return the class name
     */
    public String getClassName() {
        return this.clazz.getName();
    }

    /**
     * Gets the dependencies of the class name.
     * To get all dependencies we load all declared methods, fields and
     * other stuff.
     *
     * @return the dependencies of
     */
    public Set> getDependencies() {
        this.clazz.getDeclaredAnnotations();
        this.clazz.getDeclaredClasses();
        this.clazz.getDeclaredConstructors();
        this.clazz.getDeclaredFields();
        this.clazz.getDeclaredMethods();
        return classloader.getLoadedClasses();
    }

    /**
     * Checks if the stored class depends on another class.
     * Because the given otherClass is using probably another class loader
     * we compare the class name to decide if it is loaded.
     *
     * @param otherClass the other class
     * @return true, if successful
     */
    public boolean dependsOn(final Class otherClass) {
        return dependsOn(otherClass.getName());
    }

    /**
     * Checks if the stored class depends on another class.
     *
     * @param otherClassname the other classname
     * @return true, if successful
     */
    public boolean dependsOn(final String otherClassname) {
        Set> dependencies = this.getDependencies();
        for (Class cl : dependencies) {
            if (otherClassname.equals(cl.getName())) {
                return true;
            }
        }
        return false;
    }



    /////   STATIC UTILITY METHODS   //////////////////////////////////////////

    /**
     * Gets the dependencies of the given class name.
     *
     * @param classname the classname
     * @return the dependencies of
     * @throws ClassNotFoundException the class not found exception
     */
    public static Set> getDependenciesOf(final String classname) throws ClassNotFoundException {
        return new ClassTester(classname).getDependencies();
    }

    /**
     * Checks if the given class depends on another class.
     *
     * @param classname the classname
     * @param otherClass the other class
     * @return true, if successful
     * @throws ClassNotFoundException the class not found exception
     */
    public static boolean dependsOn(final String classname, final Class otherClass)
            throws ClassNotFoundException {
        return new ClassTester(classname).dependsOn(otherClass);
    }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy