Merge pull request #4003 from jjlharrison:master

[java] UnusedPrivateField: Ignore fields annotated with @Id, @EmbeddedId, @Version, @Mock, @Spy, or @MockBean #4003
This commit is contained in:
Andreas Dangel
2022-06-24 15:22:13 +02:00
3 changed files with 72 additions and 0 deletions

View File

@@ -18,6 +18,9 @@ This is a {{ site.pmd.release_type }} release.
* core
* [#3999](https://github.com/pmd/pmd/issues/3999): \[cli] All files are analyzed despite parameter `--file-list`
* [#4009](https://github.com/pmd/pmd/issues/4009): \[core] Cannot build PMD with Temurin 17
* java-bestpractices
* [#3824](https://github.com/pmd/pmd/issues/3824): \[java] UnusedPrivateField: Do not flag fields annotated with @Version
* [#3825](https://github.com/pmd/pmd/issues/3825): \[java] UnusedPrivateField: Do not flag fields annotated with @Id or @EmbeddedId
* java-design
* [#3823](https://github.com/pmd/pmd/issues/3823): \[java] ImmutableField: Do not flag fields in @Entity
* [#3981](https://github.com/pmd/pmd/issues/3981): \[java] ImmutableField reports fields annotated with @Value (Spring)
@@ -36,6 +39,7 @@ This is a {{ site.pmd.release_type }} release.
* [#3985](https://github.com/pmd/pmd/pull/3985): \[java] Fix false negative problem about Enum in AvoidFieldNameMatchingMethodName #3936 - [@Scrsloota](https://github.com/Scrsloota)
* [#3993](https://github.com/pmd/pmd/pull/3993): \[java] AvoidDuplicateLiterals - Add the method "buz" definition to test cases - [@dalizi007](https://github.com/dalizi007)
* [#4002](https://github.com/pmd/pmd/pull/4002): \[java] ImmutableField - Ignore fields annotated with @Value (Spring) or @Captor (Mockito) - [@jjlharrison](https://github.com/jjlharrison)
* [#4003](https://github.com/pmd/pmd/pull/4003): \[java] UnusedPrivateField - Ignore fields annotated with @Id/@EmbeddedId/@Version (JPA) or @Mock/@Spy/@MockBean (Mockito/Spring) - [@jjlharrison](https://github.com/jjlharrison)
* [#4006](https://github.com/pmd/pmd/pull/4006): \[doc] Fix eclipse plugin update site URL - [@shiomiyan](https://github.com/shiomiyan)
* [#4010](https://github.com/pmd/pmd/pull/4010): \[core] Bump kotlin to version 1.7.0 - [@maikelsteneker](https://github.com/maikelsteneker)

View File

@@ -49,6 +49,15 @@ public class UnusedPrivateFieldRule extends AbstractLombokAwareRule {
defaultValues.add("javafx.fxml.FXML");
defaultValues.add("lombok.experimental.Delegate");
defaultValues.add("lombok.EqualsAndHashCode");
defaultValues.add("javax.persistence.Id");
defaultValues.add("javax.persistence.EmbeddedId");
defaultValues.add("javax.persistence.Version");
defaultValues.add("jakarta.persistence.Id");
defaultValues.add("jakarta.persistence.EmbeddedId");
defaultValues.add("jakarta.persistence.Version");
defaultValues.add("org.mockito.Mock");
defaultValues.add("org.mockito.Spy");
defaultValues.add("org.springframework.boot.test.mock.mockito.MockBean");
return defaultValues;
}

View File

@@ -683,6 +683,65 @@ public class Foo {
}
}
}
}
]]></code>
</test-code>
<test-code>
<description>#3824 #3825 Do not flag fields annotated with @Id, @EmbeddedId, or @Version</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Version;
@Entity
@Table(name = "my_table")
public class MyTable implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "id", nullable = false)
private long m_id;
@Version
@Column(name = "optimistic_lock", nullable = false)
private short m_optimisticLock;
public MyTable() {
// nothing to do
}
}
]]></code>
</test-code>
<test-code>
<description>Do not flag Mockito fields that are injected into test target</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.mockito.Spy;
import org.mockito.Mock;
import org.mockito.InjectMocks;
import org.springframework.boot.test.mock.mockito.MockBean;
public class MyTest {
@Mock
private Object mock;
@MockBean
private Object bean;
@Spy
private Object spy;
@InjectMocks
private Object target;
void test() {
target.methodToTest();
}
}
]]></code>
</test-code>