getting return value of a method in an interface

Hello! I have a problem. I have an interface called I. One of the declared method in this interface is M. And I have a class called C. Class C declares Interface I. I want to get the return value of the method M. I used invoke method. M.invoke(anInstance, arg[]). You can't take the instance of interfaces or abstract classes. If I use a class instead of instance for anInstance variable, i get "java.lang.IllegalArgumentException: object is not an instance of declaring class" error. If I use an instance, i get "java.lang.IllegalAccessException: Can not call newInstance() on the Class for java.lang.Class" error. This is because of getting an instance of an interface. if you can help me. i will be greatfully happy.

[727 byte] By [akk512a] at [2007-9-29 21:10:02]
# 1
What?Are you trying to invoke an method without an instance?
paulcwa at 2007-7-16 1:24:06 > top of Java-index,Core,Core APIs...
# 2

There's no need for you to actually invoke the method nor do you need to instantiate the class (which doesn't work for Interfaces, as you pointed out correctly).

Instead use Class.forName(String) with the package and name of the interface to create a class object.

From that class object you can get all methods in an array to run through and get the returntypes of.

If you are looking for one specific method you know the name of, you can go the long way with the Class-object's getDeclaredMethod(String name, Class[] parameterTypes).

Regards, tom

tomvollerthuna at 2007-7-16 1:24:06 > top of Java-index,Core,Core APIs...
# 3

The method declared in the Interface I is there to enforce its implementation in an implementing class only so you can never obtain an instance of I.

You need to obtain an instance of the class implementing I, in this case C, in order to invoke a method on it. See code below for a rough example of what I mean.

Class classThatImplementsI = Class.forName("the.concrete.implementation.of.I.C");

I anInstance = (I)classThatImplementsI.newInstance();

//... create the method parameter signature, I am going to use a Single String parameter

Class[] paramTypes = { String.class };

Method M = I.getClass().getDeclaredMethod("M", paramTypes );

Object[] params = { "a parameter for M" };

Object result = m.invoke( anInstance, params );

The above code does not worry about exceptions you need to normally catch and is only given to illustrate the points made.

andybaa at 2007-7-16 1:24:06 > top of Java-index,Core,Core APIs...
# 4
> The method declared in the Interface I is there to> enforce its implementation in an implementing class> only so you can never obtain an instance of I.I wish we could edit posts...Change "so you can never" to "and you can never"
andybaa at 2007-7-16 1:24:06 > top of Java-index,Core,Core APIs...
# 5

With all respect, andyba, you seem to know quite a lot about reflection and the like, but is one of the most stupid attempts to solve the OP's problem:

OP wants to know return type of Interface's method AND OP knows that instantiation won't work.

Your solution to write an implementing class to instantiate the Interface is triple stupid because

a) it's not necessary, as I stated above

b) there may be no known implementing classes at runtime

c) even if there were such a class, instantiating it *may* have unwanted side effects, takes unnecessary time, the class may have N methods while the interface has only one, so you might look for the correct method too long

All in all, I'd say: ignore andyba's statement in this one case and use the simpler attempt I suggested above.

Regards, Tom

tomvollerthuna at 2007-7-16 1:24:06 > top of Java-index,Core,Core APIs...
# 6

Even more simple solution and some example code...

I = the name of the interface

M = the name of the method

Method[] methods = I.class.getMethods();

for (int i = 0; i < methods.length; i++)

{

Method m = methods[i];

if (m.getName().equals("M"))

m.getReturnType();

}

// or for example a method with no parameters

I.class.getMethod("M", null).getReturnType();

javaguyaimfia at 2007-7-16 1:24:06 > top of Java-index,Core,Core APIs...
# 7

> With all respect, andyba, you seem to know quite a lot

> about reflection and the like, but is one of the most

> stupid attempts to solve the OP's problem:

> OP wants to know return type of Interface's method AND

> OP knows that instantiation won't work.

Hard to say what the OP wants, but I suspect that they do want the return value and not the type. From the OP....

I want to get the return value of the method M. I used invoke method. M.invoke(anInstance, arg[]).

jschella at 2007-7-16 1:24:06 > top of Java-index,Core,Core APIs...
# 8

Thanks for the feedback Tom although you should rethink what you said.

I did not trash your suggestion, nor did I make any reference to it.

Perhaps you should read the OP to see where you went wrong.

> OP wants to know return type of Interface's method

No, the OP wants to obtain the return value.

> AND OP knows that instantiation won't work.

He was trying to instantiate an Interface, which is not possible. In order for him invoke a Method he needs a concrete implementation. I was simply pointing out what he needs to do in order to acheive his aim.

> b) there may be no known implementing classes at

> runtime

If there is no implementing class provided then he will not be able to invoke a method on its instance.

> c) even if there were such a class, instantiating it

> *may* have unwanted side effects, takes unnecessary

> time

Why is that a problem? One reason for invoking M is to see what happens.

> the class may have N methods while the interface

> has only one, so you might look for the correct method

> too long

If you bothered to study the code I supplied I obtain a Method from the Interface class so there is no problem in "looking" for a method, besides which there can be exactly 1 method with a given signature in any class so there is no searching involved.

On a personal note, such negative replies are neither helpful to the OP and can cause a credibility problem caused by being labelled a Troll.

andybaa at 2007-7-16 1:24:07 > top of Java-index,Core,Core APIs...
# 9

Andyba:

You are right. I have been far too unfriendly and by re-reading the OP, I have to admit, that the request can be interpreted your way as well.

So I'm afraid I have to excuse: Sorry for being a smartass. AND: I didn't think you could feel labeled a Troll, which you obviously are not.

I hope that settles that point.

But perhaps the OP could solve the enigma about what was actually mean in the Posting?

Regards, Tom

tomvollerthuna at 2007-7-16 1:24:07 > top of Java-index,Core,Core APIs...