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

Add Check Support for Java 21 Unnamed Variables & Patterns Syntax: AnnotationOnSameLineCheck #15061

Closed
mahfouz72 opened this issue Jun 20, 2024 · 11 comments · Fixed by #15483
Closed

Comments

@mahfouz72
Copy link
Member

mahfouz72 commented Jun 20, 2024

child of #14942
Check documentation: https://checkstyle.org/checks/annotation/annotationonsameline.html#AnnotationOnSameLine

PS D:\CS\test> javac src/Test.java
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="AnnotationOnSameLine">
        </module>
    </module>
</module>
PS D:\CS\test> cat src/Test.java  
record ColoredPoint(int p, int x, String c) { }
record Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight) { }
public class Test {
    void test(Object obj) {
        @Deprecated
        int _ = 0;

        if (obj instanceof ColoredPoint(@Deprecated // expected violation
                                          int _, _,_)){
        }
    }
}
PS D:\CS\test> java  -jar checkstyle-10.17.0-all.jar -c config.xml src/Test.java
Starting audit...
[ERROR] D:\CS\test\src\Test.java:5:9: Annotation 'Deprecated' should be on the same line with its target. [AnnotationOnSameLine]
Audit done.
Checkstyle ends with 1 errors.
PS D:\CS\test> 


This is not very specific to unnamed patterns. but for patterns in general.
I expect a violation on the annotations on the pattern variable. The annotation should be on the same line as the pattern. We should add PATTERN_VARIABLE_DEF in the tokens

@rnveach
Copy link
Member

rnveach commented Jun 22, 2024

if (obj instanceof ColoredPoint( @Deprecated int _, _,_)){

Show a violation on this line if possible.

I am ok to add an input if there is no concern.

@rnveach rnveach self-assigned this Jun 22, 2024
@mahfouz72
Copy link
Member Author

mahfouz72 commented Jun 22, 2024

PATTERN_VARAIABLE_DEF is not in the acceptable tokens of this check.


PS D:\CS\test> cat src/Test.java                                                
record ColoredPoint(int p, int x, String c) { } 
record Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight) { }  
public class Test {
    void test(Object obj) {
        if (obj instanceof ColoredPoint(@Deprecated // should this be violation ?
                                          int _, _,_)){
        }
        @Deprecated
        int _ = 0;
    }
}
PS D:\CS\test> java  -jar checkstyle-10.17.0-all.jar -c config.xml src/Test.java
Starting audit...
[ERROR] D:\CS\test\src\Test.java:8:9: Annotation 'Deprecated' should be on the same line with its target. [AnnotationOnSameLine]
Audit done.
Checkstyle ends with 1 errors.
PS D:\CS\test> 


I guess this should be ok this is all technically in the same if condition I think there is no point in requiring it to be on the same line

@rnveach
Copy link
Member

rnveach commented Jun 22, 2024

@Deprecated
int _ = 0;
8:9: Annotation 'Deprecated' should be on the same line with its target.

This seems to cover local variables but all examples look to be class/javadoc level related.

@Deprecated // should this be violation ?
int _, _,_)){

To me it seems we need to support this and print a violation. These pattern variables are almost a combination of local variables and field variables.

@mahfouz72 Please add all examples to first post.

@rnveach rnveach assigned nrmancuso and unassigned rnveach Jun 22, 2024
@mahfouz72
Copy link
Member Author

done first post is updated

@nrmancuso
Copy link
Member

nrmancuso commented Jul 3, 2024

This should not be a violation. This is a style check, so we should follow logic similar to conceptually/structurally similar code (in record component list in record definitions, constructors definition parameters, method definition parameters).

Example:

➜  src 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="AnnotationOnSameLine">
        </module>
    </module>
</module>
➜  src cat Test.java                                

record ColoredPoint(@Deprecated int p, int x, String c) { }
record ColoredPointOther(@Deprecated
                         int p, int x, String c) { }

public class Test {

    Test(@Deprecated int x) {}
    Test(@Deprecated
         String s) {}

    void m1(@Deprecated int x) {}
    void m2(@Deprecated
            int x) {}

    void test(Object obj) {
        @Deprecated
        int _ = 0;

        if (obj instanceof ColoredPoint(@Deprecated
                                        int _, _,_)){
        }

        if (obj instanceof ColoredPoint(@Deprecated int _, _,_)){
        }
    }
}
➜  src javac --enable-preview --release 21 Test.java                        
Note: Test.java uses preview features of Java SE 21.
Note: Recompile with -Xlint:preview for details.
➜  src java -jar checkstyle-10.18.1-SNAPSHOT-all.jar -c config.xml Test.java
Starting audit...
[ERROR] /home/nick/IdeaProjects/tester/src/Test.java:17:9: Annotation 'Deprecated' should be on the same line with its target. [AnnotationOnSameLine]
Audit done.
Checkstyle ends with 1 errors.

I am approving for new test case only.

@rnveach
Copy link
Member

rnveach commented Aug 11, 2024

@nrmancuso

This is a style check, so we should follow logic similar to conceptually/structurally similar code (in record component list in record definitions, constructors definition parameters, method definition parameters

So you are saying this should be treated similarly to parameters in a method and not as a local variable?

@nrmancuso
Copy link
Member

If this check doesn't place violations on record components, it shouldn't place them on record component patterns. Not only are these constructs conceptually similar, but in the case of a style check (where we care about aesthetics), it is good enough that they look similar.

If we are talking about coding checks, then we should probably consider these to be local variables in most cases.

@rnveach
Copy link
Member

rnveach commented Aug 12, 2024

By record components you are referring to record ColoredPoint and record ColoredPointOther, which looks like line 2 and 3 in your example?

@nrmancuso
Copy link
Member

By record components you are referring to record ColoredPoint and record ColoredPointOther, which looks like line 2 and 3 in your example?

See https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-8.10.1

mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Aug 13, 2024
@nrmancuso nrmancuso reopened this Aug 15, 2024
@nrmancuso
Copy link
Member

I merged the PR for this issue without realizing that @rnveach was in the review queue, I am reopening this issue to make sure that we have resolved all concerns; @rnveach please close this issue or let us know what else we need to do here.

@github-actions github-actions bot added this to the 10.18.0 milestone Aug 15, 2024
@rnveach
Copy link
Member

rnveach commented Aug 16, 2024

I am good.

@rnveach rnveach closed this as completed Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment