Skip to content

Add Check Support for Java 21 Record Pattern : IllegalType #14946

@mahfouz72

Description

@mahfouz72

child of #14890
I have read check documentation: https://checkstyle.org/checks/coding/illegaltype.html#IllegalType
I have downloaded the latest checkstyle from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the cli and showed it below, as cli describes the problem better than 1,000 words

PS D:\CS\test> javac src/RecordPatterns.java
PS D:\CS\test> cat src/RecordPatterns.java
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

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

    public void TestIllegalType() {
        List<LinkedHashMap<Integer, Integer>> l // violation
                = new ArrayList<>();

        if (l instanceof List<LinkedHashMap<Integer, Integer>> linkedHashMap) { // violation
            System.out.println(linkedHashMap);
        }
         LinkedHashMap<Integer, Integer> l2 // violation
                = new LinkedHashMap<>();
        Box<LinkedHashMap<Integer,Integer>> box = new Box<>(l2);  // violation

        if (box instanceof Box<LinkedHashMap<Integer,Integer>>(var linkedHashMap)) { // expected violation
            System.out.println(linkedHashMap);
        }
    }
}
PS D:\CS\test> cat config.xml
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
          "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">

<module name="Checker">
    <property name="charset" value="UTF-8"/>
 <module name="TreeWalker">
    <module name="IllegalType"/>
 </module>
</module>
PS D:\CS\test> java  -jar checkstyle-10.14.2-all.jar -c config.xml  src/RecordPatterns.java
Starting audit...
[ERROR] D:\CS\test\src\RecordPatterns.java:9:14: Usage of type 'LinkedHashMap' is not allowed. [IllegalType]
[ERROR] D:\CS\test\src\RecordPatterns.java:12:31: Usage of type 'LinkedHashMap' is not allowed. [IllegalType]
[ERROR] D:\CS\test\src\RecordPatterns.java:15:10: Usage of type 'LinkedHashMap' is not allowed. [IllegalType]
[ERROR] D:\CS\test\src\RecordPatterns.java:17:13: Usage of type 'LinkedHashMap' is not allowed. [IllegalType]
Audit done.
Checkstyle ends with 4 errors.

Describe what you expect in detail.

I expect a violation on the type parameters of the record pattern. The type parameter of the record pattern is of illegal type.


Metadata

Metadata

Assignees

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions