Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

getting a class if i know the class name

Thread view: 
Marc E - 26 Nov 2005 21:31 GMT
I have a String whose value is "float". I need to turn that into float.class
for passing into a method (I need to do this for a number of
primitives....int, boolean, float, double, etc).  Is there a built in way to
do this? Nothing jumped out at me in the Class class

thanks.
Chris - 26 Nov 2005 22:01 GMT
>I have a String whose value is "float". I need to turn that into
>float.class for passing into a method (I need to do this for a number of
>primitives....int, boolean, float, double, etc).  Is there a built in way
>to do this? Nothing jumped out at me in the Class class

First, think hard about why you're doing this. Manipulation of Class objects
is usually a bad idea if there is some other way to handle the problem.

That having been said, this may do the trick:

Class floatClass = Class.forName("java.lang.Float");
Marc E - 26 Nov 2005 22:12 GMT
I was doing this as a small experiment with reflection and calling
method.invoke. the arguments to the methods are primitive floats, ints, and
booleans, and so I had to pass float.class as the Class [] params to find
the method thorugh reflection.  I tried forName("java.lang.float") but of
course got an error. I'll give yours a shot.

thanks.

>>I have a String whose value is "float". I need to turn that into
>>float.class for passing into a method (I need to do this for a number of
[quoted text clipped - 8 lines]
>
> Class floatClass = Class.forName("java.lang.Float");
Chris Smith - 26 Nov 2005 22:32 GMT
> I was doing this as a small experiment with reflection and calling
> method.invoke. the arguments to the methods are primitive floats, ints, and
> booleans, and so I had to pass float.class as the Class [] params to find
> the method thorugh reflection.  I tried forName("java.lang.float") but of
> course got an error. I'll give yours a shot.

There's a difference between the Class object for Float and the Class
object for the primitive float.  The code that Chris posted won't do the
same thing as what you've written.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Roedy Green - 26 Nov 2005 22:45 GMT
>Class floatClass = Class.forName("java.lang.Float");

that is the Float class.  Float.class is not the same as float.class.
float.class represents the primitive in a kludgy sort of way.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

NullBock - 26 Nov 2005 22:34 GMT
So am I understanding you correctly?  You want to use reflection for
calling a method with a primitive argument?  This isn't a problem.  Use
the TYPE field in the wrapper class associated with the primitive:

System.out.println(Float.TYPE);

output: float

Thus, if you wanted to call the Date(long) ctor, you would do something
like this:

Constructor ctor = Date.class.getConstructor(new Class[] {
   Long.TYPE
});
Object[] args = {
 new Long(0)
};
Date d = (Date) ctor.newInstance(args);

Is this what you want?

Walter Gildersleeve
Freiburg, Germany

___________________________________________
http://linkfrog.net
URL Shortening
Free and easy, small and green
Roedy Green - 26 Nov 2005 22:44 GMT
>I have a String whose value is "float". I need to turn that into float.class
>for passing into a method (I need to do this for a number of
>primitives....int, boolean, float, double, etc).  Is there a built in way to
>do this? Nothing jumped out at me in the Class class

I think you need a little HashMap to convert String to class literal.

You could see what pops out with Class.forName.

See http://mindprod.com/jgloss/classforname.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Oliver Wong - 28 Nov 2005 20:31 GMT
>I have a String whose value is "float". I need to turn that into
>float.class for passing into a method (I need to do this for a number of
>primitives....int, boolean, float, double, etc).  Is there a built in way
>to do this? Nothing jumped out at me in the Class class

Class.forName();

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#forName(java.lang.String)

   Not sure if it works for primitives, but you can probably get
Float.class instead of float.class.

   - Oliver
Thomas Hawtin - 28 Nov 2005 20:55 GMT
> Class.forName();
>
> http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#forName(java.lang.String)
>
>     Not sure if it works for primitives, but you can probably get
> Float.class instead of float.class.

No it doesn't work. Class.forName("float") will attempt to find the
class float, strangely enough. Using a map is probably the best bet.

In any case, it is usually more appropriate to write something along the
lines of:

    Class clazz = Class.forName(
         className, true, Thread.currentThread().getContextClassLoader()
    );

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.