In this post I will describe how to convert all the String members of an object into uppercase. The same can be done to convert to lowercase. Creating a method in a class (JavaBean) can also solve this purpose, but when the JavaBeans are already available then modifying each class is a tedious process. This was exactly the problem that I was facing. Then I though of using something similar to the 'copyProperties' method of the 'BeanUtils' class in Commons BeanUtils.
The Java Reflection API came to assistance. Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique and can enable applications to perform operations which would otherwise be impossible.
The 'java.lang.reflect' package provides various classes and methods which can be used to deeply examine an object, find out its member variables and methods, invoke its setter and getter methods and much more. The below code will show how you can use the Java Reflection API to access the setter and getter methods of an object of any type and convert all the member variables of type 'String' to uppercase.
import java.lang.reflect.Field; import java.lang.reflect.Method; public class ReflectionUtil { public static void printValues(Object obj) { Class c = obj.getClass(); Field fields[] = c.getDeclaredFields(); Method method; String gMethodName, sMethodName, value; try { for(Field f : fields) { if(f.getType().toString().contains("java.lang.String")) { gMethodName = "get" + Character.toUpperCase(f.getName().charAt(0)) + f.getName().substring(1); method = c.getDeclaredMethod(gMethodName); value = (String) method.invoke(obj); System.out.println(f.getName() + ":" + f.getType() + ":" + value); } } } catch(Exception ex) {ex.printStackTrace();} } public static void convertToUpperCase(Object obj) { Class c = obj.getClass(); Field fields[] = c.getDeclaredFields(); Method method; String gMethodName, sMethodName, value; try { for(Field f : fields) { if(f.getType().toString().contains("java.lang.String")) { gMethodName = "get" + Character.toUpperCase(f.getName().charAt(0)) + f.getName().substring(1); method = c.getDeclaredMethod(gMethodName); value = (String) method.invoke(obj); value = value.toUpperCase(); sMethodName = "set" + Character.toUpperCase(f.getName().charAt(0)) + f.getName().substring(1); method = c.getDeclaredMethod(sMethodName, f.getType()); method.invoke(obj, new Object [] {value}); } } } catch(IllegalAccessException ex) {ex.printStackTrace();} catch(java.lang.reflect.InvocationTargetException ex) {ex.printStackTrace();} catch(java.lang.NoSuchMethodException ex) {ex.printStackTrace();} } public static void main(String... args) { Category category = new Category("abc", "cat name", null, "user", "code"); System.out.println("\nCalling convertToUpperCase"); ReflectionUtil.convertToUpperCase(category); System.out.println("\nCalling printValues"); ReflectionUtil.printValues(category); } }
For a detailed tutorial on Java Reflection API click here.