rspec/rules/S1068/java/rule.adoc
2024-03-19 11:34:36 +01:00

87 lines
2.2 KiB
Plaintext

== Why is this an issue?
include::../description.adoc[]
[source,java]
----
public class MyClass {
private int foo = 42; // Noncompliant: foo is unused and should be removed
public int compute(int a) {
return a * 42;
}
}
----
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.
=== Exceptions
The rule admits 3 exceptions:
* Serialization ID fields
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.
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:
[source,java]
----
public class MyClass implements java.io.Serializable {
private static final long serialVersionUID = 42L; // Compliant by exception
}
----
* Annotated fields and classes annotated with Lombok annotations
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).
[source,java]
----
public class MyClass {
@SomeAnnotation
private int unused; // Compliant by exception
}
----
* Fields from classes with native methods
The unused field in this class will not be reported by the rule as it might be used by native code.
[source,java]
----
public class MyClass {
private int unused = 42; // Compliant by exception
private native static void doSomethingNative();
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Parameters
.ignoreAnnotations
****
----
""
----
A comma separated list of fully qualified annotation class names that do not prevent a private field from being reported.
****
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]