Skip to content

Commit

Permalink
Issue checkstyle#12507: Support record patterns preview
Browse files Browse the repository at this point in the history
  • Loading branch information
nrmancuso committed Dec 10, 2022
1 parent 8382d88 commit fb1cf6f
Show file tree
Hide file tree
Showing 9 changed files with 1,459 additions and 1 deletion.
14 changes: 14 additions & 0 deletions .ci/validation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,20 @@ javac17)
fi
;;

javac19)
files=($(grep -Rl --include='*.java' ': Compilable with Java19' \
src/test/resources-noncompilable || true))
if [[ ${#files[@]} -eq 0 ]]; then
echo "No Java19 files to process"
else
mkdir -p target
for file in "${files[@]}"
do
javac --release 19 --enable-preview -d target "${file}"
done
fi
;;

jdk14-assembly-site)
mvn -e --no-transfer-progress package -Passembly
mvn -e --no-transfer-progress site -Pno-validations
Expand Down
4 changes: 4 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ workflows:
name: "javac17"
image-name: "cimg/openjdk:17.0.5"
command: "./.ci/validation.sh javac17"
- validate-with-script:
name: "javac19"
image-name: "cimg/openjdk:19.0.1"
command: "./.ci/validation.sh javac19"

site-validation:
jobs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,7 @@ arguments
pattern
: guardedPattern
| primaryPattern
| recordPattern
;

guardedPattern
Expand All @@ -881,12 +882,25 @@ primaryPattern
// of nested `PATTERN_DEF` nodes, as we also do for expressions.
(guardedPattern | primaryPattern)
RPAREN #parenPattern
| recordPattern #recordPatternDef
;

typePattern
: mods+=modifier* type=typeType[true] id
;

recordPattern
: type=typeType[true] recordStructurePattern id?
;

recordStructurePattern
: LPAREN recordComponentPatternList* RPAREN
;

recordComponentPatternList
: primaryPattern (COMMA primaryPattern)*
;

permittedSubclassesAndInterfaces
: LITERAL_PERMITS classOrInterfaceType[false] (COMMA classOrInterfaceType[false])*
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ public class JavaAstVisitorTest extends AbstractModuleTestSupport {
"visitFieldAccessNoIdent",
"visitClassType",
"visitClassOrInterfaceTypeExtended",
"visitQualifiedNameExtended"
"visitQualifiedNameExtended",

// until https://github.com/checkstyle/checkstyle/issues/12507
"visitRecordComponentPatternList",
"visitRecordPatternDef",
"visitRecordPattern",
"visitRecordStructurePattern"
);

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
///////////////////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
// Copyright (C) 2001-2022 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
///////////////////////////////////////////////////////////////////////////////////////////////

package com.puppycrawl.tools.checkstyle.grammar.java19;

import org.junit.jupiter.api.Test;

import com.puppycrawl.tools.checkstyle.AbstractTreeTestSupport;

public class Java19AstRegressionTest extends AbstractTreeTestSupport {

@Override
protected String getPackageLocation() {
return "com/puppycrawl/tools/checkstyle/grammar/java19";
}

@Test
public void testRecordPatternsWithNestedDecomposition() throws Exception {
verifyAst(getPath("ExpectedRecordPatternsPreviewNestedDecomposition.txt"),
getNonCompilablePath("RecordPatternsPreviewNestedDecomposition.java"));
}

@Test
public void testRecordPatternsPreview() throws Exception {
verifyAst(getPath("ExpectedRecordPatternsPreview.txt"),
getNonCompilablePath("InputRecordPatternsPreview.java"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//non-compiled with javac: Compilable with Java19
package com.puppycrawl.tools.checkstyle.grammar.java19;

public class InputRecordPatternsPreview {
record Box<T>(T t) {
}

class A {
}

class B extends A {
}

sealed interface I permits C, D {
}

final class C implements I {
}

final class D implements I {
}

record Pair<T>(T x, T y) {
}

record Tuple(Object o1, Object o2, Object o3) {}
record VarArgs(Object... objArgs){}


static void test1(Box<Object> bo) {
if (bo instanceof Box<Object>(String s)) {
System.out.println("String " + s);
}
}

static void test2(Box<Object> bo) {
if (bo instanceof Box<?>(var s)) {
System.out.println("String " + s);
}
}

void test3(Box<I> bo) {
C c = new C();
if (bo instanceof Box<I>(var s)) {
System.out.println("String " + s);
}
else if (bo instanceof Box b) {

}
else if (bo instanceof Box<I> b && bo.t.equals(c)) {

}
else if (bo != null && bo.t.equals(c)) {

}
}

static void test4(Pair<I> p) {
if (p instanceof Pair<I>(C t1, C t2)) {
System.out.println(t2);
}
else if (p instanceof Pair<I>(D t1, D t2)) {
System.out.println(t1);
}
}

static void test5(Tuple t) {
if (t instanceof Tuple(String x, String y, String z)) {
System.out.println(x + y + z);
}
else if (t instanceof Tuple(Integer x, Integer y, Integer z)) {
System.out.println(x + y + z);
}
}

static void test5(VarArgs v) {
if (v instanceof VarArgs(Integer[] ints)) {
int l = ints.length;
}
else if (v instanceof VarArgs(Object[] objects)
&& objects.length > 2) {
int l = objects.length;
}
else if (v instanceof VarArgs(Object[] objects)
&& objects.length < 2){
int l = objects.length;
}

}

static void m1(Pair<I> p1) {
switch (p1) {
case Pair<I>(I i,C c) ->
System.out.println("C!");
case Pair<I>(I i,D d) ->
System.out.println("D!");
}

switch (p1) {
case Pair<I>(C c,I i) ->
System.out.println("T!");
case Pair<I>(D d,C c) ->
System.out.println("C!");
case Pair<I>(D d1,D d2) ->
System.out.println("D!");
}
}

String m2(Pair<I> p1) {
return switch (p1) {
case Pair<I>(C c,I i) -> "T!";
case Pair<I>(D d,C c) -> "C!";
case Pair<I>(D d1,D d2) -> "D!";
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
public class RecordPatternsPreviewNestedDecomposition {
record A(Object o) {
}

record B(Object o) {
}

record Point(int x, int y) {
}

enum Color {RED, GREEN, BLUE}

record ColoredPoint(Point p, Color c) {
}

record Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight) {
}

void method(Object param) {
switch (param) {
case A(Object o) -> {
}
case B(var o) -> {
}
default -> {
}
}
if (param instanceof A(var o)) {

}
}

// Decompose record
static void p1(Rectangle r) {
if (r instanceof Rectangle(ColoredPoint ul,ColoredPoint lr)) {
System.out.println(ul.c());
}
}

// We can decompose this record further, since Point is also
// a record
static void p2(Rectangle r) {
if (r instanceof Rectangle(ColoredPoint(Point p1,Color c1),
ColoredPoint lr1)
&& r instanceof Rectangle(ColoredPoint(Point p2,Color c2),
ColoredPoint lr2) && lr2.c == Color.BLUE) {
System.out.println(r);
}
}

// We can keep decomposing down to the last "inner" record
static void p3(Rectangle r) {
if (r instanceof Rectangle(ColoredPoint(Point p1,Color c1),
ColoredPoint lr1)
&& r instanceof Rectangle(
ColoredPoint(Point(int x,int y),Color c2),
ColoredPoint lr2)) {
System.out.println(r);
}
}

static void p4() {
int x1 = 0;
int x2 = 0;
int y1 = 0;
int y2 = 0;
Color c1 = Color.BLUE;
Color c2 = Color.GREEN;
Rectangle r = new Rectangle(new ColoredPoint(new Point(x1, y1), c1),
new ColoredPoint(new Point(x2, y2), c2));
if (r instanceof Rectangle(
ColoredPoint(Point(var x,var y),var c),
var lr)) {
System.out.println(x);
}
}
}

0 comments on commit fb1cf6f

Please sign in to comment.