Skip to content

Commit 26d1234

Browse files
Merge pull request eugenp#11363 from thiagohora/BAEL-5197/new_features_in_javav17
[BAEL-5197] New Features in Java 17
2 parents b5a2c9b + 8953802 commit 26d1234

File tree

14 files changed

+371
-20
lines changed

14 files changed

+371
-20
lines changed

core-java-modules/core-java-17/pom.xml

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@
2323
<version>${assertj.version}</version>
2424
<scope>test</scope>
2525
</dependency>
26+
<dependency>
27+
<groupId>org.junit.jupiter</groupId>
28+
<artifactId>junit-jupiter-engine</artifactId>
29+
<version>${junit-jupiter.version}</version>
30+
<scope>test</scope>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.junit.jupiter</groupId>
34+
<artifactId>junit-jupiter-api</artifactId>
35+
<version>${junit-jupiter.version}</version>
36+
<scope>test</scope>
37+
</dependency>
2638
</dependencies>
2739

2840
<build>
@@ -38,32 +50,35 @@
3850
<target>${maven.compiler.target.version}</target>
3951
</configuration>
4052
</plugin>
41-
<plugin>
42-
<groupId>org.apache.maven.plugins</groupId>
43-
<artifactId>maven-surefire-plugin</artifactId>
44-
<version>${surefire.plugin.version}</version>
45-
<configuration>
46-
<argLine>--enable-preview</argLine>
47-
<forkCount>1</forkCount>
48-
</configuration>
49-
<dependencies>
50-
<dependency>
51-
<groupId>org.apache.maven.surefire</groupId>
52-
<artifactId>surefire-api</artifactId>
53-
<version>${surefire.plugin.version}</version>
54-
</dependency>
55-
</dependencies>
56-
</plugin>
53+
<plugin>
54+
<groupId>org.apache.maven.plugins</groupId>
55+
<artifactId>maven-surefire-plugin</artifactId>
56+
<version>${surefire.plugin.version}</version>
57+
58+
<configuration>
59+
<argLine>--enable-preview</argLine>
60+
<argLine>--enable-native-access=core.java</argLine>
61+
<forkCount>1</forkCount>
62+
</configuration>
63+
64+
<dependencies>
65+
<dependency>
66+
<groupId>org.apache.maven.surefire</groupId>
67+
<artifactId>surefire-api</artifactId>
68+
<version>${surefire.plugin.version}</version>
69+
</dependency>
70+
</dependencies>
71+
</plugin>
5772
</plugins>
5873
</build>
5974

6075
<properties>
6176
<maven.compiler.source.version>17</maven.compiler.source.version>
6277
<maven.compiler.target.version>17</maven.compiler.target.version>
63-
<maven.compiler.release>17</maven.compiler.release>
64-
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
65-
<surefire.plugin.version>3.0.0-M5</surefire.plugin.version>
66-
<assertj.version>3.17.2</assertj.version>
78+
<maven.compiler.release>17</maven.compiler.release>
79+
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
80+
<surefire.plugin.version>3.0.0-M5</surefire.plugin.version>
81+
<assertj.version>3.17.2</assertj.version>
6782
</properties>
6883

6984
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.features;
2+
3+
import java.util.random.RandomGeneratorFactory;
4+
import java.util.stream.IntStream;
5+
import java.util.stream.Stream;
6+
7+
public class JEP356 {
8+
9+
public Stream<String> getAllAlgorithms() {
10+
return RandomGeneratorFactory.all().map(RandomGeneratorFactory::name);
11+
}
12+
13+
public IntStream getPseudoInts(String algorithm, int streamSize) {
14+
// returns an IntStream with size @streamSize of random numbers generated using the @algorithm
15+
// where the lower bound is 0 and the upper is 100 (exclusive)
16+
return RandomGeneratorFactory.of(algorithm)
17+
.create()
18+
.ints(streamSize, 0,100);
19+
}
20+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.features;
2+
3+
import com.baeldung.features.JEP409.Circle;
4+
import com.baeldung.features.JEP409.Shape;
5+
import com.baeldung.features.JEP409.Triangle;
6+
7+
public class JEP406 {
8+
9+
static record Human (String name, int age, String profession) {}
10+
11+
public String checkObject(Object obj) {
12+
return switch (obj) {
13+
case Human h -> "Name: %s, age: %s and profession: %s".formatted(h.name(), h.age(), h.profession());
14+
case Circle c -> "This is a circle";
15+
case Shape s -> "It is just a shape";
16+
case null -> "It is null";
17+
default -> "It is an object";
18+
};
19+
}
20+
21+
public String checkShape(Shape shape) {
22+
return switch (shape) {
23+
case Triangle t && (t.getNumberOfSides() != 3) -> "This is a weird triangle";
24+
case Circle c && (c.getNumberOfSides() != 0) -> "This is a weird circle";
25+
default -> "Just a normal shape";
26+
};
27+
}
28+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.features;
2+
3+
public class JEP409 {
4+
5+
sealed interface Shape permits Rectangle, Circle, Square, Triangle {
6+
int getNumberOfSides();
7+
}
8+
9+
static final class Rectangle implements Shape {
10+
@Override
11+
public int getNumberOfSides() {
12+
return 4;
13+
}
14+
}
15+
16+
static final class Circle implements Shape {
17+
@Override
18+
public int getNumberOfSides() {
19+
return 0;
20+
}
21+
}
22+
23+
static final class Square implements Shape {
24+
@Override
25+
public int getNumberOfSides() {
26+
return 4;
27+
}
28+
}
29+
30+
static non-sealed class Triangle implements Shape {
31+
32+
@Override
33+
public int getNumberOfSides() {
34+
return 3;
35+
}
36+
}
37+
38+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.features;
2+
3+
import jdk.incubator.foreign.CLinker;
4+
import jdk.incubator.foreign.FunctionDescriptor;
5+
import jdk.incubator.foreign.MemoryAddress;
6+
import jdk.incubator.foreign.SymbolLookup;
7+
8+
import java.io.IOException;
9+
import java.lang.invoke.MethodType;
10+
11+
import static jdk.incubator.foreign.ResourceScope.newImplicitScope;
12+
13+
public class JEP412 {
14+
15+
private static final SymbolLookup libLookup;
16+
17+
static {
18+
var resource = JEP412.class.getResource("/compile_c.sh");
19+
try {
20+
var process = new ProcessBuilder("sh", resource.getPath()).start();
21+
while (process.isAlive()) {}
22+
} catch (IOException ex) {
23+
throw new RuntimeException(ex);
24+
}
25+
26+
var path = JEP412.class.getResource("/print_name.so").getPath();
27+
System.load(path);
28+
libLookup = SymbolLookup.loaderLookup();
29+
}
30+
31+
public String getPrintNameFormat(String name){
32+
33+
var printMethod = libLookup.lookup("printName");
34+
35+
if (printMethod.isPresent()) {
36+
var methodReference = CLinker.getInstance()
37+
.downcallHandle(
38+
printMethod.get(),
39+
MethodType.methodType(MemoryAddress.class, MemoryAddress.class),
40+
FunctionDescriptor.of(CLinker.C_POINTER, CLinker.C_POINTER)
41+
);
42+
43+
try {
44+
var nativeString = CLinker.toCString(name, newImplicitScope());
45+
var invokeReturn = methodReference.invoke(nativeString.address());
46+
var memoryAddress = (MemoryAddress) invokeReturn;
47+
return CLinker.toJavaString(memoryAddress);
48+
} catch (Throwable throwable) {
49+
throw new RuntimeException(throwable);
50+
}
51+
}
52+
throw new RuntimeException("printName function not found.");
53+
}
54+
}
55+
56+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.features;
2+
3+
import jdk.incubator.vector.FloatVector;
4+
import jdk.incubator.vector.VectorSpecies;
5+
6+
public class JEP414 {
7+
8+
private static final VectorSpecies<Float> SPECIES = FloatVector.SPECIES_PREFERRED;
9+
10+
11+
public void newVectorComputation(float[] a, float[] b, float[] c) {
12+
for (var i = 0; i < a.length; i += SPECIES.length()) {
13+
var m = SPECIES.indexInRange(i, a.length);
14+
var va = FloatVector.fromArray(SPECIES, a, i, m);
15+
var vb = FloatVector.fromArray(SPECIES, b, i, m);
16+
var vc = va.mul(vb);
17+
vc.intoArray(c, i, m);
18+
}
19+
}
20+
21+
public void commonVectorComputation(float[] a, float[] b, float[] c) {
22+
for (var i = 0; i < a.length; i ++) {
23+
c[i] = a[i] * b[i];
24+
}
25+
}
26+
27+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module core.java {
2+
requires jdk.incubator.vector;
3+
requires jdk.incubator.foreign;
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
4+
5+
gcc -c -fPIC $SCRIPTPATH/print_name.c
6+
gcc -shared -rdynamic -o print_name.so print_name.o
7+
8+
mv print_name.so $SCRIPTPATH/
9+
mv print_name.o $SCRIPTPATH/
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
4+
char* printName(char *name) {
5+
char* newString = (char*)malloc((15 + sizeof(name))*sizeof(char));
6+
sprintf(newString, "Your name is %s", name);
7+
return newString;
8+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.features;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
public class JEP356UnitTest {
8+
9+
@Test
10+
void getPseudoInts_whenUsingAlgorithmXoroshiro128PlusPlus_shouldReturnStreamOfRandomInteger() {
11+
var algorithm = "Xoshiro256PlusPlus";
12+
var streamSize = 100;
13+
14+
JEP356 jep356 = new JEP356();
15+
16+
jep356.getPseudoInts(algorithm, streamSize)
17+
.forEach(value -> assertThat(value).isLessThanOrEqualTo(99));
18+
}
19+
}

0 commit comments

Comments
 (0)