From b74922811252930023e989d6f3dc1bcbd63bbeeb Mon Sep 17 00:00:00 2001 From: Claus Ibsen Date: Mon, 17 Sep 2012 13:10:50 +0000 Subject: [PATCH] AMQ-4011: Do not use java bean property editors for type conversion. Marked stuff as @deprecated. git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1386596 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/activemq/util/BooleanEditor.java | 1 + .../org/apache/activemq/util/ListEditor.java | 1 + .../util/MemoryIntPropertyEditor.java | 1 + .../activemq/util/MemoryPropertyEditor.java | 1 + .../activemq/util/StringArrayConverter.java | 2 -- .../activemq/util/TypeConversionSupport.java | 34 ++++++++++++------- 6 files changed, 26 insertions(+), 14 deletions(-) diff --git a/activemq-core/src/main/java/org/apache/activemq/util/BooleanEditor.java b/activemq-core/src/main/java/org/apache/activemq/util/BooleanEditor.java index 53ac9877e8..d96079343d 100644 --- a/activemq-core/src/main/java/org/apache/activemq/util/BooleanEditor.java +++ b/activemq-core/src/main/java/org/apache/activemq/util/BooleanEditor.java @@ -18,6 +18,7 @@ package org.apache.activemq.util; import java.beans.PropertyEditorSupport; +@Deprecated public class BooleanEditor extends PropertyEditorSupport { public String getJavaInitializationString() { diff --git a/activemq-core/src/main/java/org/apache/activemq/util/ListEditor.java b/activemq-core/src/main/java/org/apache/activemq/util/ListEditor.java index 019a837935..3cc5b14ee7 100644 --- a/activemq-core/src/main/java/org/apache/activemq/util/ListEditor.java +++ b/activemq-core/src/main/java/org/apache/activemq/util/ListEditor.java @@ -26,6 +26,7 @@ import org.springframework.util.StringUtils; * Used to serialize lists of ActiveMQDestinations. * @see org.apache.activemq.util.IntrospectionSupport */ +@Deprecated public class ListEditor extends PropertyEditorSupport { public static final String DEFAULT_SEPARATOR = ","; diff --git a/activemq-core/src/main/java/org/apache/activemq/util/MemoryIntPropertyEditor.java b/activemq-core/src/main/java/org/apache/activemq/util/MemoryIntPropertyEditor.java index 88c5a9f049..099e5b93a6 100644 --- a/activemq-core/src/main/java/org/apache/activemq/util/MemoryIntPropertyEditor.java +++ b/activemq-core/src/main/java/org/apache/activemq/util/MemoryIntPropertyEditor.java @@ -24,6 +24,7 @@ import java.util.regex.Pattern; * Converts string values like "20 Mb", "1024kb", and "1g" to int values in * bytes. */ +@Deprecated public class MemoryIntPropertyEditor extends PropertyEditorSupport { public void setAsText(String text) throws IllegalArgumentException { diff --git a/activemq-core/src/main/java/org/apache/activemq/util/MemoryPropertyEditor.java b/activemq-core/src/main/java/org/apache/activemq/util/MemoryPropertyEditor.java index 0092530095..b906d3bd0b 100644 --- a/activemq-core/src/main/java/org/apache/activemq/util/MemoryPropertyEditor.java +++ b/activemq-core/src/main/java/org/apache/activemq/util/MemoryPropertyEditor.java @@ -24,6 +24,7 @@ import java.util.regex.Pattern; * Converts string values like "20 Mb", "1024kb", and "1g" to long values in * bytes. */ +@Deprecated public class MemoryPropertyEditor extends PropertyEditorSupport { public void setAsText(String text) throws IllegalArgumentException { diff --git a/activemq-core/src/main/java/org/apache/activemq/util/StringArrayConverter.java b/activemq-core/src/main/java/org/apache/activemq/util/StringArrayConverter.java index 25b8a85c1c..84f02a167c 100644 --- a/activemq-core/src/main/java/org/apache/activemq/util/StringArrayConverter.java +++ b/activemq-core/src/main/java/org/apache/activemq/util/StringArrayConverter.java @@ -29,8 +29,6 @@ import java.util.StringTokenizer; */ public class StringArrayConverter { - // TODO: Remove System.out - public static String[] convertToStringArray(Object value) { if (value == null) { return null; diff --git a/activemq-core/src/main/java/org/apache/activemq/util/TypeConversionSupport.java b/activemq-core/src/main/java/org/apache/activemq/util/TypeConversionSupport.java index 4e350b9e1b..1363f85ed0 100755 --- a/activemq-core/src/main/java/org/apache/activemq/util/TypeConversionSupport.java +++ b/activemq-core/src/main/java/org/apache/activemq/util/TypeConversionSupport.java @@ -18,12 +18,13 @@ package org.apache.activemq.util; import java.util.Date; import java.util.HashMap; +import java.util.Map; import org.apache.activemq.command.ActiveMQDestination; public final class TypeConversionSupport { - static class ConversionKey { + private static class ConversionKey { final Class from; final Class to; final int hashCode; @@ -48,7 +49,7 @@ public final class TypeConversionSupport { Object convert(Object value); } - private static final HashMap CONVERSION_MAP = new HashMap(); + private static final Map CONVERSION_MAP = new HashMap(); static { Converter toStringConverter = new Converter() { public Object convert(Object value) { @@ -142,20 +143,29 @@ public final class TypeConversionSupport { private TypeConversionSupport() { } - public static Object convert(Object value, Class clazz) { + public static Object convert(Object value, Class type) { - assert value != null && clazz != null; - - if (value.getClass() == clazz) { - return value; - } - - Converter c = CONVERSION_MAP.get(new ConversionKey(value.getClass(), clazz)); - if (c == null) { + if (value == null) { + // lets avoid NullPointerException when converting to boolean for null values + if (boolean.class.isAssignableFrom(type)) { + return Boolean.FALSE; + } return null; } - return c.convert(value); + // eager same instance type test to avoid the overhead of invoking the type converter + // if already same type + if (type.isInstance(value)) { + return type.cast(value); + } + + // lookup converter + Converter c = CONVERSION_MAP.get(new ConversionKey(value.getClass(), type)); + if (c != null) { + return c.convert(value); + } else { + return null; + } } }