Questions tagged [java-16]

Use this tag for questions specific to Java 16, which is version 16 of the Java platform, released on March 16, 2021. In most cases you should also specify the java tag.

This release is the Reference Implementation of version 16 of the Java SE Platform, as specified by JSR 391 in the Java Community Process.

124 questions
84
votes
12 answers

Lombok's access to jdk.compiler's internal packages incompatible with Java-16

Simply upgrading one of my projects from Java-15 to 16 (using the latest build here). On compiling the project which uses lombok such as: org.projectlombok lombok
Naman
  • 27,789
  • 26
  • 218
  • 353
75
votes
3 answers

Differences of Java 16's Stream.toList() and Stream.collect(Collectors.toList())?

JDK 16 now includes a toList() method directly on Stream instances. In previous Java versions, you always had to use the collect method and provide a Collector instance. The new method is obviously fewer characters to type. Are both methods…
knittl
  • 246,190
  • 53
  • 318
  • 364
34
votes
2 answers

Why are static methods allowed inside a non-static inner class in Java 16?

We know that a non-static inner class can be accessed using the instance of the outer class, so a static method is less meaningful inside a non-static class. But from Java 16 static methods are allowed inside a non-static inner class. Why did this…
Pradeesh tet
  • 617
  • 7
  • 16
34
votes
5 answers

Switch expression with void return type

Is there any way to force an exhaustive check of all enum values when the switch branches call methods with void return type? It's quite ugly to hard-code a yield just to coax the compiler to demand exhaustiveness. This is my current pattern (the…
Arboreal Shark
  • 2,221
  • 3
  • 17
  • 12
31
votes
7 answers

How to use @ConfigurationProperties with Records?

Java 16 introduced Records, which help to reduce boilerplate code when writing classes that carry immutable data. When I try to use a Record as @ConfigurationProperties bean as follows I get the following error…
Moritz
  • 1,954
  • 2
  • 18
  • 28
29
votes
3 answers

Why does IndexOutOfBoundsException now have a constructor with a long index as a parameter in Java 16?

I was checking the implementation of IndexOutOfBoundsException in JDK 16, and I have noticed that a new constructor with a long index has been introduced: /** * Constructs a new {@code IndexOutOfBoundsException} class with an * argument indicating…
M A
  • 71,713
  • 13
  • 134
  • 174
23
votes
3 answers

Why can't I use Stream#toList to collect a list of a class' interface in Java 16?

I'm streaming objects of a class implementing an interface. I'd like to collect them as a list of elements of the interface, rather than the implementing class. This seems impossible with Java 16.0.1's Stream#toList method. For example in the code…
TTT
  • 6,505
  • 10
  • 56
  • 82
23
votes
3 answers

Why can a Java record's canonical constructor not have more restrictive access than the record level?

I have a situation where I want record instances for a specific type to only be creatable using a factory method in a separate class within the same package. The reason for this is because before creating the record I need to perform a significant…
vab2048
  • 1,065
  • 8
  • 21
22
votes
1 answer

C-style arrays do not work with records anymore

I was previously using this contrived code record Foo(int bar[]) {} which is making use of the C-style array-notation. And it compiled fine in Java 15. Now, all of the sudden, with the official release of records in Java 16, it does not compile…
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
21
votes
3 answers

Is there a way to recognise a Java 16 record's canonical constructor via reflection?

Assuming I have a record like this (or any other record): record X(int i, int j) { X(int i) { this(i, 0); } X() { this(0, 0); } X(String i, String j) { this(Integer.parseInt(i), Integer.parseInt(j)); …
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
20
votes
1 answer

java.lang.ExceptionInInitializerError with Java-16 | j.l.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module

I have cglib as a transitive dependency in a Maven project. Despite adding what I believe to be the correct --add-opens I can't get the library to work with Java 16. How do I get cglib to work with Java 16? I raised this issue on the github…
Robert Bain
  • 9,113
  • 8
  • 44
  • 63
19
votes
1 answer

Enforce immutable collections in a Java record?

Java records are used to implement shallowly immutable data carrier types. If the constructor accepts mutable types then we should implement explicit defensive copying to enforce immutability. e.g. record Data(Set set) { public…
stridecolossus
  • 1,449
  • 10
  • 24
17
votes
1 answer

Would Stream.toList() perform better than Collectors.toList()

JDK is introducing an API Stream.toList() with JDK-8180352. Here is a benchmarking code that I have attempted to compare its performance with the existing Collectors.toList: @BenchmarkMode(Mode.All) @Fork(1) @State(Scope.Thread) @Warmup(iterations =…
Naman
  • 27,789
  • 26
  • 218
  • 353
16
votes
3 answers

How can I assert hasProperty with a Java Record?

I have a piece of code in a test that checks that a list of results contains certain properties, using Hamcrest 2.2: assertThat(result.getUsers(), hasItem( hasProperty("name", equalTo(user1.getName())) )); assertThat(result.getUsers(), hasItem( …
Christoffer Karlsson
  • 4,539
  • 3
  • 23
  • 36
15
votes
1 answer

IOException in Java 8 when reading PKCS12 keystore created with keytool from OpenJDK16

TL;DR keytool from OpenJDK16 creates PKCS12 keystore files that cannot be read from Java 8, 9, 10 and 11. Is this a bug? How to create a PKCS12 keystore that works with Java 8? Context I build a Maven project which produces an executable JAR file…
Bertrand Martin
  • 533
  • 1
  • 3
  • 16
1
2 3
8 9