public ReflectConstructor getConstructor(Class<?>... parameterTypes) { if(null == parameterTypes || parameterTypes.length == 0){ return defaultConstructor; } for(ReflectConstructor c : constructors) { Constructor<?> jc = c.getReflectedConstructor(); if(jc.getParameterTypes().length == parameterTypes.length){ boolean match = true; for(int i=0;i<parameterTypes.length;i++){ if(!jc.getParameterTypes()[i].equals(parameterTypes[i])){ match = false; break; } } if(match){ return c; } } } return null; }
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); }
private void createConstructors(){ //new an empty ArrayList. List<ReflectConstructor> constructorList = new ArrayList<ReflectConstructor>(); //iterate all declared constructors. Constructor<?>[] constructors = clazz.getDeclaredConstructors(); for(int i=0;i<constructors.length;i++) { Constructor<?> c = constructors[i]; if(!c.isSynthetic()){ ReflectConstructor rc = new ReflectConstructor(this,c); constructorList.add(rc); if(innerClass && !Modifier.isStatic(clazz.getModifiers())){ if(c.getParameterTypes().length == 1 && c.getParameterTypes()[0].equals(clazz.getEnclosingClass())){ defaultConstructor = rc; defaultConstructorInner = true; } }else if(c.getParameterTypes().length == 0){ defaultConstructor = rc; } } } this.constructors = constructorList.toArray(new ReflectConstructor[constructorList.size()]); }
if(c.getReflectedConstructor().isAnnotationPresent(DefaultConstructor.class)) { dc = c; break; Object[] args = new Object[dc.getParameters().length]; ReflectParameter p = dc.getParameters()[i]; return dc.newInstance(args); }else{ throw new BeanCreationException("Cannot create bean without default constructor, check the bean : " + bd);
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); } }
private void initialize(){ this.setAccessible(); names = createUnknownParameterNames(parameters.length);
protected ReflectConstructor(ReflectClass reflectiveClass, Constructor<?> javaConstructor){ super(reflectiveClass,javaConstructor); this.reflectedConstructor = javaConstructor; this.initialize(); }
@SuppressWarnings("unchecked") public <T> T newInstance(Object... args){ try { return (T)reflectedConstructor.newInstance(args); } catch (Exception e) { StringBuilder s = new StringBuilder(); for(int i=0;i<parameters.length;i++){ if(i>0){ s.append(','); } s.append(parameters[i].getName()); } throw new ReflectException(Strings.format( "Error newInstance in constructor '{0}({1})'", getDeclaringClass().getSimpleName(), s.toString()),e); } }
@SuppressWarnings("unchecked") public <T> T newInstance() throws ReflectException{ if(null == defaultConstructor){ throw new ReflectException(Strings.format("there is no default constructor available in class '{0}'",getName())); } if(defaultConstructorInner){ return (T)defaultConstructor.newInstance(Reflection.newInstance(clazz.getEnclosingClass())); }else{ if(null != accessor && accessor.canNewInstance()){ return (T)accessor.newInstance(); }else{ return (T)defaultConstructor.newInstance((Object[])null); } } }
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; }