ConvertUtilsBean.java

Index Score
org.apache.commons.beanutils
Commons BeanUtils

View: Reasons, Metrics, Source Code

These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.

MetricDescription
DOC_COMMENTNumber of javadoc comment lines
COMMENTSComment lines
DECL_COMMENTSComments in declarations
SIZESize of the file in bytes
LINESNumber of lines in the source file
CYCLOMATICCyclomatic complexity
OPERATORSNumber of operators
PROGRAM_LENGTHHalstead program length
OPERANDSNumber of operands
JAVA0034JAVA0034 Missing braces in if statement
LOGICAL_LINESNumber of statements
INTERFACE_COMPLEXITYInterface complexity
RETURNSNumber of return points from functions
FUNCTIONSNumber of function declarations
PARAMSNumber of formal parameter declarations
ELOCEffective lines of code
BLOCKSNumber of blocks
LOCLines of code
PROGRAM_VOCABHalstead program vocabulary
UNIQUE_OPERANDSNumber of unique operands
JAVA0117JAVA0117 Missing javadoc: method 'method'
PROGRAM_VOLUMEHalstead program volume
JAVA0136JAVA0136 N methods defined in class (maximum: M)
UNIQUE_OPERATORSNumber of unique operators
JAVA0126JAVA0126 Method declares unchecked exception in throws
JAVA0132JAVA0132 Method overload with compatible signature
JAVA0110JAVA0110 Incorrect javadoc: no @return tag
EXITSProcedure exits
JAVA0100JAVA0100 Class contains N non-final fields (maximum: M)
LOOPSNumber of loops
COMPARISONSNumber of comparison operators
JAVA0108JAVA0108 Incorrect javadoc: no @param tag for 'parameter'
JAVA0145JAVA0145 Tab character used in source file
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.beanutils; import java.io.File; import java.lang.reflect.Array; import java.math.BigDecimal; import java.math.BigInteger; import java.net.URL; import java.sql.Timestamp; import java.util.Calendar; import java.util.Collection; import org.apache.commons.beanutils.converters.ArrayConverter; import org.apache.commons.beanutils.converters.BigDecimalConverter; import org.apache.commons.beanutils.converters.BigIntegerConverter; import org.apache.commons.beanutils.converters.BooleanConverter; import org.apache.commons.beanutils.converters.ByteConverter; import org.apache.commons.beanutils.converters.CalendarConverter; import org.apache.commons.beanutils.converters.CharacterConverter; import org.apache.commons.beanutils.converters.ClassConverter; import org.apache.commons.beanutils.converters.ConverterFacade; import org.apache.commons.beanutils.converters.DateConverter; import org.apache.commons.beanutils.converters.DoubleConverter; import org.apache.commons.beanutils.converters.FileConverter; import org.apache.commons.beanutils.converters.FloatConverter; import org.apache.commons.beanutils.converters.IntegerConverter; import org.apache.commons.beanutils.converters.LongConverter; import org.apache.commons.beanutils.converters.ShortConverter; import org.apache.commons.beanutils.converters.SqlDateConverter; import org.apache.commons.beanutils.converters.SqlTimeConverter; import org.apache.commons.beanutils.converters.SqlTimestampConverter; import org.apache.commons.beanutils.converters.StringConverter; import org.apache.commons.beanutils.converters.URLConverter; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * <p>Utility methods for converting String scalar values to objects of the * specified Class, String arrays to arrays of the specified Class. The * actual {@link Converter} instance to be used can be registered for each * possible destination Class. Unless you override them, standard * {@link Converter} instances are provided for all of the following * destination Classes:</p> * <ul> * <li>java.lang.BigDecimal (no default value)</li> * <li>java.lang.BigInteger (no default value)</li> * <li>boolean and java.lang.Boolean (default to false)</li> * <li>byte and java.lang.Byte (default to zero)</li> * <li>char and java.lang.Character (default to a space)</li> * <li>java.lang.Class (no default value)</li> * <li>double and java.lang.Double (default to zero)</li> * <li>float and java.lang.Float (default to zero)</li> * <li>int and java.lang.Integer (default to zero)</li> * <li>long and java.lang.Long (default to zero)</li> * <li>short and java.lang.Short (default to zero)</li> * <li>java.lang.String (default to null)</li> * <li>java.io.File (no default value)</li> * <li>java.net.URL (no default value)</li> * <li>java.sql.Date (no default value)</li> * <li>java.sql.Time (no default value)</li> * <li>java.sql.Timestamp (no default value)</li> * </ul> * * <p>For backwards compatibility, the standard Converters for primitive * types (and the corresponding wrapper classes) return a defined * default value when a conversion error occurs. If you prefer to have a * {@link ConversionException} thrown instead, replace the standard Converter * instances with instances created with the zero-arguments constructor. For * example, to cause the Converters for integers to throw an exception on * conversion errors, you could do this:</p> * <pre> * // No-args constructor gets the version that throws exceptions * Converter myConverter = * new org.apache.commons.beanutils.converter.IntegerConverter(); * ConvertUtils.register(myConverter, Integer.TYPE); // Native type * ConvertUtils.register(myConverter, Integer.class); // Wrapper class * </pre> * * <p> * Converters generally treat null input as if it were invalid * input, ie they return their default value if one was specified when the * converter was constructed, and throw an exception otherwise. If you prefer * nulls to be preserved for converters that are converting to objects (not * primitives) then register a converter as above, passing a default value of * null to the converter constructor (and of course registering that converter * only for the .class target). * </p> * * <p> * When a converter is listed above as having no default value, then that * converter will throw an exception when passed null or an invalid value * as its input. In particular, by default the BigInteger and BigDecimal * converters have no default (and are therefore somewhat inconsistent * with the other numerical converters which all have zero as their default). * </p> * * <p> * Converters that generate <i>arrays</i> of each of the primitive types are * also automatically configured (including String[]). When passed null * or invalid input, these return an empty array (not null). See class * AbstractArrayConverter for the supported input formats for these converters. * </p> * * @author Craig R. McClanahan * @author Ralph Schaer * @author Chris Audley * @author James Strachan * @version $Revision: 687089 $ $Date: 2008-08-19 12:33:30 -0400 (Tue, 19 Aug 2008) $ * @since 1.7 */ public class ConvertUtilsBean { private static final Integer ZERO = new Integer(0); private static final Character SPACE = new Character(' '); // ------------------------------------------------------- Class Methods /** * Get singleton instance * @return The singleton instance */ protected static ConvertUtilsBean getInstance() { return BeanUtilsBean.getInstance().getConvertUtils(); } // ------------------------------------------------------- Variables /** * The set of {@link Converter}s that can be used to convert Strings * into objects of a specified Class, keyed by the destination Class. */ private WeakFastHashMap converters = new WeakFastHashMap(); /** * The <code>Log</code> instance for this class. */ private Log log = LogFactory.getLog(ConvertUtils.class); // ------------------------------------------------------- Constructors /** Construct a bean with standard converters registered */ public ConvertUtilsBean() { converters.setFast(false); deregister(); converters.setFast(true); } // --------------------------------------------------------- Public Methods /** * The default value for Boolean conversions. * @deprecated Register replacement converters for Boolean.TYPE and * Boolean.class instead */ private Boolean defaultBoolean = Boolean.FALSE; /** * Gets the default value for Boolean conversions. * @return The default Boolean value * @deprecated Register replacement converters for Boolean.TYPE and * Boolean.class instead */ public boolean getDefaultBoolean() { return (defaultBoolean.booleanValue()); } /** * Sets the default value for Boolean conversions. * @param newDefaultBoolean The default Boolean value * @deprecated Register replacement converters for Boolean.TYPE and * Boolean.class instead */ public void setDefaultBoolean(boolean newDefaultBoolean) { defaultBoolean = (newDefaultBoolean ? Boolean.TRUE : Boolean.FALSE); register(new BooleanConverter(defaultBoolean), Boolean.TYPE); register(new BooleanConverter(defaultBoolean), Boolean.class); } /** * The default value for Byte conversions. * @deprecated Register replacement converters for Byte.TYPE and * Byte.class instead */ private Byte defaultByte = new Byte((byte) 0); /** * Gets the default value for Byte conversions. * @return The default Byte value * @deprecated Register replacement converters for Byte.TYPE and * Byte.class instead */ public byte getDefaultByte() { return (defaultByte.byteValue()); } /** * Sets the default value for Byte conversions. * @param newDefaultByte The default Byte value * @deprecated Register replacement converters for Byte.TYPE and * Byte.class instead */ public void setDefaultByte(byte newDefaultByte) { defaultByte = new Byte(newDefaultByte); register(new ByteConverter(defaultByte), Byte.TYPE); register(new ByteConverter(defaultByte), Byte.class); } /** * The default value for Character conversions. * @deprecated Register replacement converters for Character.TYPE and * Character.class instead */ private Character defaultCharacter = new Character(' '); /** * Gets the default value for Character conversions. * @return The default Character value * @deprecated Register replacement converters for Character.TYPE and * Character.class instead */ public char getDefaultCharacter() { return (defaultCharacter.charValue()); } /** * Sets the default value for Character conversions. * @param newDefaultCharacter The default Character value * @deprecated Register replacement converters for Character.TYPE and * Character.class instead */ public void setDefaultCharacter(char newDefaultCharacter) { defaultCharacter = new Character(newDefaultCharacter); register(new CharacterConverter(defaultCharacter), Character.TYPE); register(new CharacterConverter(defaultCharacter), Character.class); } /** * The default value for Double conversions. * @deprecated Register replacement converters for Double.TYPE and * Double.class instead */ private Double defaultDouble = new Double(0.0); /** * Gets the default value for Double conversions. * @return The default Double value * @deprecated Register replacement converters for Double.TYPE and * Double.class instead */ public double getDefaultDouble() { return (defaultDouble.doubleValue()); } /** * Sets the default value for Double conversions. * @param newDefaultDouble The default Double value * @deprecated Register replacement converters for Double.TYPE and * Double.class instead */ public void setDefaultDouble(double newDefaultDouble) { defaultDouble = new Double(newDefaultDouble); register(new DoubleConverter(defaultDouble), Double.TYPE); register(new DoubleConverter(defaultDouble), Double.class); } /** * The default value for Float conversions. * @deprecated Register replacement converters for Float.TYPE and * Float.class instead */ private Float defaultFloat = new Float((float) 0.0); /** * Gets the default value for Float conversions. * @return The default Float value * @deprecated Register replacement converters for Float.TYPE and * Float.class instead */ public float getDefaultFloat() { return (defaultFloat.floatValue()); } /** * Sets the default value for Float conversions. * @param newDefaultFloat The default Float value * @deprecated Register replacement converters for Float.TYPE and * Float.class instead */ public void setDefaultFloat(float newDefaultFloat) { defaultFloat = new Float(newDefaultFloat); register(new FloatConverter(defaultFloat), Float.TYPE); register(new FloatConverter(defaultFloat), Float.class); } /** * The default value for Integer conversions. * @deprecated Register replacement converters for Integer.TYPE and * Integer.class instead */ private Integer defaultInteger = new Integer(0); /** * Gets the default value for Integer conversions. * @return The default Integer value * @deprecated Register replacement converters for Integer.TYPE and * Integer.class instead */ public int getDefaultInteger() { return (defaultInteger.intValue()); } /** * Sets the default value for Integer conversions. * @param newDefaultInteger The default Integer value * @deprecated Register replacement converters for Integer.TYPE and * Integer.class instead */ public void setDefaultInteger(int newDefaultInteger) { defaultInteger = new Integer(newDefaultInteger); register(new IntegerConverter(defaultInteger), Integer.TYPE); register(new IntegerConverter(defaultInteger), Integer.class); } /** * The default value for Long conversions. * @deprecated Register replacement converters for Long.TYPE and * Long.class instead */ private Long defaultLong = new Long(0); /** * Gets the default value for Long conversions. * @return The default Long value * @deprecated Register replacement converters for Long.TYPE and * Long.class instead */ public long getDefaultLong() { return (defaultLong.longValue()); } /** * Sets the default value for Long conversions. * @param newDefaultLong The default Long value * @deprecated Register replacement converters for Long.TYPE and * Long.class instead */ public void setDefaultLong(long newDefaultLong) { defaultLong = new Long(newDefaultLong); register(new LongConverter(defaultLong), Long.TYPE); register(new LongConverter(defaultLong), Long.class); } /** * The default value for Short conversions. * @deprecated Register replacement converters for Short.TYPE and * Short.class instead */ private static Short defaultShort = new Short((short) 0); /** * Gets the default value for Short conversions. * @return The default Short value * @deprecated Register replacement converters for Short.TYPE and * Short.class instead */ public short getDefaultShort() { return (defaultShort.shortValue()); } /** * Sets the default value for Short conversions. * @param newDefaultShort The default Short value * @deprecated Register replacement converters for Short.TYPE and * Short.class instead */ public void setDefaultShort(short newDefaultShort) { defaultShort = new Short(newDefaultShort); register(new ShortConverter(defaultShort), Short.TYPE); register(new ShortConverter(defaultShort), Short.class); } /** * Convert the specified value into a String. If the specified value * is an array, the first element (converted to a String) will be * returned. The registered {@link Converter} for the * <code>java.lang.String</code> class will be used, which allows * applications to customize Object->String conversions (the default * implementation simply uses toString()). * * @param value Value to be converted (may be null) * @return The converted String value */ public String convert(Object value) { if (value == null) { return ((String) null); } else if (value.getClass().isArray()) { if (Array.getLength(value) < 1) { return (null); } value = Array.get(value, 0); if (value == null) { return ((String) null); } else { Converter converter = lookup(String.class); return ((String) converter.convert(String.class, value)); } } else { Converter converter = lookup(String.class); return ((String) converter.convert(String.class, value)); } } /** * Convert the specified value to an object of the specified class (if * possible). Otherwise, return a String representation of the value. * * @param value Value to be converted (may be null) * @param clazz Java class to be converted to * @return The converted value * * @exception ConversionException if thrown by an underlying Converter */ public Object convert(String value, Class clazz) { if (log.isDebugEnabled()) { log.debug("Convert string '" + value + "' to class '" + clazz.getName() + "'"); } Converter converter = lookup(clazz); if (converter == null) { converter = lookup(String.class); } if (log.isTraceEnabled()) { log.trace(" Using converter " + converter); } return (converter.convert(clazz, value)); } /** * Convert an array of specified values to an array of objects of the * specified class (if possible). If the specified Java class is itself * an array class, this class will be the type of the returned value. * Otherwise, an array will be constructed whose component type is the * specified class. * * @param values Array of values to be converted * @param clazz Java array or element class to be converted to * @return The converted value * * @exception ConversionException if thrown by an underlying Converter */ public Object convert(String[] values, Class clazz) { Class type = clazz; if (clazz.isArray()) { type = clazz.getComponentType(); } if (log.isDebugEnabled()) { log.debug("Convert String[" + values.length + "] to class '" + type.getName() + "[]'"); } Converter converter = lookup(type); if (converter == null) { converter = lookup(String.class); } if (log.isTraceEnabled()) { log.trace(" Using converter " + converter); } Object array = Array.newInstance(type, values.length); for (int i = 0; i < values.length; i++) { Array.set(array, i, converter.convert(type, values[i])); } return (array); } /** * <p>Convert the value to an object of the specified class (if * possible).</p> * * @param value Value to be converted (may be null) * @param targetType Class of the value to be converted to * @return The converted value * * @exception ConversionException if thrown by an underlying Converter */ public Object convert(Object value, Class targetType) { Class sourceType = value == null ? null : value.getClass(); if (log.isDebugEnabled()) { if (value == null) { log.debug("Convert null value to type '" + targetType.getName() + "'"); } else { log.debug("Convert type '" + sourceType.getName() + "' value '" + value + "' to type '" + targetType.getName() + "'"); } } Object converted = value; Converter converter = lookup(sourceType, targetType); if (converter != null) { if (log.isTraceEnabled()) { log.trace(" Using converter " + converter); } converted = converter.convert(targetType, value); } if (targetType == String.class && converted != null && !(converted instanceof String)) { // NOTE: For backwards compatibility, if the Converter // doesn't handle conversion-->String then // use the registered String Converter converter = lookup(String.class); if (converter != null) { if (log.isTraceEnabled()) { log.trace(" Using converter " + converter); } converted = converter.convert(String.class, converted); } // If the object still isn't a String, use toString() method if (converted != null && !(converted instanceof String)) { converted = converted.toString(); } } return converted; } /** * Remove all registered {@link Converter}s, and re-establish the * standard Converters. */ public void deregister() { converters.clear(); registerPrimitives(false); registerStandard(false, false); registerOther(true); registerArrays(false, 0); register(BigDecimal.class, new BigDecimalConverter()); register(BigInteger.class, new BigIntegerConverter()); } /** * Register the provided converters with the specified defaults. * * @param throwException <code>true</code> if the converters should * throw an exception when a conversion error occurs, otherwise <code> * <code>false</code> if a default value should be used. * @param defaultNull <code>true</code>if the <i>standard</i> converters * (see {@link ConvertUtilsBean#registerStandard(boolean, boolean)}) * should use a default value of <code>null</code>, otherwise <code>false</code>. * N.B. This values is ignored if <code>throwException</code> is <code>true</code> * @param defaultArraySize The size of the default array value for array converters * (N.B. This values is ignored if <code>throwException</code> is <code>true</code>). * Specifying a value less than zero causes a <code>null<code> value to be used for * the default. */ public void register(boolean throwException, boolean defaultNull, int defaultArraySize) { registerPrimitives(throwException); registerStandard(throwException, defaultNull); registerOther(throwException); registerArrays(throwException, defaultArraySize); } /** * Register the converters for primitive types. * </p> * This method registers the following converters: * <ul> * <li><code>Boolean.TYPE</code> - {@link BooleanConverter}</li> * <li><code>Byte.TYPE</code> - {@link ByteConverter}</li> * <li><code>Character.TYPE</code> - {@link CharacterConverter}</li> * <li><code>Double.TYPE</code> - {@link DoubleConverter}</li> * <li><code>Float.TYPE</code> - {@link FloatConverter}</li> * <li><code>Integer.TYPE</code> - {@link IntegerConverter}</li> * <li><code>Long.TYPE</code> - {@link LongConverter}</li> * <li><code>Short.TYPE</code> - {@link ShortConverter}</li> * </ul> * @param throwException <code>true</code> if the converters should * throw an exception when a conversion error occurs, otherwise <code> * <code>false</code> if a default value should be used. */ private void registerPrimitives(boolean throwException) { register(Boolean.TYPE, throwException ? new BooleanConverter() : new BooleanConverter(Boolean.FALSE)); register(Byte.TYPE, throwException ? new ByteConverter() : new ByteConverter(ZERO)); register(Character.TYPE, throwException ? new CharacterConverter() : new CharacterConverter(SPACE)); register(Double.TYPE, throwException ? new DoubleConverter() : new DoubleConverter(ZERO)); register(Float.TYPE, throwException ? new FloatConverter() : new FloatConverter(ZERO)); register(Integer.TYPE, throwException ? new IntegerConverter() : new IntegerConverter(ZERO)); register(Long.TYPE, throwException ? new LongConverter() : new LongConverter(ZERO)); register(Short.TYPE, throwException ? new ShortConverter() : new ShortConverter(ZERO)); } /** * Register the converters for standard types. * </p> * This method registers the following converters: * <ul> * <li><code>BigDecimal.class</code> - {@link BigDecimalConverter}</li> * <li><code>BigInteger.class</code> - {@link BigIntegerConverter}</li> * <li><code>Boolean.class</code> - {@link BooleanConverter}</li> * <li><code>Byte.class</code> - {@link ByteConverter}</li> * <li><code>Character.class</code> - {@link CharacterConverter}</li> * <li><code>Double.class</code> - {@link DoubleConverter}</li> * <li><code>Float.class</code> - {@link FloatConverter}</li> * <li><code>Integer.class</code> - {@link IntegerConverter}</li> * <li><code>Long.class</code> - {@link LongConverter}</li> * <li><code>Short.class</code> - {@link ShortConverter}</li> * <li><code>String.class</code> - {@link StringConverter}</li> * </ul> * @param throwException <code>true</code> if the converters should * throw an exception when a conversion error occurs, otherwise <code> * <code>false</code> if a default value should be used. * @param defaultNull <code>true</code>if the <i>standard</i> converters * (see {@link ConvertUtilsBean#registerStandard(boolean, boolean)}) * should use a default value of <code>null</code>, otherwise <code>false</code>. * N.B. This values is ignored if <code>throwException</code> is <code>true</code> */ private void registerStandard(boolean throwException, boolean defaultNull) { Number defaultNumber = defaultNull ? null : ZERO; BigDecimal bigDecDeflt = defaultNull ? null : new BigDecimal("0.0"); BigInteger bigIntDeflt = defaultNull ? null : new BigInteger("0"); Boolean booleanDefault = defaultNull ? null : Boolean.FALSE; Character charDefault = defaultNull ? null : SPACE; String stringDefault = defaultNull ? null : ""; register(BigDecimal.class, throwException ? new BigDecimalConverter() : new BigDecimalConverter(bigDecDeflt)); register(BigInteger.class, throwException ? new BigIntegerConverter() : new BigIntegerConverter(bigIntDeflt)); register(Boolean.class, throwException ? new BooleanConverter() : new BooleanConverter(booleanDefault)); register(Byte.class, throwException ? new ByteConverter() : new ByteConverter(defaultNumber)); register(Character.class, throwException ? new CharacterConverter() : new CharacterConverter(charDefault)); register(Double.class, throwException ? new DoubleConverter() : new DoubleConverter(defaultNumber)); register(Float.class, throwException ? new FloatConverter() : new FloatConverter(defaultNumber)); register(Integer.class, throwException ? new IntegerConverter() : new IntegerConverter(defaultNumber)); register(Long.class, throwException ? new LongConverter() : new LongConverter(defaultNumber)); register(Short.class, throwException ? new ShortConverter() : new ShortConverter(defaultNumber)); register(String.class, throwException ? new StringConverter() : new StringConverter(stringDefault)); } /** * Register the converters for other types. * </p> * This method registers the following converters: * <ul> * <li><code>Class.class</code> - {@link ClassConverter}</li> * <li><code>java.util.Date.class</code> - {@link DateConverter}</li> * <li><code>java.util.Calendar.class</code> - {@link CalendarConverter}</li> * <li><code>File.class</code> - {@link FileConverter}</li> * <li><code>java.sql.Date.class</code> - {@link SqlDateConverter}</li> * <li><code>java.sql.Time.class</code> - {@link SqlTimeConverter}</li> * <li><code>java.sql.Timestamp.class</code> - {@link SqlTimestampConverter}</li> * <li><code>URL.class</code> - {@link URLConverter}</li> * </ul> * @param throwException <code>true</code> if the converters should * throw an exception when a conversion error occurs, otherwise <code> * <code>false</code> if a default value should be used. */ private void registerOther(boolean throwException) { register(Class.class, throwException ? new ClassConverter() : new ClassConverter(null)); register(java.util.Date.class, throwException ? new DateConverter() : new DateConverter(null)); register(Calendar.class, throwException ? new CalendarConverter() : new CalendarConverter(null)); register(File.class, throwException ? new FileConverter() : new FileConverter(null)); register(java.sql.Date.class, throwException ? new SqlDateConverter() : new SqlDateConverter(null)); register(java.sql.Time.class, throwException ? new SqlTimeConverter() : new SqlTimeConverter(null)); register(Timestamp.class, throwException ? new SqlTimestampConverter() : new SqlTimestampConverter(null)); register(URL.class, throwException ? new URLConverter() : new URLConverter(null)); } /** * Register array converters. * * @param throwException <code>true</code> if the converters should * throw an exception when a conversion error occurs, otherwise <code> * <code>false</code> if a default value should be used. * @param defaultArraySize The size of the default array value for array converters * (N.B. This values is ignored if <code>throwException</code> is <code>true</code>). * Specifying a value less than zero causes a <code>null<code> value to be used for * the default. */ private void registerArrays(boolean throwException, int defaultArraySize) { // Primitives registerArrayConverter(Boolean.TYPE, new BooleanConverter(), throwException, defaultArraySize); registerArrayConverter(Byte.TYPE, new ByteConverter(), throwException, defaultArraySize); registerArrayConverter(Character.TYPE, new CharacterConverter(), throwException, defaultArraySize); registerArrayConverter(Double.TYPE, new DoubleConverter(), throwException, defaultArraySize); registerArrayConverter(Float.TYPE, new FloatConverter(), throwException, defaultArraySize); registerArrayConverter(Integer.TYPE, new IntegerConverter(), throwException, defaultArraySize); registerArrayConverter(Long.TYPE, new LongConverter(), throwException, defaultArraySize); registerArrayConverter(Short.TYPE, new ShortConverter(), throwException, defaultArraySize); // Standard registerArrayConverter(BigDecimal.class, new BigDecimalConverter(), throwException, defaultArraySize); registerArrayConverter(BigInteger.class, new BigIntegerConverter(), throwException, defaultArraySize); registerArrayConverter(Boolean.class, new BooleanConverter(), throwException, defaultArraySize); registerArrayConverter(Byte.class, new ByteConverter(), throwException, defaultArraySize); registerArrayConverter(Character.class, new CharacterConverter(), throwException, defaultArraySize); registerArrayConverter(Double.class, new DoubleConverter(), throwException, defaultArraySize); registerArrayConverter(Float.class, new FloatConverter(), throwException, defaultArraySize); registerArrayConverter(Integer.class, new IntegerConverter(), throwException, defaultArraySize); registerArrayConverter(Long.class, new LongConverter(), throwException, defaultArraySize); registerArrayConverter(Short.class, new ShortConverter(), throwException, defaultArraySize); registerArrayConverter(String.class, new StringConverter(), throwException, defaultArraySize); // Other registerArrayConverter(Class.class, new ClassConverter(), throwException, defaultArraySize); registerArrayConverter(java.util.Date.class, new DateConverter(), throwException, defaultArraySize); registerArrayConverter(Calendar.class, new DateConverter(), throwException, defaultArraySize); registerArrayConverter(File.class, new FileConverter(), throwException, defaultArraySize); registerArrayConverter(java.sql.Date.class, new SqlDateConverter(), throwException, defaultArraySize); registerArrayConverter(java.sql.Time.class, new SqlTimeConverter(), throwException, defaultArraySize); registerArrayConverter(Timestamp.class, new SqlTimestampConverter(), throwException, defaultArraySize); registerArrayConverter(URL.class, new URLConverter(), throwException, defaultArraySize); } /** * Register a new ArrayConverter with the specified element delegate converter * that returns a default array of the specified size in the event of conversion errors. * * @param componentType The component type of the array * @param componentConverter The converter to delegate to for the array elements * @param throwException Whether a conversion exception should be thrown or a default * value used in the event of a conversion error * @param defaultArraySize The size of the default array */ private void registerArrayConverter(Class componentType, Converter componentConverter, boolean throwException, int defaultArraySize) { Class arrayType = Array.newInstance(componentType, 0).getClass(); Converter arrayConverter = null; if (throwException) { arrayConverter = new ArrayConverter(arrayType, componentConverter); } else { arrayConverter = new ArrayConverter(arrayType, componentConverter, defaultArraySize); } register(arrayType, arrayConverter); } /** strictly for convenience since it has same parameter order as Map.put */ private void register(Class clazz, Converter converter) { register(new ConverterFacade(converter), clazz); } /** * Remove any registered {@link Converter} for the specified destination * <code>Class</code>. * * @param clazz Class for which to remove a registered Converter */ public void deregister(Class clazz) { converters.remove(clazz); } /** * Look up and return any registered {@link Converter} for the specified * destination class; if there is no registered Converter, return * <code>null</code>. * * @param clazz Class for which to return a registered Converter * @return The registered {@link Converter} or <code>null</code> if not found */ public Converter lookup(Class clazz) { return ((Converter) converters.get(clazz)); } /** * Look up and return any registered {@link Converter} for the specified * source and destination class; if there is no registered Converter, * return <code>null</code>. * * @param sourceType Class of the value being converted * @param targetType Class of the value to be converted to * @return The registered {@link Converter} or <code>null</code> if not found */ public Converter lookup(Class sourceType, Class targetType) { if (targetType == null) { throw new IllegalArgumentException("Target type is missing"); } if (sourceType == null) { return lookup(targetType); } Converter converter = null; // Convert --> String if (targetType == String.class) { converter = lookup(sourceType); if (converter == null && (sourceType.isArray() || Collection.class.isAssignableFrom(sourceType))) { converter = lookup(String[].class); } if (converter == null) { converter = lookup(String.class); } return converter; } // Convert --> String array if (targetType == String[].class) { if (sourceType.isArray() || Collection.class.isAssignableFrom(sourceType)) { converter = lookup(sourceType); } if (converter == null) { converter = lookup(String[].class); } return converter; } return lookup(targetType); } /** * Register a custom {@link Converter} for the specified destination * <code>Class</code>, replacing any previously registered Converter. * * @param converter Converter to be registered * @param clazz Destination class for conversions performed by this * Converter */ public void register(Converter converter, Class clazz) { converters.put(clazz, converter); } }

The table below shows all metrics for ConvertUtilsBean.java.

MetricValueDescription
BLOCKS67.00Number of blocks
BLOCK_COMMENT16.00Number of block comment lines
COMMENTS428.00Comment lines
COMMENT_DENSITY 1.52Comment density
COMPARISONS39.00Number of comparison operators
CYCLOMATIC104.00Cyclomatic complexity
DECL_COMMENTS50.00Comments in declarations
DOC_COMMENT399.00Number of javadoc comment lines
ELOC282.00Effective lines of code
EXEC_COMMENTS 7.00Comments in executable code
EXITS42.00Procedure exits
FUNCTIONS34.00Number of function declarations
HALSTEAD_DIFFICULTY110.77Halstead difficulty
HALSTEAD_EFFORT 0.00Halstead effort
INTERFACE_COMPLEXITY86.00Interface complexity
JAVA0001 0.00JAVA0001 Package name does not contain only lower case letters
JAVA0002 0.00JAVA0002 Package name does not begin with a top level domain name or country code
JAVA0003 0.00JAVA0003 Minimize use of on-demand (.*) imports
JAVA0004 0.00JAVA0004 Unnecessary import from java.lang
JAVA0005 0.00JAVA0005 Imports not in specified order
JAVA0006 0.00JAVA0006 Empty finally block
JAVA0007 0.00JAVA0007 Should not declare public field
JAVA0008 0.00JAVA0008 Empty catch block
JAVA0009 0.00JAVA0009 Protected member in final class
JAVA0010 0.00JAVA0010 Non-instantiable class does not contain a non-private static member
JAVA0011 0.00JAVA0011 Abstract class does not contain an abstract method
JAVA0012 0.00JAVA0012 Non-constructor method with same name as declaring class
JAVA0013 0.00JAVA0013 Non-blank final field is not static
JAVA0014 0.00JAVA0014 Class with only static members has non-private constructor
JAVA0015 0.00JAVA0015 Package class contains public nested type
JAVA0016 0.00JAVA0016 Abstract class contains public constructor
JAVA0017 0.00JAVA0017 Class name does not have required form
JAVA0018 0.00JAVA0018 Method name does not have required form
JAVA0019 0.00JAVA0019 Interface name does not have required form
JAVA0020 0.00JAVA0020 Field name does not have required form
JAVA0021 0.00JAVA0021 Interface method name does not have required form
JAVA0022 0.00JAVA0022 Static final field name does not have required form
JAVA0023 0.00JAVA0023 Empty finalize method
JAVA0024 0.00JAVA0024 Empty class
JAVA0025 0.00JAVA0025 Method override is empty
JAVA0026 0.00JAVA0026 Finalize method with parameters
JAVA0029 0.00JAVA0029 Private method not used
JAVA0030 0.00JAVA0030 Private field not used
JAVA0031 0.00JAVA0031 Case statement not properly closed
JAVA0032 0.00JAVA0032 Switch statement missing default
JAVA0033 0.00JAVA0033 default: not last case in switch statement
JAVA0034 0.00JAVA0034 Missing braces in if statement
JAVA0035 0.00JAVA0035 Missing braces in for statement
JAVA0036 0.00JAVA0036 Missing braces in while statement
JAVA0038 0.00JAVA0038 Non-case label in switch statement
JAVA0039 0.00JAVA0039 Break statement with label
JAVA0040 0.00JAVA0040 Switch statement contains N cases (maximum: M)
JAVA0041 0.00JAVA0041 Nested synchronized block
JAVA0042 0.00JAVA0042 Empty synchronized statement
JAVA0043 0.00JAVA0043 Inner class does not use outer class
JAVA0044 0.00JAVA0044 Serializable class with no instance variables
JAVA0045 0.00JAVA0045 Serializable class with only transient fields
JAVA0046 0.00JAVA0046 Name of class not derived from Exception ends with 'Exception'
JAVA0047 0.00JAVA0047 Serializable class derives from invalid base class
JAVA0048 0.00JAVA0048 Name of class derived from Exception does not end with 'Exception'
JAVA0049 0.00JAVA0049 Nested block at depth N (maximum: M)
JAVA0050 0.00JAVA0050 Class derives from java.lang.Error
JAVA0051 0.00JAVA0051 Class derives from java.lang.RuntimeException
JAVA0052 0.00JAVA0052 Class derives from java.lang.Throwable
JAVA0053 0.00JAVA0053 Unused label
JAVA0054 0.00JAVA0054 Inheritance depth N exceeds maximum M
JAVA0055 0.00JAVA0055 Class should be interface
JAVA0056 0.00JAVA0056 Unnecessary abstract modifier for interface or annotation
JAVA0057 0.00JAVA0057 Unnecessary default constructor
JAVA0058 0.00JAVA0058 Constructor calls super()
JAVA0059 0.00JAVA0059 Method override only calls super()
JAVA0061 0.00JAVA0061 Inaccessible member in anonymous class
JAVA0062 0.00JAVA0062 Public class missing public member or protected constructor
JAVA0063 0.00JAVA0063 Identifier name should not contain '$'
JAVA0064 0.00JAVA0064 N variations of identifier name (maximum: M)
JAVA0065 0.00JAVA0065 Unnecessary final modifier for method in final class
JAVA0066 0.00JAVA0066 Unnecessary modifier for interface nested type
JAVA0067 0.00JAVA0067 Array descriptor on identifier name
JAVA0068 0.00JAVA0068 Modifiers not declared in recommended order
JAVA0071 0.00JAVA0071 Strings compared with ==
JAVA0073 0.00JAVA0073 Integer division in floating-point context
JAVA0074 0.00JAVA0074 Use of Object.notify()
JAVA0075 0.00JAVA0075 Method parameter hides field
JAVA0076 0.00JAVA0076 Use of magic number
JAVA0077 0.00JAVA0077 Private field not used in declaring class
JAVA0078 0.00JAVA0078 Floating point values compared with ==
JAVA0079 0.00JAVA0079 Use of instance to reference static member
JAVA0080 0.00JAVA0080 Import declaration not used
JAVA0081 0.00JAVA0081 Boolean literal in comparison
JAVA0082 0.00JAVA0082 Unnecessary widening cast
JAVA0083 0.00JAVA0083 Unnecessary instanceof test
JAVA0084 0.00JAVA0084 Should use compound assignment operator
JAVA0085 0.00JAVA0085 Use of sun.* class
JAVA0087 0.00JAVA0087 Use of Thread.sleep()
JAVA0089 0.00JAVA0089 Use of restricted package
JAVA0092 0.00JAVA0092 Use of restricted type
JAVA0093 0.00JAVA0093 Redundant assignment
JAVA0094 0.00JAVA0094 Field hides a superclass field
JAVA0095 0.00JAVA0095 Uninitialized private field
JAVA0096 0.00JAVA0096 Field in nested class hides outer field
JAVA0098 0.00JAVA0098 Minimize use of implicit field initializers
JAVA0100 1.00JAVA0100 Class contains N non-final fields (maximum: M)
JAVA0101 0.00JAVA0101 Unnecessary modifier for field in interface
JAVA0102 0.00JAVA0102 Last statement in finalize() not super.finalize()
JAVA0103 0.00JAVA0103 Explicit call to finalize()
JAVA0104 0.00JAVA0104 finalize() only calls super.finalize()
JAVA0105 0.00JAVA0105 Duplicate import declaration
JAVA0106 0.00JAVA0106 Unnecessary import from current package
JAVA0108 2.00JAVA0108 Incorrect javadoc: no @param tag for 'parameter'
JAVA0109 0.00JAVA0109 Incorrect javadoc: no parameter 'parameter'
JAVA0110 0.00JAVA0110 Incorrect javadoc: no @return tag
JAVA0111 0.00JAVA0111 Incorrect javadoc: @return tag for void method
JAVA0112 0.00JAVA0112 Incorrect javadoc: no exception 'exception' in throws
JAVA0113 0.00JAVA0113 Incorrect javadoc: no @author tag
JAVA0114 0.00JAVA0114 Incorrect javadoc: no @version tag
JAVA0115 0.00JAVA0115 Incorrect javadoc: no @throws or @exception tag for 'exception'
JAVA0116 0.00JAVA0116 Missing javadoc: field 'field'
JAVA0117 0.00JAVA0117 Missing javadoc: method 'method'
JAVA0118 0.00JAVA0118 Missing javadoc: type 'type'
JAVA0119 0.00JAVA0119 Control variable changed within body of for loop
JAVA0123 0.00JAVA0123 Use all three components of for loop
JAVA0125 0.00JAVA0125 Continue statement with label
JAVA0126 0.00JAVA0126 Method declares unchecked exception in throws
JAVA0128 0.00JAVA0128 Public constructor in non-public class
JAVA0130 0.00JAVA0130 Non-static method does not use instance fields
JAVA0131 0.00JAVA0131 Compatible method does not override base
JAVA0132 2.00JAVA0132 Method overload with compatible signature
JAVA0133 0.00JAVA0133 Non-synchronized method overrides synchronized method
JAVA0135 0.00JAVA0135 Only one of Object.equals and Object.hashCode defined: missing 'method'
JAVA0136 1.00JAVA0136 N methods defined in class (maximum: M)
JAVA0137 0.00JAVA0137 Non-abstract class missing constructor
JAVA0138 0.00JAVA0138 N parameters defined for method (maximum: M)
JAVA0139 0.00JAVA0139 Definition of main other than public static void main(java.lang.String[])
JAVA0141 0.00JAVA0141 Unnecessary modifier for method in interface
JAVA0143 0.00JAVA0143 Synchronized method
JAVA0144 0.00JAVA0144 Line exceeds maximum M characters
JAVA0145 0.00JAVA0145 Tab character used in source file
JAVA0150 0.00JAVA0150 java.lang.Error (or subclass) thrown
JAVA0153 0.00JAVA0153 Inefficient conversion of integer to string
JAVA0159 0.00JAVA0159 Inefficient conversion of string to integer
JAVA0160 0.00JAVA0160 Method does not throw specified exception
JAVA0161 0.00JAVA0161 Conditional wait() not in loop
JAVA0163 0.00JAVA0163 Empty statement
JAVA0165 0.00JAVA0165 Conflicting return statement in finally block
JAVA0166 0.00JAVA0166 Generic exception caught
JAVA0167 0.00JAVA0167 ThreadDeath not rethrown
JAVA0169 0.00JAVA0169 Unnecessary catch block: exception 'exception'
JAVA0170 0.00JAVA0170 Caught exception not derived from java.lang.Exception
JAVA0171 0.00JAVA0171 Unused local variable
JAVA0173 0.00JAVA0173 Unused method parameter
JAVA0174 0.00JAVA0174 Assigned local variable never used
JAVA0175 0.00JAVA0175 Successive assignment to variable
JAVA0176 0.00JAVA0176 Local variable name does not have required form
JAVA0177 0.00JAVA0177 Variable declaration missing initializer
JAVA0179 0.00JAVA0179 Local variable hides visible field
JAVA0233 0.00JAVA0233 Definition of serialVersionUID other than 'private static final long serialVersionUID'
JAVA0234 0.00JAVA0234 Class is Serializable but does not define serialVersionUID
JAVA0235 0.00JAVA0235 Class defines serialVersionUID but does not implement Serializable
JAVA0236 0.00JAVA0236 Attempt to clone an object which does not implement Cloneable
JAVA0237 0.00JAVA0237 Class implements Cloneable but does not have public clone method
JAVA0238 0.00JAVA0238 Clone method does not call super.clone()
JAVA0239 0.00JAVA0239 Class declares 'readObject' or 'writeObject' but does not implement Serializable
JAVA0240 0.00JAVA0240 Serializable class which declares readObject or writeObject but not both
JAVA0241 0.00JAVA0241 'readObject' or 'writeObject' should be declared private in Serializable class
JAVA0242 0.00JAVA0242 Transient field in non-Serializable class
JAVA0243 0.00JAVA0243 'readResolve' or 'writeReplace' should be declared private or protected
JAVA0244 0.00JAVA0244 Field or method name in subclass differs only by case from inherited field or method
JAVA0245 0.00JAVA0245 JUnit TestCase with non-trivial constructor
JAVA0246 0.00JAVA0246 JUnit assertXXX statement missing message parameter
JAVA0247 0.00JAVA0247 JUnit 'setUp()' and 'tearDown()' should call super method
JAVA0248 0.00JAVA0248 JUnit method 'setUp' or 'tearDown' with incorrect signature
JAVA0249 0.00JAVA0249 JUnit TestCase 'suite()' should be declared static
JAVA0250 0.00JAVA0250 JUnit TestCase declares testXXX method with incorrect signature
JAVA0251 0.00JAVA0251 Use '%n' for line breaks in printf/format for platform independence
JAVA0252 0.00JAVA0252 'enum' is a Java 1.5 reserved word
JAVA0253 0.00JAVA0253 Not all enum constants consumed in switch statement
JAVA0254 0.00JAVA0254 Use enhanced for loop construct instead of Iterator
JAVA0255 0.00JAVA0255 Result of method invocation not used
JAVA0256 0.00JAVA0256 Assignment of external collection/array to field
JAVA0257 0.00JAVA0257 Use of 'Constant Interface' anti-pattern
JAVA0258 0.00JAVA0258 Implement Iterable for foreach compatibility
JAVA0259 0.00JAVA0259 Return of collection/array field
JAVA0260 0.00JAVA0260 Use 'enum' instead of Enumerated Type pattern
JAVA0261 0.00JAVA0261 Use specialized Enum collection types
JAVA0262 0.00JAVA0262 Use of char in integer context
JAVA0263 0.00JAVA0263 Long literal ends with 'l' instead of 'L'
JAVA0264 0.00JAVA0264 Integer math in long context - check for overflow
JAVA0265 0.00JAVA0265 Use of Throwable.printStackTrace()
JAVA0266 0.00JAVA0266 Use of System.out
JAVA0267 0.00JAVA0267 Use of System.err
JAVA0269 0.00JAVA0269 Contents of StringBuffer never used
JAVA0270 0.00JAVA0270 Use Java 5.0 enhanced for loop construct to iterate over all elements in an array
JAVA0271 0.00JAVA0271 Minimize use of on-demand (.*) static imports
JAVA0272 0.00JAVA0272 Thread.run() called
JAVA0273 0.00JAVA0273 Non-final derivative of Thread calls start() in constructor
JAVA0274 0.00JAVA0274 Serializable class has a synchronized readObject()
JAVA0275 0.00JAVA0275 Serializable class has a synchronized writeObject() and no other synchronized methods
JAVA0276 0.00JAVA0276 Unnecessary use of String constructor
JAVA0277 0.00JAVA0277 Iterator.next() implementation does not throw NoSuchElementException
JAVA0278 0.00JAVA0278 Unnecessary use of Boolean constructor
JAVA0279 0.00JAVA0279 Serialization method readObject or readObjectNoData calls an overridable method
JAVA0280 0.00JAVA0280 IllegalMonitorStateException caught
JAVA0281 0.00JAVA0281 Iterator.next() not called in loop
JAVA0282 0.00JAVA0282 Call to Iterator.next() in loop which does not test Iterator.hasNext()
JAVA0283 0.00JAVA0283 Control variable not updated in loop body
JAVA0284 0.00JAVA0284 Explicit garbage collection
JAVA0285 0.00JAVA0285 Dereference of potentially null variable
JAVA0286 0.00JAVA0286 Dereference of null variable
JAVA0287 0.00JAVA0287 Unnecessary null check
JAVA0288 0.00JAVA0288 Inconsistent null check
LINES876.00Number of lines in the source file
LINE_COMMENT13.00Number of line comments
LOC345.00Lines of code
LOGICAL_LINES207.00Number of statements
LOOPS 1.00Number of loops
NEST_DEPTH 4.00Maximum nesting depth
OPERANDS1070.00Number of operands
OPERATORS2151.00Number of operators
PARAMS36.00Number of formal parameter declarations
PROGRAM_LENGTH3221.00Halstead program length
PROGRAM_VOCAB274.00Halstead program vocabulary
PROGRAM_VOLUME 0.00Halstead program volume
RETURNS50.00Number of return points from functions
SIZE37428.00Size of the file in bytes
UNIQUE_OPERANDS227.00Number of unique operands
UNIQUE_OPERATORS47.00Number of unique operators
WHITESPACE103.00Number of whitespace lines