There's an Eclipse plugin that you can use, based on the Checkstyle tool. With it, you can a) define code style rules; and b) make sure that your code abides by them.
Problem is, that's not really useful if you're the only one following the rules. Really, the only way to make sure everyone does is to enforce the rules at commit time. If you're using Subversion, there's a nifty utility that you can use to actually check the code that's commited, on the SVN server's side: SVN Checker. The tool uses a SVN feature called "hooks".
I do not intend to provide you with a full tutorial on how to set it up, since there's documentation around already (for example I have used this, and this one also).
The problem is, in the version I use (0.3), I've hit a bug/limitation in the Python script that consists of the actual Checkstyle hook (Checkstyle.py): first, in the line error message that would be displayed, the problematic file names would be preppended with a "/tmp/" string - most likely because files that are checked in are kept in that directory on the server-side before being committed. Second, the message would not clearly list all files for which style checks have failed...
I thus proceeded to fixing the script, and here's the results (my Python skills are not top notch, so bare with me. But it works):
# Checkstyle.py
# Copyright 2008 German Aerospace Center (DLR)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" Checks Java files for coding style errors using Checkstyle. """
from modules import Process
def run(transaction, config):
check = config.getArray("Checkstyle.CheckFiles", [".*\.java"])
ignore = config.getArray("Checkstyle.IgnoreFiles", [])
files = transaction.getFiles(check, ignore)
java = config.getString("Checkstyle.Java")
classpath = config.getString("Checkstyle.Classpath")
config = config.getString("Checkstyle.ConfigFile")
command = "%s -classpath %s com.puppycrawl.tools.checkstyle.Main -c %s " % (java, classpath, config)
files = [transaction.getFile(oneFile[0]) for oneFile in files.iteritems() if oneFile[1] in ["A", "U", "UU"]]
if(len(files)) > 0:
try:
output = Process.execute(command + " ".join(files))
if "warning" in output:
msg = "Coding style errors found:\n\n"
outputLines = output.splitlines(True)
for line in outputLines:
if "warning" in line:
if line.startswith("/tmp/tmp"):
line = line.replace("/tmp/tmp", "")
slashIndex = line.find("/")
if slashIndex >= 0:
line = line[slashIndex:]
msg += line
return (msg, 1)
except Process.ProcessException, e:
msg = "Coding style errors found:\n\n"
msg += e.output + "\n"
msg += "See Checkstyle documentation for a detailed description: http://checkstyle.sourceforge.net/"
return (msg, 1)
return ("", 0)