29
29
*/
30
30
package com .qulice .checkstyle ;
31
31
32
- import com .google .common .collect .Lists ;
33
32
import com .jcabi .aspects .Tv ;
34
33
import com .jcabi .log .Logger ;
35
34
import com .puppycrawl .tools .checkstyle .Checker ;
@@ -73,31 +72,38 @@ public final class CheckstyleValidator implements ResourceValidator {
73
72
*/
74
73
private final CheckstyleListener listener ;
75
74
75
+ /**
76
+ * Environment to use.
77
+ */
78
+ private final Environment env ;
79
+
76
80
/**
77
81
* Constructor.
78
82
* @param env Environment to use.
79
83
*/
80
84
@ SuppressWarnings ("PMD.ConstructorOnlyInitializesOrCallOtherConstructors" )
81
85
public CheckstyleValidator (final Environment env ) {
86
+ this .env = env ;
82
87
this .checker = new Checker ();
83
- this .checker .setClassLoader (env .classloader ());
88
+ this .checker .setClassLoader (this . env .classloader ());
84
89
this .checker .setModuleClassLoader (
85
90
Thread .currentThread ().getContextClassLoader ()
86
91
);
87
92
try {
88
- this .checker .configure (this .configuration (env ));
93
+ this .checker .configure (this .configuration ());
89
94
} catch (final CheckstyleException ex ) {
90
95
throw new IllegalStateException ("Failed to configure checker" , ex );
91
96
}
92
- this .listener = new CheckstyleListener (env );
97
+ this .listener = new CheckstyleListener (this . env );
93
98
this .checker .addListener (this .listener );
94
99
}
95
100
96
101
@ Override
97
102
@ SuppressWarnings ("PMD.AvoidInstantiatingObjectsInLoops" )
98
103
public Collection <Violation > validate (final Collection <File > files ) {
104
+ final List <File > sources = this .getNonExcludedFiles (files );
99
105
try {
100
- this .checker .process (Lists . newArrayList ( files ) );
106
+ this .checker .process (sources );
101
107
} catch (final CheckstyleException ex ) {
102
108
throw new IllegalStateException ("Failed to process files" , ex );
103
109
}
@@ -122,15 +128,32 @@ public Collection<Violation> validate(final Collection<File> files) {
122
128
return "Checkstyle" ;
123
129
}
124
130
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
+
125
149
/**
126
150
* Load checkstyle configuration.
127
- * @param env The environment
128
151
* @return The configuration just loaded
129
152
* @see #validate(Collection)
130
153
*/
131
- private Configuration configuration (final Environment env ) {
154
+ private Configuration configuration () {
132
155
final File cache =
133
- new File (env .tempdir (), "checkstyle/checkstyle.cache" );
156
+ new File (this . env .tempdir (), "checkstyle/checkstyle.cache" );
134
157
final File parent = cache .getParentFile ();
135
158
if (!parent .exists () && !parent .mkdirs ()) {
136
159
throw new IllegalStateException (
@@ -142,7 +165,7 @@ private Configuration configuration(final Environment env) {
142
165
}
143
166
final Properties props = new Properties ();
144
167
props .setProperty ("cache.file" , cache .getPath ());
145
- props .setProperty ("header" , this .header (env ));
168
+ props .setProperty ("header" , this .header ());
146
169
final InputSource src = new InputSource (
147
170
this .getClass ().getResourceAsStream ("checks.xml" )
148
171
);
@@ -161,13 +184,12 @@ private Configuration configuration(final Environment env) {
161
184
162
185
/**
163
186
* Create header content, from file.
164
- * @param env The environment
165
187
* @return The content of header
166
- * @see #configuration(Environment )
188
+ * @see #configuration()
167
189
*/
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 );
171
193
final String content ;
172
194
try {
173
195
content = new Replaced (
@@ -206,12 +228,11 @@ private String header(final Environment env) {
206
228
207
229
/**
208
230
* Convert file name to URL.
209
- * @param env The environment
210
231
* @param name The name of file
211
232
* @return The URL
212
- * @see #header(Environment )
233
+ * @see #header()
213
234
*/
214
- private static URL toUrl (final Environment env , final String name ) {
235
+ private URL toUrl (final String name ) {
215
236
final URL url ;
216
237
if (name .startsWith ("file:" )) {
217
238
try {
@@ -220,7 +241,7 @@ private static URL toUrl(final Environment env, final String name) {
220
241
throw new IllegalStateException ("Invalid URL" , ex );
221
242
}
222
243
} else {
223
- url = env .classloader ().getResource (name );
244
+ url = this . env .classloader ().getResource (name );
224
245
if (url == null ) {
225
246
throw new IllegalStateException (
226
247
String .format (
0 commit comments