/*
 * Copyright 2006 Takahiro Nakamura.
 *
 * Licensed 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.
 */
package woolpack.el;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import woolpack.el.convert.Convertable;

/**
 * リフレクションを使用した{@link PropertyELFactory}。
 * getter/setterのオーバロードには対応していない。
 * @author nakamura
 *
 */
public class ReflectionPropertyELFactory implements PropertyELFactory {
	private final Convertable convertable;
	
	/**
	 * コンストラクタ。
	 * @param convertable 変換器。
	 * @throws NullPointerException 引数が null の場合。
	 */
	public ReflectionPropertyELFactory(final Convertable convertable){
		convertable.getClass();
		this.convertable = convertable;
	}
	
	private BeanInfo generateBeanInfo(final Class clazz){
		try {
			return Introspector.getBeanInfo(clazz);
		} catch (final IntrospectionException e) {
			throw new RuntimeException(e);
		}
	}

	public GettingEL newGetter(final Class clazz, final String propertyName) {
		final BeanInfo beanInfo = generateBeanInfo(clazz);
		for(final PropertyDescriptor p:beanInfo.getPropertyDescriptors()){
			if(!p.getName().equals(propertyName)){
				continue;
			}
			final Method m = p.getReadMethod();
			if(m == null){
				break;
			}
			return new GettingELAdapter(){
				@Override public Object getValue(final Object root, final Class toType)
						throws ELTargetRuntimeException {
					try {
						final Object result = m.invoke(root, new Object[0]);
						return convertable.convert(result, toType, propertyName);
					} catch (final IllegalArgumentException e) {
						throw new RuntimeException(e);
					} catch (final IllegalAccessException e) {
						throw new RuntimeException(e);
					} catch (final InvocationTargetException e) {
						throw new ELTargetRuntimeException(e);
					}
				}
			};
		}
		throw new RuntimeException("property not found: " + clazz + ": " + propertyName);
	}

	public EL newSetter(final Class clazz, final String propertyName) {
		final BeanInfo beanInfo = generateBeanInfo(clazz);
		for(final PropertyDescriptor p:beanInfo.getPropertyDescriptors()){
			if(!p.getName().equals(propertyName)){
				continue;
			}
			final Method m = p.getWriteMethod();
			if(m == null){
				break;
			}
			return new ELAdapter(){	
				@Override public void setValue(final Object root, final Object value) throws ELTargetRuntimeException {
					try {
						final Object newValue = convertable.convert(value, m.getParameterTypes()[0], propertyName);
						m.invoke(root, new Object[]{newValue});
					} catch (final IllegalArgumentException e) {
						throw new RuntimeException(e);
					} catch (final IllegalAccessException e) {
						throw new RuntimeException(e);
					} catch (final InvocationTargetException e) {
						throw new ELTargetRuntimeException(e);
					}
				}
				@Override
				public Object getValue(Object root, Class clazz) throws ELTargetRuntimeException {
					return null;// カバレージがここを通過してはいけない
				}
			};
		}
		throw new RuntimeException("property not found: " + clazz + ": " + propertyName);
	}

	public Convertable getConvertable() {
		return convertable;
	}
}
