• Post Reply Bookmark Topic Watch Topic
  • New Topic

Do all methods have to be concrete?  RSS feed

 
Chris Corbyn
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In PHP5 (and I know it's not good practise) you can implement a method in a class named __call(). It takes two arguments: String methodName, Array argumentList. Basically if you have an instance of an object from that class and you try to invoke a method which does not exist, the __call() method is invoked instead, passing the method name and arguments you used. Then you can make a judgement on whether to make it appear that there is really a method and do something, otherwise throw an exception that no such method exists.

It does cause some spaghetti code and can be easily abused, but it does occassionally come in useful. For example, I've used it to write an object relational mapping system before where the method names are determined based upon your schema.

Is it possible to do anything remotely similar in Java? No big deal if not, I'm just curious really.
 
Sen George
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trust you are referring to a database schema. If so, why should the method name change in the first place?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the reflection API to find out at runtime which methods etc. a class has.

By the way, there are ofcourse already very good ORM mapping tools such as Hibernate.
 
Chris Corbyn
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sorry, I think I may have been misunderstood. I'm not looking to discover what methods exist. I know about Reflection for things like that... I'm just curious if there's a way to invoke methods that don't even exist?

Here, watch this PHP code:



I also use Hibernate. That's not what I was asking. I was just giving an example of something I did in PHP.

If you've got a base class (say "ORM") which has a whole load of reflection code in it, then you extend it with classes for each table:



Then you can implement __call() to simply look at your constant fields (aka "public final static String" in java) and create method names at runtime.



I didn't have to implement the setUsername() method because I would have had code inside __call() to see if there was a USERNAME field. If you change the real field name then I only need to change the value and the method name remains the same. If you're still unclear, here's documentation I wrote for my employer: http://code.google.com/p/aplc/wiki/ObjectRelationalMapping

I know PHP is loosely typed enough to be abe to cope with this, but is there a more complex equivalent of this sort of thing in Java?

PS: This topic has nothing to do with ORM, that's not the question I'm asking... I'm just asking about the __call() thing -- or more specifically *any* way to catch calls to undeclared methods and attempt to run code anyway, tranparently.

[ August 21, 2007: Message edited by: Chris Corbyn ]
[ August 21, 2007: Message edited by: Chris Corbyn ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chris Corbyn:
Hi sorry, I think I may have been misunderstood. I'm not looking to discover what methods exist. I know about Reflection for things like that... I'm just curious if there's a way to invoke methods that don't even exist?

No, there is no way to do this in exactly the same way as in PHP and other dynamically typed programming languages. Java is a statically typed language, which means the compiler checks at compile time if all the data types etc. are correct. If you try to call a method that doesn't exist, the compiler will complain about it; there's no way around that.

So you'll have to write your bean classes, including getter and setter methods, yourself. The most popular IDEs such as Eclipse can automatically generate getter and setter methods for you.
[ August 21, 2007: Message edited by: Jesper Young ]
 
Chris Corbyn
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks. To be honest having the ability to implement something like __call() (and __get() and __set()) just encourages laziness and makes it a nightmare to run a documenting tool over your source code and generate an API.

If anyone's interested, this is the manual page for what PHP call "overloading" -- quite different to what a Java developer would call overloading:

http://uk2.php.net/manual/en/language.oop5.overloading.php
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!