• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pmd / pmd / #3719

pending completion
#3719

push

github actions

adangel
Fix dogfood pmd violations

1 of 1 new or added line in 1 file covered. (100.0%)

66857 of 127267 relevant lines covered (52.53%)

0.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.35
/pmd-core/src/main/java/net/sourceforge/pmd/lang/document/NioTextFile.java
1
/*
2
 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3
 */
4

5
package net.sourceforge.pmd.lang.document;
6

7
import java.io.BufferedWriter;
8
import java.io.File;
9
import java.io.IOException;
10
import java.net.URI;
11
import java.nio.charset.Charset;
12
import java.nio.file.FileSystem;
13
import java.nio.file.Files;
14
import java.nio.file.Path;
15

16
import org.checkerframework.checker.nullness.qual.NonNull;
17
import org.checkerframework.checker.nullness.qual.Nullable;
18

19
import net.sourceforge.pmd.internal.util.AssertionUtil;
20
import net.sourceforge.pmd.internal.util.BaseCloseable;
21
import net.sourceforge.pmd.lang.LanguageVersion;
22

23
/**
24
 * A {@link TextFile} backed by a file in some {@link FileSystem}.
25
 */
26
class NioTextFile extends BaseCloseable implements TextFile {
27

28
    private final Path path;
29
    private final Charset charset;
30
    private final LanguageVersion languageVersion;
31
    private final @Nullable String displayName;
32
    private final String pathId;
33
    private boolean readOnly;
34

35
    NioTextFile(Path path,
36
                Charset charset,
37
                LanguageVersion languageVersion,
38
                @Nullable String displayName,
39
                boolean readOnly) {
1✔
40
        AssertionUtil.requireParamNotNull("path", path);
1✔
41
        AssertionUtil.requireParamNotNull("charset", charset);
1✔
42
        AssertionUtil.requireParamNotNull("language version", languageVersion);
1✔
43

44
        this.displayName = displayName;
1✔
45
        this.readOnly = readOnly;
1✔
46
        this.path = path;
1✔
47
        this.charset = charset;
1✔
48
        this.languageVersion = languageVersion;
1✔
49
        // using the URI here, that handles files inside zip archives automatically (schema "jar:file:...!/path/inside/zip")
50
        this.pathId = path.toUri().toString();
1✔
51
    }
1✔
52

53
    @Override
54
    public @NonNull LanguageVersion getLanguageVersion() {
55
        return languageVersion;
1✔
56
    }
57

58
    @Override
59
    public @NonNull String getDisplayName() {
60
        if (displayName != null) {
1✔
61
            return displayName;
1✔
62
        }
63
        if ("jar".equals(path.toUri().getScheme())) {
1✔
64
            return new File(URI.create(path.toUri().getSchemeSpecificPart()).getPath()).toString();
1✔
65
        }
66
        return path.toString();
1✔
67
    }
68

69
    @Override
70
    public String getPathId() {
71
        return pathId;
1✔
72
    }
73

74
    @Override
75
    public boolean isReadOnly() {
76
        return readOnly || !Files.isWritable(path);
1✔
77
    }
78

79
    @Override
80
    public void writeContents(TextFileContent content) throws IOException {
81
        ensureOpen();
1✔
82
        if (isReadOnly()) {
1✔
83
            throw new ReadOnlyFileException(this);
1✔
84
        }
85
        try (BufferedWriter bw = Files.newBufferedWriter(path, charset)) {
1✔
86
            if (TextFileContent.NORMALIZED_LINE_TERM.equals(content.getLineTerminator())) {
1✔
87
                content.getNormalizedText().writeFully(bw);
1✔
88
            } else {
89
                for (Chars line : content.getNormalizedText().lines()) {
1✔
90
                    line.writeFully(bw);
1✔
91
                    bw.write(content.getLineTerminator());
1✔
92
                }
1✔
93
            }
94
        }
95
    }
1✔
96

97
    @Override
98
    public TextFileContent readContents() throws IOException {
99
        ensureOpen();
1✔
100

101
        if (!Files.isRegularFile(path)) {
1✔
102
            throw new IOException("Not a regular file: " + path);
×
103
        }
104

105
        return TextFileContent.fromInputStream(Files.newInputStream(path), charset);
1✔
106
    }
107

108

109
    @Override
110
    protected void doClose() throws IOException {
111
        // nothing to do.
112
    }
1✔
113

114
    @Override
115
    public boolean equals(Object o) {
116
        if (this == o) {
1✔
117
            return true;
×
118
        }
119
        if (o == null || getClass() != o.getClass()) {
1✔
120
            return false;
1✔
121
        }
122
        @SuppressWarnings("PMD.CloseResource")
123
        NioTextFile that = (NioTextFile) o;
1✔
124
        return path.equals(that.path);
1✔
125
    }
126

127
    @Override
128
    public int hashCode() {
129
        return path.hashCode();
1✔
130
    }
131

132
    @Override
133
    public String toString() {
134
        return "NioTextFile[charset=" + charset + ", path=" + path + ']';
1✔
135
    }
136
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc