-1

I have a method that return a list of objects and I want to call it by the invoke method of the class Method. The only problem is that invoke method returns an Object and not a list<Object>.

The code is here:

Class<? extends AnObject> anObject = MyObject.getClass();
Method myMethod = MyObject.getMethod("getListObject"); 
Object objject = method.invoke(MyObject); // I want it to return list

How can I solve it?

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
geeko
  • 19
  • 1
  • 4

6 Answers6

1
Class<? extends AnObject>  anObject = MyObject.getClass();
Method myMethod = MyObject.getMethod("getListObject"); 
List object = (List)myMethod.invoke(anObject);
Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
  • I already did this but i have an exception: org.hibernate.collection.PersistentSet cannot be cast to java.util.List – geeko Jul 21 '11 at 07:58
  • well, then cast it to `org.hibernate.collection.PersistentSet`. If your function doesn't return a list then you can't get a list. – Petar Ivanov Jul 21 '11 at 08:00
0

what let me to this page is

when I tried to invoke a protected method without making it Accessible

take look at my now working version

DoPubService service = SpringAdapter.getBean("frontendDoPubService", DoPubService.class);
try {
    Method runUrlReplacerMethod = service.getClass().getDeclaredMethod("runUrlReplacer", String.class, String.class, String.class);

    return runUrlReplacerMethod.invoke(service, "10.21019/qna-900031", "abs", explanation);
 } catch (Exception e) {
    e.printStackTrace();
}

this is not working because the Method should be Accessible before invoking it

runUrlReplacerMethod.setAccessible(true);
Basheer AL-MOMANI
  • 14,473
  • 9
  • 96
  • 92
0

Java 8 solution:

List<?> list = (List) invokeResult;
List<String> = list.stream().map(el -> (String) el).collect(Collectors.toList());

Change String to type you need.

Aleh
  • 386
  • 4
  • 8
0

Just typecast it:

List list = (List)method.invoke(...);
user207421
  • 305,947
  • 44
  • 307
  • 483
  • I already did this but i have an exception: org.hibernate.collection.PersistentSet cannot be cast to java.util.List – geeko Jul 21 '11 at 07:55
  • then maybe you could try to cast into Set.. are you sure it returns a List? – ron Jul 21 '11 at 07:58
  • PersistentSet class implements a set, not list. – jaxb Jul 21 '11 at 08:00
  • It works with Set: Set list = (Set)method.invoke(...); thanks – geeko Jul 21 '11 at 08:02
  • @user855388 So your method doesn't return a List at all, so your question is wrong. Cast it to *PersistentSet.* – user207421 Jul 21 '11 at 08:03
  • My method returns a Set. But i thought that a List and a Set are the same because they're both collections. No need to Cast it to PersistentSet, it works with Set. – geeko Jul 21 '11 at 08:09
  • @user855388 'a List and a Set are the same because they're both collections'? No, one is a List and the other is a Set. *If you don't care which you get,* cast to a Collection. – user207421 Jul 21 '11 at 10:14
0

You can explicitly cast the result to the type you desire.

List<?> theList = (List<?>) method.invoke(anObject, new Object[] {})

This may result in a ClassCastException in runtime if the method doesn't return the type you expected.

ron
  • 9,262
  • 4
  • 40
  • 73
0

How about letting dp4j's annotations processor figure it out?

@com.dp4j.Reflect //or @org.junit.Test
public void test(){
  Object objject =  MyObject.getListObject();
}

You can also print the injected code with -Averbose=true.

simpatico
  • 10,709
  • 20
  • 81
  • 126