rspec/rules/S2150/java/rule.adoc

41 lines
1.2 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
When an SWT ``++Image++`` accesses a file directly, it holds the file handle for the life of the image. Do this many times, and the OS may run out of available file handles. At minimum, SWT ``++Image++``s which directly access files should not be ``++static++``. At best, they should access their files through ``++ImageDescriptors++``, which do not hold open file handles.
This rule looks for ``++org.eclipse.swt.graphics.Image++``s which both directly access a file on the file path and are ``++static++``.
=== 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
----
import org.eclipse.swt.graphics.Image;
public class MyView {
static Image myImage = new Image("path/to/file.png"); // Noncompliant
----
=== 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
----
import org.eclipse.swt.graphics.Image;
import org.eclipse.jface.resource.ImageDescriptor;
public class MyView {
static ImageDescription myDescriptor = ImageDescriptor.createFromFile("path/to/file.png"); // Doesn't hold file handle open
Image myImage = myDescriptor.getImage();
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
endif::env-github,rspecator-view[]