Magically call methods in Java
Asked Answered
F

2

6

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.

Farinose answered 19/2, 2012 at 14:45 Comment(3)
Upvoting as an interesting question, but this should not be tagged OOP. The feature you are interested is not object oriented and in fact isnt even necessarily dynamic.Cytogenetics
Generally you want your own getter and setters which you can generate with your IDE. For example, its not a good idea to have a getter return a mutable collection. instead you can have ClassInFoo getMyMap(String name)Lyautey
Yeah well, I can write all the getters, but I found it duplicate and ugly. I would just like this so that my HTML coding guys can request these methods simply.Christan
E
8

You absolutely can through reflection by using its features like

public Method getMethod(String name, Class<?>... parameterTypes)

that can be used to see if a class has some methods defined but I don't see how your problem couldn't be solved with a proper use of interfaces, inheritance and overriding of methods

Features like reflection are provided to manage certain, otherwise unsolvable, issues but Java is not PHP so you should try to avoid using it when possible, since it's not in the philosophy of the language.

Erk answered 19/2, 2012 at 14:55 Comment(0)
M
3

Isn't it the whole point of inheritance and overriding?

Base class:

public Object foo() {
    return this.myMap.get(defaultKey).method();
}

Subclass:

@Overrides
public Object foo() {
    return whateverIWant;
}
Morbidity answered 19/2, 2012 at 14:51 Comment(2)
I made a more precise example here: gist.github.com/1864457 - can you please explain your proposal more in detail? Thanks.Christan
If the getName() method in the Fruit class is implemented like this, it will work as expected: return getContents().get(getDefaultKey()).getName(). Are you understanding how inheritance and overriding work? Read docs.oracle.com/javase/tutorial/java/IandI/subclasses.htmlMorbidity

© 2022 - 2024 — McMap. All rights reserved.