/** * @param element */ public CyclicDependencyException(final Class<?> element) { super((Throwable)null); this.stack = new LinkedList<Class>(); push(element); }
/** * Call the observing function. The provided guard will hold the {@link Boolean} value. * If the guard is already <code>Boolean.TRUE</code> a {@link CyclicDependencyException} * will be thrown. * * @param stackFrame the current stack frame * @return the result of the <code>run</code> method */ public final T observe(final Class<?> stackFrame, final Object instance) { if (Boolean.TRUE.equals(get())) { throw new CyclicDependencyException(stackFrame); } T result = null; try { set(Boolean.TRUE); result = run(instance); } catch (final CyclicDependencyException e) { e.push(stackFrame); throw e; } finally { set(Boolean.FALSE); } return result; }
final @Test public void testRES_failingInstantiationWithCyclicDependencyException() { if ((getComponentAdapterNature() & RESOLVING) > 0) { final Set cycleInstances = new HashSet(); final ObjectReference cycleCheck = new SimpleReference(); final Object[] wrapperDependencies = new Object[]{cycleInstances, cycleCheck}; final MutablePicoContainer picoContainer = new DefaultPicoContainer(createDefaultComponentFactory()); final ComponentAdapter componentAdapter = prepRES_failingInstantiationWithCyclicDependencyException(picoContainer); assertSame(getComponentAdapterType(), componentAdapter.getClass()); assertTrue(picoContainer.getComponentAdapters().contains(componentAdapter)); final PicoContainer wrappedPicoContainer = wrapComponentInstances( CycleDetectorChangedBehavior.class, picoContainer, wrapperDependencies); try { componentAdapter.getComponentInstance(wrappedPicoContainer, ComponentAdapter.NOTHING.class); fail("Thrown CyclicDependencyException excpected"); } catch (final AbstractInjector.CyclicDependencyException e) { final Class[] dependencies = e.getDependencies(); assertSame(dependencies[0], dependencies[dependencies.length - 1]); } } }
/** * Call the observing function. The provided guard will hold the {@link Boolean} value. * If the guard is already <code>Boolean.TRUE</code> a {@link CyclicDependencyException} * will be thrown. * * @param stackFrame the current stack frame * @return the result of the <code>run</code> method */ public final T observe(final Class<?> stackFrame, final Object instance) { if (Boolean.TRUE.equals(get())) { throw new CyclicDependencyException(stackFrame); } T result = null; try { set(Boolean.TRUE); result = run(instance); } catch (final CyclicDependencyException e) { e.push(stackFrame); throw e; } finally { set(Boolean.FALSE); } return result; }
final @Test public void testRES_failingVerificationWithCyclicDependencyException() { if ((getComponentAdapterNature() & RESOLVING) > 0) { final Set cycleInstances = new HashSet(); final ObjectReference cycleCheck = new SimpleReference(); final Object[] wrapperDependencies = new Object[]{cycleInstances, cycleCheck}; final MutablePicoContainer picoContainer = new DefaultPicoContainer(createDefaultComponentFactory()); final ComponentAdapter componentAdapter = prepRES_failingVerificationWithCyclicDependencyException(picoContainer); assertSame(getComponentAdapterType(), componentAdapter.getClass()); assertTrue(picoContainer.getComponentAdapters().contains(componentAdapter)); final PicoContainer wrappedPicoContainer = wrapComponentInstances( CycleDetectorChangedBehavior.class, picoContainer, wrapperDependencies); try { componentAdapter.verify(wrappedPicoContainer); fail("Thrown PicoVerificationException excpected"); } catch (final AbstractInjector.CyclicDependencyException cycle) { final Class[] dependencies = cycle.getDependencies(); assertSame(dependencies[0], dependencies[dependencies.length - 1]); } } }
private static void assertCyclicDependencyThrowsCyclicDependencyException(final MutablePicoContainer pico) { pico.addComponent(ComponentB.class); pico.addComponent(ComponentD.class); pico.addComponent(ComponentE.class); try { pico.getComponent(ComponentD.class); fail("CyclicDependencyException expected"); } catch (AbstractInjector.CyclicDependencyException e) { // CyclicDependencyException reports now the stack. //final List dependencies = Arrays.asList(ComponentD.class.getConstructors()[0].getParameterTypes()); final List<Class> dependencies = Arrays.<Class>asList(ComponentD.class, ComponentE.class, ComponentD.class); final List<Class> reportedDependencies = Arrays.asList(e.getDependencies()); assertEquals(dependencies, reportedDependencies); } catch (StackOverflowError e) { fail(); } }
/** * @param element */ public CyclicDependencyException(final Class<?> element) { super((Throwable)null); this.stack = new LinkedList<Class>(); push(element); }