1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package com.sun.star.wizards.ui.event;
24 
25 import java.lang.reflect.InvocationTargetException;
26 import java.lang.reflect.Method;
27 
28 /**
29  * Encapsulate a Method invocation.
30  * In the constructor one defines a method, a target object and an optional
31  * Parameter.
32  * Then one calls "invoke", with or without a parameter. <br/>
33  * Limitations: I do not check anything myself. If the param is not ok, from the
34  * wrong type, or the mothod doesnot exist on the given object.
35  * You can trick this class howmuch you want: it will all throw exceptions
36  * on the java level. i throw no error warnings or my own excceptions...
37  * @author  rpiterman
38  */
39 public class MethodInvocation
40 {
41 
42     static final Class[] EMPTY_ARRAY =
43     {
44     };
45     //the method to invoke.
46     Method mMethod;
47     //the object to invoke the method on.
48     Object mObject;
49     //with one Parameter / without
50     boolean mWithParam;
51 
52     /** Creates a new instance of MethodInvokation */
MethodInvocation(String methodName, Object obj)53     public MethodInvocation(String methodName, Object obj) throws NoSuchMethodException
54     {
55         this(methodName, obj, null);
56     }
57 
MethodInvocation(Method method, Object obj)58     public MethodInvocation(Method method, Object obj)
59     {
60         this(method, obj, null);
61     }
62 
MethodInvocation(String methodName, Object obj, Class paramClass)63     public MethodInvocation(String methodName, Object obj, Class paramClass) throws NoSuchMethodException
64     {
65         this(paramClass == null ? obj.getClass().getMethod(methodName, null) : obj.getClass().getMethod(methodName, new Class[]
66                 {
67                     paramClass
68                 }), obj, paramClass);
69     }
70 
MethodInvocation(Method method, Object obj, Class paramClass)71     public MethodInvocation(Method method, Object obj, Class paramClass)
72     {
73         mMethod = method;
74         mObject = obj;
75         mWithParam = !(paramClass == null);
76     }
77 
78     /**
79      * Returns the result of calling the method on the object, or null, if no result.
80      */
invoke(Object param)81     public Object invoke(Object param) throws IllegalAccessException, InvocationTargetException
82     {
83         if (mWithParam)
84         {
85             return mMethod.invoke(mObject, (Object) param
86                     );
87         }
88         else
89         {
90             return mMethod.invoke(mObject, EMPTY_ARRAY);
91         }
92     }
93 
94     /**
95      * This method is a convenience method.
96      * It is the same as calling invoke(null);
97      */
invoke()98     public Object invoke() throws IllegalAccessException, InvocationTargetException
99     {
100         return invoke(null);
101     }
102 }
103