rspec/rules/S1319/java/rule.adoc

59 lines
1.4 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
The purpose of the Java Collections API is to provide a well defined hierarchy of interfaces in order to hide implementation details.
Implementing classes must be used to instantiate new collections, but the result of an instantiation should ideally be stored in a variable whose type is a Java Collection interface.
This rule raises an issue when an implementation class:
* is returned from a ``++public++`` method.
* is accepted as an argument to a ``++public++`` method.
* is exposed as a ``++public++`` member.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
public class Employees {
private HashSet<Employee> employees = new HashSet<Employee>(); // Noncompliant - "employees" should have type "Set" rather than "HashSet"
public HashSet<Employee> getEmployees() { // Noncompliant
return employees;
}
}
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
public class Employees {
private Set<Employee> employees = new HashSet<Employee>(); // Compliant
public Set<Employee> getEmployees() { // Compliant
return employees;
}
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]