[jbosscache-commits] JBoss Cache SVN: r4788 - core/trunk/src/main/java/org/jboss/cache/util.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Tue Nov 27 14:04:35 EST 2007


Author: manik.surtani at jboss.com
Date: 2007-11-27 14:04:35 -0500 (Tue, 27 Nov 2007)
New Revision: 4788

Added:
   core/trunk/src/main/java/org/jboss/cache/util/BeanUtils.java
Log:
JavaBean utility class

Added: core/trunk/src/main/java/org/jboss/cache/util/BeanUtils.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/util/BeanUtils.java	                        (rev 0)
+++ core/trunk/src/main/java/org/jboss/cache/util/BeanUtils.java	2007-11-27 19:04:35 UTC (rev 4788)
@@ -0,0 +1,67 @@
+package org.jboss.cache.util;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.lang.reflect.Method;
+import java.util.Locale;
+
+/**
+ * Simple JavaBean manipulation helper methods
+ *
+ * @author Manik Surtani (<a href="mailto:manik at jboss.org">manik at jboss.org</a>)
+ * @since 2.1.0
+ */
+public class BeanUtils
+{
+   private static Log log = LogFactory.getLog(BeanUtils.class);
+   /**
+    * Retrieves a setter name based on a field name passed in
+    * @param fieldName field name to find setter for
+    * @return name of setter method
+    */
+   public static String setterName(String fieldName)
+   {
+      StringBuilder sb = new StringBuilder("set");
+      if (fieldName != null && fieldName.length() > 0)
+      {
+         sb.append(fieldName.substring(0, 1).toUpperCase(Locale.ENGLISH));
+         if (fieldName.length() > 1)
+         {
+            sb.append(fieldName.substring(1));
+         }
+      }
+      return sb.toString();
+   }
+
+   /**
+    * Returns a getter for a given class
+    * @param componentClass class to find getter for
+    * @return name of getter method
+    */
+   public static String getterName(Class componentClass)
+   {
+      StringBuilder sb = new StringBuilder("get");
+      sb.append(componentClass.getSimpleName());
+      return sb.toString();
+   }
+
+   /**
+    * Returns a Method object corresponding to a getter that retrieves an instance of componentClass from target.
+    * @param target class that the getter should exist on
+    * @param componentClass component to get
+    * @return Method object, or null of one does not exist
+    */
+   public static Method getterMethod(Class target, Class componentClass)
+   {
+      try
+      {
+         return target.getMethod(getterName(componentClass));
+      }
+      catch (NoSuchMethodException e)
+      {
+         log.trace("Unable to find method " + getterName(componentClass) + " in class " + target);
+         return null;
+      }
+   }
+}




More information about the jbosscache-commits mailing list