-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue #19: Module Component #33
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/java/com/github/checkstyle/regression/module/ModuleCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// 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.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import com.github.checkstyle.regression.data.GitChange; | ||
import com.github.checkstyle.regression.data.ModifiableModuleInfo; | ||
import com.github.checkstyle.regression.data.ModuleExtractInfo; | ||
import com.github.checkstyle.regression.data.ModuleInfo; | ||
|
||
/** | ||
* Collects all the necessary information for the generation, in module level. | ||
* @author LuoLiangchen | ||
*/ | ||
public final class ModuleCollector { | ||
/** Prevents instantiation. */ | ||
private ModuleCollector() { | ||
} | ||
|
||
/** | ||
* Generates the module information from a list of changes. | ||
* @param changes the changes source | ||
* @return the module information generated from the given changes | ||
*/ | ||
public static List<ModuleInfo> generate(List<GitChange> changes) { | ||
final Map<String, ModifiableModuleInfo> moduleInfos = new LinkedHashMap<>(); | ||
|
||
for (GitChange change : changes) { | ||
if (ModuleUtils.isCheckstyleModule(change)) { | ||
final String fullName = ModuleUtils.convertJavaSourceChangeToFullName(change); | ||
final ModuleExtractInfo extractInfo = ModuleUtils.getModuleExtractInfo(fullName); | ||
final ModifiableModuleInfo moduleInfo = ModifiableModuleInfo.create() | ||
.setModuleExtractInfo(extractInfo); | ||
moduleInfos.put(extractInfo.fullName(), moduleInfo); | ||
} | ||
} | ||
|
||
return moduleInfos.values().stream() | ||
.map(ModifiableModuleInfo::toImmutable) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
191 changes: 191 additions & 0 deletions
191
src/main/java/com/github/checkstyle/regression/module/ModuleUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// 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.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
import com.github.checkstyle.regression.data.GitChange; | ||
import com.github.checkstyle.regression.data.ModuleExtractInfo; | ||
|
||
/** | ||
* Contains utility methods related to checkstyle module. | ||
* @author LuoLiangchen | ||
*/ | ||
public final class ModuleUtils { | ||
/** The compiled regex pattern of the path of Java source files. */ | ||
private static final Pattern JAVA_SOURCE_PARTTEN = | ||
Pattern.compile("src/(main|test)/java/(.+)\\.java"); | ||
|
||
/** The postfix of a test of a checkstyle module. */ | ||
private static final String TEST_POSTFIX = "Test"; | ||
|
||
/** The map of full qualified name to module extract info. */ | ||
private static final Map<String, ModuleExtractInfo> NAME_TO_MODULE_EXTRACT_INFO = | ||
new HashMap<>(); | ||
|
||
/** Prevents instantiation. */ | ||
private ModuleUtils() { | ||
} | ||
|
||
/** | ||
* Sets the map of full qualified name to module extract info with the given map. | ||
* @param map the given map | ||
*/ | ||
public static void setNameToModuleExtractInfo(Map<String, ModuleExtractInfo> map) { | ||
NAME_TO_MODULE_EXTRACT_INFO.clear(); | ||
NAME_TO_MODULE_EXTRACT_INFO.putAll(map); | ||
} | ||
|
||
/** | ||
* Checks whether the corresponding file of a change may be considered as | ||
* a checkstyle module. | ||
* Changes of checkstyle modules are Java main source files, of which full | ||
* qualified names are in the extract module info map. | ||
* @param change change to check | ||
* @return true if the corresponding file of a change may be considered as | ||
* a checkstyle module | ||
*/ | ||
public static boolean isCheckstyleModule(GitChange change) { | ||
final boolean returnValue; | ||
if (isJavaMainSource(change)) { | ||
final String fullName = convertJavaSourceChangeToFullName(change); | ||
returnValue = NAME_TO_MODULE_EXTRACT_INFO.containsKey(fullName); | ||
} | ||
else { | ||
returnValue = false; | ||
} | ||
return returnValue; | ||
} | ||
|
||
/** | ||
* Checks whether the corresponding file of a change may be considered as | ||
* a checkstyle utility class. | ||
* Changes of checkstyle utility classes are Java main source files, of which full | ||
* qualified names are not in the extract module info map. | ||
* @param change change to check | ||
* @return true if the corresponding file of a change may be considered as | ||
* a checkstyle utility class. | ||
*/ | ||
public static boolean isCheckstyleUtility(GitChange change) { | ||
final boolean returnValue; | ||
if (isJavaMainSource(change)) { | ||
final String fullName = convertJavaSourceChangeToFullName(change); | ||
returnValue = !NAME_TO_MODULE_EXTRACT_INFO.containsKey(fullName); | ||
} | ||
else { | ||
returnValue = false; | ||
} | ||
return returnValue; | ||
} | ||
|
||
/** | ||
* Checks whether the corresponding file of a change may be considered as | ||
* a test of checkstyle module. | ||
* Changes of checkstyle module tests are Java test source files, of which full | ||
* qualified names end with "Test" and the full names of corresponding modules are | ||
* in the extract module info map. | ||
* @param change change to check | ||
* @return true if the corresponding file of a change may be considered as | ||
* a test of checkstyle module | ||
*/ | ||
public static boolean isCheckstyleModuleTest(GitChange change) { | ||
final boolean returnValue; | ||
if (isJavaTestSource(change)) { | ||
final String fullName = convertJavaSourceChangeToFullName(change); | ||
if (fullName.endsWith(TEST_POSTFIX)) { | ||
returnValue = NAME_TO_MODULE_EXTRACT_INFO.containsKey( | ||
fullName.substring(0, fullName.length() - TEST_POSTFIX.length())); | ||
} | ||
else { | ||
returnValue = false; | ||
} | ||
} | ||
else { | ||
returnValue = false; | ||
} | ||
return returnValue; | ||
} | ||
|
||
/** | ||
* Checks whether the corresponding file of a change may be considered as | ||
* a Java main source file. | ||
* Changes of Java main source files are which have path matching | ||
* {@code JAVA_SOURCE_PARTTEN} and in "main" directory. | ||
* @param change change to check | ||
* @return true if the corresponding file of a change may be considered as | ||
* a Java main source file. | ||
*/ | ||
private static boolean isJavaMainSource(GitChange change) { | ||
final boolean returnValue; | ||
final Matcher matcher = JAVA_SOURCE_PARTTEN.matcher(change.getPath()); | ||
if (matcher.find()) { | ||
returnValue = "main".equals(matcher.group(1)); | ||
} | ||
else { | ||
returnValue = false; | ||
} | ||
return returnValue; | ||
} | ||
|
||
/** | ||
* Checks whether the corresponding file of a change may be considered as | ||
* a Java test source file. | ||
* Changes of Java test source files are which have path matching | ||
* {@code JAVA_SOURCE_PARTTEN} and in "test" directory. | ||
* @param change change to check | ||
* @return true if the corresponding file of a change may be considered as | ||
* a Java test source file. | ||
*/ | ||
private static boolean isJavaTestSource(GitChange change) { | ||
final boolean returnValue; | ||
final Matcher matcher = JAVA_SOURCE_PARTTEN.matcher(change.getPath()); | ||
if (matcher.find()) { | ||
returnValue = "test".equals(matcher.group(1)); | ||
} | ||
else { | ||
returnValue = false; | ||
} | ||
return returnValue; | ||
} | ||
|
||
/** | ||
* Gets the module extract info from the given full qualified name. | ||
* @param fullName the given full qualified name | ||
* @return the module extract info got from the given full qualified name | ||
*/ | ||
public static ModuleExtractInfo getModuleExtractInfo(String fullName) { | ||
return NAME_TO_MODULE_EXTRACT_INFO.get(fullName); | ||
} | ||
|
||
/** | ||
* Converts a change of Java source file to its full qualified name. | ||
* @param change the change instance of Java source file | ||
* @return the corresponding full qualified name | ||
*/ | ||
public static String convertJavaSourceChangeToFullName(GitChange change) { | ||
return Arrays.stream(JAVA_SOURCE_PARTTEN.matcher(change.getPath()) | ||
.replaceAll("$2").split("/")).collect(Collectors.joining(".")); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/github/checkstyle/regression/module/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// 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 | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
/** | ||
* Contains the module information generation and utility classes. | ||
*/ | ||
package com.github.checkstyle.regression.module; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last thing, please group
isJava...
methods together. Do the same forisCheckstyle...
methods.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.