/*
* Copyright 2012 XueSong Guo.
*
* 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 cn.webwheel.setters;
import cn.webwheel.ActionSetter;
import cn.webwheel.SetterInfo;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class BeanSetter extends AbstractSetter<Object> {
private List<SetterInfo> setters = new ArrayList<SetterInfo>();
private Class<?> type;
private Method getter;
private BeanSetter(Class<?> type, String paramName, ActionSetter actionSetter) {
this.type = type;
Field[] fields = type.getFields();
for (int i = fields.length - 1; i >= 0; i--) {
Field field = fields[i];
SetterInfo si = actionSetter.getSetterInfo(field, paramName + '.' + field.getName());
if (si != null) {
setters.add(si);
}
}
Method[] methods = type.getMethods();
for (int i = methods.length - 1; i >= 0; i--) {
Method m = methods[i];
String name = ActionSetter.isSetter(m);
if (name == null) continue;
name = paramName + '.' + name;
SetterInfo si = actionSetter.getSetterInfo(m, name);
if (si != null) {
setters.add(si);
}
}
for (int i = methods.length - 1; i >= 0; i--) {
Method m = methods[i];
String name = ActionSetter.isGetter(m);
if (name == null) continue;
name = paramName + '.' + name;
BeanSetter beanSetter = BeanSetter.create(m.getReturnType(), name, actionSetter);
if (beanSetter == null) {
continue;
}
boolean find = false;
for (SetterInfo si : setters) {
if (!si.paramName.equals(name)) continue;
if (!(si.setter instanceof BeanSetter)) continue;
if (!(si.member instanceof Method)) continue;
if (((Method) si.member).getParameterTypes().length != 1) continue;
if (((Method) si.member).getParameterTypes()[0] != m.getReturnType()) continue;
find = true;
BeanSetter bs = (BeanSetter) si.setter;
bs.setGetter(m);
break;
}
if (find) {
continue;
}
SetterInfo si = new SetterInfo();
si.paramName = name;
si.member = m;
si.setter = beanSetter;
beanSetter.setGetter(m);
setters.add(si);
}
}
@Override
public Object set(Object instance, Member member, Map<String, Object> params, String paramName) {
Object bean = null;
if (getter != null) {
try {
bean = getter.invoke(instance);
} catch (IllegalAccessException ignored) {
} catch (InvocationTargetException e) {
throw new RuntimeException(e.getCause());
}
if (bean == null && member == getter) return null;
}
if (bean == null) {
for (String param : params.keySet()) {
if (param.startsWith(paramName + '.')) {
try {
bean = type.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException ignored) {
}
break;
}
}
}
if (bean == null) {
return null;
}
for (SetterInfo si : setters) {
si.setter.set(bean, si.member, params, si.paramName);
}
if (member != getter) {
set(instance, member, bean);
}
return bean;
}
public void setGetter(Method getter) {
this.getter = getter;
}
public static BeanSetter create(Class cls, String paramName, ActionSetter actionSetter) {
if (paramName == null) return null;
try {
cls.getConstructor();
} catch (NoSuchMethodException e) {
return null;
}
BeanSetter si = new BeanSetter(cls, paramName, actionSetter);
if (si.setters.isEmpty()) return null;
return si;
}
}