001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.uml.core.eventframework;
043:
044: import java.lang.reflect.InvocationTargetException;
045: import java.lang.reflect.Method;
046: import java.util.Collection;
047: import java.util.Hashtable;
048: import java.util.Vector;
049:
050: import org.netbeans.modules.uml.core.support.umlsupport.Log;
051:
052: /**
053: * @author sumitabhk
054: *
055: */
056: public class EventFunctor {
057:
058: //it stores the <className|methodName, Method >
059: private static Hashtable<String, Method> m_Functors = new Hashtable<String, Method>();
060: private String m_ClassName = null;
061: private Class m_Class = null;
062: private String m_MethodName = null;
063: private Method m_Method = null;
064: private Object[] m_Parameters = null;
065:
066: /**
067: *
068: */
069: private EventFunctor() {
070: super ();
071: }
072:
073: public void setParameters(Object[] parms) {
074: if (parms != null && parms.length > 0) {
075: Object obj = parms[0];
076: if (obj instanceof Vector) {
077:
078: // If the first parameter is a Vector, expand it. This is necessary
079: // because the EventDispatchers pack all their parameters into a
080: // Vector before giving it to us.
081: // if (parms[0] instanceof Vector<Object>)
082: // {
083: Vector<Object> vec = new Vector<Object>(
084: (Collection) parms[0]);
085: Object[] newArray = new Object[vec.size()
086: + parms.length - 1];
087: Object[] temp = vec.toArray();
088: System.arraycopy(temp, 0, newArray, 0, temp.length);
089: if (parms.length > 1) {
090: System.arraycopy(parms, 1, newArray, temp.length,
091: parms.length - 1);
092: }
093: parms = newArray;
094: }
095: }
096: m_Parameters = parms;
097: }
098:
099: public EventFunctor(String className, String methodName) {
100: m_ClassName = className;
101: m_MethodName = methodName;
102: m_Method = retrieveMethod(className, methodName);
103: }
104:
105: public EventFunctor(Class cl, String methodName) {
106: m_Class = cl;
107: m_MethodName = methodName;
108: m_Method = retrieveMethod(cl, methodName);
109: }
110:
111: public Object execute(Object[] parms, Object objInstance) {
112: return invokeMethod(m_Method, parms, objInstance);
113: }
114:
115: public Object execute(Object objInstance) {
116: return invokeMethod(m_Method, m_Parameters, objInstance);
117: }
118:
119: public boolean isResultOK() {
120: return true;
121: }
122:
123: //here we assume that there will be only one method with this name in the class
124: public static Method retrieveMethod(Class clazz, String methodName) {
125: String hashKey = clazz.getName() + "|" + methodName;
126: Method retMeth = m_Functors.get(hashKey);
127: if (retMeth == null) {
128: //try to get the Method, return it and put it in the hashtable.
129: Method[] meths = clazz.getMethods();
130: if (meths != null) {
131: for (int i = 0; i < meths.length; i++) {
132: Method meth = meths[i];
133: if (meth.getName().equals(methodName)) {
134: retMeth = meth;
135: m_Functors.put(hashKey, meth);
136: break;
137: }
138: }
139: }
140: }
141: return retMeth;
142: }
143:
144: //here we assume that there will be only one method with this name in the class
145: public static Method retrieveMethod(String className,
146: String methodName) {
147: Method retMeth = m_Functors.get(className + "|" + methodName);
148: if (retMeth == null) {
149: //try to get the Method, return it and put it in the hashtable.
150: try {
151: Class clazz = Class.forName(className);
152: Method[] meths = clazz.getMethods();
153: if (meths != null) {
154: for (int i = 0; i < meths.length; i++) {
155: Method meth = meths[i];
156: if (meth.getName().equals(methodName)) {
157: retMeth = meth;
158: m_Functors.put(
159: className + "|" + methodName, meth);
160: break;
161: }
162: }
163: }
164: } catch (ClassNotFoundException e) {
165: }
166: }
167: return retMeth;
168: }
169:
170: public static Object retrieveAndInvokeMethod(String className,
171: String methodName, Object[] parms, Object objInstance) {
172: Object retObj = null;
173: Method meth = retrieveMethod(className, methodName);
174: if (meth != null) {
175: try {
176: retObj = meth.invoke(objInstance, parms);
177: } catch (IllegalArgumentException e) {
178: } catch (IllegalAccessException e) {
179: } catch (InvocationTargetException e) {
180: }
181: }
182: return retObj;
183: }
184:
185: private static String sayParams(Object[] objs) {
186: StringBuffer par = new StringBuffer();
187: for (int i = 0; i < objs.length; ++i) {
188: if (i > 0)
189: par.append(", ");
190: String parType = objs[i] != null ? objs[i].getClass()
191: .getName() : "null";
192: par.append(parType.substring(parType.lastIndexOf('.') + 1));
193: }
194: return par.toString();
195: }
196:
197: private static String sayParams(Method m) {
198: StringBuffer par = new StringBuffer();
199: Class[] c = m.getParameterTypes();
200: for (int i = 0; i < c.length; ++i) {
201: if (i > 0)
202: par.append(", ");
203: String parType = c[i].getName();
204: par.append(parType.substring(parType.lastIndexOf('.') + 1));
205: }
206: return par.toString();
207: }
208:
209: public static Object invokeMethod(Method meth, Object[] parms,
210: Object objInstance) {
211: Object retObj = null;
212: if (meth != null) {
213: try {
214: retObj = meth.invoke(objInstance, parms);
215: } catch (Exception e) {
216: Log.err(e.getMessage() + " when invoking "
217: + meth.getName() + "(" + sayParams(meth)
218: + ") on " + objInstance.getClass().getName()
219: + " with parameters: " + sayParams(parms));
220: Log.stackTrace(e);
221: }
222: }
223: return retObj;
224: }
225:
226: }
|