Skip to content

Commit

Permalink
Issue #37: json file processor
Browse files Browse the repository at this point in the history
  • Loading branch information
Luolc committed Jun 26, 2017
1 parent d5b776e commit 19e50c8
Show file tree
Hide file tree
Showing 11 changed files with 2,708 additions and 16 deletions.
7 changes: 7 additions & 0 deletions config/findbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@
<Bug pattern="BC_UNCONFIRMED_CAST_OF_RETURN_VALUE"/>
</Or>
</Match>
<Match>
<Or>
<Class name="~.*\.GsonAdapters.*"/>
<Class name="~.*\.Immutable.*"/>
<Class name="~.*\.Modifiable.*"/>
</Or>
</Match>
</FindBugsFilter>
8 changes: 8 additions & 0 deletions config/import-control.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
<allow pkg="java.io"/>
<allow pkg="java.util"/>
<allow pkg="com.github.checkstyle.regression.data"/>
<allow pkg="org.immutables"/>

<subpackage name="extract">
<allow pkg="com.google.gson"/>
<allow class="com.sun.tools.javac.util.ServiceLoader"/>
<allow class="java.nio.charset.Charset"/>
</subpackage>

<subpackage name="git">
<allow pkg="org.eclipse.jgit"/>
</subpackage>
Expand Down
4 changes: 3 additions & 1 deletion config/pmd.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@
<rule ref="rulesets/java/naming.xml">
<!-- we use CheckstyleCustomShortVariable, to control lenght (will be fixed in PMD 5.4) and skip Override methods -->
<exclude name="ShortVariable"/>
<!-- we already have AbstractClassName for checking Abstract class name. It is annoying to set suppression in both
checkstyle and PMD. -->
<exclude name="AbstractNaming"/>
</rule>
<rule name="CheckstyleCustomShortVariable"
message="Avoid variables with short names that shorter than 2 symbols: {0}"
Expand All @@ -164,7 +167,6 @@
</property>
</properties>
</rule>
<rule ref="rulesets/java/naming.xml/AbstractNaming" />
<rule ref="rulesets/java/naming.xml/LongVariable">
<properties>
<!-- nothing bad in long and descriptive variable names if only they fit line, but still keep it reasonable -->
Expand Down
2 changes: 2 additions & 0 deletions config/suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@
<suppress checks="AvoidStaticImport" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="WriteTag" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="MultipleStringLiterals" files=".*[\\/]src[\\/]test[\\/]"/>

<suppress checks="AbstractClassName" files=".*[\\/]data[\\/]"/>
</suppressions>
56 changes: 41 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,25 @@

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
<groupId>org.apache.maven</groupId>
<artifactId>maven-jxr</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.6</version>
<scope>test</scope>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.6</version>
<scope>test</scope>
<groupId>org.immutables</groupId>
<artifactId>value</artifactId>
<version>2.5.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-jxr</artifactId>
<version>2.5</version>
<groupId>org.immutables</groupId>
<artifactId>gson</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.eclipse.jgit</groupId>
Expand All @@ -55,6 +53,26 @@
<artifactId>checkstyle</artifactId>
<version>${checkstyle.version}</version>
</dependency>

<!-- Test scope dependencies. -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.6</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -267,10 +285,18 @@
<branchRate>0</branchRate>
<lineRate>0</lineRate>
</regex>
<regex>
<pattern>com.github.checkstyle.regression.data.ModuleInfo</pattern>
<branchRate>0</branchRate>
<lineRate>0</lineRate>
</regex>
</regexes>
</check>
<instrumentation>
<excludes>
<exclude>**/GsonAdapters*.class</exclude>
<exclude>**/Immutable*.class</exclude>
<exclude>**/Modifiable*.class</exclude>
</excludes>
</instrumentation>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
////////////////////////////////////////////////////////////////////////////////
// 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.data;

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

/**
* Represents the extract information of a checkstyle module.
* @author LuoLiangchen
*/
@Gson.TypeAdapters
@Value.Immutable
public abstract class ModuleExtractInfo {
/**
* The package name of this module.
* @return the package name of this module
*/
public abstract String packageName();

/**
* The name of this module.
* @return the name of this module.
*/
public abstract String name();

/**
* The parent of this module.
* The value should be either "TreeWalker" or "Checker".
* @return the parent of this module.
*/
public abstract String parent();

/**
* The full qualified name of this module.
* @return the full qualified name of this module
*/
public String fullName() {
return packageName() + "." + name();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
////////////////////////////////////////////////////////////////////////////////
// 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.data;

import java.util.List;

import org.immutables.value.Value;

/**
* Represents the related information of a checkstyle module.
* Including the extract module information, change of the module class, change
* of the corresponding test and changed of the utility classes that the module
* depends on.
* @author LuoLiangchen
*/
@Value.Immutable
@Value.Modifiable
public abstract class ModuleInfo {
/**
* The extract information of the module.
* @return the extract information of the module
*/
public abstract ModuleExtractInfo moduleExtractInfo();

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

/**
* The name of the module.
* @return the name of the module
*/
public String name() {
return moduleExtractInfo().name();
}

/**
* Represents the property and its settings of a module.
*/
@Value.Immutable
public interface Property {
/**
* The name of the property.
* @return the name of the property
*/
String name();

/**
* The value of the property.
* @return the value of the property
*/
String value();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
////////////////////////////////////////////////////////////////////////////////
// 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.extract;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.github.checkstyle.regression.data.ModuleExtractInfo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.sun.tools.javac.util.ServiceLoader;

/**
* Processes the module extract info grabbed from checkstyle project.
* @author LuoLiangchen
*/
public final class ExtractInfoProcessor {
/** The {@link Gson} instance. */
private static final Gson GSON;

/** Prevents instantiation. */
private ExtractInfoProcessor() {
}

static {
final GsonBuilder gsonBuilder = new GsonBuilder();
for (TypeAdapterFactory factory : ServiceLoader.load(TypeAdapterFactory.class)) {
gsonBuilder.registerTypeAdapterFactory(factory);
}
GSON = gsonBuilder.create();
}

/**
* Gets the full qualified name to module extract info map from the given input stream.
* @param stream the given input stream
* @return the full qualified name to module extract info map
*/
public static Map<String, ModuleExtractInfo> getNameToModuleExtractInfoFromInputStream(
InputStream stream) {
final List<ModuleExtractInfo> modules;

try {
final InputStreamReader reader =
new InputStreamReader(stream, Charset.forName("UTF-8"));

try {
modules = GSON.fromJson(reader, new TypeToken<List<ModuleExtractInfo>>() {
}.getType());
}
finally {
reader.close();
}
}
catch (IOException ex) {
throw new IllegalStateException(
"Failed when loaing hardcoded checkstyle module information", ex);
}

return modules.stream().collect(Collectors.toMap(
ModuleExtractInfo::fullName, extractInfo -> extractInfo));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
////////////////////////////////////////////////////////////////////////////////
// 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 checkstyle maven command executing codes and extract file processor.
* @author LuoLiangchen
*/
package com.github.checkstyle.regression.extract;
Loading

0 comments on commit 19e50c8

Please sign in to comment.