Class ThreadSafety
- 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ThreadSafety.Builder
ThreadSafety
Builderstatic interface
ThreadSafety.KnownTypes
Information about known types and whether they're known to be safe or unsafe.static class
ThreadSafety.Purpose
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
ThreadSafety.Violation
A human-friendly explanation of a thread safety violations.
-
Constructor Summary
Constructors Constructor Description ThreadSafety(VisitorState state, ThreadSafety.KnownTypes knownTypes, Set<String> markerAnnotations, Set<String> acceptedAnnotations, Class<? extends Annotation> containerOfAnnotation, Class<? extends Annotation> suppressAnnotation)
Deprecated.ThreadSafety(VisitorState state, ThreadSafety.KnownTypes knownTypes, Set<String> markerAnnotations, Set<String> acceptedAnnotations, Class<? extends Annotation> containerOfAnnotation, Class<? extends Annotation> suppressAnnotation, Class<? extends Annotation> typeParameterAnnotation)
Deprecated.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static ThreadSafety.Builder
builder()
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.ThreadSafety.Violation
checkInstantiation(Collection<com.sun.tools.javac.code.Symbol.TypeVariableSymbol> typeParameters, Collection<com.sun.tools.javac.code.Type> typeArguments)
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.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.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.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.String
getPrettyName(com.sun.tools.javac.code.Symbol sym)
Gets a human-friendly name for the givenSymbol
to use in diagnostics.boolean
hasThreadSafeTypeParameterAnnotation(com.sun.tools.javac.code.Symbol.TypeVariableSymbol symbol)
Returns true if the given type parameter's declaration is annotated withtypeParameterAnnotation
indicated it will only ever be instantiated with thread-safe types.ThreadSafety.Violation
isThreadSafeType(boolean allowContainerTypeParameters, Set<String> containerTypeParameters, com.sun.tools.javac.code.Type type)
Returns anThreadSafety.Violation
explaining whether the type is threadsafe.ThreadSafety.Violation
isThreadSafeType(Set<String> containerTypeParameters, com.sun.tools.javac.code.Type type)
Deprecated.useisThreadSafeType(boolean, Set, Type)
instead.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.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.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.
-
-
-
Constructor Detail
-
ThreadSafety
@Deprecated public ThreadSafety(VisitorState state, ThreadSafety.KnownTypes knownTypes, Set<String> markerAnnotations, Set<String> acceptedAnnotations, @Nullable Class<? extends Annotation> containerOfAnnotation, @Nullable Class<? extends Annotation> suppressAnnotation)
Deprecated.Usebuilder()
instead.
-
ThreadSafety
@Deprecated public ThreadSafety(VisitorState state, ThreadSafety.KnownTypes knownTypes, Set<String> markerAnnotations, Set<String> acceptedAnnotations, @Nullable Class<? extends Annotation> containerOfAnnotation, @Nullable Class<? extends Annotation> suppressAnnotation, @Nullable Class<? extends Annotation> typeParameterAnnotation)
Deprecated.Usebuilder()
instead.
-
-
Method Detail
-
builder
public static ThreadSafety.Builder builder()
-
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
-
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
@Deprecated public ThreadSafety.Violation isThreadSafeType(Set<String> containerTypeParameters, com.sun.tools.javac.code.Type type)
Deprecated.useisThreadSafeType(boolean, Set, Type)
instead.
-
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 true if the given type parameter's declaration is annotated withtypeParameterAnnotation
indicated it will only ever be instantiated with thread-safe types.
-
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 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 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.
-
-