Skip to content

Reject use of Optional #9641

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

Open
rwinch opened this issue Apr 14, 2021 · 0 comments
Open

Reject use of Optional #9641

rwinch opened this issue Apr 14, 2021 · 0 comments
Labels
in: build An issue in the build type: enhancement A general enhancement

Comments

@rwinch
Copy link
Member

rwinch commented Apr 14, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: build An issue in the build type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

1 participant