How to get the class of a casted null reference.

C

Cram TeXeD

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
 
C

Chris Smith

Cram TeXeD said:
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
 
X

xarax

Cram TeXeD said:
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.
 
C

Cram TeXeD

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

xarax

Cram TeXeD said:
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...;)
 

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
473,774
Messages
2,569,598
Members
45,160
Latest member
CollinStri
Top