Skip to content

Commit 519ba7a

Browse files
committed
[add] add dynamic type examples in jvm module.
1 parent 90bfef7 commit 519ba7a

File tree

8 files changed

+281
-3
lines changed

8 files changed

+281
-3
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
- [java-multithread](/java-multithread):多线程模块的 Java 源码
2121
- [java-container](/java-container):容器类模块的 Java 源码
2222
- [java-io](/java-io):IO 模块的 Java 源码
23+
- [java-jvm](/java-jvm): JVM 模块的 Java 源码
24+
- [java8](/java8): Java 8 模块的源码
2325

2426

2527
# 博客文档
@@ -58,12 +60,12 @@
5860

5961
计划将这个仓库进行重构,逐步扩充并实现下面的功能。
6062

61-
* [x] 整理成 maven 的结构,使用聚合和继承特性(2016.4.12完成)
63+
* [x] 整理成 maven 的结构,使用聚合和继承特性(2016.4.12 完成)
6264
* [ ] 原有的 Java SE 部分代码重构为 java-base 模块,并逐步上传代码
63-
* [x] 多线程部分使用 java-multithread 模块(2016.4.17完成雏形)
65+
* [x] 多线程部分使用 java-multithread 模块(2016.4.17 完成雏形)
6466
* [ ] 容器类部分使用模块 java-container
6567
* [ ] IO 部分使用模块 java-io
66-
* [ ] Java 虚拟机相关部分使用模块 java-jvm
68+
* [x] Java 虚拟机相关部分使用模块 java-jvm(2017.3.20 完成雏形)
6769
* [ ] Java 8 新特性使用模块 java8(正在进行)
6870

6971

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.brianway.learning.java.jvm.dynamictype;
2+
3+
import static java.lang.invoke.MethodHandles.lookup;
4+
5+
import java.lang.invoke.MethodHandle;
6+
import java.lang.invoke.MethodType;
7+
8+
/**
9+
* Created by brian on 17/3/20.
10+
* 使用 MethodHandle 来访问祖类方法
11+
* TODO 为什么打印出来的是 "i am father" 而不是 "i am grandfather"?
12+
*/
13+
public class GrandFatherCaller {
14+
class GrandFather {
15+
void thinking() {
16+
System.out.println("i am grandfather");
17+
}
18+
}
19+
20+
class Father extends GrandFather {
21+
void thinking() {
22+
System.out.println("i am father");
23+
}
24+
}
25+
26+
class Son extends Father {
27+
void thinking() {
28+
try {
29+
MethodType mt = MethodType.methodType(void.class);
30+
MethodHandle mh = lookup().findSpecial(
31+
GrandFather.class, "thinking", mt, Son.class);
32+
mh.invoke(this);
33+
} catch (Throwable throwable) {
34+
throwable.printStackTrace();
35+
}
36+
}
37+
}
38+
39+
public static void main(String[] args) {
40+
(new GrandFatherCaller().new Son()).thinking();
41+
}
42+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.brianway.learning.java.jvm.dynamictype;
2+
3+
import static java.lang.invoke.MethodHandles.lookup;
4+
5+
import java.lang.invoke.CallSite;
6+
import java.lang.invoke.ConstantCallSite;
7+
import java.lang.invoke.MethodHandle;
8+
import java.lang.invoke.MethodHandles;
9+
import java.lang.invoke.MethodType;
10+
11+
/**
12+
* Created by brian on 17/3/20.
13+
* invokedynamic 指令演示
14+
*/
15+
public class InvokeDynamicTest {
16+
17+
public static void main(String[] args) throws Throwable {
18+
INDY_BootstrapMethod().invokeExact("brianway");
19+
}
20+
21+
public static void testMethod(String s) {
22+
System.out.println("hello String:" + s);
23+
}
24+
25+
public static CallSite BootstrapMethod(
26+
MethodHandles.Lookup lookup, String name, MethodType mt)
27+
throws Throwable {
28+
return new ConstantCallSite(lookup.findStatic(InvokeDynamicTest.class, name, mt));
29+
}
30+
31+
private static MethodType MT_BootstrapMethod() {
32+
return MethodType.fromMethodDescriptorString(
33+
"(Ljava/lang/invoke/MethodHandles$Lookup;" +
34+
"Ljava/lang/String;Ljava/lang/invoke/MethodType;)" +
35+
"Ljava/lang/invoke/CallSite;", null
36+
);
37+
}
38+
39+
private static MethodHandle MH_BootstrapMethod() throws Throwable {
40+
return lookup().findStatic(InvokeDynamicTest.class, "BootstrapMethod",
41+
MT_BootstrapMethod());
42+
}
43+
44+
private static MethodHandle INDY_BootstrapMethod() throws Throwable {
45+
CallSite cs = (CallSite) MH_BootstrapMethod().invokeWithArguments(
46+
lookup(),
47+
"testMethod",
48+
MethodType.fromMethodDescriptorString(
49+
"(Ljava/lang/String;)V", null
50+
));
51+
return cs.dynamicInvoker();
52+
}
53+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.brianway.learning.java.jvm.dynamictype;
2+
3+
import static java.lang.invoke.MethodHandles.lookup;
4+
5+
import java.lang.invoke.MethodHandle;
6+
import java.lang.invoke.MethodType;
7+
8+
/**
9+
* Created by brian on 17/3/20.
10+
* MethodHandle 演示
11+
*/
12+
public class MethodHandleTest {
13+
static class ClassA {
14+
public void println(String s) {
15+
System.out.println("classA " + s);
16+
}
17+
}
18+
19+
public static void main(String[] args) throws Throwable {
20+
Object obj = System.currentTimeMillis() % 2 == 0 ? System.out : new ClassA();
21+
getPrintlnMH(obj).invokeExact("brianway");
22+
}
23+
24+
private static MethodHandle getPrintlnMH(Object receiver) throws Throwable {
25+
// 方法类型
26+
MethodType mt = MethodType.methodType(void.class, String.class);
27+
28+
return lookup().findVirtual(receiver.getClass(), "println", mt)
29+
.bindTo(receiver);
30+
}
31+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.brianway.learning.java.jvm.methodinvoke;
2+
3+
/**
4+
* Created by brian on 17/3/20.
5+
* 单分派、多分派演示
6+
*/
7+
public class Dispatch {
8+
static class QQ {
9+
}
10+
11+
static class _360 {
12+
}
13+
14+
public static class Father {
15+
public void hardChoice(QQ arg) {
16+
System.out.println("father choose qq");
17+
}
18+
19+
public void hardChoice(_360 arg) {
20+
System.out.println("father choose 360");
21+
}
22+
}
23+
24+
public static class Son extends Father {
25+
public void hardChoice(QQ arg) {
26+
System.out.println("son choose qq");
27+
}
28+
29+
public void hardChoice(_360 arg) {
30+
System.out.println("son choose 360");
31+
}
32+
}
33+
34+
public static void main(String[] args) {
35+
Father father = new Father();
36+
Father son = new Son();
37+
father.hardChoice(new _360());
38+
son.hardChoice(new QQ());
39+
}
40+
}
41+
42+
/*
43+
father choose 360
44+
son choose qq
45+
*/
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.brianway.learning.java.jvm.methodinvoke;
2+
3+
/**
4+
* Created by brian on 17/3/20.
5+
* 方法动态分派演示
6+
*/
7+
public class DynamicDispatch {
8+
static abstract class Human {
9+
protected abstract void sayHello();
10+
}
11+
12+
static class Man extends Human {
13+
@Override
14+
protected void sayHello() {
15+
System.out.println("man say hello");
16+
}
17+
}
18+
19+
static class Woman extends Human {
20+
@Override
21+
protected void sayHello() {
22+
System.out.println("woman say hello");
23+
}
24+
}
25+
26+
public static void main(String[] args) {
27+
Human man = new Man();
28+
Human woman = new Woman();
29+
man.sayHello();
30+
woman.sayHello();
31+
32+
man = new Woman();
33+
man.sayHello();
34+
}
35+
}
36+
37+
/*
38+
man say hello
39+
woman say hello
40+
woman say hello
41+
*/
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.brianway.learning.java.jvm.methodinvoke;
2+
3+
/**
4+
* Created by brian on 17/3/20.
5+
* 方法静态分派演示
6+
* Method Overload Resolution
7+
*/
8+
public class StaticDispatch {
9+
static abstract class Human {
10+
}
11+
12+
static class Man extends Human {
13+
}
14+
15+
static class Woman extends Human {
16+
}
17+
18+
public void sayHello(Human guy) {
19+
System.out.println("hello,guy!");
20+
}
21+
22+
public void sayHello(Man guy) {
23+
System.out.println("hello,gentleman!");
24+
}
25+
26+
public void sayHello(Woman guy) {
27+
System.out.println("hello,lady!");
28+
}
29+
30+
public static void main(String[] args) {
31+
Human man = new Man();
32+
Human woman = new Woman();
33+
StaticDispatch sr = new StaticDispatch();
34+
35+
sr.sayHello(man);
36+
sr.sayHello(woman);
37+
sr.sayHello((Man)man);
38+
sr.sayHello((Woman) woman);
39+
}
40+
}
41+
42+
/*
43+
hello,guy!
44+
hello,guy!
45+
hello,gentleman!
46+
hello,lady!
47+
*/
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.brianway.learning.java.jvm.methodinvoke;
2+
3+
/**
4+
* Created by brian on 17/3/20.
5+
* 方法静态解析演示
6+
* javac -verbose StaticResolution.java
7+
* javap -verbose StaticResolution
8+
*/
9+
public class StaticResolution {
10+
public static void sayHello() {
11+
System.out.println("hello world");
12+
}
13+
14+
public static void main(String[] args) {
15+
StaticResolution.sayHello();
16+
}
17+
}

0 commit comments

Comments
 (0)