Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically find the class that represents a primitive Java type

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.

like image 456
Mike Furtak Avatar asked Oct 07 '08 19:10

Mike Furtak


People also ask

How do you check if an object is a primitive type in Java?

The java. lang. Class. isPrimitive() method can determine if the specified object represents a primitive type.

What class does primitive datatypes use?

Wrapper classes provide a way to use primitive data types ( int , boolean , etc..) as objects.

Where are primitive type values be stored Java?

There are two kinds of memory used in Java. These are called stack memory and heap memory. Stack memory stores primitive types and the addresses of objects. The object values are stored in heap memory.

What are primitives in Java?

A primitive type is predefined by the language and is named by a reserved keyword. Primitive values do not share state with other primitive values. The eight primitive data types supported by the Java programming language are: byte: The byte data type is an 8-bit signed two's complement integer.


2 Answers

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 
like image 51
Daniel Spiewak Avatar answered Oct 12 '22 10:10

Daniel Spiewak


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 code of the method can be found e. g. here on their public repository. 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.


Edit: Thanks to commenters Dávid Horváth and Patrick for pointing out the broken link.

like image 37
Synoli Avatar answered Oct 12 '22 10:10

Synoli