Skip to content

[java] Better Lombok support #3119

@oowekyala

Description

@oowekyala

Related issues/PRs:


Many rules have some form of "lombok awareness", in that they ignore some constructs if they have lombok annotations. But PMD does not really handle the desugaring that lombok performs in a way, that all rules can benefit transparently.

The main issues are:

  • lombok.val should be treated like the var keyword of java 10. [java] UnusedPrivateMethod false positive when passing in lombok.val as argument #3118, [java] UseDiamondOperator false positive with var initializer #1624, [java] UseDiamondOperator false-positive with Lombok val initializer #2710

  • Type resolution in PMD 7 uses the AST as a source of symbols (methods, etc) to perform eg overload resolution. Those symbols are implicit when you use lombok. (eg @Getter declares a getter method), and for now are not picked up. This means, that type resolution will be much more patchy on lombok codebases, in the current state. A nice solution, would be to generate some of the symbols that correspond to annotations (the same way we add a generated symbol for eg Enum.valueOf in enum declarations). I think fully replicating the lombok desugaring is too complicated and is a non-goal. We should only generate symbols that are usable from the source language.

    For instance, the @Synchronized annotation generates a private lock field. Generating a symbol for this field is a non-goal, because it's an internal detail. It cannot mess up type resolution as it's not used in the source we're processing, only in the modified AST that lombok generates. Same thing for all annotations that just alter the AST locally, like @Cleanup.

    OTOH, @Getter is something we could care about, because some dependent code may be using the getter method. If we're not aware of this, then that dependent code will be mistyped.

I think that second item may only be implemented in PMD 7. The first item could also be implemented in pmd 6 by a motivated person.

Metadata

Metadata

Assignees

No one assigned

    Labels

    an:enhancementAn improvement on existing features / rulesin:type-resolutionAffects the type resolution code

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions