polymorphic voodoo?

  • Thread starter christian.bongiorno
  • Start date
C

christian.bongiorno

Ok, here is what I am trying to decipher. I have the following code

private void someMethod() {
ArrayList list1 = null;
LinkedList list2 = null;

cleverFunction(list1,list2);

}

private void cleverFunction(List first, List second) {
System.out.println("Static type for first is " + .....);
System.out.println("Static type for second is " + .....);
}

Ok, so, given a polymorphic call like this, is it possible, through
whatever witchcraft necessary, to determine what the static types of
the type polymorphic parameters are?

Maybe some sort of interrogation of the stack? Maybe some sorta
reflection?

Ideas?
christian
http://christian.bongiorno.org/resume.PDF
 
C

Chris Smith

Ok, here is what I am trying to decipher. I have the following code

private void someMethod() {
ArrayList list1 = null;
LinkedList list2 = null;

cleverFunction(list1,list2);

}

private void cleverFunction(List first, List second) {
System.out.println("Static type for first is " + .....);
System.out.println("Static type for second is " + .....);
}

Ok, so, given a polymorphic call like this, is it possible, through
whatever witchcraft necessary, to determine what the static types of
the type polymorphic parameters are?

You always know the static types of variables. In this case, the static
type of both 'first' and 'second' is java.util.List.

Did you want to know the static types of the actual parameters in the
calling stack frame? In that case, things get harder. You'd need to
have access to either debugging information or the source code for the
calling function, since the information you're looking for is partially
lost during the translation to bytecode.

Let me make the obligatory statement that if you care, your design has
probably gone horribly wrong.
 
E

EJP

Chris said:
Did you want to know the static types of the actual parameters in the
calling stack frame? In that case, things get harder.

Err, first.getClass()? second.getClass()?
 
S

Stefan Ram

System.out.println("Static type for first is " + .....);
System.out.println("Static type for second is " + .....);
[...] is it possible [...] to determine [...] the static types
of the [...] [arguments]?

Not really, but here is an approximation that might work
sometimes:

public class Main
{
static void printStaticTypesOfExpressions
( final java.util.ArrayList first,
final java.util.ArrayList second )
{ System.out.println( "java.util.ArrayList" );
System.out.println( "java.util.ArrayList" ); }

static void printStaticTypesOfExpressions
( final java.util.LinkedList first,
final java.util.ArrayList second )
{ System.out.println( "java.util.LinkedList" );
System.out.println( "java.util.ArrayList" ); }

static void printStaticTypesOfExpressions
( final java.util.ArrayList first,
final java.util.LinkedList second )
{ System.out.println( "java.util.ArrayList" );
System.out.println( "java.util.LinkedList" ); }

static void printStaticTypesOfExpressions
( final java.util.LinkedList first,
final java.util.LinkedList second )
{ System.out.println( "java.util.LinkedList" );
System.out.println( "java.util.LinkedList" ); }

public static void main( final java.lang.String[] args )
{ final java.util.ArrayList list1 = null;
final java.util.LinkedList list2 = null;
printStaticTypesOfExpressions( list1, list2 ); }}

java.util.ArrayList
java.util.LinkedList
 
P

Paul Davis

Stefan said:
System.out.println("Static type for first is " + .....);
System.out.println("Static type for second is " + .....);
[...] is it possible [...] to determine [...] the static types
of the [...] [arguments]?

Not really, but here is an approximation that might work
sometimes:

public class Main
{
static void printStaticTypesOfExpressions
( final java.util.ArrayList first,
final java.util.ArrayList second )
{ System.out.println( "java.util.ArrayList" );
System.out.println( "java.util.ArrayList" ); }

static void printStaticTypesOfExpressions
( final java.util.LinkedList first,
final java.util.ArrayList second )
{ System.out.println( "java.util.LinkedList" );
System.out.println( "java.util.ArrayList" ); }

static void printStaticTypesOfExpressions
( final java.util.ArrayList first,
final java.util.LinkedList second )
{ System.out.println( "java.util.ArrayList" );
System.out.println( "java.util.LinkedList" ); }

static void printStaticTypesOfExpressions
( final java.util.LinkedList first,
final java.util.LinkedList second )
{ System.out.println( "java.util.LinkedList" );
System.out.println( "java.util.LinkedList" ); }

public static void main( final java.lang.String[] args )
{ final java.util.ArrayList list1 = null;
final java.util.LinkedList list2 = null;
printStaticTypesOfExpressions( list1, list2 ); }}

java.util.ArrayList
java.util.LinkedList

Issue being, the method called from main is determined at compile time
as revealed by javap:

public static void main(java.lang.String[]);
Code:
0: aconst_null
1: astore_1
2: aconst_null
3: astore_2
4: aload_1
5: aload_2
6: invokestatic #42; //Method
printStaticTypesOfExpressions:(Ljava/util/ArrayList;Ljava/util/LinkedList;)V
9: return
 
C

Chris Smith

EJP said:
Err, first.getClass()? second.getClass()?

You're confusing type with class. As Michael pointed out, the values of
these variables in the example are null, so they don't point to any
objects and it won't work to ask for the object's class. Even if they
weren't null, though, getClass() would be the wrong answer. That would
tell you the class of the object, and not the type of the variable that
points to it.

As I said earlier, the types of the formal parameters are both
java.util.List. Any other answer is wrong. The types of the actual
parameters in the example given here are java.util.ArrayList and
java.util.LinkedList. This would remain true even if the object pointed
to were of class javax.management.AttributeList (a class that extends
ArrayList). That still wouldn't change the type of the variable.

It's worth mentioning that if one wants to know static types of the
actual parameters out of a small set of options, then method overloading
can do it. Since static types are, by definition, known at compile-
time, this should be sufficient... but only if there's only a small set
of options; otherwise, it becomes impractical or impossible.
 
O

Oliver Wong

Paul Davis said:
Stefan said:
System.out.println("Static type for first is " + .....);
System.out.println("Static type for second is " + .....);
[...] is it possible [...] to determine [...] the static types
of the [...] [arguments]?
[code snipped]

Issue being, the method called from main is determined at compile time

Which is fine, because the static types are also known at compile time.

- Oliver
 
P

Paul Davis

Oliver said:
Paul Davis said:
Stefan said:
(e-mail address removed) writes:
System.out.println("Static type for first is " + .....);
System.out.println("Static type for second is " + .....);
[...] is it possible [...] to determine [...] the static types
of the [...] [arguments]?
[code snipped]

Issue being, the method called from main is determined at compile time

Which is fine, because the static types are also known at compile time.

- Oliver
True but, I've (unfortuneately) seen more than a few cases where
someone thinks it will happen at runtime
 
C

christian.bongiorno

It's mostly academic. I have even thought about going so far as to load
the actual class bytes and interrogate them. Later posts have suggested
what I think is most likely: A Debugger.

I am off the project now where I might have used something like this,
but still think about what I might have 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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top