English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I need to treat objects differently based on whether or not they have a certain method. Is there a Java version of has_method? If not, is there a straightforward way of doing duck typing (a la Ruby)?

2007-08-09 09:29:44 · 2 answers · asked by Tres S 2 in Computers & Internet Programming & Design

2 answers

Java's strong typing makes that a little painful. You can use the Java reflection API to look up a method by name and invoke it if it exists. This is how you'd do it if not just the name of the method, but also its argument types, matter:

// Setup
Object myObject = new ArrayList();
((ArrayList) myObject).add("a");

// Look up method
Method m = myObject. getClass(). getMethod( "get", new Class[] { int.class });
// Invoke method
Object res = m.invoke(myObject, new Object[] { new Integer(0) });

In this case "res" will be the String "a".

It's a bit clunky, and it's a bit inefficient... but it's what you have to do if you can't cast the object to any of:
(a) the individual base classes that implement the method (impractical if there are many),
(b) a common interface that defines the method (may not exist), or
(c) a common superclass that defines or implements the method (may not exist).

In general, well designed code should define methods that might want to be called this way as interfaces that other types could implement, and then you can just do:

if (someObject instanceof MyInterface)
((MyInterface) someObject).myMethod( ... );

... but where you can't do that, you're stuck with the reflection API.

2007-08-09 12:28:54 · answer #1 · answered by McFate 7 · 0 0

Each class has a getDeclaredMethods() method.
Here is how to use it to get the existence of a method for a class:

Call this myprog.java :

import java.io.*;
import java.lang.*;
import java.util.Scanner;
import java.lang.reflect.Method;

public class myprog {

public static boolean hasMethod( Class c , String name)
{
boolean has = false;
Method[] meth = c.getDeclaredMethods();

for(int i=0; i< meth.length; i++)
{
if( meth[i].getName().equals( name))
has = true;
}
return has;
}

public static void main(String args[]){

System.out.println("Enter full classname then the method");
try{
Scanner sc = new Scanner(System.in);
Class c = Class.forName(sc.next());
String methodName = sc.next();

if(hasMethod(c, methodName))
System.out.println(c.getName() + " has method " + methodName);
else
System.out.println(c.getName() + " does not have method " + methodName);
}catch(ClassNotFoundException e) {
e.printStackTrace();
}
}
}

Example Output:

Enter full classname then the method
java.util.Scanner
next
java.util.Scanner has method next

Enter full classname or an exception will be thrown - you can check for valid classes this way also.

If you want to check for methods with particular paranater classes then use getDeclaredMethod.

2007-08-09 17:54:52 · answer #2 · answered by E.M.Bed 5 · 0 0

fedest.com, questions and answers