How do I finding the current static type?

From: Benno (benno.baumgartner_at_gmail.com)
Date: 03/30/05


Date: 30 Mar 2005 04:26:47 -0800

Dear Java Gurus,

I need to find out what the static type (a.k.a. compile time type) of
a given object is at runtime[1]. What I want to achieve is this:

public interface ClassA { public void f(); }
public interface ClassB extends ClassA {}

        ClassA classA;
        ClassB classB = new ClassBImpl();
        classA = classB;
        classB.f(); // does something
        classA.f(); // does something else

classB and classA are referencing the exact same object and on this
object f() is called but they should not do the same since the static
types are different (ClassA for classA and ClassB for classB).
So, ClassBImpl should look something like:

public class ClassBImpl implements ClassB {

        public void f() {
                if (
                        static type in caller of this
                        is proper subtype of ClassA
                ) {
                        //do something
                } else {
                        //do something else
                }
        }
}

I'm sure you're wondering now why the heck he wants to do something
stupid like that. The reason is that I'm working on a Eiffel to Java
compiler at the moment and there are situations where the f() from
ClassA denotes a completely different method as the f() in ClassB,
they just have the same signature. I can't convince Java that they are
not the same and Java does therefore always bind dynamically to the
most recent implementation which may be the wrong one in some cases.
I'm thinking about this problem like for two weeks now and I'm
completely stuck. You'll make my month if you have any idea how to
solve it no matter how far fetched the idea may be.

Thanks for your time,
Benno

[1] I know, objects do not have a static type, what I mean is the type
of the last variable that was referencing that object.



Relevant Pages

  • Re: How do I finding the current static type?
    ... > public interface ClassB extends ClassA ... > classB and classA are referencing the exact same object and on this ... > static type in caller of this ... The reason is that I'm working on a Eiffel to Java ...
    (comp.lang.java.programmer)
  • Re: Some thoughts about OO programming
    ... compilers, that try to make code as machine friendly as possible (with ... Alpha designs a class named ClassA. ... Company Beta wants to write a class ClassB. ... libraries are loaded by the dynamic linker of the OS and it ...
    (comp.programming)
  • Re: Can anyone help with proper use of operator CType in VB.net?
    ... public ClassA: inherits Base ... I have an instance of ClassA and ClassB and I'm trying to assign the ... Public Overrides Sub Assign ...
    (microsoft.public.dotnet.languages.vb)
  • Problems loading assembly using System.Reflection.assembly
    ... ClassA is going to be used for multable apps so I ... ClassB inherits ClassA. ... Dim returnObject As Object ... Dim asm As System.Reflection.Assembly ...
    (microsoft.public.dotnet.framework)
  • Problems loading assembly using System.Reflection.assembly
    ... ClassA is going to be used for multable apps so I ... ClassB inherits ClassA. ... Dim returnObject As Object ... Dim asm As System.Reflection.Assembly ...
    (microsoft.public.dotnet.languages.vb)