How to get the class of a casted null reference.

Discussion in 'Java' started by Cram TeXeD, Feb 6, 2005.

  1. Cram TeXeD

    Cram TeXeD Guest

    Hi guys.
    My question is for java killers and is not so dumb as she looks at
    first glance.

    Just a recall :
    For two methods like :
    public class Foo {
    public void method(String str){
    System.out.println("method with string !");
    }

    public void method(List list){
    System.out.println("method with list !");
    }
    }

    if you do
    Object arg = (List) null;
    foo.method(arg);

    you will have public void method(List list) called.
    Try if you dont't buy it !

    Ok.
    My purpose is to get the class of the null reference, like runtime
    does !
    I have try all things, in java.lang.Class, in java.lang.reflect, in
    jakarta Lang : nothing to do !!

    Has a java guru here (there' some no ?) a tips to get my null
    reference class casting ?

    Why to do all this stuff ?
    Beacause of I have to select method according to somes args.
    I get setProperty(String str) and setProperty(List list) methods in a
    list and to do a "unset" operation with a null value I have to select
    the right one !

    So if I get :
    // unset
    Object nullValue = (String) null;
    Method m = selectMethodByArg(new Object[] {nullValue));

    How Can I write my selectMethodByArg without knowing the right
    nullValue type ?

    Cram TeXeD
    --
    "On ne peut pas ne pas communiquer"
    axiome n°1 de Paul Watzlawick
    <http://texed75.free.fr>
     
    Cram TeXeD, Feb 6, 2005
    #1
    1. Advertising

  2. Cram TeXeD

    Chris Smith Guest

    Cram TeXeD <> wrote:
    > Just a recall :
    > For two methods like :


    > public void method(String str){
    > public void method(List list){


    > if you do
    > Object arg = (List) null;
    > foo.method(arg);
    >
    > you will have public void method(List list) called.


    No, that is not true. You will, instead, receive a compiler error that
    the type is ambiguous. You probably are referring to the following,
    which both DO work:

    List arg = null; // cast is unnecessary
    foo.method(arg);

    or:

    foo.method((List) null);

    The difference is that in your code, after you cast the null reference
    to List, you then assign it to a variable of type Object, which discards
    the type information.

    > My purpose is to get the class of the null reference, like runtime
    > does !


    The null reference is a reference, and references don't have classes.
    References DO have types, and the type of any reference is available at
    compile-time, so you don't need a special way to "get" it. You can
    already just look at the code and find out what it is.

    The 'null' literal expression has a special type called the null type.
    It is assignable to ANY reference type. All other references have a
    declared type of some kind.

    > Why to do all this stuff ?
    > Beacause of I have to select method according to somes args.
    > I get setProperty(String str) and setProperty(List list) methods in a
    > list and to do a "unset" operation with a null value I have to select
    > the right one !


    Your choices are to: (a) provide typed values to overloaded methods,
    which will them resolve themselves at compile-time, or (b) require a
    second parameter in the interface that contains the type, as a
    java.lang.Class object.

    The information you're looking for, though, simply does not exist.

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

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Feb 6, 2005
    #2
    1. Advertising

  3. Cram TeXeD

    xarax Guest

    "Cram TeXeD" <> wrote in message
    news:...
    > Hi guys.
    > My question is for java killers and is not so dumb as she looks at
    > first glance.
    >
    > Just a recall :
    > For two methods like :
    > public class Foo {
    > public void method(String str){
    > System.out.println("method with string !");
    > }
    >
    > public void method(List list){
    > System.out.println("method with list !");
    > }
    > }
    >
    > if you do
    > Object arg = (List) null;
    > foo.method(arg);
    >
    > you will have public void method(List list) called.
    > Try if you dont't buy it !


    You example will not compile. The compiler cannot
    distinguish with method(arg) to call, because the parameter
    'arg' is of type Object.

    ==================================================
    package fubar;

    import java.util.List;

    public class Foo
    {
    public void method(String str)
    {
    System.out.println("method with string !");
    }

    public void method(List list)
    {
    System.out.println("method with list !");
    }

    public static void main(final String argv[])
    {
    Object arg = (List) null;
    Foo foo = new Foo();

    foo.method(arg); /* compile-time error */
    foo.method((List) arg); /* this will compile */
    }
    }
    ==================================================

    > Ok.
    > My purpose is to get the class of the null reference, like runtime
    > does !


    Impossible. You premise is flawed. 'null' is an expression
    that never has a type. It can be thought of as having the
    subtype of all possible types in the universe of types. (That's
    why it's assignment-compatible with any type; it's a subtype
    of all types.)

    Parameter overloading is distinguished at compile-time,
    not run-time. The apparent type of the parameter at
    compile-time determines which method will be called.

    Your example tries to call method(Object), which does not
    exist in the class Foo. However, if you explicitly downcast
    the parameter to either (String) or (List), then the
    appropriate method will be called.

    > I have try all things, in java.lang.Class, in java.lang.reflect, in
    > jakarta Lang : nothing to do !!
    >
    > Has a java guru here (there' some no ?) a tips to get my null
    > reference class casting ?


    Impossible, because there's no such thing.

    > Why to do all this stuff ?
    > Beacause of I have to select method according to somes args.
    > I get setProperty(String str) and setProperty(List list) methods in a
    > list and to do a "unset" operation with a null value I have to select
    > the right one !
    >
    > So if I get :
    > // unset
    > Object nullValue = (String) null;
    > Method m = selectMethodByArg(new Object[] {nullValue));
    >
    > How Can I write my selectMethodByArg without knowing the right
    > nullValue type ?


    Your request makes no sense. You don't understand
    the Java language. Don't go anywhere near reflection
    until you have a firm grasp of Java. In most situations,
    you don't need reflection, and many programmers use
    reflection as a crutch.
     
    xarax, Feb 6, 2005
    #3
  4. Cram TeXeD

    Cram TeXeD Guest

    On Sun, 06 Feb 2005 17:23:05 GMT, < xarax > dit :


    >You example will not compile. The compiler cannot
    >distinguish with method(arg) to call, because the parameter
    >'arg' is of type Object.


    You're right.


    >Your request makes no sense. You don't understand
    >the Java language. Don't go anywhere near reflection
    >until you have a firm grasp of Java. In most situations,
    >you don't need reflection, and many programmers use
    >reflection as a crutch.


    Thanks for your comment.
    You're nice.

    You confirm a point I found in my books after I have posted this.
    I always forget compilation step so I'm using reflection.

    For my request, she has (nevertheless) interest for object properties
    and methods discovery and using, sort of "formalisation" java binding.

    You're interesting, so bad you're so scornful.

    Cram TeXeD
    - bref tu te la joues -
    --
    "On ne peut pas ne pas communiquer"
    axiome n°1 de Paul Watzlawick
    <http://texed75.free.fr>
     
    Cram TeXeD, Feb 6, 2005
    #4
  5. Cram TeXeD

    xarax Guest

    "Cram TeXeD" <> wrote in message
    news:eek:...
    > On Sun, 06 Feb 2005 17:23:05 GMT, < xarax > dit :
    >

    /snip/
    > For my request, she has (nevertheless) interest for object properties
    > and methods discovery and using, sort of "formalisation" java binding.


    null is not an object, and thus has no properties.

    > You're interesting, so bad you're so scornful.


    Then my work here is done...;)
     
    xarax, Feb 7, 2005
    #5
    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. Timo Qvist
    Replies:
    2
    Views:
    2,948
    Jonathan Mcdougall
    Dec 1, 2004
  2. Pat
    Replies:
    1
    Views:
    277
    Victor Bazarov
    Feb 22, 2007
  3. praveen

    problem with a casted pointer

    praveen, Jun 29, 2007, in forum: C Programming
    Replies:
    15
    Views:
    490
    David Thompson
    Jul 22, 2007
  4. kkirtac

    Problem on using a casted void pointer

    kkirtac, Sep 23, 2007, in forum: C Programming
    Replies:
    10
    Views:
    658
    kkirtac
    Sep 24, 2007
  5. Ravi
    Replies:
    11
    Views:
    1,437
Loading...

Share This Page