We all know that in Java you can call a static method as an instance method like this:
Foo foo = new Foo();
FooBar fooBar = foo.bar(); // bar is a static method on class Foo
What I want to know is:
- Is there any way to determine inside
bar
whetherbar
was called statically (Foo.bar()
) or called via a class instance as above? - If so, is there any way for
bar
to, via reflection, get a reference to the object which called it (in this casefoo
)?
Reason:
I am developing a kind of semantic syntax. I want my consumers to be able to put things like:
With.attribute("blah").and().attribute("blahblah");
Here you can see that attribute
is being called both as a static and an instance method. However, you can't define a static and an instance method with the same name in Java, for the same reason as above - the static method could be called as an instance method and so to create an instance method with the same name would create ambiguity. Therefore I want to create a single method attribute
which can be called both statically and non-statically, and inside the method body I want to try to determine if it was invoked statically or non-statically. The above questions will help me to assess the feasibility of doing this.
Aucun commentaire:
Enregistrer un commentaire