Source code
Description: source: package: org.apache.commons.lang3, class: ClassLoaderUtils

    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *     https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.lang3;
    019
    020import java.net.URL;
    021import java.net.URLClassLoader;
    022import java.util.Arrays;
    023import java.util.Objects;
    024
    025/**
    026 * Helps work with {@link ClassLoader}.
    027 *
    028 * @since 3.10
    029 */
    030public class ClassLoaderUtils {
    031
    032    private static final URL[] EMPTY_URL_ARRAY = {};
    033
    034    /**
    035     * Gets the system class loader's URLs, if any.
    036     *
    037     * @return the system class loader's URLs, if any.
    038     * @since 3.13.0
    039     */
    040    public static URL[] getSystemURLs() {
    041        return getURLs(ClassLoader.getSystemClassLoader());
    042    }
    043
    044    /**
    045     * Gets the current thread's context class loader's URLs, if any.
    046     *
    047     * @return the current thread's context class loader's URLs, if any.
    048     * @since 3.13.0
    049     */
    050    public static URL[] getThreadURLs() {
    051        return getURLs(Thread.currentThread().getContextClassLoader());
    052    }
    053
    054    private static URL[] getURLs(final ClassLoader cl) {
    055        return cl instanceof URLClassLoader ? ((URLClassLoader) cl).getURLs() : EMPTY_URL_ARRAY;
    056    }
    057
    058    /**
    059     * Converts the given class loader to a String calling {@link #toString(URLClassLoader)}.
    060     *
    061     * @param classLoader to URLClassLoader to convert.
    062     * @return the formatted string.
    063     */
    064    public static String toString(final ClassLoader classLoader) {
    065        if (classLoader instanceof URLClassLoader) {
    066            return toString((URLClassLoader) classLoader);
    067        }
    068        return Objects.toString(classLoader);
    069    }
    070
    071    /**
    072     * Converts the given URLClassLoader to a String in the format {@code "URLClassLoader.toString() + [URL1, URL2, ...]"}.
    073     *
    074     * @param classLoader to URLClassLoader to convert.
    075     * @return the formatted string.
    076     */
    077    public static String toString(final URLClassLoader classLoader) {
    078        return classLoader != null ? classLoader + Arrays.toString(classLoader.getURLs()) : "null";
    079    }
    080
    081    /**
    082     * Make private in 4.0.
    083     *
    084     * @deprecated TODO Make private in 4.0.
    085     */
    086    @Deprecated
    087    public ClassLoaderUtils() {
    088        // empty
    089    }
    090}