Changed the wrap on operator implementation to take an option rather than a

boolean. This allows it to be extended easily in the future.
This commit is contained in:
Oliver Burn 2002-07-08 02:23:38 +00:00
parent 3230cfbc88
commit e543c01abd
6 changed files with 165 additions and 19 deletions

View File

@ -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.
*/

View File

@ -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

View File

@ -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";
}

View File

@ -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))

View File

@ -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 <a href="mailto:oliver@puppycrawl.com">Oliver Burn</a>
* @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 <code>WrapOpOption</code> 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 <code>WrapOpOption</code> 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);
}
}

View File

@ -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);