Skip to content

Instantly share code, notes, and snippets.

@anatawa12
Last active February 5, 2021 02:34
Show Gist options
  • Save anatawa12/1accd8baacd067fa0751a6d6366f7159 to your computer and use it in GitHub Desktop.
Save anatawa12/1accd8baacd067fa0751a6d6366f7159 to your computer and use it in GitHub Desktop.
メソッドの引数などのクラスロードのタイミングの調査
import java.util.Objects;
class MethodClassLoadingTesting {
static {
System.out.println(" MethodClassLoadingTesting loaded");
}
public static void main(String[] args) {
System.out.println("loading MethodHolderClass");
MethodHolderClass.loader();
System.out.println("finish MethodHolderClass");
System.out.println("calling theMethod1 with null");
MethodHolderClass.theMethod1(null);
System.out.println("finish theMethod1 with null");
System.out.println("calling theMethod1 with instance");
MethodHolderClass.theMethod1(new MethodParameterClass1());
System.out.println("finish theMethod1 with instance");
System.out.println("calling theMethod2 with null");
MethodHolderClass.theMethod2(null);
System.out.println("finish theMethod2 with null");
System.out.println("calling theMethod2 with instance");
MethodHolderClass.theMethod2(new MethodParameterClass2());
System.out.println("finish theMethod2 with instance");
}
}
class MethodHolderClass {
static MethodReturningClass1 theMethod1(MethodParameterClass1 param) {
System.out.println(" theMethod1 was called");
MethodInsideUsingClass.loader();
return null;
}
static MethodReturningClass2 theMethod2(MethodParameterClass2 param) {
System.out.println(" theMethod2 was called");
MethodInsideUsingClass.loader();
return null;
}
static void loader() {}
static {
System.out.println(" MethodHolderClass loaded");
}
}
class MethodReturningClass1 {
static {
System.out.println(" MethodReturningClass1 loaded");
}
}
class MethodReturningClass2 {
static {
System.out.println(" MethodReturningClass2 loaded");
}
}
class MethodParameterNullClass1 {
static {
System.out.println(" MethodParameterNullClass1 loaded");
}
}
class MethodParameterNullClass2 {
static {
System.out.println(" MethodParameterNullClass2 loaded");
}
}
class MethodParameterClass1 {
static {
System.out.println(" MethodParameterClass1 loaded");
}
}
class MethodParameterClass2 {
static {
System.out.println(" MethodParameterClass2 loaded");
}
}
class MethodInsideUsingClass {
static void loader() {}
static {
System.out.println(" MethodInsideUsingClass loaded");
}
}
MethodClassLoadingTesting loaded
loading MethodHolderClass
MethodHolderClass loaded
finish MethodHolderClass
calling theMethod1 with null
theMethod1 was called
MethodInsideUsingClass loaded
finish theMethod1 with null
calling theMethod1 with instance
MethodParameterClass1 loaded
theMethod1 was called
finish theMethod1 with instance
calling theMethod2 with null
theMethod2 was called
finish theMethod2 with null
calling theMethod2 with instance
MethodParameterClass2 loaded
theMethod2 was called
finish theMethod2 with instance
bash-3.2$ javac MethodClassLoadingTesting.java
bash-3.2$ rm MethodParameterClass2.class
bash-3.2$ java MethodClassLoadingTesting
MethodClassLoadingTesting loaded
loading MethodHolderClass
MethodHolderClass loaded
finish MethodHolderClass
calling theMethod1 with null
theMethod1 was called
MethodInsideUsingClass loaded
finish theMethod1 with null
calling theMethod1 with instance
MethodParameterClass1 loaded
theMethod1 was called
finish theMethod1 with instance
calling theMethod2 with null
theMethod2 was called
finish theMethod2 with null
calling theMethod2 with instance
Exception in thread "main" java.lang.NoClassDefFoundError: MethodParameterClass2
at MethodClassLoadingTesting.main(MethodClassLoadingTesting.java:26)
Caused by: java.lang.ClassNotFoundException: MethodParameterClass2
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 1 more
bash-3.2$
bash-3.2$ uname -v
Darwin Kernel Version 20.2.0: Wed Dec 2 20:39:59 PST 2020; root:xnu-7195.60.75~1/RELEASE_X86_64
bash-3.2$ java -version
openjdk version "1.8.0_275"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_275-b01)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.275-b01, mixed mode)
bash-3.2$ system_profiler SPHardwareDataType
Hardware:
Hardware Overview:
Model Name: iMac
Model Identifier: iMac17,1
Processor Name: Quad-Core Intel Core i5
Processor Speed: 3.3 GHz
Number of Processors: 1
Total Number of Cores: 4
L2 Cache (per Core): 256 KB
L3 Cache: 6 MB
Memory: 32 GB
System Firmware Version: 429.60.3.0.0
SMC Version (system): 2.34f3
Serial Number (system): <privacy>
Hardware UUID: <privacy>
Provisioning UDID: <privacy>
bash-3.2$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment