Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SI-11908: support JDK16 records in Java parser
JDK16 introduced records (JEP 395) for reducing the boilerplate associated with small immutable classes. This new construct automatically * makes fields `private`/`final` and generates accessors for them * overrides `equals`/`hashCode`/`toString` * creates a `final` class that extends `java.lang.Record` The details are in "8.10. Record Classes" of the Java language specification. Fixes scala/bug#11908
- Loading branch information
1 parent
d974e99
commit f7ae7af
Showing
8 changed files
with
196 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// javaVersion: 16+ | ||
object C { | ||
|
||
def useR1 = { | ||
// constructor signature | ||
val r1 = new R1(123, "hello") | ||
|
||
// accessors signature | ||
val i: Int = r1.i | ||
val s: String = r1.s | ||
|
||
// method | ||
val s2: String = r1.someMethod() | ||
|
||
// supertype | ||
val isRecord: java.lang.Record = r1 | ||
|
||
() | ||
} | ||
|
||
def useR2 = { | ||
// constructor signature | ||
val r2 = new R2(123, "hello") | ||
|
||
// accessors signature | ||
val i: Int = r2.i | ||
val s: String = r2.s | ||
|
||
// method | ||
val i2: Int = r2.getInt | ||
|
||
// supertype | ||
val isIntLike: IntLike = r2 | ||
val isRecord: java.lang.Record = r2 | ||
|
||
() | ||
} | ||
|
||
def useR3 = { | ||
// constructor signature | ||
val r3 = new R3(123, 42L, "hi") | ||
new R3("hi", 123) | ||
|
||
// accessors signature | ||
val i: Int = r3.i | ||
val l: Long = r3.l | ||
val s: String = r3.s | ||
|
||
// method | ||
val l2: Long = r3.l(43L, 44L) | ||
|
||
// supertype | ||
val isRecord: java.lang.Record = r3 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// javaVersion: 16+ | ||
trait IntLike { | ||
def getInt: Int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// javaVersion: 16+ | ||
record R1(int i, String s) { | ||
|
||
public String someMethod() { | ||
return s + "!"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// javaVersion: 16+ | ||
final record R2(int i, String s) implements IntLike { | ||
public int getInt() { | ||
return i; | ||
} | ||
|
||
// Canonical constructor | ||
public R2(int i, String s) { | ||
this.i = i; | ||
this.s = s.intern(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// javaVersion: 16+ | ||
public record R3(int i, long l, String s) { | ||
|
||
// User-specified accessor | ||
public int i() { | ||
return i + 1; // evil >:) | ||
} | ||
|
||
// Not an accessor - too many parameters | ||
public long l(long a1, long a2) { | ||
return a1 + a2; | ||
} | ||
|
||
// Secondary constructor | ||
public R3(String s, int i) { | ||
this(i, 42L, s); | ||
} | ||
|
||
// Compact constructor | ||
public R3 { | ||
s = s.intern(); | ||
} | ||
} |