001/*
002 * Copyright 2012 Christopher Pheby
003 * Licensed under the Apache License, Version 2.0 (the "License");
004 * you may not use this file except in compliance with the License.
005 * You may obtain a copy of the License at
006 * http://www.apache.org/licenses/LICENSE-2.0
007 * Unless required by applicable law or agreed to in writing, software
008 * distributed under the License is distributed on an "AS IS" BASIS,
009 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010 * See the License for the specific language governing permissions and
011 * limitations under the License.
012 */
013package org.jadira.scanner.core.utils.reflection;
014
015/**
016 * Utility methods related to {@link ClassLoader} resolution and the like
017 */
018public final class ClassLoaderUtils {
019
020        private ClassLoaderUtils() {
021        }
022
023        /**
024         * Gets {@link ClassLoader} for the current thread
025         * @param classLoaders ClassLoaders to be used if specified
026         * @return The ClassLoaders, either those specified, or discovered
027         * ClassLoaders for the current thread
028         */
029        public static ClassLoader[] getClassLoaders(ClassLoader... classLoaders) {
030
031                if (classLoaders != null && classLoaders.length > 0) {
032                        return classLoaders;
033                }
034
035                final ClassLoader ctxClassLoader = Thread.currentThread()
036                                .getContextClassLoader();
037                final ClassLoader staticClassLoader = ClassLoaderUtils.class
038                                .getClassLoader();
039
040                if (ctxClassLoader == null) {
041                        if (staticClassLoader == null) {
042                                return new ClassLoader[] {};
043                        } else {
044                                return new ClassLoader[] { staticClassLoader };
045                        }
046                } else {
047                        if (staticClassLoader == null) {
048                                return new ClassLoader[] { ctxClassLoader };
049                        } else {
050                                return new ClassLoader[] { ctxClassLoader, staticClassLoader };
051                        }
052                }
053        }
054
055        /**
056         * Attempts to instantiate the named class, using each of the specified
057         * ClassLoaders in turn or Class.forName if these do not work
058         * @param name The class name
059         * @param classLoaders ClassLoaders to be used
060         * @return The Class instance
061         * @throws ClassNotFoundException If the class cannot be found
062         */
063        public static Class<?> classForName(String name,
064                        ClassLoader... classLoaders) throws ClassNotFoundException {
065
066                ClassLoader[] cls = getClassLoaders(classLoaders);
067
068                for (ClassLoader cl : cls) {
069                        try {
070                                if (cl != null) {
071                                        return cl.loadClass(name);
072                                }
073                        } catch (ClassNotFoundException e) {
074                                // Ignore
075                        }
076                }
077                // Try using Class.forName()
078                return Class.forName(name);
079        }
080}