diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckStyleTask.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckStyleTask.java index 9dc3ec24a..fa8edc860 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckStyleTask.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckStyleTask.java @@ -389,12 +389,6 @@ public class CheckStyleTask setBooleanProperty(Defn.IGNORE_CAST_WHITESPACE_PROP, aIgnore); } - /** @param aIgnore whether to ignore operator wrapping **/ - public void setIgnoreOpWrap(final boolean aIgnore) - { - setBooleanProperty(Defn.IGNORE_OP_WRAP_PROP, aIgnore); - } - /** @param aIgnore whether to ignore braces **/ public void setIgnoreBraces(final boolean aIgnore) { @@ -503,6 +497,18 @@ public class CheckStyleTask }); } + /** @param aTo the operator wrapping option **/ + public void setWrapOp(final String aTo) + { + mOptionMemory.add(new Runnable() + { + public void run() + { + mConfig.setWrapOpOption(extractWrapOpOption(aTo)); + } + }); + } + //////////////////////////////////////////////////////////////////////////// // The doers //////////////////////////////////////////////////////////////////////////// @@ -760,6 +766,22 @@ public class CheckStyleTask return opt; } + /** + * @param aFrom String to decode the option from + * @return the WrapOpOption represented by aFrom + * @throws BuildException if unable to decode aFrom + */ + private WrapOpOption extractWrapOpOption(String aFrom) + throws BuildException + { + final WrapOpOption opt = WrapOpOption.decode(aFrom); + if (opt == null) { + throw new BuildException("Unable to parse '" + aFrom + "'.", + location); + } + return opt; + } + /** * Applies the options that have been saved in the mOptionMemory. */ diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java index 0c94571ef..b0a953584 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java @@ -114,9 +114,10 @@ public class Configuration /** where to place right curlies **/ private RightCurlyOption mRCurly = RightCurlyOption.SAME; - /** how to pad parenthesis **/ private PadOption mParenPadOption = PadOption.NOSPACE; + /** how to wrap operators **/ + private WrapOpOption mWrapOpOption = WrapOpOption.NL; /** whether to use basedir **/ private String mBasedir; @@ -212,7 +213,6 @@ public class Configuration ILLEGAL_INSTANTIATIONS)); setBooleanProperty(aProps, Defn.IGNORE_WHITESPACE_PROP); setBooleanProperty(aProps, Defn.IGNORE_CAST_WHITESPACE_PROP); - setBooleanProperty(aProps, Defn.IGNORE_OP_WRAP_PROP); setBooleanProperty(aProps, Defn.IGNORE_BRACES_PROP); setBooleanProperty(aProps, Defn.IGNORE_LONG_ELL_PROP); setBooleanProperty(aProps, Defn.IGNORE_PUBLIC_IN_INTERFACE_PROP); @@ -240,6 +240,10 @@ public class Configuration PadOption.NOSPACE, aLog)); setBasedir(aProps.getProperty(Defn.BASEDIR_PROP)); + setWrapOpOption(getWrapOpOptionProperty(aProps, + Defn.WRAP_OP_PROP, + WrapOpOption.NL, + aLog)); } /** @@ -536,12 +540,6 @@ public class Configuration return getBooleanProperty(Defn.IGNORE_CAST_WHITESPACE_PROP); } - /** @return whether to ignore checks for operator wrapping **/ - public boolean isIgnoreOpWrap() - { - return getBooleanProperty(Defn.IGNORE_OP_WRAP_PROP); - } - /** @return whether to ignore checks for braces **/ public boolean isIgnoreBraces() { @@ -761,6 +759,18 @@ public class Configuration mParenPadOption = aTo; } + /** @return the wrapping on operator option **/ + public WrapOpOption getWrapOpOption() + { + return mWrapOpOption; + } + + /** @param aTo set the wrap on operator option **/ + public void setWrapOpOption(WrapOpOption aTo) + { + mWrapOpOption = aTo; + } + /** @return the base directory **/ public String getBasedir() { @@ -1014,6 +1024,34 @@ public class Configuration return retVal; } + /** + * @param aProps the properties set to use + * @param aLog where to log errors to + * @param aName the name of the property to parse + * @param aDefault the default value to use. + * + * @return the value of a WrapOpOption property. If the property is not + * defined or cannot be decoded, then a default value is returned. + */ + private static WrapOpOption getWrapOpOptionProperty( + Properties aProps, + String aName, + WrapOpOption aDefault, + PrintStream aLog) + { + WrapOpOption retVal = aDefault; + final String strRep = aProps.getProperty(aName); + if (strRep != null) { + retVal = WrapOpOption.decode(strRep); + if (retVal == null) { + aLog.println("Unable to parse " + aName + + " property with value " + strRep + + ", defaulting to " + aDefault + "."); + } + } + return retVal; + } + /** * @param aName name of the boolean property * @return return whether a specified property is set diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Defn.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Defn.java index 0db9ea33d..2559c201e 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Defn.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Defn.java @@ -89,8 +89,6 @@ public interface Defn String IGNORE_BRACES_PROP = "checkstyle.ignore.braces"; /** property name for ignoring long 'L' **/ String IGNORE_LONG_ELL_PROP = "checkstyle.ignore.longell"; - /** property name for ignoring wrapping lines on operators **/ - String IGNORE_OP_WRAP_PROP = "checkstyle.ignore.opwrap"; /** property name for ignoring 'public' in interface definitions **/ String IGNORE_PUBLIC_IN_INTERFACE_PROP = "checkstyle.ignore.public.in.interface"; @@ -118,4 +116,6 @@ public interface Defn String FINALLY_BLOCK_PROP = "checkstyle.block.finally"; /** property name for the base directory **/ String BASEDIR_PROP = "checkstyle.basedir"; + /** property name for wrapping lines on operators **/ + String WRAP_OP_PROP = "checkstyle.wrap.operator"; } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java index 0d7d8f8a3..425b5c0ca 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java @@ -955,7 +955,7 @@ class Verifier // Check if rest of line is whitespace, and not just the operator by // itself. This last bit is to handle the operator on a line by itself - if (!mConfig.isIgnoreOpWrap() + if ((mConfig.getWrapOpOption() != WrapOpOption.IGNORE) && !aText.equals(mLines[aLineNo - 1].trim()) && (mLines[aLineNo - 1].substring(aColNo + aText.length() - 1) .trim().length() == 0)) diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/WrapOpOption.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/WrapOpOption.java new file mode 100644 index 000000000..f6a3f7715 --- /dev/null +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/WrapOpOption.java @@ -0,0 +1,86 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2002 Oliver Burn +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//////////////////////////////////////////////////////////////////////////////// +package com.puppycrawl.tools.checkstyle; + +import java.io.Serializable; +import java.io.ObjectStreamException; +import java.util.Map; +import java.util.HashMap; + +/** + * Represents the options for wrapping on operators. + * + * @author Oliver Burn + * @version 1 + */ +public final class WrapOpOption + implements Serializable +{ + /** maps from a string representation to an option **/ + private static final Map STR_TO_OPT = new HashMap(); + + /** represents operator on a new line **/ + public static final WrapOpOption NL = new WrapOpOption("nl"); + /** represents ignoring the wrapping **/ + public static final WrapOpOption IGNORE = new WrapOpOption("ignore"); + + /** the string representation of the option **/ + private final String mStrRep; + + /** + * Creates a new WrapOpOption instance. + * @param aStrRep the string representation + */ + private WrapOpOption(String aStrRep) + { + mStrRep = aStrRep.trim().toLowerCase(); + STR_TO_OPT.put(mStrRep, this); + } + + /** + * Returns the option specified by a string representation. If no + * option exists then null is returned. + * @param aStrRep the String representation to parse + * @return the WrapOpOption value represented by aStrRep, or + * null if none exists. + */ + public static WrapOpOption decode(String aStrRep) + { + return (WrapOpOption) STR_TO_OPT.get(aStrRep.trim().toLowerCase()); + } + + /** @see Object **/ + public String toString() + { + return mStrRep; + } + + /** + * Ensures that we don't get multiple instances of one WrapOpOption + * during deserialization. See Section 3.6 of the Java Object + * Serialization Specification for details. + * + * @return the serialization replacement object + * @throws ObjectStreamException if a deserialization error occurs + */ + private Object readResolve() throws ObjectStreamException + { + return decode(mStrRep); + } +} diff --git a/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java b/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java index bf9b13441..26bc5609c 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java @@ -852,7 +852,7 @@ public class CheckerTest throws Exception { mConfig.setJavadocScope(Scope.NOTHING); - mConfig.setBooleanProperty(Defn.IGNORE_OP_WRAP_PROP, false); + mConfig.setWrapOpOption(WrapOpOption.NL); final Checker c = createChecker(); final String filepath = getPath("InputOpWrap.java"); assertNotNull(c); @@ -868,7 +868,7 @@ public class CheckerTest throws Exception { mConfig.setJavadocScope(Scope.NOTHING); - mConfig.setBooleanProperty(Defn.IGNORE_OP_WRAP_PROP, true); + mConfig.setWrapOpOption(WrapOpOption.IGNORE); final Checker c = createChecker(); final String filepath = getPath("InputOpWrap.java"); assertNotNull(c);