public ElInstanceMethod(Method m,Object instance) { this.m = ReflectClass.of(m.getDeclaringClass()).getMethod(m); this.size = m.isVarArgs() ? VAR_ARGS : m.getParameterTypes().length; this.instance = instance; }
/** * create a new instance of the given class. */ public static <T> T newInstance(Class<T> clazz){ return ReflectClass.of(clazz).newInstance(); }
protected ReflectEnum(Class<?> enumClass){ this.reflectiveClass = ReflectClass.of(enumClass); this.initialize(); }
protected void validateFields(BeanDefinition d, Object bean) { ReflectClass cls = ReflectClass.of(bean.getClass()); for(ReflectField field : cls.getFields()){ validateField(d, bean, field); } }
protected Validator createConstraintValidator(Annotation constraint, Class<?> valueType, ValidatedBy constraintAnnotation){ Class<? extends Validator> validatorClass = constraintAnnotation.validator(); ReflectClass rc = ReflectClass.of(validatorClass); ReflectConstructor constructor = rc.getConstructor(constraint.annotationType(),Class.class); if(null == constructor){ throw new IllegalStateException("Validator class '" + validatorClass.getName() + "' must define constructor (" + constraint.annotationType().getName() + ", java.lang.Class)"); } return constructor.newInstance(constraint,valueType); }
protected BeanType(Class<?> beanClass){ this.attributes = Collections.synchronizedMap(new WeakHashMap<Object, Object>(1)); this.beanClass = beanClass; this.reflectClass = ReflectClass.of(beanClass); this.properties = initProperties(); this.originalNamePropertyMap = createPropertyMap(false); this.lowerCaseNamePropertyMap = createPropertyMap(true); }
/** * Parse all the items to the given type using {@link JsonParsable#parseJson(JsonValue)}. */ public <T extends JsonParsable> List<T> parseList(Class<T> type) { List<T> newList = new ArrayList<>(); ReflectClass rc = ReflectClass.of(type); for(Object item : this.list) { T obj = rc.newInstance(); obj.parseJson(JsonValue.of(item)); newList.add(obj); } return newList; }
@Override public ElMethod resolveMethod(Class<?> cls, String name, Object[] args) { ReflectClass rc = ReflectClass.of(cls); int len = args.length; for(ReflectMethod rm : rc.getMethods()){ if(rm.getName().equals(name) && rm.getParameters().length == len){ boolean ok = true; for(int i=0;i<len;i++){ Object v = args[i]; if(null != v && !rm.getParameters()[i].getType().isAssignableFrom(v.getClass())){ ok = false; break; } } if(ok){ return rm.isStatic() ? new ElStaticMethod(rm) : new ElInstanceMethod(rm); } } } return null; }
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(); }
ReflectClass rc = ReflectClass.of(cls);
ReflectClass rc = ReflectClass.of(bean.getClass()); for(ReflectMethod m : rc.getMethods()) { if(m.getParameters().length == 0 && m.isAnnotationPresent(Destroy.class)) {
String v = (String)ReflectClass.of(a.getClass()).getMethod(nameAnnotation.value()).invoke(a); if(!v.isEmpty()) { name = v;
@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 Object doCreateProxy(BeanDefinitionBase pd, Class<?> type, Object bean){ Object proxy; if(ProxyBean.class.isAssignableFrom(pd.getBeanClass())) { proxy = doCreateBeanOnly(pd); ((ProxyBean)proxy).setTargetBean(bean); }else{ ReflectConstructor c = ReflectClass.of(pd.getBeanClass()).getConstructor(type); if(null == c) { throw new BeanCreationException("Can't create proxy '" + pd.getBeanClass() + "', No valid constructor"); } proxy = c.newInstance(bean); beforeBeanCreation(pd); processBeanCreation(pd, proxy); afterBeanCreation(pd); pd.setInited(true); } return proxy; }
@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; }
ReflectClass rc = ReflectClass.of(bean.getClass()); for (ReflectMethod m : rc.getMethods()) { if (m.getParameters().length == 0 && m.isAnnotationPresent(Init.class)) {
ReflectClass c = ReflectClass.of(type); for(ReflectMethod m : c.getMethods()){
ReflectClass rc = ReflectClass.of(beanClass);
if (null == ReflectClass.of(bean.getBeanClass()).getConstructor(bean.getType())) { throw new BeanDefinitionException("Bean proxy class'" + bean.getBeanClass() + "' must be sub-class of '" + ProxyBean.class +