views:

3379

answers:

4

I need to make some reflective method calls in Java. Those calls will include methods that have arguments that are primitive types (int, double, etc.). The way to specify such types when looking up the method reflectively is int.class, double.class, etc.

The challenge is that I am accepting input from an outside source that will specify the types dynamically. Therefore, I need to come up with these Class references dynamically as well. Imagine a delimited file a list of method names with lists of parameter types:

doSomething int double
doSomethingElse java.lang.String boolean

If the input was something like java.lang.String, I know I could use Class.forName("java.lang.String") to that Class instance back. Is there any way to use that method, or another, to get the primitive type Classes back?

Edit: Thanks to all the respondents. It seems clear that there is no built-in way to cleanly do what I want, so I will settle for reusing the ClassUtils class from the Spring framework. It seems to contain a replacement for Class.forName() that will work with my requirements.

+2  A: 

The Class instances for the primitive types are obtainable as you said using e.g. int.class, but it is also possible to get the same values using something like Integer.TYPE. Each primitive wrapper class contains a static field, TYPE, which has the corresponding primitive class instance.

You cannot obtain the primitive class via forName, but you can get it from a class which is readily available. If you absolutely must use reflection, you can try something like this:

Class clazz = Class.forName("java.lang.Integer");
Class intClass = clazz.getField("TYPE").get(null);

intClass.equals(int.class);         // => true
Daniel Spiewak
This is true, and I was not aware of it. However, in my situation, I need a way of specifying any method can be called reflectively. Therefore, if I accept java.lang.Integer to represent int values, I'll lose the ability to call methods that accept Integer values.
Mike Furtak
You can just test to see if you have received some arbitrary special type (say: "int") and then do the rewrite appropriately. There is no string value that you can feed to forName that will retrieve the class of a primitive type.
Daniel Spiewak
Ok, thanks. Your reply was helpful.
Mike Furtak
+3  A: 

The Spring framework contains a utility class ClassUtils which contains the static method forName. This method can be used for the exact purpose you described.

In case you don’t like to have a dependency on Spring: the source of the method can be found e. g. here. The class source code is licensed under the Apache 2.0 model.

Note however that the algorithm uses a hard-coded map of primitive types.

Sypholux
+4  A: 

Probably you just need to map the primitives and for the rest of the classes perform the "forName" method:

I would do something like:

void someWhere(){
     String methodDescription = "doSomething int double java.lang.Integer java.lang.String"
     String [] parts = methodDescription.split();
     String methodName= parts[0]
     Class [] paramsTypes = getParamTypes( parts ); // Well, not all the array, but a, sub array from 1 to arr.length..  

    Method m = someObject.class.getMethod( methodName, paramTypes );
    etc. etc etc.
}

public Class[] paramTypes( String [] array ){
     List<Class> list = new ArrayList<Class>();
     for( String type : array ) {
         if( builtInMap.contains( type )) {
             list.add( builtInMap.get( type ) );
          }else{
             list.add( Class.forName( type ) );
          }
     }
     return list.toArray();
}  

// That's right.
private Map<String,Class> builtInMap = new HashMap<String,Class>();{
       builtInMap("int", Integer.TYPE );
       builtInMap("long", Long.TYPE );
       builtInMap("double", Double.TYPE );
       builtInMap("float", Float.TYPE );
       builtInMap("bool", Boolean.TYPE );
       builtInMap("char", Character.TYPE );
       builtInMap("byte", Byte.TYPE );
       builtInMap("void", Void.TYPE );
       builtInMap("short", Short.TYPE );
}

That is, create a map where the primitives types are stored and if the description belong to a primitive then use the mapped class. This map may also be loaded from an external configuration file, to add flexibility so you add String as a built in instead of java.lang.String or potentially have method like this.

"doSomething string yes|no "

There are lots of this kind of code in OS projects like Struts, Hibernate, Spring and Apache libs ( just to mention a few ) , so you don't need to start from zero.

BTW. I did not compile the above code, but I'm pretty sure it works with little modifications don't down vote me for that.

OscarRyz
This is what is done inside of ObjectInputStream as well, it simply does a forName() lookup and if it throws a ClassNotFoundException, does a map lookup.
Robin
Thanks for the reply. I will reuse the utility method in Spring's ClassUtils that uses this lookup process.
Mike Furtak
Where are you using this "low level" code? Take a deep look on how to secure your service, otherwise you might get some "System.exit()" call from your remote client, and believe me the server will shut down!
OscarRyz
A: 

A number of Class methods don't handle primitives in a consistent fashion unfortunately. A common way around this in forName is to have a table like;

private static final Map<String, Class> BUILT_IN_MAP = 
    new ConcurrentHashMap<String, Class>();

static {
    for (Class c : new Class[]{void.class, boolean.class, byte.class, char.class,  
            short.class, int.class, float.class, double.class, long.class})
        BUILT_IN_MAP.put(c.getName(), c);
}

public static Class forName(String name) throws ClassNotFoundException {
    Class c = BUILT_IN_MAP.get(name);
    if (c == null)
        // assumes you have only one class loader!
        BUILT_IN_MAP.put(name, c = Class.forName(name));
    return c;
}
Peter Lawrey