java.lang.Object
com.google.errorprone.bugpatterns.threadsafety.ThreadSafety

public final class ThreadSafety extends Object
A class which gives information about the annotation of types; if a type isn't annotated, ThreadSafety.Violation gives information as to why it is not.
  • Method Details

    • builder

      public static ThreadSafety.Builder builder()
    • threadSafeBuilder

      public static ThreadSafety.Builder threadSafeBuilder(WellKnownThreadSafety wellKnownThreadSafety)
    • threadSafeInstantiation

      public ThreadSafety.Violation threadSafeInstantiation(Set<String> containerTypeParameters, AnnotationInfo annotation, com.sun.tools.javac.code.Type type)
      Check that a type-use of an @ThreadSafe-annotated type is instantiated with threadsafe type arguments where required by its annotation's containerOf element.
      Parameters:
      containerTypeParameters - the in-scope threadsafe type parameters, declared on some enclosing class.
      annotation - the type's @ThreadSafe info
      type - the type to check
    • threadSafeInstantiation

      public ThreadSafety.Violation threadSafeInstantiation(Set<String> containerTypeParameters, AnnotationInfo annotation, com.sun.tools.javac.code.Type type, Set<com.sun.tools.javac.code.Symbol.TypeVariableSymbol> recursiveThreadSafeTypeParameter)
    • checkSuperInstantiation

      public ThreadSafety.Violation checkSuperInstantiation(Set<String> containerTypeParameters, AnnotationInfo annotation, com.sun.tools.javac.code.Type type)
      Check that the super-type of a @ThreadSafe-annotated type is instantiated with threadsafe type arguments where required by its annotation's containerOf element, and that any type arguments that correspond to containerOf type parameters on the sub-type are also in the super-type's containerOf spec.
      Parameters:
      containerTypeParameters - the in-scope threadsafe type parameters, declared on some enclosing class.
      annotation - the type's @ThreadSafe info
      type - the type to check
    • isThreadSafeType

      public ThreadSafety.Violation isThreadSafeType(boolean allowContainerTypeParameters, Set<String> containerTypeParameters, com.sun.tools.javac.code.Type type)
      Returns an ThreadSafety.Violation explaining whether the type is threadsafe.
      Parameters:
      allowContainerTypeParameters - true when checking the instantiation of an typeParameterAnnotation-annotated type parameter; indicates that containerTypeParameters should be ignored
      containerTypeParameters - type parameters in enclosing elements' containerOf specifications
      type - to check for thread-safety
    • hasThreadSafeTypeParameterAnnotation

      public boolean hasThreadSafeTypeParameterAnnotation(com.sun.tools.javac.code.Symbol.TypeVariableSymbol symbol)
      Returns whether the given type parameter's declaration is annotated with typeParameterAnnotation indicating it will only ever be instantiated with thread-safe types.
    • hasThreadSafeElementAnnotation

      public boolean hasThreadSafeElementAnnotation(com.sun.tools.javac.code.Symbol.TypeVariableSymbol symbol)
      Returns whether the given type parameter's declaration is annotated with containerOfAnnotation indicating its type-safety determines the type-safety of the outer class.
    • getMarkerOrAcceptedAnnotation

      public AnnotationInfo getMarkerOrAcceptedAnnotation(com.sun.tools.javac.code.Symbol sym, VisitorState state)
      Gets the Symbol's annotation info, either from a marker annotation on the symbol, from an accepted annotation on the symbol, or from the list of well-known types.
    • mutableEnclosingInstance

      @Nullable public com.sun.tools.javac.code.Type mutableEnclosingInstance(Optional<com.sun.source.tree.ClassTree> tree, com.sun.tools.javac.code.Type.ClassType type)
      Returns an enclosing instance for the specified type if it is thread-safe.
    • threadSafeTypeParametersInScope

      public Set<String> threadSafeTypeParametersInScope(com.sun.tools.javac.code.Symbol sym)
      Gets the set of in-scope threadsafe type parameters from the containerOf specs on annotations.

      Usually only the immediately enclosing declaration is searched, but it's possible to have cases like:

      
       @MarkerAnnotation(containerOf="T") class C<T> {
         class Inner extends ThreadSafeCollection<T> {}
       }
       
    • getInheritedAnnotation

      public AnnotationInfo getInheritedAnnotation(com.sun.tools.javac.code.Symbol sym, VisitorState state)
      Gets the possibly inherited marker annotation on the given symbol, and reverse-propagates containerOf spec's from super-classes.
    • getPrettyName

      public String getPrettyName(com.sun.tools.javac.code.Symbol sym)
      Gets a human-friendly name for the given Symbol to use in diagnostics.
    • checkInstantiation

      public ThreadSafety.Violation checkInstantiation(Collection<com.sun.tools.javac.code.Symbol.TypeVariableSymbol> typeParameters, Collection<com.sun.tools.javac.code.Type> typeArguments)
    • checkInstantiation

      public ThreadSafety.Violation checkInstantiation(com.sun.tools.javac.code.Symbol.TypeVariableSymbol typeParameter, Collection<com.sun.tools.javac.code.Type> instantiations)
      Checks that any thread-safe type parameters are instantiated with thread-safe types.
    • checkInvocation

      public ThreadSafety.Violation checkInvocation(com.sun.tools.javac.code.Type methodType, com.sun.tools.javac.code.Symbol symbol)
      Checks the instantiation of any thread-safe type parameters in the current invocation.