/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * $Header:$ */ package org.apache.beehive.controls.api.events; import java.lang.reflect.Method; import java.util.HashMap; import java.util.WeakHashMap; /** * The EventRef class represents a reference to a specific Control event. EventRefs can * be used to fire external events into a Control, in contexts where the event source may * not share the associated EventSet class instance with the event target, or even have * access to the EventSet class itself. *
* It is roughly equivalent to the java.lang.reflect.Method object that refers to a method * on an EventSet interface, but has several additional properties: *
** where eventSet refers to the fully qualified name of the EventSet class, * eventName refers to the name of the event Method, and eventDescriptor * describes the event argument and return types using the method descriptor format * defined in the Java Language Specification. *. *
* For example, given the following EventSet interface: *
** the eventDescriptor for myEvent would be: *@ControlInterface * public interface MyControl * { * @EventSet * public interface MyEvents * { * public String myEvent(int arg0, Object arg2); * } * } *
* MyControl.MyEvents.myEvent(ILjava/lang/Object;)Ljava/lang/String; ** @param eventDescriptor the event descriptor string associated with the event */ public EventRef(String eventDescriptor) { _descriptor = eventDescriptor; } /** * Returns the event descriptor string associated with the EventRef. * @param controlInterface the ControlInterface */ public String getEventDescriptor(Class controlInterface) { // // NOTE: The input controlInterface is currently unused, but included to // enable downstream optimization of serialization representation. See the // OPTIMIZE comment below for more details. If implemented, the interface // is needed to reverse the transformation from a hash back to a method or // descriptor. // if (_descriptor == null) _descriptor = computeEventDescriptor(_method); return _descriptor; } /** * Helper method that computes the event descriptor sting for a method */ private String computeEventDescriptor(Method method) { StringBuilder sb = new StringBuilder(); // Add event class and method name sb.append(method.getDeclaringClass().getName()); sb.append("."); sb.append(method.getName()); // Add event arguments Class [] parms = method.getParameterTypes(); sb.append("("); for (int i = 0; i < parms.length; i++) appendTypeDescriptor(sb, parms[i]); sb.append(")"); // Add event return type appendTypeDescriptor(sb, method.getReturnType()); return sb.toString(); } /** * Helper method that appends a type descriptor to a StringBuilder. Used * while accumulating an event descriptor string. */ private void appendTypeDescriptor(StringBuilder sb, Class clazz) { if (clazz.isPrimitive()) sb.append(_primToType.get(clazz)); else if (clazz.isArray()) sb.append(clazz.getName().replace('.','/')); else { sb.append("L"); sb.append(clazz.getName().replace('.','/')); sb.append(";"); } } /** * Returns the event Method associated with this EventRef. */ public Method getEventMethod(Class controlInterface) { // // If we already hold a method reference and its loader matches up with the input // interface, then just return it. // if (_method != null && _method.getDeclaringClass().getClassLoader().equals(controlInterface.getClassLoader())) return _method; // // Otherwise, obtain the mapping from descriptors to methods, and use it to // convert back to a method. // String eventDescriptor = getEventDescriptor(controlInterface); HashMap