private static Parameter toParameter(final Object obj) { return obj instanceof Parameter ? (Parameter) obj : new ConstantParameter(obj); }
protected boolean isAssignable(final Type expectedType) { if (expectedType instanceof Class) { Class<?> expectedClass = (Class<?>) expectedType; if (checkPrimitive(expectedClass) || expectedClass.isInstance(value)) { return true; } } return false; }
/** * {@inheritDoc} * * @see Parameter#verify(com.picocontainer.PicoContainer,com.picocontainer.ComponentAdapter,java.lang.reflect.Type,com.picocontainer.NameBinding,boolean,java.lang.annotation.Annotation) */ public void verify(final PicoContainer container, final ComponentAdapter<?> adapter, final Type expectedType, final NameBinding expectedNameBinding, final boolean useNames, final Annotation binding) throws PicoException { if (!isAssignable(expectedType)) { throw new PicoCompositionException( expectedType + " is not assignable from " + (value != null ? value.getClass().getName() : "null")); } }
private void addPermissions(final ClassPathElement classPathElement, final Element classPathXmlElement) throws ClassNotFoundException { NodeList children = classPathXmlElement.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { if (children.item(i) instanceof Element) { Element childElement = (Element) children.item(i); String permissionClassName = childElement.getAttribute(CLASSNAME); String action = childElement.getAttribute(CONTEXT); String value = childElement.getAttribute(VALUE); MutablePicoContainer mpc = new DefaultPicoContainer(); mpc.addComponent(Permission.class, Class.forName(permissionClassName), new ConstantParameter(action), new ConstantParameter(value)); Permission permission = mpc.getComponent(Permission.class); classPathElement.grantPermission(permission); } } }
/** * {@inheritDoc} * * @see Parameter#verify(com.picocontainer.PicoContainer,com.picocontainer.ComponentAdapter,java.lang.reflect.Type,com.picocontainer.NameBinding,boolean,java.lang.annotation.Annotation) */ public void verify(final PicoContainer container, final ComponentAdapter<?> adapter, final Type expectedType, final NameBinding expectedNameBinding, final boolean useNames, final Annotation binding) throws PicoException { if (!isAssignable(expectedType)) { throw new PicoCompositionException( expectedType + " is not assignable from " + (value != null ? value.getClass().getName() : "null")); } }
protected boolean isAssignable(final Type expectedType) { if (expectedType instanceof Class) { Class<?> expectedClass = (Class<?>) expectedType; if (checkPrimitive(expectedClass) || expectedClass.isInstance(value)) { return true; } } return false; }
private Touchable getTouchableFromSerializedContainer() throws IOException, ClassNotFoundException { MutablePicoContainer pico = createPicoContainerWithTouchableAndDependsOnTouchable(); // Add a list too, using a constant parameter pico.addComponent("list", ArrayList.class, new ConstantParameter(10)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(pico); ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())); pico = (MutablePicoContainer)ois.readObject(); DependsOnTouchable dependsOnTouchable = pico.getComponent(DependsOnTouchable.class); assertNotNull(dependsOnTouchable); return pico.getComponent(Touchable.class); }
public Resolver resolve(final PicoContainer container, final ComponentAdapter<?> forAdapter, final ComponentAdapter<?> injecteeAdapter, final Type expectedType, final NameBinding expectedNameBinding, final boolean useNames, final Annotation binding) { if (expectedType instanceof Class) { return new Parameter.ValueResolver(isAssignable(expectedType), value, null); } else if (expectedType instanceof ParameterizedType) { return new Parameter.ValueResolver(isAssignable(((ParameterizedType)expectedType).getRawType()), value, null); } return new Parameter.ValueResolver(true, value, null); }
private ContainerBuilder createContainerBuilder(Reader reader) { ClassLoadingPicoContainer scripted = new DefaultClassLoadingPicoContainer(getClassLoader()); Parameter[] parameters = new Parameter[] { new ConstantParameter(reader), new ConstantParameter(getClassLoader()) }; scripted.addComponent(containerBuilderClassName, new ClassName(containerBuilderClassName), parameters); return scripted.getComponent(ContainerBuilder.class); }
public Resolver resolve(final PicoContainer container, final ComponentAdapter<?> forAdapter, final ComponentAdapter<?> injecteeAdapter, final Type expectedType, final NameBinding expectedNameBinding, final boolean useNames, final Annotation binding) { if (expectedType instanceof Class) { return new Parameter.ValueResolver(isAssignable(expectedType), value, null); } else if (expectedType instanceof ParameterizedType) { return new Parameter.ValueResolver(isAssignable(((ParameterizedType)expectedType).getRawType()), value, null); } return new Parameter.ValueResolver(true, value, null); }
private ContainerBuilder createContainerBuilder(Reader reader) { ClassLoadingPicoContainer scripted = new DefaultClassLoadingPicoContainer(getClassLoader()); Parameter[] parameters = new Parameter[] { new ConstantParameter(reader), new ConstantParameter(getClassLoader()) }; scripted.addComponent(containerBuilderClassName, new ClassName(containerBuilderClassName), parameters); return scripted.getComponent(ContainerBuilder.class); }
@Test public void testPico52() { MutablePicoContainer pico = createPicoContainer(null); pico.addComponent("foo", JMSService.class, new ConstantParameter("0"), new ConstantParameter("something")); JMSService jms = (JMSService)pico.getComponent("foo"); assertEquals("0", jms.serverid); assertEquals("something", jms.path); }
throw new ScriptedPicoContainerMarkupException("could not parse constant parameter"); parameters.add(new ConstantParameter(parseResult)); } else if (DEPENDENCY.equals(name)) {
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); } }
final protected PicoContainer wrapComponentInstances( final Class decoratingComponentAdapterClass, final PicoContainer picoContainer, final Object[] wrapperDependencies) { assertTrue(AbstractBehavior.AbstractChangedBehavior.class.isAssignableFrom(decoratingComponentAdapterClass)); final MutablePicoContainer mutablePicoContainer = new DefaultPicoContainer(); final int size = (wrapperDependencies != null ? wrapperDependencies.length : 0) + 1; final Collection allComponentAdapters = picoContainer.getComponentAdapters(); for (Object allComponentAdapter : allComponentAdapters) { final Parameter[] parameters = new Parameter[size]; parameters[0] = new ConstantParameter(allComponentAdapter); for (int i = 1; i < parameters.length; i++) { parameters[i] = new ConstantParameter(wrapperDependencies[i - 1]); } final MutablePicoContainer instantiatingPicoContainer = new DefaultPicoContainer( new ConstructorInjection()); instantiatingPicoContainer.addComponent( "decorator", decoratingComponentAdapterClass, parameters); mutablePicoContainer.addAdapter((ComponentAdapter)instantiatingPicoContainer .getComponent("decorator")); } return mutablePicoContainer; }
} else { Object instance = createInstance(pico, element); parameter = new ConstantParameter(instance);
@Test public void testNullValuesDoNotInject() { MutablePicoContainer pico = createPicoContainer(null); pico.addComponent(ConstantParameterTestService.class, ConstantParameterTestService.class, new ConstantParameter(null)); try { ConstantParameterTestService service = pico.getComponent(ConstantParameterTestService.class); fail("Should have thrown unsatisfiable dependencies exception. Instead got " + service + " as a return value"); } catch (UnsatisfiableDependenciesException e) { assertNotNull(e.getMessage()); } }
.getComponentAdapter(ArrayList.class, (NameBinding) null); Parameter componentParameter = BasicComponentParameter.BASIC_DEFAULT; Parameter throwableParameter = new ConstantParameter(new Throwable("bar")); ConstructorInjection.ConstructorInjector<Exception> ci = new ConstructorInjection.ConstructorInjector<Exception>(Exception.class, Exception.class, componentParameter, throwableParameter);
@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); } }