new操作时调用当前线程的类加载器,还是调用方的类加载器 (四)完美证明用例
A cl1
{
new thread().setClassLoader(cl2)
new cl3 -> new B
b.func {
C c = new C
}
}
三个类加载器把C纳入可见范围内
1 A C
2 C
3 B C

/Users/joyce/work/MyTest/ClWhichClassLoader/ClWhich/src/main/java/com/demolc/TestMain.java
ClCaller.A
package com.demolc;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;
/**
* Created by joyce on 2022/6/29.
*/
public class A {
public void func() throws Exception {
String urlThread = "file:/Users/joyce/work/MyTest/ClWhichClassLoader/ClThread/target/ClThread-1.0.0-jar-with-dependencies.jar";
URLClassLoader clThread = new URLClassLoader(new URL[]{new URL(urlThread)});
Thread thread = new Thread(new MyThread());
thread.setContextClassLoader(clThread);
thread.start();
}
private static class MyThread implements Runnable {
@Override
public void run() {
String urlLast = "file:/Users/joyce/work/MyTest/ClWhichClassLoader/ClLast/target/ClLast-1.0.0-jar-with-dependencies.jar";
try {
URLClassLoader clLast = new URLClassLoader(new URL[]{new URL(urlLast)});
Class cB = clLast.loadClass("com.demolc.B");
Method method = cB.getMethod("func");
method.invoke(cB.newInstance());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
主
package com.demolc;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
/**
* 证明C c=new C调用this.getClass().getClassLoader
* 前提:让com.demolc.C对3个类加载器可见
* Chttps://www.cnblogs.com/silyvin/articles/16423447.html
*/
public class TestMain {
public static void main(String[] f) throws Exception {
String urlCaller = "file:/Users/joyce/work/MyTest/ClWhichClassLoader/ClCaller/target/ClCaller-1.0.0-jar-with-dependencies.jar";
URLClassLoader clCaller = new URLClassLoader(new URL[]{new URL(urlCaller)});
Class cA = clCaller.loadClass("com.demolc.A");
Method method = cA.getMethod("func");
method.invoke(cA.newInstance());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sunyuming</groupId>
<artifactId>ClWhich</artifactId>
<version>1.0.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<!--<manifest>-->
<!--<mainClass>agent.AgentClient</mainClass>-->
<!--</manifest>-->
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
浙公网安备 33010602011771号