1 /* 2 * Copyright 2004-2008 the Seasar Foundation and the Others. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 * either express or implied. See the License for the specific language 14 * governing permissions and limitations under the License. 15 */ 16 package org.seasar.uruma.binding.method.impl; 17 18 import java.lang.reflect.Method; 19 20 import org.eclipse.swt.events.TypedEvent; 21 import org.eclipse.swt.widgets.Event; 22 import org.seasar.uruma.binding.method.ArgumentsFilter; 23 import org.seasar.uruma.util.ClassUtil; 24 25 /** 26 * 引数を {@link TypedEvent} オブジェクトへ変換する {@link ArgumentsFilter} です。<br /> 27 * 28 * ターゲットメソッドの引数型が {@link TypedEvent} のサブクラスであった場合、引数オブジェクトをその型にあわせてラップします。<br /> 29 * 与えられる引数は {@link Event} のサブクラスである必要があります。<br /> 30 * ターゲットメソッドの引数型が {@link TypedEvent} のサブクラスでない場合や、与えられた引数が {@link Event} 31 * のサブクラスではない場合、変換は行いません。<br /> 32 * 33 * @author bskuroneko 34 * @author y-komori 35 */ 36 public class TypedEventArgumentsFilter implements ArgumentsFilter { 37 38 private Class<?>[] targetParamTypes; 39 40 /** 41 * {@link TypedEventArgumentsFilter} を構築します。<br /> 42 * 43 * @param targetMethod 44 * 対象メソッド 45 */ 46 public TypedEventArgumentsFilter(final Method targetMethod) { 47 this.targetParamTypes = targetMethod.getParameterTypes(); 48 } 49 50 /* 51 * @see org.seasar.uruma.binding.method.ArgumentsFilter#filter(java.lang.Object[]) 52 */ 53 public Object[] filter(final Object[] args) { 54 if (args == null) { 55 return null; 56 } 57 58 if (targetParamTypes.length != args.length) { 59 return args; 60 } 61 62 Object[] filteredArgs = new Object[args.length]; 63 for (int i = 0; i < args.length; i++) { 64 Class<?> paramType = targetParamTypes[i]; 65 if (TypedEvent.class.isAssignableFrom(paramType) 66 && args[i] instanceof Event) { 67 // TODO 別タイプのイベントでもインスタンス化できてしまうが、制限するかを検討 68 filteredArgs[i] = ClassUtil.newInstance(paramType, args[i]); 69 } else { 70 filteredArgs[i] = args[i]; 71 } 72 } 73 return filteredArgs; 74 } 75 }