Skip to content

Commit e01cedb

Browse files
committed
Issue #78: extract: grab property info
1 parent f92362c commit e01cedb

File tree

15 files changed

+804
-7
lines changed

15 files changed

+804
-7
lines changed

config/findbugs-exclude.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,9 @@
2727
<Class name="~.*\.Immutable.*"/>
2828
<Bug pattern="NP_METHOD_PARAMETER_TIGHTENS_ANNOTATION"/>
2929
</Match>
30+
<Match>
31+
<!-- We can't change generated source code. -->
32+
<Class name="~.*\.GsonAdapters.*"/>
33+
<Bug pattern="URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD"/>
34+
</Match>
3035
</FindBugsFilter>

config/import-control.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,9 @@
5151
<allow pkg="org.eclipse.jgit"/>
5252
<allow pkg="org.apache.commons.lang"/>
5353
</subpackage>
54+
55+
<subpackage name="module">
56+
<allow pkg="com.puppycrawl.tools.checkstyle"/>
57+
<allow class="java.lang.reflect.Field"/>
58+
</subpackage>
5459
</import-control>

config/pmd.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@
3434
<!-- we use class comments as source for xdoc files, so content is big and that is by design -->
3535
<exclude name="CommentSize"/>
3636
</rule>
37-
<rule ref="rulesets/java/comments.xml/CommentRequired" />
37+
<rule ref="rulesets/java/comments.xml/CommentRequired">
38+
<properties>
39+
<property name="publicMethodCommentRequirement" value="Ignored"/>
40+
</properties>
41+
</rule>
3842
<rule ref="rulesets/java/comments.xml/CommentSize">
3943
<properties>
4044
<!-- we use class comments as source for xdoc files, so content is big and that is by design -->

config/sevntu_suppressions.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
"http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
55

66
<suppressions>
7-
<suppress checks="MultipleStringLiteralsExtended" files=".*[\\/]src[\\/]test[\\/]"/>
7+
<suppress checks="MultipleStringLiteralsExtended" files="[\\/]ExtractInfoGeneratorTest.java$|.*[\\/]src[\\/]test[\\/]"/>
88
</suppressions>

config/suppressions.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<suppress checks="MagicNumber" files=".*[\\/]src[\\/]test[\\/]"/>
1414
<suppress checks="AvoidStaticImport" files=".*[\\/]src[\\/]test[\\/]"/>
1515
<suppress checks="WriteTag" files=".*[\\/]src[\\/]test[\\/]"/>
16-
<suppress checks="MultipleStringLiterals" files=".*[\\/]src[\\/]test[\\/]"/>
16+
<suppress checks="MultipleStringLiterals" files="[\\/]ExtractInfoGeneratorTest.java$|.*[\\/]src[\\/]test[\\/]"/>
1717

1818
<suppress checks="AbstractClassName" files=".*[\\/]data[\\/]"/>
1919
</suppressions>

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,11 @@
393393
<branchRate>0</branchRate>
394394
<lineRate>0</lineRate>
395395
</regex>
396+
<regex>
397+
<pattern>com.github.checkstyle.regression.module.UnitTestProcessorCheck</pattern>
398+
<branchRate>76</branchRate>
399+
<lineRate>92</lineRate>
400+
</regex>
396401
<regex>
397402
<pattern>com.github.checkstyle.regression.report.ReportGenerator</pattern>
398403
<branchRate>0</branchRate>

src/main/java/com/github/checkstyle/regression/data/ModuleExtractInfo.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
package com.github.checkstyle.regression.data;
2121

22+
import java.util.List;
23+
2224
import org.immutables.gson.Gson;
2325
import org.immutables.value.Value;
2426

@@ -48,11 +50,46 @@ public abstract class ModuleExtractInfo {
4850
*/
4951
public abstract String parent();
5052

53+
/**
54+
* The properties of this module.
55+
* @return the properties of this module
56+
*/
57+
public abstract List<ModuleProperty> properties();
58+
5159
/**
5260
* The full qualified name of this module.
5361
* @return the full qualified name of this module
5462
*/
5563
public String fullName() {
5664
return packageName() + "." + name();
5765
}
66+
67+
/** Represents a property of checkstyle module. */
68+
@Gson.TypeAdapters
69+
@Value.Immutable
70+
public interface ModuleProperty {
71+
/**
72+
* The name of this property.
73+
* @return the name of this property
74+
*/
75+
String name();
76+
77+
/**
78+
* The type of this property.
79+
* The value should be one of the followings:
80+
* - Pattern
81+
* - SeverityLevel
82+
* - boolean
83+
* - Scope
84+
* - double[]
85+
* - int[]
86+
* - String[]
87+
* - String
88+
* - URI
89+
* - AccessModifier[]
90+
* - int
91+
* @return the type of this property
92+
*/
93+
String type();
94+
}
5895
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
////////////////////////////////////////////////////////////////////////////////
2+
// checkstyle: Checks Java source code for adherence to a set of rules.
3+
// Copyright (C) 2001-2017 the original author or authors.
4+
//
5+
// This library is free software; you can redistribute it and/or
6+
// modify it under the terms of the GNU Lesser General Public
7+
// License as published by the Free Software Foundation; either
8+
// version 2.1 of the License, or (at your option) any later version.
9+
//
10+
// This library is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
// Lesser General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Lesser General Public
16+
// License along with this library; if not, write to the Free Software
17+
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18+
////////////////////////////////////////////////////////////////////////////////
19+
20+
package com.github.checkstyle.regression.module;
21+
22+
import java.io.File;
23+
import java.lang.reflect.Field;
24+
import java.util.Collections;
25+
import java.util.List;
26+
import java.util.Map;
27+
import java.util.Set;
28+
29+
import com.github.checkstyle.regression.data.ModuleInfo;
30+
import com.puppycrawl.tools.checkstyle.Checker;
31+
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
32+
import com.puppycrawl.tools.checkstyle.TreeWalker;
33+
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
34+
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
35+
import com.puppycrawl.tools.checkstyle.api.Configuration;
36+
import com.puppycrawl.tools.checkstyle.api.FileSetCheck;
37+
38+
/**
39+
* Processes the unit test class of a checkstyle module.
40+
* This utility class would run {@link UnitTestProcessorCheck} on the file with
41+
* the given path and return the collected property info.
42+
* @author LuoLiangchen
43+
*/
44+
public final class UnitTestProcessor {
45+
/** Prevents instantiation. */
46+
private UnitTestProcessor() {
47+
}
48+
49+
/**
50+
* Processes the unit test class with the given path.
51+
* @param path the path of the unit test class
52+
* @return the unit test method name to properties map
53+
* @throws CheckstyleException failure when running the check
54+
* @throws ReflectiveOperationException failure of reflection on checker and tree walker
55+
*/
56+
public static Map<String, Set<ModuleInfo.Property>> process(String path)
57+
throws CheckstyleException, ReflectiveOperationException {
58+
final DefaultConfiguration moduleConfig = createModuleConfig(UnitTestProcessorCheck.class);
59+
final Configuration dc = createTreeWalkerConfig(moduleConfig);
60+
final Checker checker = new Checker();
61+
checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
62+
checker.configure(dc);
63+
final List<File> processedFiles = Collections.singletonList(new File(path));
64+
checker.process(processedFiles);
65+
66+
final UnitTestProcessorCheck check = getCheckInstance(checker);
67+
return check.getUnitTestToPropertiesMap();
68+
}
69+
70+
/**
71+
* Gets the instance of {@link UnitTestProcessorCheck} from the checker.
72+
* @param checker the checker which run the check
73+
* @return the instance of {@link UnitTestProcessorCheck}
74+
* @throws ReflectiveOperationException failure of reflection
75+
*/
76+
@SuppressWarnings("unchecked")
77+
private static UnitTestProcessorCheck getCheckInstance(Checker checker)
78+
throws ReflectiveOperationException {
79+
final Field fileSetChecks = checker.getClass().getDeclaredField("fileSetChecks");
80+
fileSetChecks.setAccessible(true);
81+
final TreeWalker treeWalker =
82+
(TreeWalker) ((List<FileSetCheck>) fileSetChecks.get(checker)).get(0);
83+
final Field ordinaryChecks = treeWalker.getClass().getDeclaredField("ordinaryChecks");
84+
ordinaryChecks.setAccessible(true);
85+
final Set<AbstractCheck> checks = (Set<AbstractCheck>) ordinaryChecks.get(treeWalker);
86+
return (UnitTestProcessorCheck) checks.iterator().next();
87+
}
88+
89+
/**
90+
* Creates {@link DefaultConfiguration} for the {@link TreeWalker}
91+
* based on the given {@link Configuration} instance.
92+
* @param config {@link Configuration} instance.
93+
* @return {@link DefaultConfiguration} for the {@link TreeWalker}
94+
* based on the given {@link Configuration} instance.
95+
*/
96+
private static DefaultConfiguration createTreeWalkerConfig(Configuration config) {
97+
final DefaultConfiguration dc =
98+
new DefaultConfiguration("configuration");
99+
final DefaultConfiguration twConf = createModuleConfig(TreeWalker.class);
100+
// make sure that the tests always run with this charset
101+
dc.addAttribute("charset", "UTF-8");
102+
dc.addChild(twConf);
103+
twConf.addChild(config);
104+
return dc;
105+
}
106+
107+
/**
108+
* Creates {@link DefaultConfiguration} for the given class.
109+
* @param clazz the class of module
110+
* @return the {@link DefaultConfiguration} of the module class
111+
*/
112+
private static DefaultConfiguration createModuleConfig(Class<?> clazz) {
113+
return new DefaultConfiguration(clazz.getName());
114+
}
115+
}

0 commit comments

Comments
 (0)