View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components.introspection;
21  
22  import java.lang.reflect.Method;
23  
24  
25  /**
26   * A class that holds the necessary information for invoking 
27   * a method on a class (if the method is static) or an object.
28   * 
29   * @author <a href="mailto:miha.kadunc@cosylab.com">Miha Kadunc</a>
30   * @version $id$
31   */
32  public class MethodInvocationSettings {
33  	private Method method=null;
34  	private Object[] parameters=null;
35  	private Object object=null;
36  	private boolean staticMethod=false;
37  	
38  	/**
39  	 * @param method Method
40  	 */
41  	public MethodInvocationSettings(Method method){
42  		super();
43  		setMethod(method);
44  	}
45  	private boolean checkObject(){
46  		if (isStaticMethod()) return true;
47  		if (method.getDeclaringClass().isAssignableFrom(object.getClass())) return true;
48  		return false;
49  	}
50  	private boolean checkParameters(){
51  		if (method==null) return false;
52  		Class[] paramTypes=method.getParameterTypes();
53  		if (paramTypes.length!=parameters.length) return false;
54  		for (int i=0; i<parameters.length;i++){
55  			if (!paramTypes[i].isAssignableFrom(parameters[i].getClass())) return(false);
56  		}
57  		return true;
58  	}
59  	/**
60  	 * Gets the method
61  	 * @return Returns a Method
62  	 */
63  	public Method getMethod() {
64  		return method;
65  	}
66  	/**
67  	 * Gets the object
68  	 * @return Returns a Object
69  	 */
70  	public Object getObject() {
71  		return object;
72  	}
73  	/**
74  	 * Gets the parameters
75  	 * @return Returns a String[]
76  	 */
77  	public Object[] getParameters() {
78  		return parameters;
79  	}
80  	/**
81  	 * Gets the staticMethod
82  	 * @return Returns a boolean
83  	 */
84  	public boolean isStaticMethod() {
85  		return staticMethod;
86  	}
87  	/**
88  	 * Sets the method
89  	 * @param method The method to set
90  	 */
91  	
92  	public void setMethod(Method method){
93  		this.method=method;
94  		staticMethod=java.lang.reflect.Modifier.isStatic(method.getModifiers());
95  	}
96  	/**
97  	 * Sets the object
98  	 * @param object The object to set
99  	 */
100 	public void setObject(Object object) {
101 		this.object = object;
102 	}
103 	/**
104 	 * Sets the parameters
105 	 * @param parameters The parameters to set
106 	 */
107 	public void setParameters(Object[] parameters) {
108 		this.parameters = parameters;
109 	}
110 	/**
111 	 * @return String
112 	 */
113 	public String toString(){
114 		String className=method.getClass().getName();
115 		StringBuffer b=new StringBuffer(className.substring(className.lastIndexOf(".")));
116 		b.append(".");
117 		b.append(method.getName());
118 		b.append("(");
119 		b.append(")");
120 		if (staticMethod) b.append("<S>");
121 		return b.toString();
122 	}
123 }