Java 类javax.inject.Scope 实例源码

项目:Spork    文件:QualifierCacheTests.java   
@Test
public void annotationWithoutValueMethod() {
    Annotation annotation = Singleton.class.getAnnotation(Scope.class);
    cache.getQualifier(annotation);

    InOrder inOrder = inOrder(lock, cache);

    // initial call
    inOrder.verify(cache).getQualifier(annotation);

    // internal thread safe method lookup
    inOrder.verify(cache).getValueMethodThreadSafe(Scope.class);
    inOrder.verify(lock).lock();
    inOrder.verify(cache).getValueMethod(Scope.class);
    inOrder.verify(lock).unlock();
    // no value Method found, so no invocation needed

    inOrder.verifyNoMoreInteractions();
    verifyZeroInteractions(cache);
    assertThat(bindActionMap.size(), is(1));
}
项目:toothpick    文件:FactoryProcessor.java   
/**
 * Lookup {@link javax.inject.Scope} annotated annotations to provide the name of the scope the {@code typeElement} belongs to.
 * The method logs an error if the {@code typeElement} has multiple scope annotations.
 *
 * @param typeElement the element for which a scope is to be found.
 * @return the scope of this {@code typeElement} or {@code null} if it has no scope annotations.
 */
private String getScopeName(TypeElement typeElement) {
  String scopeName = null;
  for (AnnotationMirror annotationMirror : typeElement.getAnnotationMirrors()) {
    TypeElement annotationTypeElement = (TypeElement) annotationMirror.getAnnotationType().asElement();
    if (annotationTypeElement.getAnnotation(Scope.class) != null) {
      checkScopeAnnotationValidity(annotationTypeElement);
      if (scopeName != null) {
        error(typeElement, "Only one @Scope qualified annotation is allowed : %s", scopeName);
      }
      scopeName = annotationTypeElement.getQualifiedName().toString();
    }
  }

  return scopeName;
}
项目:baratine    文件:InjectorImpl.java   
/**
 * Finds the scope for a bean producing declaration, either a method or
 * a type.
 */
private <T> InjectScope<T> findScope(AnnotatedElement annElement)
{
  for (Annotation ann : annElement.getAnnotations()) {
    Class<? extends Annotation> annType = ann.annotationType();

    if (annType.isAnnotationPresent(Scope.class)) {
      Supplier<InjectScope<T>> scopeGen = (Supplier) _scopeMap.get(annType);

      if (scopeGen != null) {
        return scopeGen.get();
      }
      else {
        log.fine(L.l("@{0} is an unknown scope", annType.getSimpleName()));
      }
    }
  }

  return new InjectScopeFactory<>();
}
项目:baratine    文件:InjectorBuilderImpl.java   
@Override
public BindingBuilderImpl<T> scope(Class<? extends Annotation> scopeType)
{
  Objects.requireNonNull(scopeType);

  if (! scopeType.isAnnotationPresent(Scope.class)) {
    throw error("'@{0}' is an invalid scope type because it is not annotated with @Scope",
                scopeType.getSimpleName());
  }

  if (_scopeMap.get(scopeType) == null) {
    throw error("'@{0}' is an unsupported scope. Only @Singleton and @Factory are supported.",
                scopeType.getSimpleName());

  }

  _scopeType = scopeType;

  return this;
}
项目:tiger    文件:Utils.java   
/**
 * Returns the scope of this module, null for unscoped modules. Dagger requires that each module
 * can only contain no more than one scope type of scoped binding. Unscoped bindings are not
 * limited.
 * TODO(freeman): supported included modules.
 */
@Nullable
public static TypeElement getModuleScope(DeclaredType moduleType) {
  TypeElement moduleElement = (TypeElement) moduleType.asElement();
  Preconditions.checkArgument(
      moduleElement.getAnnotation(Module.class) != null,
      String.format("not module: %s.", moduleType));
  for (Element element : moduleElement.getEnclosedElements()) {
    if (element.getKind().equals(ElementKind.METHOD)
        && (element.getAnnotation(Provides.class) != null)) {
      for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
        Annotation scope =
            annotationMirror.getAnnotationType().asElement().getAnnotation(Scope.class);
        if (scope != null) {
          return (TypeElement) annotationMirror.getAnnotationType().asElement();
        }
      }
    }
  }
  return null;
}
项目:Mortar-architect    文件:ScopeExtractor.java   
/**
 * Find annotation that is itself annoted with @Scope
 * If there is one, it will be later applied on the generated component
 * Otherwise the component will be unscoped
 * Throw error if more than one scope annotation found
 */
private AnnotationMirror findScope() {
    AnnotationMirror annotationTypeMirror = null;

    for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
        Element annotationElement = annotationMirror.getAnnotationType().asElement();
        if (MoreElements.isAnnotationPresent(annotationElement, Scope.class)) {
            // already found one scope
            if (annotationTypeMirror != null) {
                errors.addInvalid("Several dagger scopes on same element are not allowed");
                continue;
            }

            annotationTypeMirror = annotationMirror;
        }
    }

    return annotationTypeMirror;
}
项目:Auto-Mortar    文件:ScreenExtractor.java   
/**
 * Find annotation that is itself annoted with @Scope
 * If there is one, it will be later applied on the generated component
 * Otherwise the component will be unscoped
 * Throw error if more than one scope annotation found
 */
private AnnotationMirror findScope() {
    AnnotationMirror annotationTypeMirror = null;

    for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
        Element annotationElement = annotationMirror.getAnnotationType().asElement();
        if (MoreElements.isAnnotationPresent(annotationElement, Scope.class)) {
            // already found one scope
            if (annotationTypeMirror != null) {
                errors.addInvalid("Several dagger scopes on same element are not allowed");
                continue;
            }

            annotationTypeMirror = annotationMirror;
        }
    }

    return annotationTypeMirror;
}
项目:ann-docu-gen    文件:DocumentedAnnotations.java   
public DocumentedAnnotations(Element element) {
    List<AnnotationMirror> allAnnotations = new ArrayList<AnnotationMirror>();
    allAnnotations.addAll(annotationsOf(element, Qualifier.class));
    allAnnotations.addAll(annotationsOf(element, Scope.class));

    if (allAnnotations.isEmpty()) {
        this.description = Optional.absent();
        return;
    }

    StringBuilder result = new StringBuilder();
    for (AnnotationMirror annotation : allAnnotations) {
        if (result.length() > 0) {
            result.append(" ");
        }
        result.append(annotation.toString());
    }

    this.description = Optional.of(result.toString());
}
项目:Spork    文件:QualifierCacheTests.java   
@Test
public void methodCaching() {
    Annotation annotation = Singleton.class.getAnnotation(Scope.class);
    // call twice - should cache once
    cache.getQualifier(annotation);
    cache.getQualifier(annotation);

    assertThat(bindActionMap.size(), is(1));
}
项目:Spork    文件:AnnotationsTests.java   
@Test
public void fieldScopeTest() throws NoSuchFieldException {
    Field field = Testable.class.getDeclaredField("scopedField");
    Annotation namedAnnotation = Annotations.findAnnotationAnnotatedWith(Scope.class, field);

    assertThat(namedAnnotation, allOf(notNullValue(), annotationOfType(Singleton.class)));
}
项目:Spork    文件:AnnotationsTests.java   
@Test
public void methodScopeTest() throws NoSuchMethodException {
    Method method = Testable.class.getDeclaredMethod("scopedMethod");
    Annotation namedAnnotation = Annotations.findAnnotationAnnotatedWith(Scope.class, method);

    assertThat(namedAnnotation, allOf(notNullValue(), annotationOfType(Singleton.class)));
}
项目:Spork    文件:AnnotationsTests.java   
@Test
public void methodArgumentScopeTest() throws NoSuchMethodException {
    Method method = Testable.class.getDeclaredMethod("scopedMethodArguments", Object.class);
    Annotation[] annotations = method.getParameterAnnotations()[0];
    Annotation namedAnnotation = Annotations.findAnnotationAnnotatedWith(Scope.class, annotations);

    assertThat(namedAnnotation, allOf(notNullValue(), annotationOfType(Singleton.class)));
}
项目:Spork    文件:AnnotationsTests.java   
@Test
public void methodArgumentScopeMissingTest() throws NoSuchMethodException {
    Method method = Testable.class.getDeclaredMethod("scopedMethodArgumentsMissing", Object.class);
    Annotation[] annotations = method.getParameterAnnotations()[0];
    Annotation namedAnnotation = Annotations.findAnnotationAnnotatedWith(Scope.class, annotations);

    assertThat(namedAnnotation, is(nullValue()));
}
项目:toothpick    文件:FactoryProcessor.java   
private void createFactoriesForClassesAnnotatedWithScopeAnnotations(RoundEnvironment roundEnv, Set<? extends TypeElement> annotations) {
  for (TypeElement annotation : annotations) {
    if (annotation.getAnnotation(Scope.class) != null) {
      checkScopeAnnotationValidity(annotation);
      createFactoriesForClassesAnnotatedWith(roundEnv, annotation);
    }
  }
}
项目:toothpick    文件:FactoryProcessor.java   
private void checkScopeAnnotationValidity(TypeElement annotation) {
  if (annotation.getAnnotation(Scope.class) == null) {
    error(annotation, "Scope Annotation %s does not contain Scope annotation.", annotation.getQualifiedName());
    return;
  }

  Retention retention = annotation.getAnnotation(Retention.class);
  if (retention == null || retention.value() != RetentionPolicy.RUNTIME) {
    error(annotation, "Scope Annotation %s does not have RUNTIME retention policy.", annotation.getQualifiedName());
  }
}
项目:baratine    文件:InjectorBuilderImpl.java   
private Class<? extends Annotation> scope(Method method)
{
  for (Annotation ann : method.getAnnotations()) {
    if (ann.annotationType().isAnnotationPresent(Scope.class)) {
      return ann.annotationType();
    }
  }

  return Singleton.class;
}
项目:java-restify    文件:RestifyProxyCdiBean.java   
@Override
public Class<? extends Annotation> getScope() {
    Class<? extends Annotation> scope = Arrays.stream(javaType.getAnnotations())
        .map(a -> a.getClass())
            .filter(a -> a.getAnnotation(Scope.class) != null)
                .findFirst().orElse(null);

    return scope == null ? Dependent.class : scope;
}
项目:Auto-Dagger2    文件:SubcomponentExtractor.java   
/**
 * Find annotation that is itself annoted with @Scope
 * If there is one, it will be later applied on the generated component
 * Otherwise the component will be unscoped
 * Throw error if more than one scope annotation found
 */
private AnnotationMirror findScope() {
    List<AnnotationMirror> annotationMirrors = ExtractorUtil.findAnnotatedAnnotation(element, Scope.class);
    if (annotationMirrors.isEmpty()) {
        return null;
    }

    if (annotationMirrors.size() > 1) {
        errors.getParent().addInvalid(element, "Cannot have several scope (@Scope).");
        return null;
    }

    return annotationMirrors.get(0);
}
项目:Auto-Dagger2    文件:ComponentExtractor.java   
private AnnotationMirror findScope(Element element) {
    List<AnnotationMirror> annotationMirrors = ExtractorUtil.findAnnotatedAnnotation(element, Scope.class);
    if (annotationMirrors.isEmpty()) {
        return null;
    }

    if (annotationMirrors.size() > 1) {
        errors.getParent().addInvalid(element, "Cannot have several scope (@Scope).");
        return null;
    }

    return annotationMirrors.get(0);
}
项目:dinistiq    文件:QualifierTest.java   
/**
 * Test scope handling with the given single scope of dinistiq.
 */
@Test
public void testScopes() {
    Set<String> packs = new HashSet<>();
    packs.add(Scope.class.getPackage().getName());
    ClassResolver classResolver = new SimpleClassResolver(packs);
    Set<Class<Object>> scopes = classResolver.getAnnotatedItems(Scope.class);
    Assert.assertEquals(scopes.size(), 1, "Unexpected number of scopes discovered in class path.");
}
项目:Spork    文件:ObjectGraphNode.java   
ObjectGraphNode(InjectSignature injectSignature, Object parent, Method method) {
    this.injectSignature = injectSignature;
    this.parent = parent;
    this.method = method;
    this.scopeAnnotation = Annotations.findAnnotationAnnotatedWith(Scope.class, method); // TODO: cache?
}
项目:tiger    文件:Utils.java   
/**
 * Return if the given {@link TypeElement} is annotated with {@link Scope}.
 */
public static boolean isScopeTypeElement(TypeElement element) {
  return element.getAnnotation(Scope.class) != null;
}
项目:dinistiq    文件:QualifierTest.java   
/**
 * Test if beans get handled depending on their respective given qualifiers.
 */
@Test
public void testQualifiedBeans() {
    Named n = new Named() {
        /**
         * Get named for named annotation.
         */
        @Override
        public String value() {
            return "test";
        }


        /**
         * Get annotation type for named annotation.
         */
        @Override
        public Class<? extends Annotation> annotationType() {
            return Named.class;
        }

    };
    Set<Annotation> qualifiers = new HashSet<>();
    qualifiers.add(n);
    Set<Object> qualifiedBeans = d.findQualifiedBeans(qualifiers);
    Assert.assertEquals(qualifiedBeans.size(), 8, "Unexpected number of quaified beans discovered in dinistiq scope.");
    boolean thrown = false;
    try {
        Scope s = new Scope() {
            /**
             * Get annotation type.
             */
            @Override
            public Class<? extends Annotation> annotationType() {
                return Scope.class;
            }

        };
        qualifiers = new HashSet<>();
        qualifiers.add(s);
        d.findQualifiedBeans(qualifiers);
    } catch (Exception e) {
        if (e.getMessage().startsWith("Not a qualifier")) {
            thrown = true;
        } // if
    } // try/catch
    Assert.assertTrue(thrown, "Should not issue results when no qualifier is given.");
}