public void setValue(Object instance, Object value) { setValue(instance,value,false); }
public void setValue(Object bean,Object value){ if(!writable) { throw new IllegalStateException("Property '" + name + "' is not writable at '" + beanType.getBeanClass().getName() + "'"); } if(null != value && !type.isAssignableFrom(value.getClass())){ value = Converts.convert(value, type,genericType); } if(null != setter){ setter.invoke(bean,value); }else{ field.setValue(bean, value, true); } }
public boolean trySetValue(Object bean,Object value) { if(writable){ try { if(null != value && !type.isAssignableFrom(value.getClass())){ value = Converts.convert(value, type,genericType); } if(null != setter){ setter.invoke(bean,value); }else{ field.setValue(bean, value, true); } return true; } catch (ConvertUnsupportedException e) { log.debug("cannot set property '{}' of bean '{}' -> {}",name,beanType.getBeanClass().getSimpleName(),e.getMessage()); } } return false; }
protected void injectApiBeans(App app, Api api) { Set<Object> controllers = new HashSet<>(); for(ApiRoute ar : api.getConfig().getApiRoutes()) { Route route = ar.getRoute(); //Inject ApiConfig & ApiMetadata. Object controller = route.getAction().getController(); if(null != controller && !controllers.contains(controller)) { controllers.add(controller); ReflectClass rc = ReflectClass.of(controller.getClass()); for(ReflectField rf : rc.getFields()) { if(rf.getType().equals(ApiConfig.class)) { rf.setValue(controller, api.getConfig()); break; } if(rf.getType().equals(ApiMetadata.class)) { rf.setValue(controller, api.getMetadata()); } } if(controller instanceof ApiInitializable) { ((ApiInitializable) controller).postApiInitialized(api); } } } controllers.clear(); }
@Override public void injectStatic(Class<?> cls) throws BeanException { BeanDefinitionBase bd = createBeanDefinition(cls); ReflectClass rc = ReflectClass.of(cls); BeanFactory factory = null != beanFactory ? beanFactory : this; for (ReflectField rf : rc.getFields()) { if (rf.isStatic() && rf.isAnnotationPresent(Inject.class)) { try { //skip when bean value already set. if (null != rf.getValue(null)) { continue; } Object injectedBean = resolveInjectValue(factory, bd, rf.getName(), rf.getType(), rf.getGenericType(), rf.getAnnotations()); if (null != injectedBean) { rf.setValue(null, injectedBean); } } catch (Exception e) { log.error("Error injecting static field '{}' in class '{}' : {}", rf.getName(), cls.getName(), e.getMessage()); throw e; } } } }
throw new BeanCreationException("Cannot inject not writable property '" + bp.getName() + "' in bean '" + bd + "'"); bp.getReflectField().setValue(bean, injectedValue); rf.setValue(bean, injectedValue); } catch (Exception e) { log.error("Error injecting field '{}' in bean '{}' : {}", rf.getName(), bd, e.getMessage());