protected Class<?> resolveBeanType(Class<?> cls, Bean a) { if(!a.type().equals(void.class)){ if(a.type().isAssignableFrom(cls)){ throw new BeanDefinitionException(a.type() + " is not assignable from " + cls.getName() + ", source:"+cls.getName()); } return a.type(); }else if(cls.getInterfaces().length == 1){ return cls.getInterfaces()[0]; } return cls; }
protected void readBeanFactoryDef(BeanContainer container, XmlReader reader, LoaderContext context, BeanDefinitionBase bean) { if (!(FactoryBean.class.isAssignableFrom(bean.getBeanClass()))) { throw new BeanDefinitionException("Bean [" + bean + "] must be implements FactoryBean"); } String targetTypeName = reader.getRequiredAttribute(TARGET_TYPE_ATTRIBUTE); if (Strings.isEmpty(targetTypeName)) { throw new BeanDefinitionException("Attribute '" + TARGET_TYPE_ATTRIBUTE + "' of element '" + REGISTER_BEAN_FACTORY_ELEMENT + "' must not be empty, source : " + reader.getSource()); } Class<?> targetType = tryForName(targetTypeName); if (null == targetType) { throw new BeanDefinitionException("Target type '" + targetTypeName + "' not found, source : " + reader.getSource()); } bean.addFactoryBeanDef(new FactoryBeanDefinitionBase(targetType)); }
@SuppressWarnings("unchecked") protected Collection<Object> createCollection(BeanDefinitionBase bd, ValueDefinition vd,Collection<Object> col){ try { Class<?> definedType = vd.getDefinedType(); if(definedType != null){ if(definedType.isInterface() || Modifier.isAbstract(definedType.getModifiers())){ boolean isAssignableFrom = definedType.isAssignableFrom(vd.getDefinedValue().getClass()); if(!isAssignableFrom){ throw new BeanDefinitionException("Error create instance of collection type '" + vd.getDefinedType() + "' bean " + bd + " cause "+definedType+" is interface or abstract class and the value type " + vd.getDefinedValue().getClass().getName() + " is not the subclass of "+definedType); }else{ definedType = vd.getDefinedValue().getClass(); } } return (Collection<Object>)Reflection.newInstance(definedType); } } catch (ReflectException e) { throw new BeanDefinitionException("Error create instance of collection type '" + vd.getDefinedType() + "' bean " + bd, e); } if(col instanceof List){ return new ArrayList<Object>(); }else if(col instanceof Set){ return new ArrayList<Object>(); }else{ throw new BeanDefinitionException("Invalid collection type '" + col.getClass().getName() + "', only 'list' and 'set' are supported, please check the bean " + bd); } }
throw new BeanDefinitionException("The bean list's element must be instance of '" + type.getName() + "' in '" + bld.getSource() + "'");
if(!Strings.isEmpty(bd.getName())){ if(!bd.isSingleton()){ throw new BeanDefinitionException("the bean '" + bd.getName() + "' must be singleton, cannot cache the named beans for type '" + type.getName() + "'");
protected void addAliasDefinition(AliasDefinition ad){ if(!Strings.isEmpty(ad.getId())){ AliasDefinition existsAliasDefinition = bds.aliasDefinitions.get(ad.getAlias()); if(null != existsAliasDefinition){ throw new BeanDefinitionException("Found duplicated bean alias '" + ad.getAlias() + "' in resource : " + ad.getSource() + ", " + ad.getSource()); } bds.aliasDefinitions.put(ad.getAlias(),ad); }else{ String key = ad.getType().getName() + "$" + ad.getAlias(); AliasDefinition existsAliasDefinition = bds.aliasDefinitions.get(key); if(null != existsAliasDefinition){ throw new BeanDefinitionException("Found duplicated bean alias '" + key + "' in resource : " + ad.getSource() + ", " + ad.getSource()); } bds.aliasDefinitions.put(key,ad); } }
protected Object doGetBeanReferenceInstance(BeanReference br){ BeanDefinitionBase referenced = findBeanDefinition(br); if(referenced == null){ throw new BeanDefinitionException("The referenced bean not exists : " + br); } br.setTargetBeanDefinition(referenced); Object bean = doGetBean(referenced); if(null == bean) { throw new BeanCreationException("The referenced bean '" + referenced + "' not exists!"); } return bean; }
throw new BeanDefinitionException("the init class name '" + initClassName + "' not found, source : " + init.getSource()); throw new BeanDefinitionException("the init method '" + initMethodName + "' with no parameters not found in class '" + clazz.getName() + "', source : " + init.getSource()); throw new BeanDefinitionException("the init method '" + initMethodName + "' in class '" + clazz.getName() + "' must be static, source : " + init.getSource());
protected Class<?> javaTypeAttribute(XmlReader reader, String name) { String className = reader.getAttribute(name); if (Strings.isEmpty(className)) { return null; } try { return Classes.forName(className); } catch (ObjectNotFoundException e) { throw new BeanDefinitionException("invalid java type '" + className + "', must be a fully qualified class name, source : " + reader.getSource()); } }
@Override public <T> void addBean(String id, boolean lazyInit, Class<? extends T> beanClass, Object... constructorArgs) throws BeanException { Args.notNull(beanClass); Args.notEmpty(id); if(tryGetBean(id) != null){ throw new BeanDefinitionException("the to be added bean '" + id + "' aleady exists"); } this.beanContainer.addBean(id, lazyInit, beanClass, constructorArgs); }
protected void addBeanList(BeanListDefinition bld){ String key = bld.getType().getName(); if(!Strings.isEmpty(bld.getQualifier())){ key = key + "$" + bld.getQualifier(); } if(!bld.isOverride()){ BeanListDefinition exists = beanListDefinitions.get(key); if(null != exists){ throw new BeanDefinitionException("Found duplicated bean list of type '" + bld.getType().getName() + "', qualifier '" + bld.getQualifier() + "' in " + exists.getSource() + ", " + bld.getSource()); } } beanListDefinitions.put(key, bld); }
@Override public <T> void addBean(Class<? super T> beanType, boolean primary, String name, boolean lazyInit, Class<T> beanClass, Object... constructorArgs) throws BeanException { Args.notNull(beanClass); Args.notNull(beanType); Args.notEmpty(name); /* if(primary && tryGetBean(beanType) != null){ throw new BeanDefinitionException("primary bean for type '" + beanType.getName() + "' aleady exists"); } */ if(tryGetBean(beanType, name) != null){ throw new BeanDefinitionException("the bean '" + name + "' for type '" + beanType.getName() + "' aleady exists"); } this.beanContainer.addBean(beanType, primary, name, lazyInit, beanClass, constructorArgs); }
protected ValueDefinition readRefElement(LoaderContext context, XmlReader reader) { String refId = reader.resolveAttribute(ID_ATTRIBUTE); String refType = reader.resolveAttribute(TYPE_ATTRIBUTE); String refName = reader.resolveAttribute(NAME_ATTRIBUTE); if (Strings.isAllEmpty(refId, refType, refName)) { throw new BeanDefinitionException("Invalid bean reference in '" + reader.getCurrentLocation() + "'"); } if (Strings.isNotEmpty(refName) && Strings.isEmpty(refType)) { throw new BeanDefinitionException("The referenced type must be exists in '" + reader.getCurrentLocation() + "'"); } return tryCreateBeanReference(context, reader, refId, refType, refName); }
protected void parseAdditionalTypeDef(BeanDefinitionBase bd,Class<?> cls,Bean a){ if(a.additionalTypeDef() != null && a.additionalTypeDef().length > 0){ typeEach: for(Class<?> additional : a.additionalTypeDef()){ for(TypeDefinition def : bd.getAdditionalTypeDefs()){ if(def.getType() == additional){ continue typeEach; } } if(!additional.isAssignableFrom(cls)){ throw new BeanDefinitionException(additional.getName() + " is not assignable from " + cls.getName()); } TypeDefinitionBase def = new TypeDefinitionBase(); def.setType(additional); bd.addAdditionalTypeDef(def); } } }
boolean isAssignableFrom = clzz.isAssignableFrom(vd.getDefinedValue().getClass()); if(!isAssignableFrom){ throw new BeanDefinitionException("Error create instance of collection type '" + vd.getDefinedType() + "' bean " + bd + " cause "+clzz+" is interface or abstract class and the value type " + vd.getDefinedValue().getClass().getName() + " is not the subclass of "+clzz);
throw new BeanDefinitionException("No matched method '" + methodName + "' with " + md.getArguments().size() + " arguments in '" + bd + "', source : " + bd.getSource());
protected ValueDefinition tryCreateBeanReference(LoaderContext context, XmlReader reader, String refId, String refType, String refName) { if (!Strings.isEmpty(refId)) { return beanReference(context, refId); } if (!Strings.isAllEmpty(refType, refName)) { if (Strings.isEmpty(refType)) { throw new BeanDefinitionException("'The referenced bean type must not be empty, check :" + reader.getCurrentLocation()); } try { Class<?> c = forName(refType); return beanReference(context, c, refName); } catch (NestedClassNotFoundException e) { throw new BeanDefinitionException("Invalid class name '" + refType + "', source : " + reader.getSource(), e); } } return null; }
protected ValueDefinition readProps(BeanContainer container, XmlReader reader, LoaderContext context) { boolean merge = boolAttribute(reader, MERGE_ATTRIBUTE, false); Properties props = new Properties(); while (reader.nextWhileNotEnd(PROPS_ELEMENT)) { if (reader.isStartElement()) { if (reader.isStartElement(PROP_ELEMENT)) { props.put(reader.getRequiredAttribute(KEY_ATTRIBUTE), reader.getElementTextAndEnd()); } else { throw new BeanDefinitionException("unsupported child element '" + reader.getElementLocalName() + "' in 'props' element, source : " + reader.getSource()); } } } return new ValueDefinition(props, merge); }
protected void readFactoryDefinition(BeanContainer container, XmlReader reader, LoaderContext context, BeanDefinitionBase bd) { FactoryDefinition fd = new FactoryDefinition(); String className = reader.getRequiredAttribute(CLASS_ATTRIBUTE); if(!Strings.isEmpty(className)) { Class<?> cls = tryForName(className); if(null == cls) { throw new BeanDefinitionException("Invalid factory class name '" + className + "' in " + bd); } readMethodDefinition(reader, context, bd, cls, fd, false); } bd.setFactoryDefinition(fd); }
protected Class<?> classAttribute(XmlReader reader, String name, boolean required) { String value = required ? reader.getRequiredAttribute(name) : reader.getAttribute(name); if (Strings.isEmpty(value)) { return null; } Class<?> clazz = tryForName(value); if (null == clazz) { throw new BeanDefinitionException("invalid class name '" + value + "' in source : " + reader.getSource() + ", line number : " + reader.getLineNumber()); } return clazz; }