2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-10-10 18:27:17 +02:00
include::../description.adoc[]
2020-06-30 12:47:33 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2020-06-30 12:47:33 +02:00
----
public class MyClass {
2023-10-10 18:27:17 +02:00
private int foo = 42; // Noncompliant: foo is unused and should be removed
2020-06-30 12:47:33 +02:00
public int compute(int a) {
return a * 42;
}
}
----
2023-10-10 18:27:17 +02:00
Note that this rule does not take reflection into account, which means that issues will be raised on ``++private++`` fields that are only accessed using the reflection API.
2020-06-30 12:47:33 +02:00
2023-05-03 11:06:20 +02:00
=== Exceptions
2020-06-30 12:47:33 +02:00
2021-08-31 12:10:35 +02:00
The rule admits 3 exceptions:
2021-09-02 12:00:02 +02:00
2023-10-10 18:27:17 +02:00
* Serialization ID fields
2021-08-31 12:10:35 +02:00
2023-10-10 18:27:17 +02:00
The Java serialization runtime associates with each serializable class a version number called `serialVersionUID`, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible for serialization.
2021-02-02 15:02:10 +01:00
2023-10-10 18:27:17 +02:00
A serializable class can declare its own `serialVersionUID` explicitly by declaring a field named `serialVersionUID` that must be static, final, and of type long. By definition, those `serialVersionUID` fields should not be reported by this rule:
2020-06-30 12:47:33 +02:00
2021-02-02 15:02:10 +01:00
2023-05-25 14:18:12 +02:00
[source,java]
2020-06-30 12:47:33 +02:00
----
public class MyClass implements java.io.Serializable {
2023-10-10 18:27:17 +02:00
private static final long serialVersionUID = 42L; // Compliant by exception
2020-06-30 12:47:33 +02:00
}
----
2023-10-10 18:27:17 +02:00
2023-12-21 15:07:24 +01:00
* Annotated fields and classes annotated with Lombok annotations
2021-08-31 12:10:35 +02:00
2024-03-19 11:34:36 +01:00
The unused field in this class will not be reported by the rule as it is annotated,
except if annotation class `SomeAnnotation` is listed in the `ignoreAnnotations` parameter (see Parameters).
2023-05-25 14:18:12 +02:00
[source,java]
2021-08-31 12:10:35 +02:00
----
public class MyClass {
@SomeAnnotation
2023-10-10 18:27:17 +02:00
private int unused; // Compliant by exception
2021-08-31 12:10:35 +02:00
}
----
2023-05-03 11:06:20 +02:00
* Fields from classes with native methods
2021-08-31 12:10:35 +02:00
The unused field in this class will not be reported by the rule as it might be used by native code.
2023-05-25 14:18:12 +02:00
[source,java]
2021-08-31 12:10:35 +02:00
----
public class MyClass {
2023-10-10 18:27:17 +02:00
private int unused = 42; // Compliant by exception
2021-08-31 12:10:35 +02:00
private native static void doSomethingNative();
}
----
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2024-03-19 11:34:36 +01:00
=== Parameters
.ignoreAnnotations
****
----
""
----
A comma separated list of fully qualified annotation class names that do not prevent a private field from being reported.
****
2021-09-20 15:38:42 +02:00
include::../message.adoc[]
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
2023-06-22 10:38:01 +02:00
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]