static List<BeanArgument> getBeanArguments(BeanDefinition definition) { List<BeanArgument> temp; ConstructorArgumentValues ctorValues = definition.getConstructorArgumentValues(); // get indexed values Map<Integer, ValueHolder> indexedArguments = ctorValues.getIndexedArgumentValues(); // check first the indexed arguments if (!indexedArguments.isEmpty()) { temp = new ArrayList<BeanArgument>(indexedArguments.size()); for (Map.Entry<Integer, ValueHolder> entry : indexedArguments.entrySet()) { temp.add(new SimpleBeanArgument(entry.getKey(), entry.getValue())); } } else { // followed by the generic arguments List<ValueHolder> args = ctorValues.getGenericArgumentValues(); temp = new ArrayList<BeanArgument>(args.size()); for (ValueHolder valueHolder : args) { temp.add(new SimpleBeanArgument(valueHolder)); } } return Collections.unmodifiableList(temp); }
private void tag(Collection<ValueHolder> values, String name, BeanDefinition definition) { boolean trace = log.isTraceEnabled(); for (ValueHolder value : values) { Object val = value.getValue(); if (val instanceof BeanMetadataElement) { if (val instanceof RuntimeBeanReference) { String beanName = ((RuntimeBeanReference) val).getBeanName(); if (trace) { log.trace("Adding (cycle breaking) depends-on on " + name + " to " + beanName); } addSyntheticDependsOn(definition, beanName); } } } }
public void testConstructorArg() throws Exception { AbstractBeanDefinition def = (AbstractBeanDefinition) context.getBeanDefinition("constructor-arg"); assertEquals(Integer.class.getName(), def.getBeanClassName()); assertEquals("description", def.getDescription()); ValueHolder argumentValue = def.getConstructorArgumentValues().getArgumentValue(0, int.class); assertNotNull(argumentValue); }
private boolean checkName(String name, Collection<ValueHolder> values) { for (ValueHolder holder : values) { if (name.equals(holder.getName())) { return true; } } return false; }
private void updatePostProcessor(BeanDefinitionRegistry registry, Set<String> packagesToScan) { BeanDefinition definition = registry.getBeanDefinition(BEAN_NAME); ValueHolder constructorArguments = definition.getConstructorArgumentValues().getGenericArgumentValue(Set.class); @SuppressWarnings("unchecked") Set<String> mergedPackages = (Set<String>) constructorArguments.getValue(); mergedPackages.addAll(packagesToScan); constructorArguments.setValue(mergedPackages); }
private void updatePostProcessor(BeanDefinitionRegistry registry, Set<String> packagesToScan) { BeanDefinition definition = registry.getBeanDefinition(BEAN_NAME); ValueHolder constructorArguments = definition.getConstructorArgumentValues() .getGenericArgumentValue(Set.class); @SuppressWarnings("unchecked") Set<String> mergedPackages = (Set<String>) constructorArguments.getValue(); mergedPackages.addAll(packagesToScan); constructorArguments.setValue(mergedPackages); }
private void updateEntityScanBeanPostProcessor(BeanDefinitionRegistry registry, Set<String> packagesToScan) { BeanDefinition definition = registry.getBeanDefinition(BEAN_NAME); ValueHolder constructorArguments = definition.getConstructorArgumentValues() .getGenericArgumentValue(String[].class); Set<String> mergedPackages = new LinkedHashSet<String>(); mergedPackages.addAll(Arrays.asList((String[]) constructorArguments.getValue())); mergedPackages.addAll(packagesToScan); constructorArguments.setValue(toArray(mergedPackages)); }
/** * Register the processor with a {@link BeanDefinitionRegistry}. Not required when * using the {@link SpringRunner} as registration is automatic. * @param registry the bean definition registry * @param postProcessor the post processor class to register * @param definitions the initial mock/spy definitions */ @SuppressWarnings("unchecked") public static void register(BeanDefinitionRegistry registry, Class<? extends MockitoPostProcessor> postProcessor, Set<Definition> definitions) { SpyPostProcessor.register(registry); BeanDefinition definition = getOrAddBeanDefinition(registry, postProcessor); ValueHolder constructorArg = definition.getConstructorArgumentValues() .getIndexedArgumentValue(0, Set.class); Set<Definition> existing = (Set<Definition>) constructorArg.getValue(); if (definitions != null) { existing.addAll(definitions); } }
private Type findBeanType(AbstractBeanDefinition definition, MethodMetadataReadingVisitor visitor) { Class<?> factory = ClassUtils .resolveClassName(visitor.getDeclaringClassName(), null); List<Class<?>> params = new ArrayList<>(); for (ValueHolder holder : definition.getConstructorArgumentValues() .getIndexedArgumentValues().values()) { params.add(ClassUtils.resolveClassName(holder.getType(), null)); } Method method = ReflectionUtils.findMethod(factory, visitor.getMethodName(), params.toArray(new Class<?>[0])); Type type = method.getGenericReturnType(); return type; }
/** * JAVADOC Method Level Comments */ @Test public void testDoParseElementBeanDefinitionBuilder() { Element element = mock(Element.class); when(element.getAttribute(SingletonBeanFactory.INSTANCE_FACTORY_ID)).thenReturn("exex"); when(element.getAttribute("tokenRepository")).thenReturn("hoho"); when(element.getAttribute("tokenClass")).thenReturn("pipi"); BeanDefinitionBuilder builder = mock(BeanDefinitionBuilder.class); when(builder.addConstructorArgReference("exex")).thenReturn(builder); when(builder.addConstructorArgReference("hoho")).thenReturn(builder); when(builder.addConstructorArgReference("pipi")).thenReturn(builder); AbstractBeanDefinition abd = mock(AbstractBeanDefinition.class); ConstructorArgumentValues cav = mock(ConstructorArgumentValues.class); cav.addGenericArgumentValue(any(ValueHolder.class)); cav.addGenericArgumentValue(any(ValueHolder.class)); cav.addGenericArgumentValue(any(ValueHolder.class)); when(abd.getConstructorArgumentValues()).thenReturn(cav); when(builder.getRawBeanDefinition()).thenReturn(abd); parser.doParse(element, builder); }
private ObjectConfiguration parseBeanDefinition(BeanDefinition beanDefinition) { LOGGER.debug("Resource description : " + beanDefinition.getResourceDescription()); ObjectConfiguration objectConfiguration = objectConfigurationFactory.createObjectConfiguration(); objectConfiguration.setClassName(hashHelper.hashClassName(beanDefinition.getBeanClassName())); objectConfiguration.setSingleton(beanDefinition.isSingleton()); Map<String, ElementConfiguration> fields = new LinkedHashMap<String, ElementConfiguration>(); objectConfiguration.setFields(fields); for (PropertyValue ppt : beanDefinition.getPropertyValues().getPropertyValueList()) { try { ElementConfiguration fieldConfiguration = beanPropertyParser.parseProperty(ppt.getValue()); fields.put(hashHelper.hashFieldName(beanDefinition.getBeanClassName(), ppt.getName()), fieldConfiguration); } catch (Exception e) { LOGGER.error("The value " + ppt.getValue() + " of property " + ppt + " cannot be parsed", e); } } List<ElementConfiguration> constructorArguments = new ArrayList<ElementConfiguration>(); objectConfiguration.setConstructorArguments(constructorArguments); for (ValueHolder va : beanDefinition.getConstructorArgumentValues().getGenericArgumentValues()) { ElementConfiguration constructorAgrument = beanPropertyParser.parseProperty(va.getValue()); constructorArguments.add(constructorAgrument); } return objectConfiguration; }
static Object getValue(ValueHolder valueHolder) { // return (valueHolder.isConverted() ? valueHolder.getConvertedValue() : valueHolder.getValue()); return valueHolder.getValue(); }
public SimpleBeanArgument(ValueHolder valueHolder) { this(UNSPECIFIED_INDEX, valueHolder); }
@Override public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) { if (node instanceof Attr) { Attr attr = (Attr) node; String argName = StringUtils.trimWhitespace(parserContext.getDelegate().getLocalName(attr)); String argValue = StringUtils.trimWhitespace(attr.getValue()); ConstructorArgumentValues cvs = definition.getBeanDefinition().getConstructorArgumentValues(); boolean ref = false; // handle -ref arguments if (argName.endsWith(REF_SUFFIX)) { ref = true; argName = argName.substring(0, argName.length() - REF_SUFFIX.length()); } ValueHolder valueHolder = new ValueHolder(ref ? new RuntimeBeanReference(argValue) : argValue); valueHolder.setSource(parserContext.getReaderContext().extractSource(attr)); // handle "escaped"/"_" arguments if (argName.startsWith(DELIMITER_PREFIX)) { String arg = argName.substring(1).trim(); // fast default check if (!StringUtils.hasText(arg)) { cvs.addGenericArgumentValue(valueHolder); } // assume an index otherwise else { int index = -1; try { index = Integer.parseInt(arg); } catch (NumberFormatException ex) { parserContext.getReaderContext().error( "Constructor argument '" + argName + "' specifies an invalid integer", attr); } if (index < 0) { parserContext.getReaderContext().error( "Constructor argument '" + argName + "' specifies a negative index", attr); } if (cvs.hasIndexedArgumentValue(index)){ parserContext.getReaderContext().error( "Constructor argument '" + argName + "' with index "+ index+" already defined using <constructor-arg>." + " Only one approach may be used per argument.", attr); } cvs.addIndexedArgumentValue(index, valueHolder); } } // no escaping -> ctr name else { String name = Conventions.attributeNameToPropertyName(argName); if (containsArgWithName(name, cvs)){ parserContext.getReaderContext().error( "Constructor argument '" + argName + "' already defined using <constructor-arg>." + " Only one approach may be used per argument.", attr); } valueHolder.setName(Conventions.attributeNameToPropertyName(argName)); cvs.addGenericArgumentValue(valueHolder); } } return definition; }
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) { if (node instanceof Attr) { Attr attr = (Attr) node; String argName = StringUtils.trimWhitespace(parserContext.getDelegate().getLocalName(attr)); String argValue = StringUtils.trimWhitespace(attr.getValue()); ConstructorArgumentValues cvs = definition.getBeanDefinition().getConstructorArgumentValues(); boolean ref = false; // handle -ref arguments if (argName.endsWith(REF_SUFFIX)) { ref = true; argName = argName.substring(0, argName.length() - REF_SUFFIX.length()); } ValueHolder valueHolder = new ValueHolder(ref ? new RuntimeBeanReference(argValue) : argValue); valueHolder.setSource(parserContext.getReaderContext().extractSource(attr)); // handle "escaped"/"_" arguments if (argName.startsWith(DELIMITER_PREFIX)) { String arg = argName.substring(1).trim(); // fast default check if (!StringUtils.hasText(arg)) { cvs.addGenericArgumentValue(valueHolder); } // assume an index otherwise else { int index = -1; try { index = Integer.parseInt(arg); } catch (NumberFormatException ex) { parserContext.getReaderContext().error( "Constructor argument '" + argName + "' specifies an invalid integer", attr); } if (index < 0) { parserContext.getReaderContext().error( "Constructor argument '" + argName + "' specifies a negative index", attr); } if (cvs.hasIndexedArgumentValue(index)){ parserContext.getReaderContext().error( "Constructor argument '" + argName + "' with index "+ index+" already defined using <constructor-arg>." + " Only one approach may be used per argument.", attr); } cvs.addIndexedArgumentValue(index, valueHolder); } } // no escaping -> ctr name else { String name = Conventions.attributeNameToPropertyName(argName); if (containsArgWithName(name, cvs)){ parserContext.getReaderContext().error( "Constructor argument '" + argName + "' already defined using <constructor-arg>." + " Only one approach may be used per argument.", attr); } valueHolder.setName(Conventions.attributeNameToPropertyName(argName)); cvs.addGenericArgumentValue(valueHolder); } } return definition; }
/** * Constructs a new <code>SimpleBeanArgument</code> instance. * * @param index * @param typeName * @param value */ public SimpleBeanArgument(int index, ValueHolder valueHolder) { this.index = index; this.typeName = valueHolder.getType(); this.value = ValueFactory.buildValue(MetadataUtils.getValue(valueHolder)); }