add checkstyle

master
AlanFenng 4 years ago
parent 5cee205379
commit 4449785336

@ -0,0 +1,137 @@
<?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">
<module name="Checker">
<!--编码字符集-->
<property name="charset" value="UTF-8"/>
<property name="severity" value="warning"/>
<property name="fileExtensions" value="java"/>
<module name="FileTabCharacter">
<property name="eachLine" value="true"/>
</module>
<module name="FileLength">
<property name="max" value="1000"/>
<property name="severity" value="error"/>
</module>
<!--检查项规则配置-->
<module name="TreeWalker">
<!-- 命名检查 -->
<module name="AbbreviationAsWordInName"/>
<module name="AbstractClassName">
<property name="ignoreModifier" value="true"/>
</module>
<module name="CatchParameterName"/>
<module name="ClassTypeParameterName"/>
<module name="ConstantName"/>
<module name="InterfaceTypeParameterName">
<property name="format" value="^[a-zA-Z]$"/>
</module>
<module name="LambdaParameterName"/>
<module name="LocalFinalVariableName"/>
<module name="LocalVariableName"/>
<module name="MemberName">
<property name="format" value="^[a-z][a-zA-Z0-9]*$"/>
<property name="severity" value="error"/>
</module>
<module name="MethodName">
<property name="format" value="^[a-z]([a-zA-Z0-9])*$"/>
</module>
<module name="MethodTypeParameterName">
<property name="format" value="^[a-zA-Z]$"/>
</module>
<module name="PackageName">
<property name="format" value="^[a-z]+(\.[a-z][a-z0-9]*)*$"/>
</module>
<module name="ParameterName">
<property name="format" value="^[a-z][_a-zA-Z0-9]+$"/>
</module>
<module name="StaticVariableName">
<property name="format" value="^[a-z](_?[a-zA-Z0-9]+)*$"/>
</module>
<module name="TypeName">
<property name="format" value="^[A-Z](_?[a-zA-Z0-9]+)*$"/>
</module>
<module name="TypeName">
<property name="format"
value="^I_[a-zA-Z0-9]*$"/>
<property name="tokens"
value="INTERFACE_DEF"/>
</module>
<!--
空格检查,该检查是为了增加代码的可读性、可维护性
-->
<module name="EmptyForInitializerPad">
<property name="option" value="nospace"/>
</module>
<module name="EmptyForIteratorPad">
<property name="option" value="space"/>
</module>
<module name="EmptyLineSeparator">
<property name="tokens" value="VARIABLE_DEF, METHOD_DEF"/>
</module>
<module name="MethodParamPad">
<property name="tokens" value="METHOD_DEF"/>
<property name="option" value="nospace"/>
<property name="allowLineBreaks" value="true"/>
</module>
<module name="NoLineWrap">
<property name="tokens" value="IMPORT"/>
</module>
<module name="NoWhitespaceAfter">
<property name="tokens" value="DOT"/>
<property name="allowLineBreaks" value="false"/>
</module>
<module name="NoWhitespaceBefore">
<property name="tokens" value="DOT"/>
<property name="allowLineBreaks" value="true"/>
</module>
<module name="OperatorWrap"/>
<module name="ParenPad"/>
<module name="SingleSpaceSeparator"/>
<module name="TypecastParenPad">
<property name="option" value="space"/>
</module>
<module name="WhitespaceAfter">
<property name="tokens" value="COMMA, SEMI, TYPECAST, LITERAL_ELSE"/>
</module>
<module name="WhitespaceAround">
<!-- else、catch、finally、赋值符、运算符、连接符-->
<property name="tokens" value="ASSIGN, BOR, BOR_ASSIGN, EQUAL, GE, GT, LAMBDA, LAND, LE, LITERAL_CATCH, LITERAL_ELSE, LITERAL_FINALLY, LITERAL_SYNCHRONIZED, LOR, LT, MINUS, MINUS_ASSIGN, MOD, MOD_ASSIGN, NOT_EQUAL, PLUS, PLUS_ASSIGN, QUESTION, SL, SL_ASSIGN, SR, SR_ASSIGN, STAR, STAR_ASSIGN"/>
</module>
<!--
Size检查依然是解决可读性
方法体大小、文件大小等
-->
<module name="AnonInnerLength">
<property name="max" value="80"/>
<property name="severity" value="error"/>
</module>
<module name="ExecutableStatementCount"/>
<module name="MethodCount">
<property name="maxTotal" value="30"/>
<property name="severity" value="error"/>
</module>
<module name="MethodLength">
<property name="tokens" value="METHOD_DEF"/>
<property name="max" value="80"/>
<property name="countEmpty" value="false"/>
<property name="severity" value="error"/>
</module>
<module name="OuterTypeNumber"/>
<module name="ParameterNumber"/>
<!-- NeedBraces 检查是否应该使用括号的地方没有加括号
tokens: 定义检查的类型 -->
<module name="NeedBraces">
<property name="severity" value="error"/>
</module>
<!-- if-else嵌套语句个数 最多4层 -->
<module name="NestedIfDepth">
<property name="max" value="3"/>
<property name="severity" value="error"/>
</module>
</module>
</module>

@ -0,0 +1,62 @@
#! /bin/bash
# @author:haloo#
#@func:pre-commit#
## cp ./checkstyle/pre-commit ./.git/hooks/
echo 避免NPE是程序员的基本修养
echo 开始style checking
wd=`pwd`
echo "当前工作目录:$wd"
# check-style版本号
check_style_version="checkstyle-8.38-all.jar"
check_style_xml_name="GeekCheckStyle.xml"
check_jar_path="$wd/checkstyle/$check_style_version"
check_xml_path="$wd/checkstyle/$check_style_xml_name"
## 清空temp文件
rm -f temp
is_err=0
errorCount=0
warnCount=0
## 查找add到git 缓冲区中,以.java后缀的文件
for file in `git status --porcelain | sed s/^...// | grep '\.java$'`; do
path="$wd/$file"
echo "检查文件: $path"
re=`java -jar $check_jar_path -c $check_xml_path $path >> temp`
warn=`cat temp | grep "WARN"`
if [[ $warn = *"WARN"* ]];then
echo "${warn}"
needle="WARN"
number_of_occurrences=$(grep -o "$needle" <<< "$warn" | wc -l)
((warnCount = warnCount + number_of_occurrences))
is_err=1
fi
err=`cat temp | grep "ERROR"`
if [[ $err = *"ERROR"* ]];then
echo "${err}"
needle="ERROR"
number_of_occurrences=$(grep -o "$needle" <<< "$err" | wc -l)
((errorCount = errorCount + number_of_occurrences))
is_err=1
fi
done
echo "检查完成,祝你好运"
rm -f temp
if [ $is_err -ne 0 ];then
echo "出现了 $errorCount 个error"
echo "出现了 $warnCount 个warn"
echo "请先符合style才能提交"
exit 1
fi
echo "No Bug ,It is Good!!"
exit 0

@ -0,0 +1,2 @@
cp checkstyle/pre-commit .git/hooks/
chmod +x .git/hooks/pre-commit
Loading…
Cancel
Save