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 17 Sealed Classes: RedundantModifer #15087

Open
mahfouz72 opened this issue Jun 21, 2024 · 4 comments
Open

Add Check Support for Java 17 Sealed Classes: RedundantModifer #15087

mahfouz72 opened this issue Jun 21, 2024 · 4 comments

Comments

@mahfouz72
Copy link
Member

child of #14969 :

Check documentation : https://checkstyle.org/checks/modifier/redundantmodifier.html#RedundantModifier


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="RedundantModifier"/>
    </module>
</module>
PS D:\CS\test> cat src/Test.java                                                
sealed interface I permits A, R {
    public static final String s = null; // 3 violations
    public abstract void m(); // 2 violations
    public static abstract sealed interface J permits A, R {  // 3 violations
       public abstract void m();  // 2 violations
        public static final String s = null;  // 3 violation
    }
}
final class A implements I, I.J {
    @Override
    public void m() {
    }
}
final record R() implements I, I.J {    // violation
    @Override
    public void m() {
    }
}
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:2:5: Redundant 'public' modifier. [RedundantModifier]
[ERROR] D:\CS\test\src\Test.java:2:12: Redundant 'static' modifier. [RedundantModifier]
[ERROR] D:\CS\test\src\Test.java:2:19: Redundant 'final' modifier. [RedundantModifier]
[ERROR] D:\CS\test\src\Test.java:3:5: Redundant 'public' modifier. [RedundantModifier]
[ERROR] D:\CS\test\src\Test.java:3:12: Redundant 'abstract' modifier. [RedundantModifier]
[ERROR] D:\CS\test\src\Test.java:4:5: Redundant 'public' modifier. [RedundantModifier]
[ERROR] D:\CS\test\src\Test.java:4:12: Redundant 'static' modifier. [RedundantModifier]
[ERROR] D:\CS\test\src\Test.java:4:19: Redundant 'abstract' modifier. [RedundantModifier]
[ERROR] D:\CS\test\src\Test.java:5:8: Redundant 'public' modifier. [RedundantModifier]
[ERROR] D:\CS\test\src\Test.java:5:15: Redundant 'abstract' modifier. [RedundantModifier]
[ERROR] D:\CS\test\src\Test.java:6:9: Redundant 'public' modifier. [RedundantModifier]
[ERROR] D:\CS\test\src\Test.java:6:16: Redundant 'static' modifier. [RedundantModifier]
[ERROR] D:\CS\test\src\Test.java:6:23: Redundant 'final' modifier. [RedundantModifier]
[ERROR] D:\CS\test\src\Test.java:14:1: Redundant 'final' modifier. [RedundantModifier]
Audit done.
Checkstyle ends with 14 errors.
PS D:\CS\test> 


The check works correctly when the redundant modifiers are on sealed classes. I read the doc and JEP related to sealed classes and I don't find a new case that the modifier is redundant. So, this check doesn't need an update.

@rnveach
Copy link
Member

rnveach commented Jun 22, 2024

Can you have a final sealed class? Is the final (or even the sealed) redundant if the class is sealed?

permits seems redundant if it is all the classes in the file, but we have discussed another check for this.

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

we can't have a final sealed class this will not compile as It is not possible for a class to be both sealed (implying subclasses) and final (implying no subclasses)

PS D:\CS\test> cat src/Test.java                                                
public class Test {
}
final sealed class A permits B{
}

final class B extends A {
}
PS D:\CS\test> javac src/Test.java                                              
src\Test.java:3: error: illegal combination of modifiers: final and sealed
final sealed class A permits B{
             ^
src\Test.java:6: error: cannot inherit from final A
final class B extends A {
                      ^
2 errors
PS D:\CS\test> 

@rnveach
Copy link
Member

rnveach commented Jun 23, 2024

I am good.

@rnveach rnveach assigned nrmancuso and unassigned rnveach Jun 23, 2024
@nrmancuso
Copy link
Member

Let's add an input file for this.

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