Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.infinispan.commons.util;
  
  import java.util.Locale;

Simple JavaBean manipulation helper methods

Author(s):
Manik Surtani (manik@jboss.org)
Since:
4.0
 
 public class BeanUtils {
   
Retrieves a setter name based on a field name passed in

Parameters:
fieldName field name to find setter for
Returns:
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(.));
          if (fieldName.length() > 1) {
             sb.append(fieldName.substring(1));
          }
       }
       return sb.toString();
    }

   
Retrieves a setter name based on a field name passed in

Parameters:
fieldName field name to find setter for
Returns:
name of setter method
 
    public static String fluentSetterName(String fieldName) {
       StringBuilder sb = new StringBuilder();
       if (fieldName != null && fieldName.length() > 0) {
          sb.append(fieldName.substring(0, 1));
          if (fieldName.length() > 1) {
             sb.append(fieldName.substring(1));
          }
       }
       return sb.toString();
    }

   
Returns a getter for a given class

Parameters:
componentClass class to find getter for
Returns:
name of getter method
 
    public static String getterName(Class<?> componentClass) {
       if (componentClass == nullreturn null;
       StringBuilder sb = new StringBuilder("get");
       sb.append(componentClass.getSimpleName());
       return sb.toString();
    }

   
Returns a setter for a given class

Parameters:
componentClass class to find setter for
Returns:
name of getter method
 
    public static String setterName(Class<?> componentClass) {
       if (componentClass == nullreturn null;
       StringBuilder sb = new StringBuilder("set");
       sb.append(componentClass.getSimpleName());
       return sb.toString();
    }


   
Returns a Method object corresponding to a getter that retrieves an instance of componentClass from target.

Parameters:
target class that the getter should exist on
componentClass component to get
Returns:
Method object, or null of one does not exist
 
    public static Method getterMethod(Class<?> targetClass<?> componentClass) {
       try {
          return target.getMethod(getterName(componentClass));
       }
       catch (NoSuchMethodException e) {
          //if (log.isTraceEnabled()) log.trace("Unable to find method " + getterName(componentClass) + " in class " + target);
          return null;
       }
       catch (NullPointerException e) {
          return null;
       }
    }

   
Returns a Method object corresponding to a setter that sets an instance of componentClass from target.

Parameters:
target class that the setter should exist on
componentClass component to set
Returns:
Method object, or null of one does not exist
   public static Method setterMethod(Class<?> targetClass<?> componentClass) {
      try {
         return target.getMethod(setterName(componentClass), componentClass);
      }
      catch (NoSuchMethodException e) {
         //if (log.isTraceEnabled()) log.trace("Unable to find method " + setterName(componentClass) + " in class " + target);
         return null;
      }
      catch (NullPointerException e) {
         return null;
      }
   }
New to GrepCode? Check out our FAQ X