mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-16 14:23:28 +07:00
32 lines
1.4 KiB
HTML
32 lines
1.4 KiB
HTML
<html>
|
|
<body>
|
|
Reports multiple sequential <code>java.io.File</code> attribute checks, such as:
|
|
<ul>
|
|
<li><code>isDirectory()</code></li>
|
|
<li><code>isFile()</code></li>
|
|
<li><code>lastModified()</code></li>
|
|
<li><code>length()</code></li>
|
|
</ul>
|
|
Such calls can be replaced with a bulk <code>Files.readAttributes()</code> call.
|
|
This is usually more performant than multiple separate attribute checks.
|
|
<p>Example:</p>
|
|
<pre><code>
|
|
boolean isNewFile(File file, long lastModified) throws IOException {
|
|
return file.isFile() && file.lastModified() > lastModified;
|
|
}
|
|
</code></pre>
|
|
<p>After the quick-fix is applied:</p>
|
|
<pre><code>
|
|
boolean isNewFile(File file, long lastModified) throws IOException {
|
|
var fileAttributes = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
|
|
return fileAttributes.isRegularFile() && fileAttributes.lastModifiedTime().toMillis() > lastModified;
|
|
}
|
|
</code></pre>
|
|
<!-- tooltip end -->
|
|
<p>This inspection does not show a warning if <code>IOException</code> is not handled in the current context, but the quick-fix is still available.</p>
|
|
<p>Note that the replacements are usually not completely equivalent and should be applied with care. In particular, the behavior could differ if
|
|
the file does not exist at all.</p>
|
|
<p>This inspection only reports if the language level of the project or module is 7 or higher.</p>
|
|
<p><small>New in 2022.1</small></p>
|
|
</body>
|
|
</html> |