Is there some way of using magic methods in Java like there is in PHP with __call
?
For instance:
class foo {
@Setter @Getter
int id;
@Getter
Map <String, ClassInFoo> myMap;
protected class ClassInFoo {
@Setter @Getter
String name;
}
@Setter
String defaultKey;
}
I'm using Project Lombok annotations for getter and setter methods to simplify the code.
Let's consider that that my map contains several items mapped by String and the defaultKey defines the default one.
What I would like is to be able to call foo.getName()
which would return the default name as foo.myMap.get(defaultKey).getName()
.
The reason I can't just write all the getters manually is that the Foo class is in fact inherited with generics and the the inner class might be different.
I sort of need something like:
function Object __call(method) {
if (exist_method(this.method)
return this.method();
else
return this.myMap.get(defaultKey).method();
}
Is this somehow possible in Java?
EDIT:
I made a more precise example of what I am trying to achieve here: https://gist.github.com/1864457
The only reason of doing this is to "shorthand" the methods in the inner class.
ClassInFoo getMyMap(String name)
– Lyautey