private ReflectMethod findGetter(Class<?> fieldType,String nameToFind){ for(ReflectMethod rm : reflectClass.getMethods()){ Method m = rm.getReflectedMethod(); if(m.getParameterTypes().length == 0 && Primitives.wrap(m.getReturnType()).isAssignableFrom(fieldType)){ if(m.getName().equals(nameToFind)){ return rm; } } } return null; }
/** * create a new instance of the given class. */ public static <T> T newInstance(Class<T> clazz){ return ReflectClass.of(clazz).newInstance(); }
@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; }
ReflectClass rc = ReflectClass.of(beanClass); for(ReflectConstructor c : rc.getConstructors()) { if(c.getReflectedConstructor().isAnnotationPresent(DefaultConstructor.class)) { dc = c; if(rc.hasDefaultConstructor()) { dc = rc.getDefaultConstructor(); }else if(rc.getConstructors().length == 1) { dc = rc.getConstructors()[0];
ReflectMethod setByStringMethod = bt.getReflectClass().getMethod(setByStringMethodName, new Class<?>[]{String.class}); for(ReflectField field : bt.getReflectClass().getFields()) { if(done.contains(field)) { continue; for(ReflectMethod m : bt.getReflectClass().getMethods()) { if(m.isSetterMethod() || m.isGetterMethod()) { continue;
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); }
for(ReflectField field : reflectClass.getFields()){ for(ReflectMethod rm : reflectClass.getMethods()){ Method m = rm.getReflectedMethod();
protected Object newInstance(ConvertContext context, BeanType bt, Map<String, Object> map){ ReflectClass cls = bt.getReflectClass(); ConvertContext.ConcreteTypes types = null == context ? null : context.getConcreteTypes(); if(null != types) { Object instance = types.newInstance(context, bt.getBeanClass(), null, map); if(null != instance) { return instance; } } if(cls.isAbstract() || cls.isInterface()) { throw new ConvertException("Cannot new instance for abstract class or interface '" + bt.getBeanClass().getName() + "'"); }else if(cls.hasDefaultConstructor()){ return bt.newInstance(); }else{ ReflectConstructor c = cls.getConstructors()[0]; ReflectParameter[] ps = c.getParameters(); Object[] args = new Object[c.getParameters().length]; for(int i=0;i<args.length;i++){ ReflectParameter p = ps[i]; args[i] = Converts.convert(map.get(p.getName()), p.getType(), p.getGenericType(), context); } return c.newInstance(args); } }
protected ReflectEnum(Class<?> enumClass){ this.reflectiveClass = ReflectClass.of(enumClass); this.initialize(); }
@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; }
for (ReflectField rf : bt.getReflectClass().getFields()) {
public ReflectMethod getMethod(String name,Class<?> returnType,Class<?>... argumentTypes){ ReflectMethod m = getMethod(name, argumentTypes); return null == m ? null : (m.getReflectedMethod().getReturnType().equals(returnType) ? m : null); }
public static ReflectClass of(Class<?> clazz) { ReflectClass rclass = cache.get(clazz); if(null == rclass){ rclass = new ReflectClass(clazz); cache.put(clazz, rclass); } return (ReflectClass)rclass; }
ReflectClass rc = ReflectClass.of(cls); for(ReflectMethod m : rc.getMethods()){ if(m.isPublic() && m.isStatic()) {
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; }
/** * 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; }
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); }
ReflectClass rc = ReflectClass.of(bean.getClass()); for(ReflectMethod m : rc.getMethods()) { if(m.getParameters().length == 0 && m.isAnnotationPresent(Destroy.class)) { try {