private boolean isDefaultParameter(final Parameter parameter) { if (parameter == ComponentParameter.DEFAULT || parameter == JSR330ComponentParameter.DEFAULT) { return true; } if (parameter instanceof ComponentParameter) { return !((ComponentParameter) parameter).isKeyDefined(); } return false; }
/** * If there is no Parameter defined for the constructor arg, (Other than * Default), then checks to see if there are * {@linkplain javax.inject.Named} annotations or Qualifier annotations * attached to the argument and uses those as ComponentParameters * instead. If not it uses the superclass default behavior. */ @Override protected Parameter getParameterToUse(final Constructor<?> constructorToExamine, final int constructorParameterIndex, final Parameter parameter) { if (isDefaultParameter(parameter)) { // Search for Named class for (Annotation eachAnnotation : constructorToExamine.getParameterAnnotations()[constructorParameterIndex]) { if (eachAnnotation.annotationType().equals(Named.class)) { return new ComponentParameter(((Named) eachAnnotation).value()); } } // Search for a qualifier that isn't @Named annotation. Annotation qualifier = JSR330PicoContainer .getQualifier(constructorToExamine.getParameterAnnotations()[constructorParameterIndex]); if (qualifier != null) { return new ComponentParameter(qualifier.annotationType().getName()); } } return super.getParameterToUse(constructorToExamine, constructorParameterIndex, parameter); }
/** * If there is no Parameter defined for the constructor arg, (Other than * Default), then checks to see if there are * {@linkplain javax.inject.Named} annotations or Qualifier annotations * attached to the argument and uses those as ComponentParameters * instead. If not it uses the superclass default behavior. */ @Override protected Parameter getParameterToUse(final Constructor<?> constructorToExamine, final int constructorParameterIndex, final Parameter parameter) { if (isDefaultParameter(parameter)) { // Search for Named class for (Annotation eachAnnotation : constructorToExamine.getParameterAnnotations()[constructorParameterIndex]) { if (eachAnnotation.annotationType().equals(Named.class)) { return new ComponentParameter(((Named) eachAnnotation).value()); } } // Search for a qualifier that isn't @Named annotation. Annotation qualifier = JSR330PicoContainer .getQualifier(constructorToExamine.getParameterAnnotations()[constructorParameterIndex]); if (qualifier != null) { return new ComponentParameter(qualifier.annotationType().getName()); } } return super.getParameterToUse(constructorToExamine, constructorParameterIndex, parameter); }
"either key or class must be present for dependency"); } else { parameters.add(new ComponentParameter(getClassLoader().loadClass(dependencyClass))); parameters.add(new ComponentParameter(dependencyKey));
private boolean isDefaultParameter(final Parameter parameter) { if (parameter == ComponentParameter.DEFAULT || parameter == JSR330ComponentParameter.DEFAULT) { return true; } if (parameter instanceof ComponentParameter) { return !((ComponentParameter) parameter).isKeyDefined(); } return false; }
private static Parameter getParameter(KeyOrValue keyOrValue) { if (keyOrValue == null) { return NullParameter.INSTANCE; } if (keyOrValue.key == null && keyOrValue.value == null && keyOrValue.type == null) { return DefaultConstructorParameter.INSTANCE; } if (keyOrValue.key == null) { if (keyOrValue.value == null) { throw new PicoJsonException("Either value or key must be set in: " + keyOrValue); } Object value = getValue(keyOrValue.value, keyOrValue.type); if ("class".equals(keyOrValue.type)) { return new ComponentParameter(value); } else return new ConstantParameter(value); } else { if (keyOrValue.value != null) { throw new PicoJsonException("It is not allowed to set both value and key in: " + keyOrValue); } return new ComponentParameter(keyOrValue.key); } }
parameter = new ComponentParameter(key); } else if (keyTypeString != null && !EMPTY.equals(keyTypeString)) { if (emptyCollectionString == null || componentValueTypeString == null || parameter = new ComponentParameter(keyType, Generic.get(componentValueType), emptyCollection); } else if (componentValueTypeString != null && !EMPTY.equals(componentValueTypeString)) { if (emptyCollectionString == null || EMPTY.equals(emptyCollectionString)) { parameter = new ComponentParameter(Generic.get(componentValueType), emptyCollection); } else if (emptyCollectionString != null && !EMPTY.equals(emptyCollectionString)) { boolean emptyCollection = Boolean.valueOf(emptyCollectionString); parameter = new ComponentParameter(emptyCollection); parameter = new ComponentParameter(); } else { Object instance = createInstance(pico, element);
@Test public void testIntegrationWithConverters() { MutablePicoContainer mpc = createPicoContainer(null); if (!(mpc instanceof Converting)) { System.out.println("Skipping 'testIntegrationWithConverters' " + "because pico implementation is not Converting"); return; } mpc.addComponent("converterParameter", "42") .addComponent(ConverterSample.class, ConverterSample.class, new ComponentParameter("converterParameter")); ConverterSample result = mpc.getComponent(ConverterSample.class); assertEquals(42, result.value); }
@Test public void testMultiWithSatisfyingDependencyAndParametersWorks() throws PicoException { MutablePicoContainer pico = createPicoContainer(); pico.addComponent("MultiOneTwo", Multi.class, ComponentParameter.DEFAULT, new ComponentParameter("Two")); pico.addComponent("MultiTwoOne", Multi.class, new ComponentParameter("Two"), ComponentParameter.DEFAULT); pico.addComponent("MultiOneString", Multi.class, ComponentParameter.DEFAULT, new ConstantParameter("")); pico.addComponent("MultiOneInt", Multi.class, ComponentParameter.DEFAULT, new ConstantParameter(5)); pico.addComponent("MultiNone", Multi.class, Parameter.ZERO); pico.addComponent(One.class); pico.addComponent("Two", Two.class); Multi multiOneTwo = (Multi) pico.getComponent("MultiOneTwo"); assertEquals("one two", multiOneTwo.message); Multi multiTwoOne = (Multi) pico.getComponent("MultiTwoOne"); assertEquals("two one", multiTwoOne.message); Multi multiOneString = (Multi) pico.getComponent("MultiOneString"); assertEquals("one string", multiOneString.message); Multi multiOneInt = (Multi) pico.getComponent("MultiOneInt"); assertEquals("one int", multiOneInt.message); Multi multiNone = (Multi) pico.getComponent("MultiNone"); assertEquals("none", multiNone.message); } }