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

io.microsphere.util.ClassPathUtils Maven / Gradle / Ivy

The newest version!
/**
 *
 */
package io.microsphere.util;


import javax.annotation.Nonnull;
import java.lang.management.RuntimeMXBean;
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.Set;

import static io.microsphere.collection.SetUtils.of;
import static io.microsphere.constants.SeparatorConstants.PATH_SEPARATOR;
import static io.microsphere.util.ClassLoaderUtils.getClassResource;
import static io.microsphere.util.ClassLoaderUtils.isLoadedClass;
import static io.microsphere.util.StringUtils.split;
import static java.lang.Thread.currentThread;
import static java.lang.management.ManagementFactory.getRuntimeMXBean;
import static java.util.Collections.emptySet;

/**
 * {@link ClassPathUtils}
 *
 * @author Mercy
 * @version 1.0.0
 * @see ClassPathUtils
 * @since 1.0.0
 */
public abstract class ClassPathUtils extends BaseUtils {

    protected static final RuntimeMXBean runtimeMXBean = getRuntimeMXBean();

    private static final Set bootstrapClassPaths = initBootstrapClassPaths();

    private static final Set classPaths = initClassPaths();

    private static Set initBootstrapClassPaths() {
        if (runtimeMXBean.isBootClassPathSupported()) {
            return resolveClassPaths(runtimeMXBean.getBootClassPath());
        }
        return emptySet();
    }

    private static Set initClassPaths() {
        return resolveClassPaths(runtimeMXBean.getClassPath());
    }

    private static Set resolveClassPaths(String classPath) {
        String[] classPathsArray = split(classPath, PATH_SEPARATOR);
        return of(classPathsArray);
    }


    /**
     * Get Bootstrap Class Paths {@link Set}
     *
     * @return If {@link RuntimeMXBean#isBootClassPathSupported()} == false, will return empty set.
     * @version 1.0.0
     * @since 1.0.0
     **/
    @Nonnull
    public static Set getBootstrapClassPaths() {
        return bootstrapClassPaths;
    }

    /**
     * Get {@link #classPaths}
     *
     * @return Class Paths {@link Set}
     * @version 1.0.0
     * @since 1.0.0
     **/
    @Nonnull
    public static Set getClassPaths() {
        return classPaths;
    }

    /**
     * Get Class Location URL from specified class name at runtime
     *
     * @param className class name
     * @return If className associated class is loaded on {@link Thread#getContextClassLoader() Thread
     * context ClassLoader} , return class location URL, or return null
     * @see #getRuntimeClassLocation(Class)
     */
    public static URL getRuntimeClassLocation(String className) {
        ClassLoader classLoader = currentThread().getContextClassLoader();
        URL location = null;
        if (classLoader != null) {
            if (isLoadedClass(classLoader, className)) {
                try {
                    location = getRuntimeClassLocation(classLoader.loadClass(className));
                } catch (ClassNotFoundException ignored) {
                }
            }
        }
        return location;
    }

    /**
     * Get Class Location URL from specified {@link Class} at runtime
     *
     * @param type {@link Class}
     * @return If type is {@link Class#isPrimitive() primitive type}, {@link
     * Class#isArray() array type}, {@link Class#isSynthetic() synthetic type} or {a security
     * manager exists and its checkPermission method doesn't allow getting the ProtectionDomain., return
     * null
     */
    public static URL getRuntimeClassLocation(Class type) {
        ClassLoader classLoader = type.getClassLoader();
        URL location = null;
        if (classLoader != null) { // Non-Bootstrap
            try {
                ProtectionDomain protectionDomain = type.getProtectionDomain();
                CodeSource codeSource = protectionDomain.getCodeSource();
                location = codeSource == null ? null : codeSource.getLocation();
            } catch (SecurityException exception) {

            }
        } else if (!type.isPrimitive() && !type.isArray() && !type.isSynthetic()) { // Bootstrap ClassLoader
            // Class was loaded by Bootstrap ClassLoader
            location = getClassResource(ClassLoader.getSystemClassLoader(), type.getName());
        }
        return location;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy