2ronwalf/*
2ronwalf * Created on Apr 15, 2005
2ronwalf */
3daenzeroramapackage org.mindswap.utils;
2ronwalf
28daenzeroramaimport java.lang.reflect.Method;
2ronwalfimport java.net.MalformedURLException;
2ronwalfimport java.net.URL;
28daenzeroramaimport java.util.Hashtable;
2ronwalf
2ronwalf/**
2ronwalf * This class offers a few helpful methods for handling primitive datatypes in the Java Reflection API
2ronwalf *
2ronwalf * @author Michael Daenzer
2ronwalf */
28daenzeroramapublic class ReflectionUtils {
2ronwalf
2ronwalf /**
2ronwalf * Takes a Class Variable for primitive Datatype or its Wrapper-Classes and a String.
2ronwalf * Then an instance of the Class (for primitive Datatypes an instance of its Wrapper-Class)
2ronwalf * is created and the string value is assigned to the instance.
2ronwalf *
2ronwalf * @param lexicalValue
2ronwalf * @param type
2ronwalf * @return An instance of type with the assigned lexicalValue
2ronwalf */
2ronwalf public static Object getCastedObjectFromStringValue(String lexicalValue, Class type) {
2ronwalf String strClass = type.getName();
2ronwalf Object retVal = null;
2ronwalf
2ronwalf if ("String".equals(strClass) || "java.lang.String".equals(strClass))
2ronwalf return lexicalValue;
2ronwalf else if ("java.net.URL".equals(strClass)) {
2ronwalf try {
2ronwalf return new URL(lexicalValue);
2ronwalf }
2ronwalf catch( MalformedURLException e ) {
2ronwalf e.printStackTrace();
2ronwalf }
2ronwalf }
2ronwalf else if ("short".equals(strClass) || "java.lang.Short".equals(strClass))
2ronwalf return new Short(lexicalValue);
2ronwalf else if ("byte".equals(strClass) || "java.lang.Byte".equals(strClass))
2ronwalf return new Byte(lexicalValue);
2ronwalf else if ("int".equals(strClass) || "java.lang.Integer".equals(strClass))
2ronwalf //return new Integer(lexicalValue);
2ronwalf retVal = new Integer(lexicalValue);
2ronwalf else if ("long".equals(strClass) || "java.lang.Long".equals(strClass))
2ronwalf return new Long(lexicalValue);
2ronwalf else if ("float".equals(strClass) || "java.lang.Float".equals(strClass))
2ronwalf return new Float(lexicalValue);
2ronwalf else if ("double".equals(strClass) || "java.lang.Double".equals(strClass))
2ronwalf return new Double(lexicalValue);
2ronwalf else if ("char".equals(strClass) || "java.lang.Character".equals(strClass))
2ronwalf return new Character(lexicalValue.charAt(0));
2ronwalf else if ("boolean".equals(strClass) || "java.lang.Boolean".equals(strClass))
2ronwalf return new Boolean(lexicalValue);
2ronwalf
2ronwalf // TODO dmi catch classes with a constructor with one string param
2ronwalf return retVal;
2ronwalf }
2ronwalf
2ronwalf /**
2ronwalf * Takes a String and returns a Class Object represented by the value of the String. This is
2ronwalf * intended for use with Class.getDeclaredMethods()
2ronwalf * Returns xx.TYPE Classes for primitive Datatypes
2ronwalf *
2ronwalf * @return Class Object specified by strClass
2ronwalf * @see java.lang.Class#getDeclaredMethod(java.lang.String, java.lang.Class[])
2ronwalf */
2ronwalf public static Class getClassFromString(String strClass) {
2ronwalf Class javaClass = null;
2ronwalf
2ronwalf // treat primitive datatypes
2ronwalf if ("short".equals(strClass))
2ronwalf return Short.TYPE;
2ronwalf else if ("byte".equals(strClass))
2ronwalf return Byte.TYPE;
2ronwalf else if ("int".equals(strClass))
2ronwalf return Integer.TYPE;
2ronwalf else if ("long".equals(strClass))
2ronwalf return Long.TYPE;
2ronwalf else if ("float".equals(strClass))
2ronwalf return Float.TYPE;
2ronwalf else if ("double".equals(strClass))
2ronwalf return Double.TYPE;
2ronwalf else if ("char".equals(strClass))
2ronwalf return Character.TYPE;
2ronwalf else if ("boolean".equals(strClass))
2ronwalf return Boolean.TYPE;
2ronwalf // treat real classes
2ronwalf try {
2ronwalf javaClass = Class.forName(strClass);
2ronwalf } catch (Exception e) {
27daenzerorama javaClass = null;
2ronwalf }
2ronwalf
2ronwalf return javaClass;
2ronwalf }
2ronwalf
27daenzerorama /**
27daenzerorama * Takes a primitive data type (as a string) and returns the corresponding
27daenzerorama * wrapper class (as a string), i.e. returns java.lang.Integer for int.
27daenzerorama *
27daenzerorama * @return String the name of the wrapper class for the given primitive data type
27daenzerorama */
27daenzerorama public static String getWrapperTypeForPrimitive(String primitive) {
27daenzerorama // treat primitive datatypes
27daenzerorama if ("short".equals(primitive))
27daenzerorama return Short.class.getName();
27daenzerorama else if ("byte".equals(primitive))
27daenzerorama return Byte.class.getName();
27daenzerorama else if ("int".equals(primitive))
27daenzerorama return Integer.class.getName();
27daenzerorama else if ("long".equals(primitive))
27daenzerorama return Long.class.getName();
27daenzerorama else if ("float".equals(primitive))
27daenzerorama return Float.class.getName();
27daenzerorama else if ("double".equals(primitive))
27daenzerorama return Double.class.getName();
27daenzerorama else if ("char".equals(primitive))
27daenzerorama return Character.class.getName();
27daenzerorama else if ("boolean".equals(primitive))
27daenzerorama return Boolean.class.getName();
27daenzerorama // treat real classes
27daenzerorama return primitive;
27daenzerorama }
28daenzerorama
28daenzerorama public static Hashtable inferFieldFromGetter(Class claz) {
28daenzerorama Hashtable inferredFields = new Hashtable();
28daenzerorama
28daenzerorama Method[] methods = claz.getMethods();
28daenzerorama for (int i = 0; i < methods.length; i++) {
28daenzerorama String methodName = methods[i].getName();
28daenzerorama if (methodName.startsWith("get")) {
28daenzerorama inferredFields.put(methodName.substring(3), methods[i].getReturnType());
28daenzerorama } else if (methodName.startsWith("set") && methods[i].getParameterTypes().length == 1) {
28daenzerorama inferredFields.put(methodName.substring(3), methods[i].getParameterTypes()[0]);
28daenzerorama }
28daenzerorama }
28daenzerorama return inferredFields;
28daenzerorama }
2ronwalf}