@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; } } } }
protected void validateField(BeanDefinition d, Object bean, ReflectField field) { if((field.isAnnotationPresent(NotNull.class) || field.isAnnotationPresent(M.class)) && null == field.getValue(bean)){ throw new BeanException("Field '" + field.getName() + "'(" + field.getType() + ") must not null in bean " + (null == d ? bean : d)); } if((field.isAnnotationPresent(NotEmpty.class) || field.isAnnotationPresent(R.class) ) && Objects2.isEmpty(field.getValue(bean))){ throw new BeanException("Field '" + field.getName() + "' must not empty in bean " + (null == d ? bean : d)); } }
private String getTranditionalPropertyName(ReflectField field) { String name = null; if(field.hasSetter()){ name = field.getSetter().getName().substring(SETTER_PREFIX.length()); }else if(field.hasGetter()){ name = field.getGetter().getName(); if(name.startsWith(IS_PREFIX)){ name = name.substring(IS_PREFIX.length()); }else{ name = name.substring(GETTER_PREFIX.length()); } }else{ name = field.getName().startsWith("_") ? field.getName().substring(1) : field.getName(); } return name.length() == 1 ? name.toLowerCase() : Character.toLowerCase(name.charAt(0)) + name.substring(1); }
if(field.isStatic()){ continue; if(field.isPublic() || field.hasGetter() || field.hasSetter()){ prop.setType(field.getType()); prop.setGenericType(field.getGenericType()); prop.setTypeInfo(Types.getTypeInfo(field.getType(), field.getGenericType())); prop.setDeclaringClass(field.getDeclaringClass()); prop.setGetter(field.getGetter()); prop.setReadable(field.isPublicGet()); prop.setSetter(field.getSetter()); prop.setWritable(field.isPublicSet() || (field.isPublic() && !field.isFinal())); prop.setTransient(field.isTransient()); prop.setAnnotations(field.getReflectedField().getAnnotations());
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(); }
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()); throw e;
@Override public Object getValue(ElEvalContext context, Object instance) throws Throwable { return field.getValue(instance); }
public void setValue(Object instance, Object value) { setValue(instance,value,false); }
public static ElConstantField of(ReflectField field) { if(!field.isFinal()){ return null; } Class<?> cls = field.getDeclaringClass(); String name = field.getName(); synchronized (lock) { Map<String, ElConstantField> cache = constants.get(cls); if(null == cache){ cache = new WeakHashMap<String, ElConstantField>(); constants.put(cls, cache); } ElConstantField f = cache.get(name); if(null == f){ f = new ElConstantField(field); cache.put(name, f); } return f; } }
public void setValue(Object instance, Object value, boolean useSetter) { try { if(field.isSynthetic() || isFinal()){ field.set(instance, safeValue(value)); }else{ if(useSetter && null != setter){ if(setterAccessorIndex != -1){ accessor.invokeMethod(instance, setterAccessorIndex, safeValue(value)); }else{ setter.invoke(instance, safeValue(value)); } }else{ if(fieldAccessorIndex != -1){ accessor.setFieldValue(instance, fieldAccessorIndex, safeValue(value)); }else{ field.set(instance, safeValue(value)); } } } } catch (Exception e) { throw new ReflectException(Strings.format("Error setting value '{0}' to field '{1}'",value,getName()),e); } }
@Override public ElProperty resolveProperty(Class<?> cls, String name) { ReflectClass rc = ReflectClass.of(cls); ReflectField rf = rc.getField(name); if(null != rf && rf.isPublicGet()){ if(rf.isStatic()){ return rf.isFinal() ? ElConstantField.of(rf) : new ElStaticField(rf); }else{ return new ElInstanceField(rf); } } final ElMethod m = resolveMethod(cls, name, Arrays2.EMPTY_OBJECT_ARRAY); if(null != m) { return new ElProperty() { @Override public Object getValue(ElEvalContext context, Object instance) throws Throwable { return m.invoke(context, instance, Arrays2.EMPTY_OBJECT_ARRAY); } }; } return null; }
if(Property.class.isAssignableFrom(field.getType())) { doBeanConfigure(bean, field, keyPrefix, field.getAnnotation(ConfigProperty.class)); continue; Configurable.Nested nested = field.getAnnotation(Configurable.Nested.class); if(null != nested) { configureNested(bean, keyPrefix, field, nested); ConfigProperty a = field.getAnnotation(ConfigProperty.class); if(null == a) { continue;
private void initialize(){ this.valueField = reflectiveClass.getField(VALUE_FIELD_NAME); this.hasValueField = null != valueField; this.valueType = hasValueField ? valueField.getType() : null; } }
protected ReflectField(ReflectClass reflectiveClass, Field field){ super(reflectiveClass,field); this.field = field; this.type = field.getType(); this.setter = findSetter(); this.getter = findGetter(); this.accessor = reflectiveClass.getAccessor(); if(null != setter){ setter.setSetterOf(this); } if(null != getter){ getter.setGetterOf(this); } this.fieldAccessorIndex = null == accessor ? -1 : accessor.getFieldIndex(field); this.setterAccessorIndex = null == setter || null == accessor ? -1 : accessor.getMethodIndex(setter.getReflectedMethod()); this.getterAccessorIndex = null == getter || null == accessor ? -1 : accessor.getMethodIndex(getter.getReflectedMethod()); this.initialize(); }
public boolean test(ReflectField object) { return object.getName().equals(name); } });
public ElStaticField(ReflectField field) { Args.assertTrue(field.isStatic(),"Must be static field"); this.field = field; }
boolean writable = false; if(bp.isField() && bp.getReflectField().isAnnotationPresent(Managed.class)) { readable = bp.isReadable(); writable = bp.isWritable();
private void createFields(){ List<ReflectField> fieldList = new ArrayList<ReflectField>(); for(Field f : Reflection.getFields(clazz)){ fieldList.add(new ReflectField(this,f)); } this.fields = fieldList.toArray(new ReflectField[fieldList.size()]); this.declaredFields = getDeclaredMembers(fieldList).toArray(new ReflectField[]{}); }
private ReflectMethod findGetter(){ String fieldName = field.getName().startsWith("_") ? field.getName().substring(1) : field.getName(); String nameToFind = "get" + Character.toUpperCase(fieldName.charAt(0)) + (fieldName.length() > 1 ? fieldName.substring(1) : ""); Class<?> fieldType = Primitives.wrap(field.getType()); ReflectMethod m = findGetter(fieldType,nameToFind); if(null == m && Classes.isBoolean(fieldType)){ if(fieldName.startsWith("is") && fieldName.length() > 2){ if(Boolean.class.equals(field.getType())){ nameToFind = "get" + Strings.upperFirst(fieldName.substring(2)); if(null != (m = findGetter(fieldType, nameToFind))){ return m; } } nameToFind = fieldName; }else{ nameToFind = "is" + Character.toUpperCase(fieldName.charAt(0)) + (fieldName.length() > 1 ? fieldName.substring(1) : ""); } m = findGetter(fieldType,nameToFind); } return m; }