Skip to content

Commit

Permalink
Add checkstyle plugin (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
Linary authored Mar 19, 2021
1 parent 98844d8 commit 265f74b
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 30 deletions.
89 changes: 89 additions & 0 deletions checkstyle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<!-- 参考:https://checkstyle.sourceforge.io/checks.html -->
<module name="Checker">
<!--检查不通过时被判定的违规级别,必须修复后才能使build通过-->
<property name="severity" value="error"/>
<!--对java文件做检查-->
<property name="fileExtensions" value="java"/>
<!--对UTF-8编码的文件做检查-->
<property name="charset" value="UTF-8"/>
<!--文件中不允许包含制表符-->
<module name="FileTabCharacter">
<property name="eachLine" value="true"/>
</module>

<!--检查java源文件并定义一些适用于检查此类文件的一些属性-->
<module name="TreeWalker">
<!--检查行长度-->
<module name="LineLength">
<property name="max" value="80"/>
<!--可以忽略的行-->
<property name="ignorePattern"
value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
</module>
<!--检查没有import语句使用*号-->
<module name="AvoidStarImport"/>
<!--检查是否存在多余的import语句,比如重复的,java自带的包,相同包下的其他类-->
<module name="RedundantImport"/>
<!--检查是否存在没有使用的import语句-->
<module name="UnusedImports"/>
<!--检查包名称是否遵守命名规约-->
<module name="PackageName">
<property name="format" value="^[a-z]+(\.[a-z][a-z0-9]*)*$"/>
</module>
<!--检查局部变量的名称是否遵守命名规约-->
<module name="LocalVariableName">
<property name="format" value="^[a-z][a-zA-Z0-9_]*$"/>
</module>
<!--检查成员变量(非静态字段)的名称是否遵守命名规约-->
<module name="MemberName">
<property name="format" value="^[a-z][a-zA-Z0-9]*$"/>
</module>
<!--检查方法名称是否遵守命名规约-->
<module name="MethodName">
<property name="format" value="^[a-z][a-zA-Z0-9]*$"/>
</module>
<!--检查参数名称是否遵守命名规约-->
<module name="ParameterName">
<property name="format" value="^[a-z][a-zA-Z0-9]*$"/>
</module>
<!--检查常量(用static final修饰的字段)的名称是否遵守命名规约-->
<module name="ConstantName">
<property name="format" value="^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"/>
</module>
<!--检查数组是否属于java风格,方括号放在类型后面,而不是变量后面,比如:int[] nums(合法),int nums[](不合法)-->
<module name="ArrayTypeStyle">
<property name="javaStyle" value="true"/>
</module>
<!--long类型的字面量如果要以"L"结尾,必须是大写的"L",而非小写的"l"-->
<module name="UpperEll"/>
<!--代码换行时,运算符必须在当前行的末尾,比如:+、&&、?、: 等-->
<module name="OperatorWrap">
<property name="option" value="eol"/>
</module>
<!--检查指定标记的周围是否有空格,比如:if、for、while、synchoronized 等-->
<module name="WhitespaceAround"/>
<!--左圆括号之后和右圆括号之前是否需要有一个空格,不需要-->
<module name="ParenPad"/>
<!--检查修饰符是否符合Java建议,顺序是:public、protected、private、abstract、default、static、final、transient、volatile、synchronized、native、strictfp-->
<module name="ModifierOrder"/>
<!--检查代码块的左花括号的放置位置,必须在当前行的末尾-->
<module name="LeftCurly">
<property name="option" value="eol"/>
<property name="ignoreEnums" value="false"/>
</module>
<!--代码中不允许有空语句,也就是单独的;符号-->
<module name="EmptyStatement"/>
<!--覆盖equals()方法的类必须也覆盖了hashCode()方法-->
<module name="EqualsHashCode"/>
<!--switch语句必须含有default子句-->
<module name="MissingSwitchDefault"/>
<!--switch语句的default必须放在所有的case分支之后-->
<module name="DefaultComesLast"/>
<!--覆盖clone()方法时调用了super.clone()方法-->
<module name="SuperClone"/>
</module>
</module>
23 changes: 22 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,28 @@
</compilerArgs>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<configLocation>checkstyle.xml</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
<includeTestSourceDirectory>false</includeTestSourceDirectory>
</configuration>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/com/baidu/hugegraph/config/OptionSpace.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ public final class OptionSpace {

private static final Logger LOG = Log.logger(OptionSpace.class);

private static final Map<String, Class<? extends OptionHolder>> holders;
private static final Map<String, TypedOption<?, ?>> options;
private static final Map<String, Class<? extends OptionHolder>> HOLDERS;
private static final Map<String, TypedOption<?, ?>> OPTIONS;
private static final String INSTANCE_METHOD = "instance";

static {
holders = new ConcurrentHashMap<>();
options = new ConcurrentHashMap<>();
HOLDERS = new ConcurrentHashMap<>();
OPTIONS = new ConcurrentHashMap<>();
}

public static void register(String module, String holder) {
Expand Down Expand Up @@ -98,26 +98,26 @@ public static void register(String module, String holder) {

public static void register(String module, OptionHolder holder) {
// Check exists
if (holders.containsKey(module)) {
if (HOLDERS.containsKey(module)) {
LOG.warn("Already registered option holder: {} ({})",
module, holders.get(module));
module, HOLDERS.get(module));
}
E.checkArgumentNotNull(holder, "OptionHolder can't be null");
holders.put(module, holder.getClass());
options.putAll(holder.options());
HOLDERS.put(module, holder.getClass());
OPTIONS.putAll(holder.options());
LOG.debug("Registered options for OptionHolder: {}",
holder.getClass().getSimpleName());
}

public static Set<String> keys() {
return Collections.unmodifiableSet(options.keySet());
return Collections.unmodifiableSet(OPTIONS.keySet());
}

public static boolean containKey(String key) {
return options.containsKey(key);
return OPTIONS.containsKey(key);
}

public static TypedOption<?, ?> get(String key) {
return options.get(key);
return OPTIONS.get(key);
}
}
6 changes: 0 additions & 6 deletions src/main/java/com/baidu/hugegraph/date/SafeDateFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,4 @@ public String format(Date date) {
public Object toPattern() {
return this.pattern;
}

@Override
public Object clone() {
// No need to clone due to itself is thread safe
return this;
}
}
16 changes: 8 additions & 8 deletions src/main/java/com/baidu/hugegraph/rest/RestResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

public class RestResult {

private static final ObjectMapper mapper = new ObjectMapper();
private static final ObjectMapper MAPPER = new ObjectMapper();

private final int status;
private final MultivaluedMap<String, Object> headers;
Expand All @@ -59,7 +59,7 @@ public String content() {

public <T> T readObject(Class<T> clazz) {
try {
return mapper.readValue(this.content, clazz);
return MAPPER.readValue(this.content, clazz);
} catch (Exception e) {
throw new SerializeException(
"Failed to deserialize: %s", e, this.content);
Expand All @@ -69,16 +69,16 @@ public <T> T readObject(Class<T> clazz) {
@SuppressWarnings("deprecation")
public <T> List<T> readList(String key, Class<T> clazz) {
try {
JsonNode root = mapper.readTree(this.content);
JsonNode root = MAPPER.readTree(this.content);
JsonNode element = root.get(key);
if (element == null) {
throw new SerializeException(
"Can't find value of the key: %s in json.", key);
}
JavaType type = mapper.getTypeFactory()
JavaType type = MAPPER.getTypeFactory()
.constructParametrizedType(ArrayList.class,
List.class, clazz);
return mapper.convertValue(element, type);
return MAPPER.convertValue(element, type);
} catch (IOException e) {
throw new SerializeException(
"Failed to deserialize %s", e, this.content);
Expand All @@ -87,11 +87,11 @@ public <T> List<T> readList(String key, Class<T> clazz) {

@SuppressWarnings("deprecation")
public <T> List<T> readList(Class<T> clazz) {
JavaType type = mapper.getTypeFactory()
JavaType type = MAPPER.getTypeFactory()
.constructParametrizedType(ArrayList.class,
List.class, clazz);
try {
return mapper.readValue(this.content, type);
return MAPPER.readValue(this.content, type);
} catch (IOException e) {
throw new SerializeException(
"Failed to deserialize %s", e, this.content);
Expand All @@ -105,6 +105,6 @@ public String toString() {
}

public static void registerModule(Module module) {
mapper.registerModule(module);
MAPPER.registerModule(module);
}
}
4 changes: 3 additions & 1 deletion src/main/java/com/baidu/hugegraph/testutil/Whitebox.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ public static Method method(Class<?> clazz, String methodName,
if (superclass != null) {
try {
return method(superclass, methodName, argsClasses);
} catch (Exception ignored){}
} catch (Exception ignored) {
// pass
}
}
throw new RuntimeException(String.format(
"Can't find method '%s' with args %s of class '%s'",
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/baidu/hugegraph/util/CheckSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class CheckSocket {
private static final String MSG_USAGE =
"Usage: " + CheckSocket.class.getSimpleName() + " hostname port";

public static void main(String args[]) {
public static void main(String[] args) {
if (args.length != 2) {
System.err.println(MSG_USAGE);
System.exit(E_USAGE);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/baidu/hugegraph/util/NumericUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public static Number maxValueOf(Class<?> clazz) {
E.checkArgumentNotNull(clazz, "The clazz can't be null");

if (Long.class.isAssignableFrom(clazz) ||
Double.class.isAssignableFrom(clazz) ) {
Double.class.isAssignableFrom(clazz)) {
return Long.MAX_VALUE;
}
if (Integer.class.isAssignableFrom(clazz) ||
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/baidu/hugegraph/util/VersionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Objects;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

Expand Down Expand Up @@ -121,7 +122,9 @@ public static String getPomVersion() {
if (isr != null) {
try {
isr.close();
} catch (Exception ignored) {}
} catch (Exception ignored) {
// pass
}
}

// Destroy child process
Expand Down Expand Up @@ -205,6 +208,11 @@ public boolean equals(Object that) {
return this.compareTo((Version) that) == 0;
}

@Override
public int hashCode() {
return Objects.hash(this.version);
}

@Override
public String toString() {
return this.version;
Expand Down

0 comments on commit 265f74b

Please sign in to comment.