KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jwebunit > util > reflect > MethodInvoker


1 /********************************************************************************
2  * Copyright (c) 2001, ThoughtWorks, Inc.
3  * Distributed open-source, see full license under licenses/jwebunit_license.txt
4 **********************************/

5 package net.sourceforge.jwebunit.util.reflect;
6
7 import java.lang.reflect.InvocationTargetException JavaDoc;
8 import java.lang.reflect.Method JavaDoc;
9
10 /**
11  * Wrapper to java reflection for method invocation.
12  *
13  * @author Wilkes Joiner
14  */

15 public class MethodInvoker {
16     private Object JavaDoc receiver;
17     private String JavaDoc methodName;
18     private Class JavaDoc[] argTypes;
19     private Class JavaDoc receiverType;
20     private Object JavaDoc[] args;
21
22     public MethodInvoker(Object JavaDoc receiver, String JavaDoc methodName) {
23         this(receiver, methodName, new Object JavaDoc[0]);
24     }
25
26     public MethodInvoker(Object JavaDoc receiver, String JavaDoc methodName, Object JavaDoc arg) {
27         this(receiver, methodName, new Object JavaDoc[]{arg});
28     }
29
30     public MethodInvoker(Object JavaDoc receiver, String JavaDoc methodName, Object JavaDoc[] args) {
31         this.receiver = receiver;
32         receiverType = receiver.getClass();
33         this.methodName = methodName;
34         this.args = args;
35         argTypes = new Class JavaDoc[args.length];
36         for (int i = 0; i < args.length; i++) {
37             argTypes[i] = args[i].getClass();
38         }
39     }
40
41     public Method JavaDoc getMethod() throws NoSuchMethodException JavaDoc {
42         try {
43             return receiverType.getMethod(methodName, argTypes);
44         } catch (NoSuchMethodException JavaDoc e) {
45             convertToPrimitives();
46             try {
47                 return receiverType.getMethod(methodName, argTypes);
48             } catch (NoSuchMethodException JavaDoc e1) {
49                 String JavaDoc classes = "(";
50                 for (int i = 0; i < argTypes.length; i++) {
51                     Class JavaDoc argType = argTypes[i];
52                     classes += " " + argType.getName();
53                     if (i != argTypes.length - 1) {
54                         classes += ",";
55                     }
56                 }
57                 classes += ")";
58                 throw new NoSuchMethodException JavaDoc(methodName + classes);
59             }
60
61         }
62     }
63
64     private void convertToPrimitives() {
65         Class JavaDoc[] newArgTypes = new Class JavaDoc[argTypes.length];
66         for (int i = 0; i < argTypes.length; i++) {
67             Class JavaDoc argType = argTypes[i];
68             if (argType.equals(Boolean JavaDoc.class)) {
69                 newArgTypes[i] = Boolean.TYPE;
70             } else if (argType.equals(Byte JavaDoc.class)) {
71                 newArgTypes[i] = Byte.TYPE;
72             } else if (argType.equals(Character JavaDoc.class)) {
73                 newArgTypes[i] = Character.TYPE;
74             } else if (argType.equals(Double JavaDoc.class)) {
75                 newArgTypes[i] = Double.TYPE;
76             } else if (argType.equals(Float JavaDoc.class)) {
77                 newArgTypes[i] = Float.TYPE;
78             } else if (argType.equals(Integer JavaDoc.class)) {
79                 newArgTypes[i] = Integer.TYPE;
80             } else if (argType.equals(Long JavaDoc.class)) {
81                 newArgTypes[i] = Long.TYPE;
82             } else if (argType.equals(Short JavaDoc.class)) {
83                 newArgTypes[i] = Short.TYPE;
84             } else {
85                 newArgTypes[i] = argType;
86             }
87         }
88         argTypes = newArgTypes;
89     }
90
91     public Object JavaDoc invoke() throws NoSuchMethodException JavaDoc, IllegalAccessException JavaDoc,
92             IllegalArgumentException JavaDoc, InvocationTargetException JavaDoc {
93         return getMethod().invoke(receiver, args);
94     }
95 }
96
Popular Tags