Skip to content

Commit f614b2e

Browse files
committed
Allow file level checks to be excluded
Update the filtering logic so that top-level file checks can also be excluded. Closes gh-210
1 parent 92e9ad9 commit f614b2e

File tree

4 files changed

+36
-22
lines changed

4 files changed

+36
-22
lines changed

spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/FilteredModuleFactory.java

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2019 the original author or authors.
2+
* Copyright 2017-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
import com.puppycrawl.tools.checkstyle.TreeWalkerFilter;
2424
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
2525
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
26+
import com.puppycrawl.tools.checkstyle.api.Configuration;
2627

2728
class FilteredModuleFactory implements ModuleFactory {
2829

@@ -47,17 +48,25 @@ public boolean accept(TreeWalkerAuditEvent treeWalkerAuditEvent) {
4748
@Override
4849
public Object createModule(String name) throws CheckstyleException {
4950
Object module = this.moduleFactory.createModule(name);
50-
if (module instanceof AbstractCheck) {
51-
module = filter((AbstractCheck) module);
51+
if (isFiltered(module)) {
52+
if (module instanceof AbstractCheck) {
53+
return FILTERED;
54+
}
55+
throw new IllegalStateException("Unable to filter module " + module.getClass().getName());
5256
}
5357
return module;
5458
}
5559

56-
private Object filter(AbstractCheck check) {
57-
if (this.excludes != null && this.excludes.contains(check.getClass().getName())) {
58-
return FILTERED;
59-
}
60-
return check;
60+
boolean nonFiltered(Configuration configuration) {
61+
return !isFiltered(configuration.getName());
62+
}
63+
64+
private boolean isFiltered(Object module) {
65+
return isFiltered(module.getClass().getName());
66+
}
67+
68+
private boolean isFiltered(String name) {
69+
return this.excludes != null && this.excludes.contains(name);
6170
}
6271

6372
}

spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/SpringChecks.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2019 the original author or authors.
2+
* Copyright 2017-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -87,7 +87,7 @@ public void setModuleFactory(ModuleFactory moduleFactory) {
8787

8888
@Override
8989
public void finishLocalSetup() {
90-
ModuleFactory moduleFactory = new FilteredModuleFactory(this.moduleFactory, this.excludes);
90+
FilteredModuleFactory moduleFactory = new FilteredModuleFactory(this.moduleFactory, this.excludes);
9191
DefaultContext context = new DefaultContext();
9292
context.add("classLoader", this.classLoader);
9393
context.add("severity", getSeverity());

spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/SpringConfigurationLoader.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2019 the original author or authors.
2+
* Copyright 2017-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,7 +23,6 @@
2323

2424
import com.puppycrawl.tools.checkstyle.ConfigurationLoader;
2525
import com.puppycrawl.tools.checkstyle.ConfigurationLoader.IgnoredModulesOptions;
26-
import com.puppycrawl.tools.checkstyle.ModuleFactory;
2726
import com.puppycrawl.tools.checkstyle.PropertyResolver;
2827
import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
2928
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
@@ -42,17 +41,18 @@ class SpringConfigurationLoader {
4241

4342
private final Context context;
4443

45-
private final ModuleFactory moduleFactory;
44+
private final FilteredModuleFactory moduleFactory;
4645

47-
SpringConfigurationLoader(Context context, ModuleFactory moduleFactory) {
46+
SpringConfigurationLoader(Context context, FilteredModuleFactory moduleFactory) {
4847
this.context = context;
4948
this.moduleFactory = moduleFactory;
5049
}
5150

5251
public Collection<FileSetCheck> load(PropertyResolver propertyResolver) {
5352
Configuration config = loadConfiguration(getClass().getResourceAsStream("spring-checkstyle.xml"),
5453
propertyResolver);
55-
return Arrays.stream(config.getChildren()).map(this::load).collect(Collectors.toList());
54+
return Arrays.stream(config.getChildren()).filter(this.moduleFactory::nonFiltered).map(this::load)
55+
.collect(Collectors.toList());
5656
}
5757

5858
private Configuration loadConfiguration(InputStream inputStream, PropertyResolver propertyResolver) {

spring-javaformat/spring-javaformat-checkstyle/src/test/java/io/spring/javaformat/checkstyle/SpringConfigurationLoaderTests.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2019 the original author or authors.
2+
* Copyright 2017-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,7 +22,6 @@
2222
import java.util.Set;
2323

2424
import com.puppycrawl.tools.checkstyle.DefaultContext;
25-
import com.puppycrawl.tools.checkstyle.ModuleFactory;
2625
import com.puppycrawl.tools.checkstyle.PackageObjectFactory;
2726
import com.puppycrawl.tools.checkstyle.PropertiesExpander;
2827
import com.puppycrawl.tools.checkstyle.PropertyResolver;
@@ -63,13 +62,19 @@ public void loadWithExcludeShouldExcludeChecks() {
6362
assertThat(ordinaryChecks).hasSize(59);
6463
}
6564

65+
@Test
66+
public void loadWithExcludeHeaderShouldExcludeChecks() {
67+
Set<String> excludes = Collections.singleton("io.spring.javaformat.checkstyle.check.SpringHeaderCheck");
68+
Object[] checks = load(excludes).stream().toArray();
69+
assertThat(checks).hasSize(2);
70+
}
71+
6672
private Collection<FileSetCheck> load(Set<String> excludes) {
6773
DefaultContext context = new DefaultContext();
68-
ModuleFactory moduleFactory = new PackageObjectFactory(getClass().getPackage().getName(),
69-
getClass().getClassLoader());
70-
moduleFactory = new FilteredModuleFactory(moduleFactory, excludes);
71-
context.add("moduleFactory", moduleFactory);
72-
Collection<FileSetCheck> checks = new SpringConfigurationLoader(context, moduleFactory)
74+
FilteredModuleFactory filteredModuleFactory = new FilteredModuleFactory(
75+
new PackageObjectFactory(getClass().getPackage().getName(), getClass().getClassLoader()), excludes);
76+
context.add("moduleFactory", filteredModuleFactory);
77+
Collection<FileSetCheck> checks = new SpringConfigurationLoader(context, filteredModuleFactory)
7378
.load(getPropertyResolver());
7479
return checks;
7580
}

0 commit comments

Comments
 (0)