spring-projects / spring-projects/spring-security

Reject use of Optional

Open
#9,641 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

in: build type: enhancement
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the PMD ruleset configuration suggested in the issue and review the linked gh-7155 context. Add a rule that rejects Optional type usage, including cases where it is not imported and method chaining is used, then verify that the intended usages are reported.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.