Get class from a primitive datatype

D

deegoogle

Hi,
I have an array of Strings:
String[] somePrimitives = {"int", "char", "void" };
I want to achieve the equivalent of :
for(int i = 0; i < somePrimitives.length; i++) {
Class myClass = Class.forName(somePrimitives); //Wont work
}

We all know, we cannot do Class.forName("int") etc since "int" is not a path to
an actual class - whereas Class.forName("java.lang.Integer") will work.
Can anyone tell me how to achieve the above ?
thanks.
 
C

Chris Smith

deegoogle said:
I have an array of Strings:
String[] somePrimitives = {"int", "char", "void" };
I want to achieve the equivalent of :
for(int i = 0; i < somePrimitives.length; i++) {
Class myClass = Class.forName(somePrimitives); //Wont work
}

We all know, we cannot do Class.forName("int") etc since "int" is not a path to
an actual class - whereas Class.forName("java.lang.Integer") will work.
Can anyone tell me how to achieve the above ?


Yep. Here goes:

public Class getPrimitiveType(String name)
{
if (name.equals("byte")) return byte.class;
if (name.equals("short")) return short.class;
if (name.equals("int")) return int.class;
if (name.equals("long")) return long.class;
if (name.equals("char")) return char.class;
if (name.equals("float")) return float.class;
if (name.equals("double")) return double.class;
if (name.equals("boolean")) return boolean.class;
if (name.equals("void")) return void.class;

throw new IllegalArgumentException();
}

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

deegoogle said:
Hi,
I have an array of Strings:
String[] somePrimitives = {"int", "char", "void" };
I want to achieve the equivalent of :
for(int i = 0; i < somePrimitives.length; i++) {
Class myClass = Class.forName(somePrimitives); //Wont work
}

We all know, we cannot do Class.forName("int") etc since "int" is not a path to
an actual class - whereas Class.forName("java.lang.Integer") will work.
Can anyone tell me how to achieve the above ?
thanks.


Integer.TYPE, Byte.TYPE etc. Obviously you have to parse your strings first.
 
D

deegoogle

Thanks for the reply. But I don't want to put in a bunch of 'if'
conditions/case statements or even use a static map. In fact I don't
need to know the various primitive datatypes available in java (in
fact, I shouldn't need to care!)...the language should have a
provision to provided it.
With the above in mind, how can I get the class by just using the
string value as input ?
 
S

Stanimir Stamenkov

/deegoogle/:
Thanks for the reply. But I don't want to put in a bunch of 'if'
conditions/case statements or even use a static map. In fact I don't
need to know the various primitive datatypes available in java (in
fact, I shouldn't need to care!)...the language should have a
provision to provided it.
With the above in mind, how can I get the class by just using the
string value as input ?

No other than using "bunch of 'if' statements or use a static map".
Primitives are small finite set so neither the "bunch of 'if'
statements" nor the "static map" would change, once written. That's
not the case with normal classes and that's why there is
Class.forName(String) method.
 
C

Chris Smith

deegoogle said:
Thanks for the reply. But I don't want to put in a bunch of 'if'
conditions/case statements or even use a static map. In fact I don't
need to know the various primitive datatypes available in java (in
fact, I shouldn't need to care!)...the language should have a
provision to provided it.
With the above in mind, how can I get the class by just using the
string value as input ?

I don't know how you determined that you don't need to know the list of
primitive types supported by the language, but whoever told you that was
wrong. You do, indeed, need to know exactly that if you want to
accomplish what you asked for.

Your attitude is a bit unusual. No one did anything but try to be
helpful and answer your question. I'll try not to make that mistake
again.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
T

Timo Kinnunen

Hi,
I have an array of Strings:
String[] somePrimitives = {"int", "char", "void" };
I want to achieve the equivalent of :
for(int i = 0; i < somePrimitives.length; i++) {
Class myClass = Class.forName(somePrimitives); //Wont work
}

We all know, we cannot do Class.forName("int") etc since "int" is not a path to
an actual class - whereas Class.forName("java.lang.Integer") will work.
Can anyone tell me how to achieve the above ?
thanks.


There does appear to be a slight irregularity in how Java handles Classes
for primitives. int.class.getName() produces "int", but
Class.forName("int") throws ClassNotFoundException. However,
int[].class.getName() produces "[I" and Class.forName("[I") succeeds.

This suggests a hackish solution:

String[] somePrimitives = {"[I", "[C" };
for(int i = 0; i < somePrimitives.length; i++) {
Class myClass = Class.forName(somePrimitives).getComponentType();
}

but it doesn't help you with void.

Why this irregularity?
 
T

Timo Kinnunen

There does appear to be a slight irregularity in how Java handles Classes
for primitives. int.class.getName() produces "int", but
Class.forName("int") throws ClassNotFoundException. However,
int[].class.getName() produces "[I" and Class.forName("[I") succeeds.

This suggests a hackish solution:

After reading Daniel Sjöblom's reply the proper solution using reflection
became obvious:

String[] somePrimitives = {"java.lang.Integer", "java.lang.Character",
"java.lang.Void" };
for(int i = 0; i < somePrimitives.length; i++) {
Class myClassWrapper = Class.forName(somePrimitives);
Class myClass = (Class) myClassWrapper.getField("TYPE").get(null);
}

But still, why the irregularity with primitive types and their array
counterparts?
 
G

Gordon Beaton

but it doesn't help you with void.

Why this irregularity?

"void" isn't a datatype in Java, it's a keyword used to indicate the
absence of a return value.

Off hand I can't think of any other situations where void can be used.
For example, you can't use void to declare variables or method
arguments.

/gordon
 
C

Chris Uppal

Gordon said:
"void" isn't a datatype in Java, it's a keyword used to indicate the
absence of a return value.

But it does have a java.lang.class (available as java.lang.Void.TYPE).

It's needed, for instance, by java.lang.reflect.Method.getReturnType().

As for the "irregularity" that Timo mentioned. Part of it is that "int" is a
legal class name (though not in Java). At the reflective level of
Class.forName() the "proper" name for int is more like "I", but that *also* is
a legal class name. So Class.forName() doesn't have a way it can usefully find
the primitive types.

OTOH, the array prefix '[' makes things unambiguous, so it *is* possible to get
the classes of arrays of primitives via Class.forName() (the type of an array
of a class called "I" would be named "[LI;" at this level -- though I haven't
tested whether class.forName() works with that). So you can get the class
objects for the primitive types by that route. Except void, but that's because
there is no such type as void[].

-- chris
 
T

Timo Kinnunen

"void" isn't a datatype in Java, it's a keyword used to indicate the
absence of a return value.

Sorry for being unclear. I didn't mean the irregularity between primitive
types and void. I meant the irregularity with

boolean test(Class c) { return Class.forName(c.getName()) == c; }

returning true when c represents a reference type, array of references or
array of primitives, but failing when c represents a primitive type.
Off hand I can't think of any other situations where void can be used.
For example, you can't use void to declare variables or method
arguments.

void.class is valid Java, but I'll be darned if I can think of anything to
do with the returned Class.
 
M

Michael Borgwardt

Timo said:
void.class is valid Java, but I'll be darned if I can think of anything to
do with the returned Class.

Compare it with the result of java.lang.reflect.Method.getReturnType()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top