how do I make Class.forName("Integer") returning java.lang.Integer?

Discussion in 'Java' started by Johannes Zellner, Dec 18, 2005.

  1. 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!

    --
    Johannes
     
    Johannes Zellner, Dec 18, 2005
    #1
    1. Advertising

  2. Johannes Zellner

    Roedy Green Guest

    Re: how do I make Class.forName("Integer") returning java.lang.Integer ?

    On Sun, 18 Dec 2005 16:59:19 +0100, Johannes Zellner
    <> 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
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Dec 18, 2005
    #2
    1. Advertising

  3. Johannes Zellner

    Roedy Green Guest

    Re: how do I make Class.forName("Integer") returning java.lang.Integer ?

    On Sun, 18 Dec 2005 16:59:19 +0100, Johannes Zellner
    <> 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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Dec 18, 2005
    #3
  4. Johannes Zellner

    Stefan Ram Guest

    Re: how do I make Class.forName("Integer") returning java.lang.Integer ?

    Johannes Zellner <> writes:
    >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!)
     
    Stefan Ram, Dec 18, 2005
    #4
  5. Johannes Zellner

    J. Verdrengh Guest

    Re: 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.


    Afaik each class in Java is represented by an instance of type Class, so a
    class (==instance of Class) can be returned..
     
    J. Verdrengh, Dec 18, 2005
    #5
  6. Johannes Zellner

    J. Verdrengh Guest

    Re: 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.


    Afaik each class in Java is represented by an instance of type Class, so a
    class (==instance of Class) can be returned..
     
    J. Verdrengh, Dec 18, 2005
    #6
  7. Johannes Zellner

    Stefan Ram Guest

    Re: how do I make Class.forName("Integer") returning java.lang.Integer ?

    "J. Verdrengh" <> writes:
    >>"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«.
     
    Stefan Ram, Dec 18, 2005
    #7
  8. Johannes Zellner

    Alan Krueger Guest

    Stefan Ram wrote:
    > "J. Verdrengh" <> writes:
    >
    >>>"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«.


    Not if you were calling Class.forName.
     
    Alan Krueger, Dec 18, 2005
    #8
  9. Johannes Zellner

    Stefan Ram Guest

    Re: how do I make Class.forName("Integer") returning java.lang.Integer ?

    Alan Krueger <> writes:
    >>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.
     
    Stefan Ram, Dec 18, 2005
    #9
  10. Johannes Zellner

    Roedy Green Guest

    Re: how do I make Class.forName("Integer") returning java.lang.Integer ?

    On 18 Dec 2005 16:35:28 GMT, -berlin.de (Stefan Ram)
    wrote, quoted or indirectly quoted someone who said :

    > "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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Dec 19, 2005
    #10
  11. Johannes Zellner

    Roedy Green Guest

    Re: how do I make Class.forName("Integer") returning java.lang.Integer ?

    On 18 Dec 2005 20:36:37 GMT, -berlin.de (Stefan Ram)
    wrote, quoted or indirectly quoted someone who said :

    > 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.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Dec 19, 2005
    #11
  12. Johannes Zellner

    Guest

    Re: how do I make Class.forName("Integer") returning java.lang.Integer ?

    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.
     
    , Dec 19, 2005
    #12
  13. Johannes Zellner

    Stefan Ram Guest

    Re: how do I make Class.forName("Integer") returning java.lang.Integer ?

    Roedy Green <>
    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.
     
    Stefan Ram, Dec 19, 2005
    #13
  14. Johannes Zellner

    Roedy Green Guest

    Re: how do I make Class.forName("Integer") returning java.lang.Integer ?

    On 19 Dec 2005 01:48:56 GMT, -berlin.de (Stefan Ram)
    wrote, quoted or indirectly quoted someone who said :

    > However, a class "Class" could be written to implement
    > the behavior required.


    There already is a Class class.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Dec 19, 2005
    #14
  15. Johannes Zellner

    Stefan Ram Guest

    Re: how do I make Class.forName("Integer") returning java.lang.Integer ?

    Roedy Green <>
    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.
     
    Stefan Ram, Dec 19, 2005
    #15
  16. Johannes Zellner

    Chris Smith Guest

    Re: how do I make Class.forName("Integer") returning java.lang.Integer ?

    Stefan Ram <-berlin.de> wrote:
    > 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?

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

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Dec 19, 2005
    #16
  17. Johannes Zellner

    Stefan Ram Guest

    Re: how do I make Class.forName("Integer") returning java.lang.Integer ?

    Chris Smith <> writes:
    >Stefan Ram <-berlin.de> wrote:
    >>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
     
    Stefan Ram, Dec 19, 2005
    #17
  18. Johannes Zellner

    Chris Smith Guest

    Re: how do I make Class.forName("Integer") returning java.lang.Integer ?

    Stefan Ram <-berlin.de> wrote:
    > >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.

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

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Dec 19, 2005
    #18
  19. Re: how do I make Class.forName("Integer") returning java.lang.Integer ?

    <> schrieb:
    > 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.


    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?


    --
    Johannes
     
    Johannes Zellner, Dec 19, 2005
    #19
  20. Johannes Zellner

    Roedy Green Guest

    Re: how do I make Class.forName("Integer") returning java.lang.Integer ?

    On 19 Dec 2005 02:33:13 GMT, -berlin.de (Stefan Ram)
    wrote, quoted or indirectly quoted someone who said :

    > 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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Dec 19, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. H.MuthuKumaraRajan

    .class and Class.forName

    H.MuthuKumaraRajan, Nov 6, 2003, in forum: Java
    Replies:
    11
    Views:
    1,751
    H.MuthuKumaraRajan
    Nov 7, 2003
  2. cyril
    Replies:
    2
    Views:
    3,894
    cyril
    Aug 25, 2004
  3. Madni
    Replies:
    1
    Views:
    5,909
  4. Alex Hunsley
    Replies:
    14
    Views:
    593
    Heiko Wundram
    Jul 19, 2004
  5. jean-francois
    Replies:
    2
    Views:
    613
Loading...

Share This Page