/*
 * 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;
import woolpack.utils.CheckUtils;

/**
 * リフレクションを使用した{@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) {
		CheckUtils.checkNotNull(convertable);
		this.convertable = convertable;
	}

	private BeanInfo generateBeanInfo(final Class clazz) {
		try {
			return Introspector.getBeanInfo(clazz);
		} catch (final IntrospectionException e) {
			throw new IllegalStateException(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 AbstractGettingEL() {
				@Override
				public Object getValue(final Object root, final Class toType) {
					try {
						final Object result = m.invoke(root, new Object[0]);
						return convertable
								.convert(result, toType, propertyName);
					} catch (final IllegalAccessException e) {
						throw new IllegalStateException(e);
					} catch (final InvocationTargetException e) {
						throw new ELTargetRuntimeException(e);
					}
				}
			};
		}
		throw new IllegalArgumentException("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 AbstractEL() {
				@Override
				public void setValue(final Object root, final Object value) {
					try {
						final Object newValue = convertable.convert(value, m
								.getParameterTypes()[0], propertyName);
						m.invoke(root, new Object[] { newValue });
					} catch (final IllegalAccessException e) {
						throw new IllegalStateException(e);
					} catch (final InvocationTargetException e) {
						throw new ELTargetRuntimeException(e);
					}
				}

				@Override
				public Object getValue(final Object root, final Class clazz) {
					return null; // カバレージがここを通過してはいけない
				}
			};
		}
		throw new IllegalArgumentException("property not found:" + clazz + ':'
				+ propertyName);
	}

	public Convertable getConvertable() {
		return convertable;
	}
}
