#!/bin/sh # javac-xlint can be run as a PREFIX to a javac line (javac-xlint is not a # replacement for javac), like so: # # javac-xlint [-p regexp] javac [javac-options] file... # # javac-xlint makes 3 changes to the invocation of javac that it prefixes: # 1. It supplies the -Xlint option. # 2. It suppresses certain warning messages. (See note below.) # 3. It returns non-zero status if any other warnings (or errors) exist. # Ordinarily, javac returns non-zero status only if errors exist. # # The suppressed warnings are of two sorts. # 1. Any warning in a file that matches the regexp provided with -p # (mnemonic: "prune"). # 2. Specific warnings disabled on a per-line basis by a source code # comment. The javac warning category is given in square brackets, # such as "warning: [unchecked]" or "warning: [deprecation]". A # comment of the form "// CATEGORY" disables warnings of that category # on that line. Other warnings (of different categories) on that line # are retained. # Regarding the warning message suppression feature of this script: As of # Java 5 update 6 (JDK 5.0u6), javac has a way to suppress warnings: # @SuppressWarnings("unchecked"). This is more portable and standard than # this script. However: # * it does not permit suppression per-line, only per declaration. # (A way to get per-line suppression is to put the offending statement # in a variable declaration, and then annotate the declaration.) # * it does not permit suppression based on directory/package (I think) # * it does not give a non-zero return status if non-suppressed warnings exist. # Regarding the warnings-as-errors feature of this script: As of Java 6, # the -Werror flag has the same effect. # This script uses the separate) javac-xlint-prune script. # As an alternative to this script, pipe javac output into javac-xlint-prune: # JAVA-COMMAND-LINE 2>&1 | javac-xlint-prune # (That's all that this script does.) # Design note: # Here are two possible ways a wrapper script around javac could work # (this script does the latter). # 1. Drop-in replacement for javac: "javac-xlint args..." # An advantage is that it looks more like javac, and may be somewhat # easier to remember/use. # 2. Prefix to javac line, like so: "javac-xlint javac args..." # An advantage is that we can continue to respect user settings of # javac (to "javac -g", "javac-1.5.0", etc.), rather than # overriding them. Note that even so, this script doesn't # currently permit using (say) jikes: prune-xlint is hard-coded # for "javac" output. # DEBUG_JAVAC_XLINT=1 if [ ! -z $DEBUG_JAVAC_XLINT ]; then echo "all javac-xlint args: $*" echo "javac-xlint arg 1: $1" fi if [ "$1" = "-p" ]; then if [ ! -z $DEBUG_JAVAC_XLINT ]; then echo "found p option" fi PRUNE_XLINT_ARGS=$2 shift shift if [ ! -z $DEBUG_JAVAC_XLINT ]; then echo "all remaining javac-xlint args: $*" fi fi ## Older option processing code. ## A problem with this is that it does not halt the script, nor is its error ## message (e.g., "illegal option -- d") comprehensible. ## I could do my own such processing, however, using the getopts return value. # # Parse arguments. # while getopts "p:" Option # do # case $Option in # p ) # # echo "found p!" # PRUNE_XLINT_ARGS=$OPTARG # esac # done # shift $(($OPTIND - 1)) # Separate the first argument (the Java compiler) from all the others. SUPPLIED_JAVAC=$1 shift ## This does not work because the given javac may be a command name rather ## than a fully-qualified pathname. However, I would like to find a way to ## warn in the common case that the javac argument is omitted. # if [ ! -x ${SUPPLIED_JAVAC} ]; then # echo "Supplied Java compiler is not executable: ${SUPPLIED_JAVAC}" # exit 1 # fi # Put filenames in a file in order to avoid errors due to overly long # command lines. FILES_FILE=/tmp/javac-xlint-files-$$ echo $* > $FILES_FILE # Fixup the filename for cygwin JAVA_FILES_FILE=$FILES_FILE if [ "$OSTYPE" = "cygwin" ]; then JAVA_FILES_FILE=`cygpath -w $JAVA_FILES_FILE` fi if [ ! -z $DEBUG_JAVAC_XLINT ]; then echo ${SUPPLIED_JAVAC} -Xlint @$JAVA_FILES_FILE PIPED-INTO `dirname ${0}`/javac-xlint-prune -p "${PRUNE_XLINT_ARGS}" fi if [ ! -z "${PRUNE_XLINT_ARGS}" ]; then ${SUPPLIED_JAVAC} -Xlint @$JAVA_FILES_FILE 2>&1 | `dirname ${0}`/javac-xlint-prune -p "${PRUNE_XLINT_ARGS}" else ${SUPPLIED_JAVAC} -Xlint @$JAVA_FILES_FILE 2>&1 | `dirname ${0}`/javac-xlint-prune fi STATUS=$? if [ -z $DEBUG_JAVAC_XLINT ]; then rm -f $FILES_FILE fi exit $STATUS