Skip to content

Commit

Permalink
Issue checkstyle#78: extract: grab property info
Browse files Browse the repository at this point in the history
  • Loading branch information
Luolc committed Aug 21, 2017
1 parent f92362c commit e01cedb
Show file tree
Hide file tree
Showing 15 changed files with 804 additions and 7 deletions.
5 changes: 5 additions & 0 deletions config/findbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@
<Class name="~.*\.Immutable.*"/>
<Bug pattern="NP_METHOD_PARAMETER_TIGHTENS_ANNOTATION"/>
</Match>
<Match>
<!-- We can't change generated source code. -->
<Class name="~.*\.GsonAdapters.*"/>
<Bug pattern="URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD"/>
</Match>
</FindBugsFilter>
5 changes: 5 additions & 0 deletions config/import-control.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@
<allow pkg="org.eclipse.jgit"/>
<allow pkg="org.apache.commons.lang"/>
</subpackage>

<subpackage name="module">
<allow pkg="com.puppycrawl.tools.checkstyle"/>
<allow class="java.lang.reflect.Field"/>
</subpackage>
</import-control>
6 changes: 5 additions & 1 deletion config/pmd.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@
<!-- we use class comments as source for xdoc files, so content is big and that is by design -->
<exclude name="CommentSize"/>
</rule>
<rule ref="rulesets/java/comments.xml/CommentRequired" />
<rule ref="rulesets/java/comments.xml/CommentRequired">
<properties>
<property name="publicMethodCommentRequirement" value="Ignored"/>
</properties>
</rule>
<rule ref="rulesets/java/comments.xml/CommentSize">
<properties>
<!-- we use class comments as source for xdoc files, so content is big and that is by design -->
Expand Down
2 changes: 1 addition & 1 deletion config/sevntu_suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">

<suppressions>
<suppress checks="MultipleStringLiteralsExtended" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="MultipleStringLiteralsExtended" files="[\\/]ExtractInfoGeneratorTest.java$|.*[\\/]src[\\/]test[\\/]"/>
</suppressions>
2 changes: 1 addition & 1 deletion config/suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<suppress checks="MagicNumber" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="AvoidStaticImport" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="WriteTag" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="MultipleStringLiterals" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="MultipleStringLiterals" files="[\\/]ExtractInfoGeneratorTest.java$|.*[\\/]src[\\/]test[\\/]"/>

<suppress checks="AbstractClassName" files=".*[\\/]data[\\/]"/>
</suppressions>
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,11 @@
<branchRate>0</branchRate>
<lineRate>0</lineRate>
</regex>
<regex>
<pattern>com.github.checkstyle.regression.module.UnitTestProcessorCheck</pattern>
<branchRate>76</branchRate>
<lineRate>92</lineRate>
</regex>
<regex>
<pattern>com.github.checkstyle.regression.report.ReportGenerator</pattern>
<branchRate>0</branchRate>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package com.github.checkstyle.regression.data;

import java.util.List;

import org.immutables.gson.Gson;
import org.immutables.value.Value;

Expand Down Expand Up @@ -48,11 +50,46 @@ public abstract class ModuleExtractInfo {
*/
public abstract String parent();

/**
* The properties of this module.
* @return the properties of this module
*/
public abstract List<ModuleProperty> properties();

/**
* The full qualified name of this module.
* @return the full qualified name of this module
*/
public String fullName() {
return packageName() + "." + name();
}

/** Represents a property of checkstyle module. */
@Gson.TypeAdapters
@Value.Immutable
public interface ModuleProperty {
/**
* The name of this property.
* @return the name of this property
*/
String name();

/**
* The type of this property.
* The value should be one of the followings:
* - Pattern
* - SeverityLevel
* - boolean
* - Scope
* - double[]
* - int[]
* - String[]
* - String
* - URI
* - AccessModifier[]
* - int
* @return the type of this property
*/
String type();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2017 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////

package com.github.checkstyle.regression.module;

import java.io.File;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.github.checkstyle.regression.data.ModuleInfo;
import com.puppycrawl.tools.checkstyle.Checker;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.TreeWalker;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.api.FileSetCheck;

/**
* Processes the unit test class of a checkstyle module.
* This utility class would run {@link UnitTestProcessorCheck} on the file with
* the given path and return the collected property info.
* @author LuoLiangchen
*/
public final class UnitTestProcessor {
/** Prevents instantiation. */
private UnitTestProcessor() {
}

/**
* Processes the unit test class with the given path.
* @param path the path of the unit test class
* @return the unit test method name to properties map
* @throws CheckstyleException failure when running the check
* @throws ReflectiveOperationException failure of reflection on checker and tree walker
*/
public static Map<String, Set<ModuleInfo.Property>> process(String path)
throws CheckstyleException, ReflectiveOperationException {
final DefaultConfiguration moduleConfig = createModuleConfig(UnitTestProcessorCheck.class);
final Configuration dc = createTreeWalkerConfig(moduleConfig);
final Checker checker = new Checker();
checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
checker.configure(dc);
final List<File> processedFiles = Collections.singletonList(new File(path));
checker.process(processedFiles);

final UnitTestProcessorCheck check = getCheckInstance(checker);
return check.getUnitTestToPropertiesMap();
}

/**
* Gets the instance of {@link UnitTestProcessorCheck} from the checker.
* @param checker the checker which run the check
* @return the instance of {@link UnitTestProcessorCheck}
* @throws ReflectiveOperationException failure of reflection
*/
@SuppressWarnings("unchecked")
private static UnitTestProcessorCheck getCheckInstance(Checker checker)
throws ReflectiveOperationException {
final Field fileSetChecks = checker.getClass().getDeclaredField("fileSetChecks");
fileSetChecks.setAccessible(true);
final TreeWalker treeWalker =
(TreeWalker) ((List<FileSetCheck>) fileSetChecks.get(checker)).get(0);
final Field ordinaryChecks = treeWalker.getClass().getDeclaredField("ordinaryChecks");
ordinaryChecks.setAccessible(true);
final Set<AbstractCheck> checks = (Set<AbstractCheck>) ordinaryChecks.get(treeWalker);
return (UnitTestProcessorCheck) checks.iterator().next();
}

/**
* Creates {@link DefaultConfiguration} for the {@link TreeWalker}
* based on the given {@link Configuration} instance.
* @param config {@link Configuration} instance.
* @return {@link DefaultConfiguration} for the {@link TreeWalker}
* based on the given {@link Configuration} instance.
*/
private static DefaultConfiguration createTreeWalkerConfig(Configuration config) {
final DefaultConfiguration dc =
new DefaultConfiguration("configuration");
final DefaultConfiguration twConf = createModuleConfig(TreeWalker.class);
// make sure that the tests always run with this charset
dc.addAttribute("charset", "UTF-8");
dc.addChild(twConf);
twConf.addChild(config);
return dc;
}

/**
* Creates {@link DefaultConfiguration} for the given class.
* @param clazz the class of module
* @return the {@link DefaultConfiguration} of the module class
*/
private static DefaultConfiguration createModuleConfig(Class<?> clazz) {
return new DefaultConfiguration(clazz.getName());
}
}
Loading

0 comments on commit e01cedb

Please sign in to comment.