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

PARAMETER_DEF in FinalLocalVariable doesn't seem to work in interfaces #16081

Open
Machine-Maker opened this issue Dec 29, 2024 · 2 comments · May be fixed by #16132
Open

PARAMETER_DEF in FinalLocalVariable doesn't seem to work in interfaces #16081

Machine-Maker opened this issue Dec 29, 2024 · 2 comments · May be fixed by #16132
Labels

Comments

@Machine-Maker
Copy link

Machine-Maker commented Dec 29, 2024

/var/tmp $ javac test.java
# success

/var/tmp $ cat checkstyle.xml
<!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="FinalLocalVariable">
      <property name="tokens" value="VARIABLE_DEF,PARAMETER_DEF"/>
    </module>
  </module>
</module>

/var/tmp $ cat YOUR_FILE.java
interface Test {
  static void test(String param) {
    String local = "";
    System.out.println(param);
  }
}

/var/tmp $ java -jar checkstyle-10.21.0-all.jar -c checkstyle.xml test.java
Starting audit...
[ERROR] /Users/jake/Projects/Minecraft/PaperMC/test.java:3:12: Variable 'local' should be declared final. [FinalLocalVariable]
Audit done.
Checkstyle ends with 1 errors.

The param local variable in the static method inside the Test interface should be reported as needing the final modifier which, if I'm understanding the documentation correctly, adding the PARAMETER_DEF token to the FinalLocalVariable check should do.

If I change Test to a class instead of interface, it correctly reports both, so I'm guessing someone just forgot about methods inside of interfaces.

@Machine-Maker Machine-Maker changed the title PARAMETER_DEF in FinalLocalVariables doesn't seem to work PARAMETER_DEF in FinalLocalVariable doesn't seem to work Dec 29, 2024
@Machine-Maker Machine-Maker changed the title PARAMETER_DEF in FinalLocalVariable doesn't seem to work PARAMETER_DEF in FinalLocalVariable doesn't seem to work in interfaces Dec 29, 2024
@romani
Copy link
Member

romani commented Dec 29, 2024

Yes, code in interface is recently new feature of java :)

@mahfouz72
Copy link
Member

Just to make it clear abstract methods in the interface still should be ignored, and only the default and static methods (methods that can have a body) should be checked. Methods with bodies (static, default or private) were introduced in Java 8 and 9.

interface test {
    void test(int a);   // ok, this is an abstract method we should not violate parameter 'a'

    default void test2(int b) { // violation, b should be final
            // body
    }

    static void test3(int c) { // violation, c should be final
         // body
    }
}

Machine-Maker added a commit to Machine-Maker/checkstyle that referenced this issue Jan 8, 2025
Machine-Maker added a commit to Machine-Maker/checkstyle that referenced this issue Jan 12, 2025
Machine-Maker added a commit to Machine-Maker/checkstyle that referenced this issue Jan 12, 2025
Machine-Maker added a commit to Machine-Maker/checkstyle that referenced this issue Jan 14, 2025
Machine-Maker added a commit to Machine-Maker/checkstyle that referenced this issue Jan 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants