7110974: (javac) add coding conventions and style checkers for langtools
authorksrini
Mon, 14 Nov 2011 08:09:47 -0800
changeset 11054 c4224d65eb3e
parent 11053 48713f779b1d
child 11055 ec1418effa77
7110974: (javac) add coding conventions and style checkers for langtools Reviewed-by: jjg
langtools/make/build.properties
langtools/make/build.xml
langtools/make/conf/checkstyle-emacs.xsl
langtools/make/conf/checkstyle-langtools.xml
--- a/langtools/make/build.properties	Tue Nov 08 17:06:58 2011 -0800
+++ b/langtools/make/build.properties	Mon Nov 14 08:09:47 2011 -0800
@@ -196,3 +196,7 @@
 # An empty value means all tests
 # Override as desired to run a specific set of tests
 jtreg.tests =
+
+# Check style configuration
+# overridable name and version
+checkstyle.name.version = checkstyle-5.4
--- a/langtools/make/build.xml	Tue Nov 08 17:06:58 2011 -0800
+++ b/langtools/make/build.xml	Mon Nov 14 08:09:47 2011 -0800
@@ -131,8 +131,10 @@
     <property name="dist.bin.dir" location="${dist.dir}/bin"/>
     <property name="dist.coverage.dir" location="${dist.dir}/coverage"/>
     <property name="dist.findbugs.dir" location="${dist.dir}/findbugs"/>
+    <property name="dist.checkstyle.dir" location="${dist.dir}/checkstyle"/>
     <property name="dist.lib.dir" location="${dist.dir}/lib"/>
     <property name="make.dir" location="make"/>
+    <property name="make.conf.dir" location="${make.dir}/conf"/>
     <property name="make.tools.dir" location="${make.dir}/tools"/>
     <property name="src.dir" location="src"/>
     <property name="src.bin.dir" location="${src.dir}/share/bin"/>
@@ -263,6 +265,41 @@
         <jtreg-tool name="all" tests="${jtreg.tests}"/>
     </target>
 
+    <target name="checkstyle" depends="-def-checkstyle"
+        description="Generates reports for code convention violations.">
+        <mkdir dir="${dist.checkstyle.dir}"/>
+        <checkstyle config="${make.conf.dir}/checkstyle-langtools.xml"
+              failureProperty="checkstyle.failure"
+              failOnViolation="false">
+            <formatter type="xml" tofile="${dist.checkstyle.dir}/checkstyle_report.xml"/>
+            <fileset dir="src" includes="**/*.java, **/*.properties"/>
+        </checkstyle>
+        <!-- transform the output to a simple html -->
+        <xslt  in="${dist.checkstyle.dir}/checkstyle_report.xml"
+               out="${dist.checkstyle.dir}/checkstyle_report.html"
+               style="${checkstyle.home}/contrib/checkstyle-simple.xsl"/>            
+        <!-- transform the output to a very simple emacs friendly text file -->
+        <xslt  in="${dist.checkstyle.dir}/checkstyle_report.xml"
+               out="${dist.checkstyle.dir}/checkstyle_report.tmp"
+               style="${make.conf.dir}/checkstyle-emacs.xsl"/>
+        <!-- beautify remove extra lines -->
+        <move file="${dist.checkstyle.dir}/checkstyle_report.tmp"
+             toFile="${dist.checkstyle.dir}/checkstyle_report.emacs.txt">
+            <filterchain>
+                <ignoreblank/>
+                <replaceregex byline="true" pattern="^File:" replace="${line.separator}File:"/>
+            </filterchain>
+        </move>
+    </target>
+    <!-- target can be invoked from an ide, the output of which can be used
+         to access and fix the errors directly.
+     -->
+    <target name="checkstyle-ide" depends="checkstyle">
+        <concat>
+            <fileset file="${dist.checkstyle.dir}/checkstyle_report.emacs.txt"/>
+        </concat>  
+    </target>
+    
     <target name="findbugs" depends="-def-findbugs,build-all-tools">
         <property name="findbugs.reportLevel" value="medium"/>
         <mkdir dir="${dist.findbugs.dir}"/>
@@ -406,6 +443,7 @@
         <echo level="info">target.java.home = ${target.java.home}</echo>
         <echo level="info">jtreg.home = ${jtreg.home}</echo>
         <echo level="info">findbugs.home = ${findbugs.home}</echo>
+        <echo level="info">checkstyle.home = ${checkstyle.home}</echo>
     </target>
 
     <target name="post-sanity" depends="-def-jtreg,sanity,build"
@@ -690,6 +728,10 @@
         <check name="findbugs" property="findbugs.home" marker="lib/findbugs.jar"/>
     </target>
 
+    <target name="-check-checkstyle.home" depends="-def-check">
+        <check name="checkstyle" property="checkstyle.home" marker="${checkstyle.name.version}.jar"/>
+    </target>
+    
     <target name="-check-jtreg.home" depends="-def-check">
         <check name="jtreg" property="jtreg.home" marker="lib/jtreg.jar"/>
     </target>
@@ -989,6 +1031,16 @@
         <taskdef classpathref="cobertura.classpath" resource="tasks.properties"/>
     </target>
 
+    <target name="-def-checkstyle" unless="checkstyle.defined"
+        depends="-check-checkstyle.home">
+        <taskdef resource="checkstyletask.properties">
+            <classpath>
+                <pathelement location="${checkstyle.home}/${checkstyle.name.version}-all.jar"/>
+            </classpath>
+        </taskdef>
+        <property name="checkstyle.defined" value="true"/>
+    </target>
+    
     <target name="-def-findbugs" unless="findbugs.defined"
         depends="-check-findbugs.home,-check-target.java.home">
         <taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask">
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/make/conf/checkstyle-emacs.xsl	Mon Nov 14 08:09:47 2011 -0800
@@ -0,0 +1,24 @@
+<?xml version="1.0"?>
+<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+<xsl:output method="text" omit-xml-declaration="yes"/>
+
+<xsl:template match="/">
+Coding Style Check Results
+--------------------------
+Total files checked: <xsl:number level="any" value="count(descendant::file)"/>
+  Files with errors: <xsl:number level="any" value="count(descendant::file[error])"/>
+       Total errors: <xsl:number level="any" value="count(descendant::error)"/>
+    Errors per file: <xsl:number level="any" value="count(descendant::error) div count(descendant::file)"/>
+<xsl:apply-templates/>
+</xsl:template>
+
+<xsl:template match="file[error]">
+<xsl:apply-templates select="error"/>
+</xsl:template>
+
+<xsl:template match="error">
+<xsl:value-of select="../@name"/>:<xsl:value-of select="@line"/><xsl:text>: </xsl:text><xsl:value-of select="@message"/><xsl:text>
+</xsl:text>
+</xsl:template>
+
+</xsl:stylesheet>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/make/conf/checkstyle-langtools.xml	Mon Nov 14 08:09:47 2011 -0800
@@ -0,0 +1,47 @@
+<?xml version="1.0"?>
+<!DOCTYPE module PUBLIC
+          "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
+          "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
+
+<!--
+   Checks for initial langtools code conventions, we are starting with
+   imports and import orders and this will grow to encompass other
+   violations over time. 
+-->
+
+<module name="Checker">
+    
+    <!-- Checks for whitespace. -->
+    <module name="FileTabCharacter">
+        <property name="fileExtensions" value=".java"/>
+    </module>
+
+    <!-- Miscellaneous other checks.                   -->
+    <module name="RegexpSingleline">
+        <property name="format" value="\s+$"/>
+        <property name="minimum" value="0"/>
+        <property name="maximum" value="0"/>
+        <property name="message" value="Line has trailing spaces."/>
+        <property name="fileExtensions" value=".java .html"/>
+    </module>
+
+    <module name="TreeWalker">
+    <!-- Checks for imports                              -->
+    <!--
+        <module name="AvoidStarImport"/>
+        <module name="IllegalImport"/>
+    -->
+        <module name="GenericWhitespace"/>
+        <module name="RedundantImport"/>
+        <module name="UnusedImports"/>  
+        <module name="ImportOrder">
+            <property name="groups" value="java, javax, org, com"/>
+            <property name="ordered" value="true"/>
+            <property name="separated" value="true"/>
+        </module>        
+        <module name="EmptyForInitializerPad">
+            <property name="option" value="space"/>
+        </module>
+        <module name="WhitespaceAfter"/>
+    </module>
+</module>
\ No newline at end of file