Java Forum / General / December 2005
how do I make Class.forName("Integer") returning java.lang.Integer ?
Johannes Zellner - 18 Dec 2005 15:59 GMT Hello,
How do I make Class.forName("Integer") returning java.lang.Integer? To be more general:
1. How do I make Class.forName searching in some packages?
2. Can I make Class.forName respecting the import statements of the calling class? -- e.g. in
import java.lang.Integer; class Fred() { Fred() { Class.forName("Integer"); } }
I'd like Class.forName() returning java.lang.Integer, because it was imported earlier.
Any help much appreciated!
 Signature Johannes
Roedy Green - 18 Dec 2005 16:23 GMT On Sun, 18 Dec 2005 16:59:19 +0100, Johannes Zellner <johannes@zellner.org> wrote, quoted or indirectly quoted someone who said :
>How do I make Class.forName("Integer") returning java.lang.Integer? >To be more general: see http://mindprod.com/jgloss/classforname.html
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 18 Dec 2005 16:24 GMT On Sun, 18 Dec 2005 16:59:19 +0100, Johannes Zellner <johannes@zellner.org> wrote, quoted or indirectly quoted someone who said :
>2. Can I make Class.forName respecting the import statements of the > calling class? -- e.g. in classForName can't see the imports.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Stefan Ram - 18 Dec 2005 16:35 GMT >How do I make Class.forName("Integer") returning java.lang.Integer? "java.lang.Integer" is a Class. A class is not a value, so it can not be returned.
To make "Class.forName" return the string »"java.lang.Integer"«:
public class Class { public static java.lang.String forName ( final java.lang.Object dummy ) { return "java.lang.Integer"; }}
(Sorry!)
J. Verdrengh - 18 Dec 2005 16:42 GMT > "java.lang.Integer" is a Class. > A class is not a value, so it can not be returned. Afaik each class in Java is represented by an instance of type Class, so a class (==instance of Class) can be returned..
J. Verdrengh - 18 Dec 2005 16:42 GMT > "java.lang.Integer" is a Class. > A class is not a value, so it can not be returned. Afaik each class in Java is represented by an instance of type Class, so a class (==instance of Class) can be returned..
Stefan Ram - 18 Dec 2005 16:53 GMT >>"java.lang.Integer" is a Class. >>A class is not a value, so it can not be returned. >Afaik each class in Java is represented by an instance of type Class, >so a class (==instance of Class) can be returned.. This, I would have written as »java.lang.Integer.class«.
Alan Krueger - 18 Dec 2005 20:27 GMT >>>"java.lang.Integer" is a Class. >>>A class is not a value, so it can not be returned. [quoted text clipped - 3 lines] > > This, I would have written as »java.lang.Integer.class«. Not if you were calling Class.forName.
Stefan Ram - 18 Dec 2005 20:36 GMT >>This, I would have written as »java.lang.Integer.class«. >Not if you were calling Class.forName. The OP wrote:
How do I make Class.forName("Integer") returning java.lang.Integer? ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ So "java.lang.Integer" was intended to be the value of this expression, not the argument.
Roedy Green - 19 Dec 2005 01:18 GMT > How do I make Class.forName("Integer") returning > java.lang.Integer? By writing your own. Class.forName wants fully qualified names.
You can use Class c = Integer.class; by itself which will pay attention to imports.
See http://mindprod.com/jgloss/classforname.html
the imports are erased in the class file. Class.forName can't thus use them.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Stefan Ram - 19 Dec 2005 01:48 GMT Roedy Green <my_email_is_posted_on_my_website@munged.invalid> wrote, quoted or indirectly quoted someone who said :
>How do I make Class.forName("Integer") returning >java.lang.Integer? The method "forName" of the class "java.lang.Class" expects fully qualified class names, i.e., names preceded by a package.
However, a class "Class" could be written to implement the behavior required.
Roedy Green - 19 Dec 2005 02:20 GMT > However, a class "Class" could be written to implement > the behavior required. There already is a Class class.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Stefan Ram - 19 Dec 2005 02:33 GMT Roedy Green <my_email_is_posted_on_my_website@munged.invalid> wrote, quoted or indirectly quoted someone who said :
>There already is a Class class. Actually a class "java.lang.Class" - but not a class "Class" in an unnamed package.
Without certain import declarations, the identifier "Class" refers to the class "Class" of an unnamed package - should such a class declaration exist.
Chris Smith - 19 Dec 2005 04:44 GMT > Without certain import declarations, the identifier "Class" > refers to the class "Class" of an unnamed package - should > such a class declaration exist. Are you sure of that last bit?
 Signature www.designacourse.com The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation
Stefan Ram - 19 Dec 2005 04:56 GMT >>Without certain import declarations, the identifier "Class" >>refers to the class "Class" of an unnamed package - should >>such a class declaration exist. >Are you sure of that last bit? I am never sure about anything.
What I had I mind was:
class Class { final static java.lang.Class forName ( final java.lang.String name ) { return java.lang.Double.class; }} public class Main { public static void main( final java.lang.String[] args ) { java.lang.System.out.println ( Class.forName( "java.lang.Integer" ).getName() ); }}
This compiles and prints:
java.lang.Double
Chris Smith - 19 Dec 2005 05:14 GMT > >Are you sure of that last bit? > > I am never sure about anything. > > What I had I mind was: Yes. What doesn't work is using an import to get the default-package Class to be used in preference to the java.lang.Class, from code that resides outside the default package. If you are in the default package, then this works and doesn't require an import at all. In other named packages, you could make it work by placing Class in the same package as the code. But you cannot import something from the default package.
That's all I meant.
 Signature www.designacourse.com The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation
Roedy Green - 19 Dec 2005 08:48 GMT > Without certain import declarations, the identifier "Class" > refers to the class "Class" of an unnamed package - should > such a class declaration exist. I would not try naming classes the same as those in java.lang. Even if you don't confuse the compiler, you will confuse anyone reading the code.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
ricky.clarkson@gmail.com - 19 Dec 2005 01:20 GMT If you want to find the class that represents java.lang.Integer, you can do this with:
Class<Integer> integerClass=Integer.class; (I might have got the thing between <> wrong).
If you don't mind fully qualifying, you can do Class.forName("java.lang.Integer");.
If you want to find all classes on the classpath called Integer, you can iterate through the entries in the classpath via the system classloader, which happens to be a URLClassLoader, normally.
Otherwise, state your requirements.
Johannes Zellner - 19 Dec 2005 08:16 GMT ricky.clarkson@gmail.com <ricky.clarkson@gmail.com> schrieb:
> If you want to find the class that represents java.lang.Integer, you > can do this with: [quoted text clipped - 8 lines] > can iterate through the entries in the classpath via the system > classloader, which happens to be a URLClassLoader, normally. the last on is excactly what I'd like to do. But HOW do I do this? I can easily get all the packages from the system class loader by
ClassLoader.getSystemClassLoader().getPackages()
but how can I get the class names each package provides?
 Signature Johannes
ricky.clarkson@gmail.com - 19 Dec 2005 11:18 GMT This forum post [1] describes how to *change* the classpath at runtime. However, URLClassLoader also has methods [2] to get the URLs of the classpath entries, which you can then look at using filesystem and jar extraction methods. It isn't pretty.
I don't know of any uses for reflection that are appropriate outside dynamically loading plugins, and even then a fully-qualified class name is a good idea. Please explain WHY you want this, i.e., the real-world problem you are trying to solve. There is a better way.
[1] http://forum.java.sun.com/thread.jspa?forumID=32&messageID=1961099&threadID=300557 [2] http://forum.java.sun.com/thread.jspa?forumID=32&messageID=1961099&threadID=300557
Stefan Ram - 19 Dec 2005 11:12 GMT >If you want to find the class that represents >java.lang.Integer, you can do this with: >Class<Integer> integerClass=Integer.class; The class that represents »java.lang.Integer« is »java.lang.Integer.class«.
The class that represents »Integer« is »Integer.class«.
If both class designations refer to a class, in a specific expression, they do not have to refer to the same class.
However, a specific import declaration, such as
import java.lang.Integer;
would make the two class designations refer to the same class.
ricky.clarkson@gmail.com - 19 Dec 2005 11:22 GMT Stefan,
> If both class designations refer to a class, in a specific > expression, they do not have to refer to the same class. What do you mean by class designations, and 'both' here?
I think there's some context you're keeping to yourself!
Ricky.
Roedy Green - 19 Dec 2005 01:16 GMT > "java.lang.Integer" is a Class. > A class is not a value, so it can not be returned. java.lang.Integer is the name of a class
"java.lang.Integer" is a String, and hence an object.
java.lang.Integer.class is the Class object for Integer.
Strings and Class objects can be returned from methods.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Free MagazinesGet 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 ...
|
|
|