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

checkstyle 10.3.3 and newer throws NullPointerException on annotated generic types from IllegalType rule #12443

Closed
motlin opened this issue Nov 22, 2022 · 4 comments · Fixed by #12444
Assignees
Milestone

Comments

@motlin
Copy link
Contributor

motlin commented Nov 22, 2022

I minimized my repro to narrow down the cause.

The config:

❯ /bin/cat checkstyle-configuration.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">
            <property name="illegalClassNames"
                    value="java.lang.StringBuffer, " />
        </module>
    </module>
</module>

The code:

❯ cat ExampleClass.java

package com.example;

import java.util.List;

import javax.validation.constraints.Pattern;

public class ExampleClass
{
    public void example(List<@Pattern String> strings)
    {
    }
}
❯ RUN_LOCALE="-Duser.language=en -Duser.country=US"
❯ java $RUN_LOCALE -jar checkstyle-10.3.3-all.jar -c checkstyle-configuration.xml ExampleClass.java 
Starting audit...
com.puppycrawl.tools.checkstyle.api.CheckstyleException: Exception was thrown while processing ExampleClass.java
	at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:307)
	at com.puppycrawl.tools.checkstyle.Checker.process(Checker.java:224)
	at com.puppycrawl.tools.checkstyle.Main.runCheckstyle(Main.java:416)
	at com.puppycrawl.tools.checkstyle.Main.runCli(Main.java:339)
	at com.puppycrawl.tools.checkstyle.Main.execute(Main.java:196)
	at com.puppycrawl.tools.checkstyle.Main.main(Main.java:131)
Caused by: java.lang.NullPointerException: Cannot invoke "com.puppycrawl.tools.checkstyle.api.DetailAST.getLineNo()" because "ast" is null
	at com.puppycrawl.tools.checkstyle.api.AbstractCheck.log(AbstractCheck.java:276)
	at com.puppycrawl.tools.checkstyle.checks.coding.IllegalTypeCheck.checkIdent(IllegalTypeCheck.java:645)
	at com.puppycrawl.tools.checkstyle.checks.coding.IllegalTypeCheck.checkType(IllegalTypeCheck.java:674)
	at com.puppycrawl.tools.checkstyle.utils.TokenUtil.forEachChild(TokenUtil.java:244)
	at com.puppycrawl.tools.checkstyle.checks.coding.IllegalTypeCheck.checkTypeArguments(IllegalTypeCheck.java:719)
	at com.puppycrawl.tools.checkstyle.checks.coding.IllegalTypeCheck.checkType(IllegalTypeCheck.java:675)
	at com.puppycrawl.tools.checkstyle.checks.coding.IllegalTypeCheck.checkClassName(IllegalTypeCheck.java:633)
	at com.puppycrawl.tools.checkstyle.checks.coding.IllegalTypeCheck.visitParameterDef(IllegalTypeCheck.java:563)
	at com.puppycrawl.tools.checkstyle.checks.coding.IllegalTypeCheck.visitToken(IllegalTypeCheck.java:478)
	at com.puppycrawl.tools.checkstyle.TreeWalker.notifyVisit(TreeWalker.java:336)
	at com.puppycrawl.tools.checkstyle.TreeWalker.processIter(TreeWalker.java:407)
	at com.puppycrawl.tools.checkstyle.TreeWalker.walk(TreeWalker.java:274)
	at com.puppycrawl.tools.checkstyle.TreeWalker.processFiltered(TreeWalker.java:155)
	at com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck.process(AbstractFileSetCheck.java:98)
	at com.puppycrawl.tools.checkstyle.Checker.processFile(Checker.java:335)
	at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:294)
	... 5 more
Checkstyle ends with 1 errors.

❯ java $RUN_LOCALE -jar checkstyle-10.3.2-all.jar -c checkstyle-configuration.xml ExampleClass.java
Starting audit...
Audit done.

Note that any little change to the config file prevents this problem. Even getting rid of the trailing comma or space at the end of StringBuffer in <property name="illegalClassNames" value="java.lang.StringBuffer, " /> prevents the problem.


@rnveach
Copy link
Member

rnveach commented Nov 22, 2022

This issue is tied to #12017 and commits 3080013 and 01b7de6 and PRs #12034 and #12035.

Rationale was No need to trim strings here, they are trimmed further down in the method.

It appears it was not taken into account to have an extra comma on the end with an empty space. There is no exception if there is just an extra comma. The exception comes in when there is a space after the comma.

It is not user friendly to throw an exception. I think we should restore the code and drop the remaining empty item.

@romani
Copy link
Member

romani commented Nov 23, 2022

We should restore trim.
As we do for all other.

Example: #12258

@nrmancuso
Copy link
Member

On it

@nrmancuso nrmancuso added the bug label Nov 23, 2022
@nrmancuso nrmancuso self-assigned this Nov 23, 2022
@nrmancuso nrmancuso added this to the 10.5 milestone Nov 23, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Nov 23, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Nov 23, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Nov 23, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Nov 23, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Nov 23, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Nov 24, 2022
@romani
Copy link
Member

romani commented Nov 26, 2022

Fix is merged

motlin added a commit to motlin/klass that referenced this issue Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants