for (Field field : fields) {
Inject inject = field.getAnnotation(Inject.class);
// 无需检查,因为字段名是唯一的
// if(fieldList.contains(field.getName()))
// throw duplicateField(classZ,field.getName());
IocField iocField = new IocField();
iocField.setName(field.getName());
IocValue iocValue;
if (Strings.isBlank(inject.value())) {
iocValue = new IocValue();
iocValue.setType(IocValue.TYPE_REFER);
iocValue.setValue(field.getName());
} else
iocValue = convert(inject.value());
iocField.setValue(iocValue);
iocField.setOptional(inject.optional());
iocObject.addField(iocField);
fieldList.add(iocField.getName());
}
// 处理字段(以@Inject方式,位于set方法)
Method[] methods;
try {
methods = classZ.getMethods();
}
catch (Exception e) {
// 如果获取失败,就忽略之
log.infof("Fail to call getMethods() in Class=%s, miss class or Security Limit, ignore it", classZ, e);
methods = new Method[0];
}
catch (NoClassDefFoundError e) {
log.infof("Fail to call getMethods() in Class=%s, miss class or Security Limit, ignore it", classZ, e);
methods = new Method[0];
}
for (Method method : methods) {
Inject inject = method.getAnnotation(Inject.class);
if (inject == null)
continue;
// 过滤特殊方法
int m = method.getModifiers();
if (Modifier.isAbstract(m) || (!Modifier.isPublic(m)) || Modifier.isStatic(m))
continue;
String methodName = method.getName();
if (methodName.startsWith("set")
&& methodName.length() > 3
&& method.getParameterTypes().length == 1) {
IocField iocField = new IocField();
iocField.setName(Strings.lowerFirst(methodName.substring(3)));
if (fieldList.contains(iocField.getName()))
throw duplicateField(classZ, iocField.getName());
IocValue iocValue;
if (Strings.isBlank(inject.value())) {
iocValue = new IocValue();
iocValue.setType(IocValue.TYPE_REFER);
iocValue.setValue(Strings.lowerFirst(methodName.substring(3)));
} else
iocValue = convert(inject.value());
iocField.setValue(iocValue);
iocObject.addField(iocField);
fieldList.add(iocField.getName());
}
}
// 处理字段(以@IocBean.field方式)
String[] flds = iocBean.fields();
if (flds != null && flds.length > 0) {
for (String fieldInfo : flds) {
if (fieldList.contains(fieldInfo))
throw duplicateField(classZ, fieldInfo);
IocField iocField = new IocField();
if (fieldInfo.contains(":")) { // dao:jndi:dataSource/jdbc形式
String[] datas = fieldInfo.split(":", 2);
// 完整形式, 与@Inject完全一致了
iocField.setName(datas[0]);
iocField.setValue(convert(datas[1]));
iocObject.addField(iocField);
} else {
// 基本形式, 引用与自身同名的bean
iocField.setName(fieldInfo);
IocValue iocValue = new IocValue();
iocValue.setType(IocValue.TYPE_REFER);
iocValue.setValue(fieldInfo);
iocField.setValue(iocValue);
iocObject.addField(iocField);
}
fieldList.add(iocField.getName());
}
}
// 处理工厂方法
if (!Strings.isBlank(iocBean.factory())) {