java - Is there a way to get the object which called my static method as an instance method? -
this question has answer here:
we know in java can call static method instance method this:
foo foo = new foo(); foobar foobar = foo.bar(); // bar static method on class foo what want know is:
- is there way determine inside
barwhetherbarcalled statically (foo.bar()) or called via class instance above? - if so, there way
barreference object called (in casefoo)?
reason:
i developing kind of semantic syntax. want consumers able put things like:
with.attribute("blah").and().attribute("blahblah"); // both "attribute" , "and" methods return object of type "with" here can see attribute being called both static , instance method. however, can't define static , instance method same name in java, same reason above - static method called instance method , create instance method same name create ambiguity. therefore want create single method attribute can called both statically , non-statically, , inside method body want try determine if invoked statically or non-statically. above questions me assess feasibility of doing this.
no, there no way method know whether called on class or on instance (at jvm level there no difference), , there no way instance method called on.
the term kind of "semantic syntax" domain-specific language (dsl).
possible solution: name static method withattribute, make this:
withattribute("blah").and().attribute("blahblah");
Comments
Post a Comment