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

[java] AtLeastOneConstructor #3944

Open
1 of 3 tasks
dgroup opened this issue Apr 29, 2022 · 1 comment
Open
1 of 3 tasks

[java] AtLeastOneConstructor #3944

dgroup opened this issue Apr 29, 2022 · 1 comment
Labels
a:false-positive PMD flags a piece of code that is not problematic

Comments

@dgroup
Copy link

dgroup commented Apr 29, 2022

Affects PMD Version:
6.44.0+
7.0.0

Rule: AtLeastOneConstructor

Please provide the rule name and a link to the rule documentation:
https://pmd.github.io/latest/pmd_rules_java_codestyle.html#atleastoneconstructor

Description:

Code Sample demonstrating the issue:

  • Case 1: when class implement functional interface that doesn't need the constructor
public class ToString implements java.util.Function<Object, String> {
     @Override
     public String apply(Object arg) {
         return arg.toString();
    }
}
  • Case 2: When methods in class have some particular annotations like @org.junit.Test, etc.
public class AbstractForSomeReason { // violation at this line
     
     @org.junit.Test / @Before / @After / @BeforeSuite / @BeforeGroup / @etc.
     public void test() {
          // implementation
    }
}
  • Case 3: When we have unit test class, thus we don't need the constructor
public class ClassTest { // violation at this line, but here is workaround below
     
     public void test() {
          // implementation
    }
}

could be fixed by the following configuration

<rule ref="category/java/codestyle.xml/AtLeastOneConstructor">
    <properties>
      <property name="violationSuppressXPath" value=".[ends-with(@SimpleName,'Test')]"/>
      <property name="ignoredAnnotations" value="lombok.Data|lombok.Value|lombok.Builder|lombok.NoArgsConstructor|lombok.RequiredArgsConstructor|lombok.AllArgsConstructor|org.springframework.context.annotation.Configuration|org.springframework.web.bind.annotation.ControllerAdvice|org.springframework.web.bind.annotation.RestController|lombok.experimental.SuperBuilder|org.springframework.stereotype.Component|org.springframework.stereotype.Service|org.springframework.stereotype.Repository|org.springframework.boot.context.properties.ConfigurationProperties|org.mapstruct.Mapper|javax.xml.bind.annotation.XmlRootElement|org.junit.jupiter.api.extension.ExtendWith|org.mockserver.junit.jupiter.MockServerSettings|org.springframework.test.context.ContextConfiguration|org.springframework.test.context.web.WebAppConfiguration|org.junit.runner.RunWith" />
    </properties>
  </rule>

Expected outcome:

PMD reports a violation in case1 and case2, but that's wrong. That's a false positive.
The possible ways of resolution:

  1. Make ignoredAnnotations to support method-level (not only class-level as implemented now)
    <property name="ignoredAnnotations" value="lombok.Data|org.junit.Test" />
    where @lombok.Data is class-level annotation, and @org.junit.Test is method-level annotation
  2. Make a new property 'allowedInterfaces' where you can specify full class name or package like
    <property name="allowedInterfaces" value="java.lang|org.apache.camel.Processor"/>
  3. Deprecate the rule if there is no exact reason to keep it

Running PMD through: Gradle

@dgroup dgroup added the a:false-positive PMD flags a piece of code that is not problematic label Apr 29, 2022
@cemuener
Copy link

Rule AtLeastOneConstructor does not make sense for @UtilityClass annotations.
Interferres with IDE rule: No constructors for UtilityClasses.

@jsotuyod jsotuyod added needs:pmd7-revalidation The issue hasn't yet been retested vs PMD 7 and may be stale and removed needs:pmd7-revalidation The issue hasn't yet been retested vs PMD 7 and may be stale labels Mar 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-positive PMD flags a piece of code that is not problematic
Projects
None yet
Development

No branches or pull requests

3 participants