Skip to content

Reject use of Optional #9641

Open
Open
@rwinch

Description

@rwinch

This should reject Optional usage even if it is not imported since users may use method chaining. I think this might be able to be achieved using PMD with a configuration of something like this:

<?xml version="1.0"?>

<ruleset name="Spring Security"
    xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">

    <description>
Rules which enforce generally accepted best practices.
    </description>

 <rule name="AvoidOptional"
          language="java"
          since="3.4"
          message="Please do not use Optional to avoid unnecessary Garbage Collection."
          class="net.sourceforge.pmd.lang.rule.XPathRule"
          externalInfoUrl="https://github.com/spring-projects/spring-security/issues/7155">
        <description>
Please do not use Optional to avoid unnecessary Garbage Collection.
        </description>
        <priority>3</priority>
        <properties>
            <property name="version" value="2.0"/>
            <property name="xpath">
                <value>//Type/ReferenceType/ClassOrInterfaceType[@Image='Optional']</value>
            </property>
        </properties>
        <example>
<![CDATA[
public class Foo {
    void bar() {
        Optional h = zoop.findOne();
    }
}
]]>
        </example>
    </rule>

</ruleset>

See gh-7155

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions