Skip to content

Commit a731e43

Browse files
Java Examples
1 parent 36725e2 commit a731e43

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.howtodoinjava.reflection;
2+
3+
import java.lang.invoke.MethodHandles;
4+
import java.lang.invoke.MethodHandles.Lookup;
5+
import java.lang.invoke.MethodType;
6+
import java.lang.reflect.Constructor;
7+
import java.lang.reflect.InvocationHandler;
8+
import java.lang.reflect.Proxy;
9+
10+
public class CallingInterfaceMethodWithoutImplementing {
11+
12+
public static void main(String[] args) {
13+
14+
//Since Java 16
15+
16+
Movable movableProxy = (Movable) Proxy.newProxyInstance(
17+
Movable.class.getClassLoader(),
18+
new Class<?>[]{Movable.class}, (o, m, p) -> {
19+
if (m.isDefault()) {
20+
return InvocationHandler.invokeDefault(o, m, p);
21+
}
22+
return null;
23+
});
24+
25+
// invoke interface methods
26+
movableProxy.move();
27+
String returnedValue = movableProxy.moveWithParamAndReturn("lion");
28+
System.out.println(returnedValue);
29+
30+
//Java 15 and Before
31+
32+
Movable movableProxy2 = (Movable) Proxy.newProxyInstance(
33+
Movable.class.getClassLoader(),
34+
new Class<?>[]{Movable.class}, (o, m, p) -> {
35+
if (m.isDefault()) {
36+
// Adjust the method type as per the default method's signature
37+
MethodType methodType;
38+
if (m.getName().equals("move")) {
39+
methodType = MethodType.methodType(void.class);
40+
} else if (m.getName().equals("moveWithParamAndReturn")) {
41+
methodType = MethodType.methodType(String.class, String.class);
42+
} else {
43+
throw new UnsupportedOperationException("Unknown default method: " + m.getName());
44+
}
45+
return MethodHandles.lookup()
46+
.findSpecial(Movable.class, m.getName(),
47+
methodType,
48+
Movable.class)
49+
.bindTo(o)
50+
.invokeWithArguments(p);
51+
}
52+
return null;
53+
});
54+
55+
movableProxy2.move();
56+
returnedValue = movableProxy2.moveWithParamAndReturn("lion");
57+
System.out.println(returnedValue);
58+
59+
//JDK 8
60+
61+
Movable movableProxy3 = (Movable) Proxy.newProxyInstance(
62+
Movable.class.getClassLoader(),
63+
new Class<?>[]{Movable.class}, (o, m, p) -> {
64+
if (m.isDefault()) {
65+
Constructor<Lookup> cntr = Lookup.class
66+
.getDeclaredConstructor(Class.class);
67+
cntr.setAccessible(true);
68+
return cntr.newInstance(Movable.class)
69+
.in(Movable.class)
70+
.unreflectSpecial(m, Movable.class)
71+
.bindTo(o)
72+
.invokeWithArguments(p);
73+
}
74+
return null;
75+
});
76+
77+
movableProxy3.move();
78+
returnedValue = movableProxy3.moveWithParamAndReturn("lion");
79+
System.out.println(returnedValue);
80+
}
81+
82+
}
83+
84+
interface Movable {
85+
86+
default void move() {
87+
System.out.println("Inside Movable.move()");
88+
}
89+
90+
default String moveWithParamAndReturn(String param) {
91+
System.out.println("Inside Movable.moveWithParamAndReturn()");
92+
return STR."\{param}-returned";
93+
}
94+
95+
void moveToImplement();
96+
}

0 commit comments

Comments
 (0)