/** * @param scopeAnnotationClass an annotation that should be qualified by {@link javax.inject.Scope}. If not, * an exception is thrown. * @return the parent {@link ScopeNode} of this scope that is bound to {@code scopeAnnotationClass}. * The current {@code scope} (this) can be returned if it is bound to {@code scopeAnnotationClass}. * If no such parent exists, it throws an exception. This later case means that something scoped * is using a lower scoped dependency, which is conceptually flawed and not allowed in Toothpick. * Note that is {@code scopeAnnotationClass} is {@link Singleton}, the root scope is always returned. * Thus the {@link Singleton} scope annotation class doesn't need to be bound, it's built-in. */ @SuppressWarnings({ "unused", "used by generated code" }) @Override public ScopeNode getParentScope(Class scopeAnnotationClass) { checkIsAnnotationScope(scopeAnnotationClass); if (scopeAnnotationClass == Singleton.class) { return getRootScope(); } ScopeNode currentScope = this; while (currentScope != null) { if (currentScope.isBoundToScopeAnnotation(scopeAnnotationClass)) { return currentScope; } currentScope = currentScope.getParentScope(); } throw new IllegalStateException(format("There is no parent scope of %s bound to scope scopeAnnotationClass %s", // this.name, // scopeAnnotationClass.getName())); }
@Test public void testReset_shouldRebindScopeAnnotation() throws Exception { //GIVEN ScopeNode scope = new ScopeImpl(CustomScope.class); //WHEN scope.reset(); //THEN assertThat(scope.isBoundToScopeAnnotation(CustomScope.class), is(true)); } }
@Test public void testReset_shouldClearBoundAnnotations_andFlagTheScopeAsOpen() throws Exception { //GIVEN ScopeNode scope = new ScopeImpl("root"); scope.bindScopeAnnotation(CustomScope.class); scope.close(); //WHEN scope.reset(); //THEN assertThat(scope.isBoundToScopeAnnotation(CustomScope.class), is(false)); assertThat(scope.isOpen, is(true)); }
/** * @param scopeAnnotationClass an annotation that should be qualified by {@link javax.inject.Scope}. If not, * an exception is thrown. * @return the parent {@link ScopeNode} of this scope that is bound to {@code scopeAnnotationClass}. * The current {@code scope} (this) can be returned if it is bound to {@code scopeAnnotationClass}. * If no such parent exists, it throws an exception. This later case means that something scoped * is using a lower scoped dependency, which is conceptually flawed and not allowed in Toothpick. * Note that is {@code scopeAnnotationClass} is {@link Singleton}, the root scope is always returned. * Thus the {@link Singleton} scope annotation class doesn't need to be bound, it's built-in. */ @SuppressWarnings({ "unused", "used by generated code" }) @Override public ScopeNode getParentScope(Class scopeAnnotationClass) { checkIsAnnotationScope(scopeAnnotationClass); if (scopeAnnotationClass == Singleton.class) { return getRootScope(); } ScopeNode currentScope = this; while (currentScope != null) { if (currentScope.isBoundToScopeAnnotation(scopeAnnotationClass)) { return currentScope; } currentScope = currentScope.getParentScope(); } throw new IllegalStateException(format("There is no parent scope of %s bound to scope scopeAnnotationClass %s", // this.name, // scopeAnnotationClass.getName())); }
/** * @param scopeAnnotationClass an annotation that should be qualified by {@link javax.inject.Scope}. If not, * an exception is thrown. * @return the parent {@link ScopeNode} of this scope that is bound to {@code scopeAnnotationClass}. * The current {@code scope} (this) can be returned if it is bound to {@code scopeAnnotationClass}. * If no such parent exists, it throws an exception. This later case means that something scoped * is using a lower scoped dependency, which is conceptually flawed and not allowed in Toothpick. * Note that is {@code scopeAnnotationClass} is {@link Singleton}, the root scope is always returned. * Thus the {@link Singleton} scope annotation class doesn't need to be bound, it's built-in. */ @SuppressWarnings({ "unused", "used by generated code" }) @Override public ScopeNode getParentScope(Class scopeAnnotationClass) { checkIsAnnotationScope(scopeAnnotationClass); if (scopeAnnotationClass == Singleton.class) { return getRootScope(); } ScopeNode currentScope = this; while (currentScope != null) { if (currentScope.isBoundToScopeAnnotation(scopeAnnotationClass)) { return currentScope; } currentScope = currentScope.getParentScope(); } throw new IllegalStateException(format("There is no parent scope of %s bound to scope scopeAnnotationClass %s", // this.name, // scopeAnnotationClass.getName())); }