in core-it-support/core-it-plugins/maven-it-plugin-class-loader/maven-it-plugin-class-loader/src/main/java/org/apache/maven/plugin/coreit/AbstractLoadMojo.java [70:171]
protected void execute(File outputFile, ClassLoader classLoader) throws MojoExecutionException {
getLog().info("[MAVEN-CORE-IT-LOG] Using class loader " + classLoader);
/*
* NOTE: This one is a little subtle. For all properly implemented class loaders, loading a class/resource from
* a child class loader (with the usual parent-first delegation and no additional search path) will deliver the
* same result as loading directly from the parent class loader. However, Maven or better Plexus Classworlds
* employs custom class loaders which - as history has shown (MNG-1898) - might not always be cleanly
* implemented. To catch potential class loader defects, we check both the results from the original class
* loader and a delegating child class loader for consistency. The key point is that querying the child class
* loader will use a slightly different code path in the original class loader during parent delegation, thereby
* increasing test coverage for its implementation.
*/
ClassLoader childClassLoader = new URLClassLoader(new URL[0], classLoader);
Properties loaderProperties = new Properties();
if (classNames != null && classNames.length() > 0) {
String[] names = classNames.split(",");
for (String name : names) {
getLog().info("[MAVEN-CORE-IT-LOG] Loading class " + name);
// test ClassLoader.loadClass(String) and (indirectly) ClassLoader.loadClass(String, boolean)
try {
Class type = classLoader.loadClass(name);
getLog().info("[MAVEN-CORE-IT-LOG] Loaded class from " + type.getClassLoader());
try {
if (!type.equals(childClassLoader.loadClass(name))) {
throw new ClassNotFoundException(name);
}
} catch (ClassNotFoundException cnfe) {
getLog().error("[MAVEN-CORE-IT-LOG] Detected class loader defect while loading " + name);
throw cnfe;
}
loaderProperties.setProperty(name, "" + type.hashCode());
Method[] methods = type.getDeclaredMethods();
List methodNames = new ArrayList();
for (Method method : methods) {
if (Modifier.isPublic(method.getModifiers())) {
methodNames.add(method.getName());
}
}
Collections.sort(methodNames);
StringBuilder buffer = new StringBuilder(1024);
for (Object methodName : methodNames) {
if (buffer.length() > 0) {
buffer.append(',');
}
buffer.append(methodName);
}
loaderProperties.setProperty(name + ".methods", buffer.toString());
} catch (ClassNotFoundException e) {
// ignore, will be reported by means of missing keys in the properties file
getLog().info("[MAVEN-CORE-IT-LOG] Class not available");
} catch (LinkageError e) {
// ignore, will be reported by means of missing keys in the properties file
getLog().info("[MAVEN-CORE-IT-LOG] Class not linkable", e);
}
}
}
if (resourcePaths != null && resourcePaths.length() > 0) {
String[] paths = resourcePaths.split(",");
for (String path : paths) {
getLog().info("[MAVEN-CORE-IT-LOG] Loading resource " + path);
// test ClassLoader.getResource()
URL url = classLoader.getResource(path);
getLog().info("[MAVEN-CORE-IT-LOG] Loaded resource from " + url);
if (url != null && !url.equals(childClassLoader.getResource(path))) {
getLog().error("[MAVEN-CORE-IT-LOG] Detected class loader defect while getting " + path);
url = null;
}
if (url != null) {
loaderProperties.setProperty(path, url.toString());
}
// test ClassLoader.getResources()
try {
List urls = Collections.list(classLoader.getResources(path));
if (!urls.equals(Collections.list(childClassLoader.getResources(path)))) {
getLog().error("[MAVEN-CORE-IT-LOG] Detected class loader defect while getting " + path);
urls = Collections.EMPTY_LIST;
}
loaderProperties.setProperty(path + ".count", "" + urls.size());
for (int j = 0; j < urls.size(); j++) {
loaderProperties.setProperty(path + "." + j, urls.get(j).toString());
}
} catch (IOException e) {
throw new MojoExecutionException("Resources could not be enumerated: " + path, e);
}
}
}
getLog().info("[MAVEN-CORE-IT-LOG] Creating output file " + outputFile);
PropertiesUtil.write(outputFile, loaderProperties);
getLog().info("[MAVEN-CORE-IT-LOG] Created output file " + outputFile);
}