You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/02/09 16:17:59 UTC

[commons-beanutils] 06/06: Generics

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-beanutils.git

commit 3526b1613618c1d1c2d16559613a27d99ee4b95c
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Feb 9 11:17:47 2023 -0500

    Generics
---
 .../commons/beanutils2/ConvertUtilsBean.java       | 35 +++++++++++-----------
 .../commons/beanutils2/ConvertUtilsBean2.java      |  2 +-
 .../memoryleaktests/MemoryLeakTestCase.java        | 11 ++++---
 3 files changed, 23 insertions(+), 25 deletions(-)

diff --git a/src/main/java/org/apache/commons/beanutils2/ConvertUtilsBean.java b/src/main/java/org/apache/commons/beanutils2/ConvertUtilsBean.java
index 385db594..04cb7ee1 100644
--- a/src/main/java/org/apache/commons/beanutils2/ConvertUtilsBean.java
+++ b/src/main/java/org/apache/commons/beanutils2/ConvertUtilsBean.java
@@ -234,9 +234,9 @@ public class ConvertUtilsBean {
 
     /**
      * Convert the specified value to an object of the specified class (if
-     * possible). Otherwise, return a String representation of the value.
+     * possible). Otherwise, return a {@link String} representation of the value.
      *
-     * @param <T> The desired return type
+     * @param <T> The <em>desired</em> return type
      * @param value Value to be converted (may be null)
      * @param clazz Java class to be converted to (must not be null)
      * @return The converted value
@@ -274,20 +274,19 @@ public class ConvertUtilsBean {
      *
      * @throws ConversionException if thrown by an underlying Converter
      */
-    public Object convert(final String[] values, final Class<?> clazz) {
-
-        Class<?> type = clazz;
-        if (clazz.isArray()) {
-            type = clazz.getComponentType();
-        }
+    public <T> Object convert(final String[] values, final Class<T> clazz) {
+        final Class<?> type = clazz.isArray() ? clazz.getComponentType() : clazz;
         if (LOG.isDebugEnabled()) {
-            LOG.debug("Convert String[" + values.length + "] to class '" +
-                      type.getName() + "[]'");
+            LOG.debug("Convert String[" + values.length + "] to class '" + type.getName() + "[]'");
         }
         Converter converter = lookup(type);
         if (converter == null) {
             converter = lookup(String.class);
         }
+        return convert(values, type, converter);
+    }
+
+    private <T> Object convert(final String[] values, final Class<T> type, final Converter<T> converter) {
         if (LOG.isTraceEnabled()) {
             LOG.trace("  Using converter " + converter);
         }
@@ -296,7 +295,6 @@ public class ConvertUtilsBean {
             Array.set(array, i, converter.convert(type, values[i]));
         }
         return array;
-
     }
 
     /**
@@ -310,11 +308,12 @@ public class ConvertUtilsBean {
      *
      * @throws ConversionException if thrown by an underlying Converter
      */
-    public Object convert(final Object value, final Class<?> targetType) {
-        final Class<?> sourceType = value == null ? null : value.getClass();
+    public <T> Object convert(final Object value, final Class<T> targetType) {
+        final boolean nullValue = value == null;
+        final Class<?> sourceType = nullValue ? null : value.getClass();
 
         if (LOG.isDebugEnabled()) {
-            if (value == null) {
+            if (nullValue) {
                 LOG.debug("Convert null value to type '" + targetType.getName() + "'");
             } else {
                 LOG.debug("Convert type '" + sourceType.getName() + "' value '" + value + "' to type '"
@@ -323,7 +322,7 @@ public class ConvertUtilsBean {
         }
 
         Object converted = value;
-        Converter converter = lookup(sourceType, targetType);
+        Converter<T> converter = lookup(sourceType, targetType);
         if (converter != null) {
             if (LOG.isTraceEnabled()) {
                 LOG.trace("  Using converter " + converter);
@@ -335,12 +334,12 @@ public class ConvertUtilsBean {
             // 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) {
+            Converter<String> strConverter = lookup(String.class);
+            if (strConverter != null) {
                 if (LOG.isTraceEnabled()) {
                     LOG.trace("  Using converter " + converter);
                 }
-                converted = converter.convert(String.class, converted);
+                converted = strConverter.convert(String.class, converted);
             }
 
             // If the object still isn't a String, use toString() method
diff --git a/src/main/java/org/apache/commons/beanutils2/ConvertUtilsBean2.java b/src/main/java/org/apache/commons/beanutils2/ConvertUtilsBean2.java
index 20526610..e268d49c 100644
--- a/src/main/java/org/apache/commons/beanutils2/ConvertUtilsBean2.java
+++ b/src/main/java/org/apache/commons/beanutils2/ConvertUtilsBean2.java
@@ -70,7 +70,7 @@ public class ConvertUtilsBean2 extends ConvertUtilsBean {
      * @see ConvertUtilsBean#convert(String[], Class)
      */
     @Override
-    public Object convert(final String[] value, final Class<?> clazz) {
+    public <T> Object convert(final String[] value, final Class<T> clazz) {
         return convert((Object) value, clazz);
     }
 
diff --git a/src/test/java/org/apache/commons/beanutils2/memoryleaktests/MemoryLeakTestCase.java b/src/test/java/org/apache/commons/beanutils2/memoryleaktests/MemoryLeakTestCase.java
index 596baed7..d14d284d 100644
--- a/src/test/java/org/apache/commons/beanutils2/memoryleaktests/MemoryLeakTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/memoryleaktests/MemoryLeakTestCase.java
@@ -361,9 +361,8 @@ public class MemoryLeakTestCase {
 
         // The classLoader will go away only when these following variables are released
         ClassLoader loader = newClassLoader();
-        Class<?> beanClass    = loader.loadClass(className);
-        Object bean        = beanClass.newInstance();
-
+        Class<?> beanClass = loader.loadClass(className);
+        Object bean = beanClass.newInstance();
 
         final WeakReference<ClassLoader> someRef = new WeakReference<>(loader);
 
@@ -375,13 +374,13 @@ public class MemoryLeakTestCase {
 
         // if you comment the following two lines, the test will work, and the ClassLoader will be released.
         // That proves that nothing is wrong with the test, and ConvertUtilsBean is holding a reference
-        ConvertUtils.register(new IntegerConverter(), beanClass);
+        ConvertUtils.register(new IntegerConverter(), (Class<Integer>) beanClass);
         assertEquals("12345", ConvertUtils.convert(bean, String.class));
 
         // this should make the reference go away.
-        loader    = null;
+        loader = null;
         beanClass = null;
-        bean      = null;
+        bean = null;
 
         forceGarbageCollection(); /* Try to force the garbage collector to run by filling up memory */