-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava17.java
More file actions
164 lines (139 loc) · 6.25 KB
/
Copy pathJava17.java
File metadata and controls
164 lines (139 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package pl.mperor.lab.java;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import pl.mperor.lab.java.lang.JavaBean;
import pl.mperor.lab.java.sealed.*;
import java.applet.Applet;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InaccessibleObjectException;
import java.util.HexFormat;
import java.util.Set;
import java.util.random.RandomGeneratorFactory;
/// Java 17™ (September 2021)
/// [JDK 17](https://openjdk.org/projects/jdk/17)
///
/// - STANDARD FEATURES:
/// - 409: Sealed Classes
/// - 403: Strongly Encapsulate JDK Internals
/// - 356: Enhanced Pseudo-Random Number Generators
/// - 306: Restore Always-Strict Floating-Point Semantics
/// - 398: Deprecate the Applet API for Removal
/// - 411: Deprecate the Security Manager for Removal
/// - 407: Remove RMI Activation
/// - 410: Remove the Experimental AOT and JIT Compiler
/// - 415: Context-Specific Deserialization Filters
/// - 382: New macOS Rendering Pipeline
/// - 391: macOS/AArch64 Port
///
/// - PREVIEW & INCUBATOR:
/// - 406: Pattern Matching for switch (Preview)
/// - 412: Foreign Function & Memory API (Incubator)
/// - 414: Vector API (Second Incubator)
public class Java17 {
@Test
public void testSealedClassesAndInterfaces() {
Assertions.assertTrue(Sealed.class.isSealed());
Assertions.assertFalse(NonSealed.class.isSealed());
Assertions.assertFalse(Final.class.isSealed());
Assertions.assertTrue(AlsoSealed.class.isSealed());
Assertions.assertFalse(AlsoFinal.class.isSealed());
Assertions.assertFalse(ImplicitlyFinal.class.isSealed());
Assertions.assertEquals("final", switchSealed(new Final()));
Assertions.assertEquals("non-sealed", switchSealed(new NonSealed()));
Assertions.assertEquals("sealed", switchSealed(new AlsoSealed()));
Assertions.assertEquals("implicitly final", switchSealed(new ImplicitlyFinal()));
SealedClient client = new SealedClient(new NonSealedChild());
Assertions.assertEquals("non-sealed", client.call());
}
private static String switchSealed(Sealed sealed) {
return switch (sealed) {
case AlsoSealed a -> a.alsoSealedMethod();
case Final f -> f.finalMethod();
case NonSealed ns -> ns.nonSealedMethod();
case ImplicitlyFinal r -> r.implicitlyFinalMethod();
};
}
private static class NonSealedChild extends NonSealed {}
private static class NotPermittedSealedChild /* implements Sealed */ {}
private static class SealedClient {
private Sealed s;
public SealedClient(Sealed s) {
this.s = s;
}
public String call() {
return switchSealed(s);
}
}
/**
* You may need this JVM argument:
* --add-opens java.base/java.lang=ALL-UNNAMED
*/
@Test
public void testStronglyEncapsulatedInternals() {
Assertions.assertThrows(InaccessibleObjectException.class, () -> deepLookIntoStringBytes("Hello"));
new ProcessBuilder("java", "--add-opens", "java.base/java.lang=ALL-UNNAMED");
}
private static byte[] deepLookIntoStringBytes(String string) throws IllegalAccessException, NoSuchFieldException {
Field valueField = String.class.getDeclaredField("value");
valueField.setAccessible(true);
return (byte[]) valueField.get(string);
}
@Test
public void testRandomGeneratorFactory() {
var defaultRandomGenerator = RandomGeneratorFactory.getDefault().create();
Assertions.assertEquals("jdk.internal.random.L32X64MixRandom", defaultRandomGenerator.getClass().getCanonicalName());
var generators = RandomGeneratorFactory.all()
.map(fac -> fac.create().getClass().getCanonicalName())
.peek(System.out::println)
.toList();
Assertions.assertTrue(generators.contains("java.util.Random"));
}
@Test
public void testHexFormat() {
HexFormat hexFormat = HexFormat.of();
String hex = hexFormat.formatHex(new byte[]{0x1A, 0x2B, 0x3C});
Assertions.assertEquals("1a2b3c", hex);
Assertions.assertArrayEquals(new byte[]{0x1A, 0x2B, 0x3C}, hexFormat.parseHex(hex));
Assertions.assertFalse(HexFormat.isHexDigit('g'));
Assertions.assertEquals(16 + 10, HexFormat.fromHexDigits("1a"));
}
@Test
public void testAlwaysStrictFloatingPointSemantics() {
Assertions.assertEquals(4, strictPower(2, 2));
}
// warning: [strictfp] as of release 17, all floating-point expressions are evaluated strictly and 'strictfp' is not required
@SuppressWarnings("strictfp")
public static strictfp double strictPower(double a, double b) {
return Math.pow(a, b);
}
@SuppressWarnings("removal")
@Test
public void testDeprecated() {
// warning: [removal] Applet in java.applet has been deprecated and marked for removal
Applet appletDeprecatedForRemoval;
// warning: [removal] SecurityManager in java.lang has been deprecated and marked for removal
SecurityManager securityManager = System.getSecurityManager();
}
@Test
public void testRmiActivationRemoved() {
Assertions.assertFalse(ModuleLayer.boot().findModule("java.rmi").stream()
.map(Module::getPackages)
.flatMap(Set::stream)
.anyMatch("java.rmi.activation"::equals));
}
@Test
public void testDeserializationFilters() throws IOException, ClassNotFoundException {
var file = new File("src/test/resources/bean.bin");
var basePackageFilter = ObjectInputFilter.Config.createFilter("java.base/*;!*");
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(file))) {
in.setObjectInputFilter(basePackageFilter);
Assertions.assertThrows(InvalidClassException.class, () -> in.readObject());
}
ObjectInputFilter onlyJavaBeansFilter = ObjectInputFilter.allowFilter(JavaBean.class::equals, ObjectInputFilter.Status.REJECTED);
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(file))) {
in.setObjectInputFilter(onlyJavaBeansFilter);
Assertions.assertNotNull(in.readObject());
}
}
}