com.github.frtu.schema.utils.ClassloaderUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of schema-maven-plugin Show documentation
Show all versions of schema-maven-plugin Show documentation
A maven plugin for schemas (schema2dot - generate a dot notation for Avro schema | pojo2avro -
generation from POJO).
package com.github.frtu.schema.utils;
import org.reflections.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ClassUtils;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* This is the class that help to add all projects class into the current Maven plugin classpath.
*
* @author frtu
* @see ClassloaderUtil.java
* @since 0.3.3
*/
public class ClassloaderUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(ClassloaderUtil.class);
/**
* Put all the Project classloader libs into the current maven classloader.
*
* @return newly created classloader
*/
public static ClassLoader initMavenWithCompileClassloader(List classpathElements) {
try {
Set urls = new HashSet<>();
for (String element : classpathElements) {
URL url = new File(element).toURI().toURL();
urls.add(url);
LOGGER.debug("Add to url list:{}", urls);
}
LOGGER.info("All urls to the Thread.currentThread().setContextClassLoader()");
// ClassLoader currentContextClassLoader = Thread.currentThread().getContextClassLoader();
ClassLoader currentContextClassLoader = ClassUtils.getDefaultClassLoader();
ClassLoader newContextClassLoader = URLClassLoader.newInstance(urls.toArray(new URL[0]), currentContextClassLoader);
// Thread.currentThread().setContextClassLoader(newContextClassLoader);
ClassUtils.overrideThreadContextClassLoader(newContextClassLoader);
return newContextClassLoader;
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
}
}
/**
* Scan the package packageToScan for all the subtypes of subtypesOf
*
* @param packageToScan
* @param subtypesOf
* @param
* @return
* @see Reflections library
*/
public static Set> scanPackageForSubtypesOf(String packageToScan, Class subtypesOf) {
Reflections reflections = new Reflections(packageToScan);
final Set> result = reflections.getSubTypesOf(subtypesOf);
return result;
}
/**
* Scan the package packageToScan for all the subtypes of subtypesOf
*
* @param packageToScan
* @param subtypesOf
* @param
* @return
*/
public static Set> scanPackageForSubtypesOf(ClassLoader classLoader, String packageToScan, String subtypesOf) {
final Class parentClass = (Class) ClassUtils.resolveClassName(subtypesOf, classLoader);
return scanPackageForSubtypesOf(packageToScan, parentClass);
}
}