/** * Finds an instance of an Enum constant on a class. Useful for safely * getting the value of an enum constant without an exception being thrown * like the Enum.valueOf() method causes. Searches for enum constant * where case is sensitive. */ public static Object findEnumConstant(Class<?> type, String constantName) { return findEnumConstant(type, constantName, true); }
/** * Checks if the class implements public "bean" methods (get and set) for the * name. For example, for a name such as "firstName", this method will * check if the class has both getFirstName() and setFirstName() methods. * Also, this method will validate the return type matches the paramType * on the getXXX() method and that the setXXX() method only accepts that * paramType. The search is optionally case sensitive. * @param type The class to search * @param propertyName The property name to search for, if "firstName", then * this method will internally add the "get" and "set" to the beginning. * @param propertyType The class type of the property * @param caseSensitive If the search is case sensitive or not * @return True if the "bean" methods are correct, otherwise false. */ public static boolean hasBeanMethods(Class<?> type, String propertyName, Class<?> propertyType, boolean caseSensitive) { try { // if this succeeds without an exception, then the properties exist! @SuppressWarnings("unused") Method[] methods = getBeanMethods(type, propertyName, propertyType, caseSensitive); return true; } catch (Exception e) { return false; } }
/** * Gets the methods within this class that implements public "bean" methods * (get and set) for the property name. Returns an array of methods where * index=0 is the get method and index=1 is the set method. * For example, for a name such as "firstName", this method will * check if the class has both getFirstName() and setFirstName() methods. * Also, this method will validate the return type matches the propertyType * on the getXXX() method and that the setXXX() method only accepts that * propertyType. The search is optionally case sensitive. * @param type The class to search * @param propertyName The name to search for * @param paramType The class type of the property * @param caseSensitive If the search is case sensitive or not * @return An array of Methods were index=0 is the get method and index=1 is the set method * @throws java.lang.IllegalAccessException If the method was found, but is not public. * @throws java.lang.NoSuchMethodException If the method was not found */ public static Method[] getBeanMethods(Class<?> type, String propertyName, Class<?> propertyType, boolean caseSensitive) throws IllegalAccessException, NoSuchMethodException { Method methods[] = new Method[2]; // search for the "get" methods[0] = getMethod(type, "get"+propertyName, propertyType, null, caseSensitive); // search for the "set" methods[1] = getMethod(type, "set"+propertyName, null, propertyType, caseSensitive); return methods; }
Class[] hierarchy = ClassUtil.getClassHierarchy(type);
Class[] hierarchy = ClassUtil.getClassHierarchy(type);
/** * Finds an instance of an Enum constant on a class. Useful for safely * getting the value of an enum constant without an exception being thrown * like the Enum.valueOf() method causes. Searches for enum constant * where case is sensitive. */ public static Object findEnumConstant(Class<?> type, String constantName) { return findEnumConstant(type, constantName, true); }
/** * Gets the methods within this class that implements public "bean" methods * (get and set) for the property name. Returns an array of methods where * index=0 is the get method and index=1 is the set method. * For example, for a name such as "firstName", this method will * check if the class has both getFirstName() and setFirstName() methods. * Also, this method will validate the return type matches the propertyType * on the getXXX() method and that the setXXX() method only accepts that * propertyType. The search is optionally case sensitive. * @param type The class to search * @param propertyName The name to search for * @param paramType The class type of the property * @param caseSensitive If the search is case sensitive or not * @return An array of Methods were index=0 is the get method and index=1 is the set method * @throws java.lang.IllegalAccessException If the method was found, but is not public. * @throws java.lang.NoSuchMethodException If the method was not found */ public static Method[] getBeanMethods(Class<?> type, String propertyName, Class<?> propertyType, boolean caseSensitive) throws IllegalAccessException, NoSuchMethodException { Method methods[] = new Method[2]; // search for the "get" methods[0] = getMethod(type, "get"+propertyName, propertyType, null, caseSensitive); // search for the "set" methods[1] = getMethod(type, "set"+propertyName, null, propertyType, caseSensitive); return methods; }
/** * Checks if the class implements public "bean" methods (get and set) for the * name. For example, for a name such as "firstName", this method will * check if the class has both getFirstName() and setFirstName() methods. * Also, this method will validate the return type matches the paramType * on the getXXX() method and that the setXXX() method only accepts that * paramType. The search is optionally case sensitive. * @param type The class to search * @param propertyName The property name to search for, if "firstName", then * this method will internally add the "get" and "set" to the beginning. * @param propertyType The class type of the property * @param caseSensitive If the search is case sensitive or not * @return True if the "bean" methods are correct, otherwise false. */ public static boolean hasBeanMethods(Class<?> type, String propertyName, Class<?> propertyType, boolean caseSensitive) { try { // if this succeeds without an exception, then the properties exist! @SuppressWarnings("unused") Method[] methods = getBeanMethods(type, propertyName, propertyType, caseSensitive); return true; } catch (Exception e) { return false; } }
/** * Converts the string value into an Object of the Class type. Will either * delegate conversion to a PropertyConverter or will handle creating enums * directly. * * @param string0 The string value to convert * @param type The Class type to convert it into * @return A new Object converted from the String value */ static public <E> E convert(String s, Class<E> type) throws ConversionException { // if enum, handle differently if (type.isEnum()) { Object obj = ClassUtil.findEnumConstant(type, s); if (obj == null) { throw new ConversionException("Invalid constant [" + s + "] used, supported values [" + toListString(type.getEnumConstants()) + "]"); } return (E)obj; // else, handle normally } else { TypeConverter converter = REGISTRY.get(type); if (converter == null) { throw new ConversionException("The type [" + type.getSimpleName() + "] is not supported"); } return (E)converter.convert(s); } }
try { Method getMethod = ClassUtil.getMethod(type, getMethodName, beanProperty.getType(), null, caseSensitive); try { Method setMethod = ClassUtil.getMethod(type, setMethodName, null, beanProperty.getType(), caseSensitive); try { Method addMethod = ClassUtil.getMethod(type, addMethodName, null, beanProperty.getType(), caseSensitive);
try { Method getMethod = ClassUtil.getMethod(type, getMethodName, beanProperty.getType(), null, caseSensitive); try { Method setMethod = ClassUtil.getMethod(type, setMethodName, null, beanProperty.getType(), caseSensitive); try { Method addMethod = ClassUtil.getMethod(type, addMethodName, null, beanProperty.getType(), caseSensitive);