Class ThreadSafety
java.lang.Object
com.google.errorprone.bugpatterns.threadsafety.ThreadSafety
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.-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
ThreadSafety
Builderstatic enum
TheThreadSafety
utility class can be used by either the bug checker that enforces immutability or by the bug checker that enforces thread-safety.static class
A human-friendly explanation of a thread safety violations. -
Method Summary
Modifier and TypeMethodDescriptionstatic ThreadSafety.Builder
builder()
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.checkInstantiation
(Collection<com.sun.tools.javac.code.Symbol.TypeVariableSymbol> typeParameters, Collection<com.sun.tools.javac.code.Type> typeArguments) 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.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.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.getMarkerOrAcceptedAnnotation
(com.sun.tools.javac.code.Symbol sym, VisitorState state) Gets theSymbol
'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.getPrettyName
(com.sun.tools.javac.code.Symbol sym) Gets a human-friendly name for the givenSymbol
to use in diagnostics.boolean
hasThreadSafeElementAnnotation
(com.sun.tools.javac.code.Symbol.TypeVariableSymbol symbol) Returns whether the given type parameter's declaration is annotated withcontainerOfAnnotation
indicating its type-safety determines the type-safety of the outer class.boolean
hasThreadSafeTypeParameterAnnotation
(com.sun.tools.javac.code.Symbol.TypeVariableSymbol symbol) Returns whether the given type parameter's declaration is annotated withtypeParameterAnnotation
indicating it will only ever be instantiated with thread-safe types.isThreadSafeType
(boolean allowContainerTypeParameters, Set<String> containerTypeParameters, com.sun.tools.javac.code.Type type) Returns anThreadSafety.Violation
explaining whether the type is threadsafe.@Nullable 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.static ThreadSafety.Builder
threadSafeBuilder
(WellKnownThreadSafety wellKnownThreadSafety) 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.threadSafeInstantiation
(Set<String> containerTypeParameters, AnnotationInfo annotation, com.sun.tools.javac.code.Type type, Set<com.sun.tools.javac.code.Symbol.TypeVariableSymbol> recursiveThreadSafeTypeParameter) threadSafeTypeParametersInScope
(com.sun.tools.javac.code.Symbol sym) Gets the set of in-scope threadsafe type parameters from the containerOf specs on annotations.
-
Method Details
-
builder
-
threadSafeBuilder
-
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
infotype
- 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
infotype
- the type to check
-
isThreadSafeType
public ThreadSafety.Violation isThreadSafeType(boolean allowContainerTypeParameters, Set<String> containerTypeParameters, com.sun.tools.javac.code.Type type) Returns anThreadSafety.Violation
explaining whether the type is threadsafe.- Parameters:
allowContainerTypeParameters
- true when checking the instantiation of antypeParameterAnnotation
-annotated type parameter; indicates thatcontainerTypeParameters
should be ignoredcontainerTypeParameters
- type parameters in enclosing elements' containerOf specificationstype
- 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 withtypeParameterAnnotation
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 withcontainerOfAnnotation
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 theSymbol
'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
public @Nullable 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
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
Gets a human-friendly name for the givenSymbol
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.
-