Skip to content

Commit 00375a0

Browse files
author
Polina Volkhontseva
committedApr 25, 2022
Excluding files from Checkstyle validation
1 parent 382aa0c commit 00375a0

File tree

1 file changed

+39
-18
lines changed

1 file changed

+39
-18
lines changed
 

‎qulice-checkstyle/src/main/java/com/qulice/checkstyle/CheckstyleValidator.java

+39-18
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
*/
3030
package com.qulice.checkstyle;
3131

32-
import com.google.common.collect.Lists;
3332
import com.jcabi.aspects.Tv;
3433
import com.jcabi.log.Logger;
3534
import com.puppycrawl.tools.checkstyle.Checker;
@@ -73,31 +72,38 @@ public final class CheckstyleValidator implements ResourceValidator {
7372
*/
7473
private final CheckstyleListener listener;
7574

75+
/**
76+
* Environment to use.
77+
*/
78+
private final Environment env;
79+
7680
/**
7781
* Constructor.
7882
* @param env Environment to use.
7983
*/
8084
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
8185
public CheckstyleValidator(final Environment env) {
86+
this.env = env;
8287
this.checker = new Checker();
83-
this.checker.setClassLoader(env.classloader());
88+
this.checker.setClassLoader(this.env.classloader());
8489
this.checker.setModuleClassLoader(
8590
Thread.currentThread().getContextClassLoader()
8691
);
8792
try {
88-
this.checker.configure(this.configuration(env));
93+
this.checker.configure(this.configuration());
8994
} catch (final CheckstyleException ex) {
9095
throw new IllegalStateException("Failed to configure checker", ex);
9196
}
92-
this.listener = new CheckstyleListener(env);
97+
this.listener = new CheckstyleListener(this.env);
9398
this.checker.addListener(this.listener);
9499
}
95100

96101
@Override
97102
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
98103
public Collection<Violation> validate(final Collection<File> files) {
104+
final List<File> sources = this.getNonExcludedFiles(files);
99105
try {
100-
this.checker.process(Lists.newArrayList(files));
106+
this.checker.process(sources);
101107
} catch (final CheckstyleException ex) {
102108
throw new IllegalStateException("Failed to process files", ex);
103109
}
@@ -122,15 +128,32 @@ public Collection<Violation> validate(final Collection<File> files) {
122128
return "Checkstyle";
123129
}
124130

131+
/**
132+
* Filters out excluded files from further validation.
133+
* @param files Files to validate
134+
* @return List of relevant files
135+
*/
136+
public List<File> getNonExcludedFiles(final Collection<File> files) {
137+
final List<File> relevant = new LinkedList<>();
138+
for (final File file : files) {
139+
final String name = file.getPath().substring(
140+
this.env.basedir().toString().length()
141+
);
142+
if (!this.env.exclude("checkstyle", name)) {
143+
relevant.add(file);
144+
}
145+
}
146+
return relevant;
147+
}
148+
125149
/**
126150
* Load checkstyle configuration.
127-
* @param env The environment
128151
* @return The configuration just loaded
129152
* @see #validate(Collection)
130153
*/
131-
private Configuration configuration(final Environment env) {
154+
private Configuration configuration() {
132155
final File cache =
133-
new File(env.tempdir(), "checkstyle/checkstyle.cache");
156+
new File(this.env.tempdir(), "checkstyle/checkstyle.cache");
134157
final File parent = cache.getParentFile();
135158
if (!parent.exists() && !parent.mkdirs()) {
136159
throw new IllegalStateException(
@@ -142,7 +165,7 @@ private Configuration configuration(final Environment env) {
142165
}
143166
final Properties props = new Properties();
144167
props.setProperty("cache.file", cache.getPath());
145-
props.setProperty("header", this.header(env));
168+
props.setProperty("header", this.header());
146169
final InputSource src = new InputSource(
147170
this.getClass().getResourceAsStream("checks.xml")
148171
);
@@ -161,13 +184,12 @@ private Configuration configuration(final Environment env) {
161184

162185
/**
163186
* Create header content, from file.
164-
* @param env The environment
165187
* @return The content of header
166-
* @see #configuration(Environment)
188+
* @see #configuration()
167189
*/
168-
private String header(final Environment env) {
169-
final String name = env.param("license", "LICENSE.txt");
170-
final URL url = CheckstyleValidator.toUrl(env, name);
190+
private String header() {
191+
final String name = this.env.param("license", "LICENSE.txt");
192+
final URL url = this.toUrl(name);
171193
final String content;
172194
try {
173195
content = new Replaced(
@@ -206,12 +228,11 @@ private String header(final Environment env) {
206228

207229
/**
208230
* Convert file name to URL.
209-
* @param env The environment
210231
* @param name The name of file
211232
* @return The URL
212-
* @see #header(Environment)
233+
* @see #header()
213234
*/
214-
private static URL toUrl(final Environment env, final String name) {
235+
private URL toUrl(final String name) {
215236
final URL url;
216237
if (name.startsWith("file:")) {
217238
try {
@@ -220,7 +241,7 @@ private static URL toUrl(final Environment env, final String name) {
220241
throw new IllegalStateException("Invalid URL", ex);
221242
}
222243
} else {
223-
url = env.classloader().getResource(name);
244+
url = this.env.classloader().getResource(name);
224245
if (url == null) {
225246
throw new IllegalStateException(
226247
String.format(

0 commit comments

Comments
 (0)
Please sign in to comment.