Mon, 14 Nov 2011 08:09:47 -0800
7110974: (javac) add coding conventions and style checkers for langtools
Reviewed-by: jjg
1.1 --- a/make/build.properties Tue Nov 08 17:06:58 2011 -0800 1.2 +++ b/make/build.properties Mon Nov 14 08:09:47 2011 -0800 1.3 @@ -196,3 +196,7 @@ 1.4 # An empty value means all tests 1.5 # Override as desired to run a specific set of tests 1.6 jtreg.tests = 1.7 + 1.8 +# Check style configuration 1.9 +# overridable name and version 1.10 +checkstyle.name.version = checkstyle-5.4
2.1 --- a/make/build.xml Tue Nov 08 17:06:58 2011 -0800 2.2 +++ b/make/build.xml Mon Nov 14 08:09:47 2011 -0800 2.3 @@ -131,8 +131,10 @@ 2.4 <property name="dist.bin.dir" location="${dist.dir}/bin"/> 2.5 <property name="dist.coverage.dir" location="${dist.dir}/coverage"/> 2.6 <property name="dist.findbugs.dir" location="${dist.dir}/findbugs"/> 2.7 + <property name="dist.checkstyle.dir" location="${dist.dir}/checkstyle"/> 2.8 <property name="dist.lib.dir" location="${dist.dir}/lib"/> 2.9 <property name="make.dir" location="make"/> 2.10 + <property name="make.conf.dir" location="${make.dir}/conf"/> 2.11 <property name="make.tools.dir" location="${make.dir}/tools"/> 2.12 <property name="src.dir" location="src"/> 2.13 <property name="src.bin.dir" location="${src.dir}/share/bin"/> 2.14 @@ -263,6 +265,41 @@ 2.15 <jtreg-tool name="all" tests="${jtreg.tests}"/> 2.16 </target> 2.17 2.18 + <target name="checkstyle" depends="-def-checkstyle" 2.19 + description="Generates reports for code convention violations."> 2.20 + <mkdir dir="${dist.checkstyle.dir}"/> 2.21 + <checkstyle config="${make.conf.dir}/checkstyle-langtools.xml" 2.22 + failureProperty="checkstyle.failure" 2.23 + failOnViolation="false"> 2.24 + <formatter type="xml" tofile="${dist.checkstyle.dir}/checkstyle_report.xml"/> 2.25 + <fileset dir="src" includes="**/*.java, **/*.properties"/> 2.26 + </checkstyle> 2.27 + <!-- transform the output to a simple html --> 2.28 + <xslt in="${dist.checkstyle.dir}/checkstyle_report.xml" 2.29 + out="${dist.checkstyle.dir}/checkstyle_report.html" 2.30 + style="${checkstyle.home}/contrib/checkstyle-simple.xsl"/> 2.31 + <!-- transform the output to a very simple emacs friendly text file --> 2.32 + <xslt in="${dist.checkstyle.dir}/checkstyle_report.xml" 2.33 + out="${dist.checkstyle.dir}/checkstyle_report.tmp" 2.34 + style="${make.conf.dir}/checkstyle-emacs.xsl"/> 2.35 + <!-- beautify remove extra lines --> 2.36 + <move file="${dist.checkstyle.dir}/checkstyle_report.tmp" 2.37 + toFile="${dist.checkstyle.dir}/checkstyle_report.emacs.txt"> 2.38 + <filterchain> 2.39 + <ignoreblank/> 2.40 + <replaceregex byline="true" pattern="^File:" replace="${line.separator}File:"/> 2.41 + </filterchain> 2.42 + </move> 2.43 + </target> 2.44 + <!-- target can be invoked from an ide, the output of which can be used 2.45 + to access and fix the errors directly. 2.46 + --> 2.47 + <target name="checkstyle-ide" depends="checkstyle"> 2.48 + <concat> 2.49 + <fileset file="${dist.checkstyle.dir}/checkstyle_report.emacs.txt"/> 2.50 + </concat> 2.51 + </target> 2.52 + 2.53 <target name="findbugs" depends="-def-findbugs,build-all-tools"> 2.54 <property name="findbugs.reportLevel" value="medium"/> 2.55 <mkdir dir="${dist.findbugs.dir}"/> 2.56 @@ -406,6 +443,7 @@ 2.57 <echo level="info">target.java.home = ${target.java.home}</echo> 2.58 <echo level="info">jtreg.home = ${jtreg.home}</echo> 2.59 <echo level="info">findbugs.home = ${findbugs.home}</echo> 2.60 + <echo level="info">checkstyle.home = ${checkstyle.home}</echo> 2.61 </target> 2.62 2.63 <target name="post-sanity" depends="-def-jtreg,sanity,build" 2.64 @@ -690,6 +728,10 @@ 2.65 <check name="findbugs" property="findbugs.home" marker="lib/findbugs.jar"/> 2.66 </target> 2.67 2.68 + <target name="-check-checkstyle.home" depends="-def-check"> 2.69 + <check name="checkstyle" property="checkstyle.home" marker="${checkstyle.name.version}.jar"/> 2.70 + </target> 2.71 + 2.72 <target name="-check-jtreg.home" depends="-def-check"> 2.73 <check name="jtreg" property="jtreg.home" marker="lib/jtreg.jar"/> 2.74 </target> 2.75 @@ -989,6 +1031,16 @@ 2.76 <taskdef classpathref="cobertura.classpath" resource="tasks.properties"/> 2.77 </target> 2.78 2.79 + <target name="-def-checkstyle" unless="checkstyle.defined" 2.80 + depends="-check-checkstyle.home"> 2.81 + <taskdef resource="checkstyletask.properties"> 2.82 + <classpath> 2.83 + <pathelement location="${checkstyle.home}/${checkstyle.name.version}-all.jar"/> 2.84 + </classpath> 2.85 + </taskdef> 2.86 + <property name="checkstyle.defined" value="true"/> 2.87 + </target> 2.88 + 2.89 <target name="-def-findbugs" unless="findbugs.defined" 2.90 depends="-check-findbugs.home,-check-target.java.home"> 2.91 <taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask">
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/make/conf/checkstyle-emacs.xsl Mon Nov 14 08:09:47 2011 -0800 3.3 @@ -0,0 +1,24 @@ 3.4 +<?xml version="1.0"?> 3.5 +<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 3.6 +<xsl:output method="text" omit-xml-declaration="yes"/> 3.7 + 3.8 +<xsl:template match="/"> 3.9 +Coding Style Check Results 3.10 +-------------------------- 3.11 +Total files checked: <xsl:number level="any" value="count(descendant::file)"/> 3.12 + Files with errors: <xsl:number level="any" value="count(descendant::file[error])"/> 3.13 + Total errors: <xsl:number level="any" value="count(descendant::error)"/> 3.14 + Errors per file: <xsl:number level="any" value="count(descendant::error) div count(descendant::file)"/> 3.15 +<xsl:apply-templates/> 3.16 +</xsl:template> 3.17 + 3.18 +<xsl:template match="file[error]"> 3.19 +<xsl:apply-templates select="error"/> 3.20 +</xsl:template> 3.21 + 3.22 +<xsl:template match="error"> 3.23 +<xsl:value-of select="../@name"/>:<xsl:value-of select="@line"/><xsl:text>: </xsl:text><xsl:value-of select="@message"/><xsl:text> 3.24 +</xsl:text> 3.25 +</xsl:template> 3.26 + 3.27 +</xsl:stylesheet>
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/make/conf/checkstyle-langtools.xml Mon Nov 14 08:09:47 2011 -0800 4.3 @@ -0,0 +1,47 @@ 4.4 +<?xml version="1.0"?> 4.5 +<!DOCTYPE module PUBLIC 4.6 + "-//Puppy Crawl//DTD Check Configuration 1.3//EN" 4.7 + "http://www.puppycrawl.com/dtds/configuration_1_3.dtd"> 4.8 + 4.9 +<!-- 4.10 + Checks for initial langtools code conventions, we are starting with 4.11 + imports and import orders and this will grow to encompass other 4.12 + violations over time. 4.13 +--> 4.14 + 4.15 +<module name="Checker"> 4.16 + 4.17 + <!-- Checks for whitespace. --> 4.18 + <module name="FileTabCharacter"> 4.19 + <property name="fileExtensions" value=".java"/> 4.20 + </module> 4.21 + 4.22 + <!-- Miscellaneous other checks. --> 4.23 + <module name="RegexpSingleline"> 4.24 + <property name="format" value="\s+$"/> 4.25 + <property name="minimum" value="0"/> 4.26 + <property name="maximum" value="0"/> 4.27 + <property name="message" value="Line has trailing spaces."/> 4.28 + <property name="fileExtensions" value=".java .html"/> 4.29 + </module> 4.30 + 4.31 + <module name="TreeWalker"> 4.32 + <!-- Checks for imports --> 4.33 + <!-- 4.34 + <module name="AvoidStarImport"/> 4.35 + <module name="IllegalImport"/> 4.36 + --> 4.37 + <module name="GenericWhitespace"/> 4.38 + <module name="RedundantImport"/> 4.39 + <module name="UnusedImports"/> 4.40 + <module name="ImportOrder"> 4.41 + <property name="groups" value="java, javax, org, com"/> 4.42 + <property name="ordered" value="true"/> 4.43 + <property name="separated" value="true"/> 4.44 + </module> 4.45 + <module name="EmptyForInitializerPad"> 4.46 + <property name="option" value="space"/> 4.47 + </module> 4.48 + <module name="WhitespaceAfter"/> 4.49 + </module> 4.50 +</module> 4.51 \ No newline at end of file