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