Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java14 enhanced instanceof check validation: IllegalTypeCheck #8365

Closed
nrmancuso opened this issue Jun 29, 2020 · 4 comments
Closed

Java14 enhanced instanceof check validation: IllegalTypeCheck #8365

nrmancuso opened this issue Jun 29, 2020 · 4 comments

Comments

@nrmancuso
Copy link
Member

nrmancuso commented Jun 29, 2020

Child of #8391
Check documentation: https://checkstyle.sourceforge.io/config_coding.html#IllegalType
Identified at #8294 (comment)

We need to validate that IllegalTypeCheck works as intended once #8294 is merged.

Fix summary:
New token was added to Check

@nrmancuso
Copy link
Member Author

From the check documentation:
Checks that particular classes or interfaces are never used.
Rationale: Helps reduce coupling on concrete classes.

Consider:

➜  instanceof-pattern-var-def /usr/lib/jvm/java-14-openjdk/bin/javac --enable-preview --source 14 TestClass.java
Note: TestClass.java uses preview language features.
Note: Recompile with -Xlint:preview for details.
➜  instanceof-pattern-var-def cat config.xml    
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
          "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
  <module name="TreeWalker">
      <module name="IllegalType"/>

  </module>
</module>
➜  instanceof-pattern-var-def cat TestClass.java
import java.util.LinkedHashMap;

public class TestClass {
    static {
        // lhm is a violation
        LinkedHashMap<Integer, Integer> lhm
                = new LinkedHashMap<Integer, Integer>();
        // Should l be a violation?
        if (lhm instanceof LinkedHashMap<Integer, Integer> l) {
            System.out.println(l);
        }
    }
}%                                                                              ➜  instanceof-pattern-var-def java $RUN_LOCALE -jar ~/IdeaProjects/checkstyle/target/checkstyle-8.35-SNAPSHOT-all.jar -c config.xml TestClass.java
Starting audit...
[ERROR] /home/nick/Desktop/instanceof-pattern-var-def/TestClass.java:6:9: Usage of type 'LinkedHashMap' is not allowed. [IllegalType]
Audit done.
Checkstyle ends with 1 errors.

What do you think @pbludov @esilkensen ?

@esilkensen
Copy link
Member

I think so... seems like this would be a good check to add PATTERN_VARIABLE_DEF as one of the default tokens?

@nrmancuso
Copy link
Member Author

I agree. I'm on it.

@romani
Copy link
Member

romani commented Jul 19, 2020

Fix is merged

@romani romani closed this as completed Jul 19, 2020
@romani romani added this to the 8.35 milestone Jul 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants