Class BaseTypeChecker

All Implemented Interfaces:
Processor, OptionConfiguration
Direct Known Subclasses:
AccumulationChecker, AliasingChecker, ClassValChecker, FenumChecker, FormatterChecker, GuiEffectChecker, I18nFormatterChecker, I18nSubchecker, InitializationChecker, InterningChecker, KeyForSubchecker, LessThanChecker, LockChecker, LowerBoundChecker, MethodValChecker, MustCallChecker, NonEmptyChecker, OptionalChecker, PropertyKeyChecker, PurityChecker, RegexChecker, ReportChecker, ReturnsReceiverChecker, SameLenChecker, SearchIndexChecker, SignatureChecker, SignednessChecker, SqlQuotesChecker, SubstringIndexChecker, SubtypingChecker, TaintingChecker, TypeOutputtingChecker, UnitsChecker, UpperBoundChecker, ValueChecker

public abstract class BaseTypeChecker extends SourceChecker
An abstract SourceChecker that provides a simple SourceVisitor implementation that type-checks assignments, pseudo-assignments such as parameter passing and method invocation, and method overriding.

Most type-checker annotation processors should extend this class, instead of SourceChecker. Checkers that require annotated types but not subtype checking (e.g. for testing purposes) should extend SourceChecker. Non-type checkers (e.g. checkers to enforce coding styles) can extend SourceChecker or AbstractTypeProcessor; the Checker Framework is not specifically designed to support such checkers.

It is a convention that, for a type system Foo, the checker, the visitor, and the annotated type factory are named as FooChecker, FooVisitor, and FooAnnotatedTypeFactory. Some factory methods use this convention to construct the appropriate classes reflectively.

BaseTypeChecker encapsulates a group for factories for various representations/classes related the type system, mainly:

  • QualifierHierarchy: to represent the supported qualifiers in addition to their hierarchy, mainly, subtyping rules
  • TypeHierarchy: to check subtyping rules between annotated types rather than qualifiers
  • AnnotatedTypeFactory: to construct qualified types enriched with default qualifiers according to the type system rules
  • BaseTypeVisitor: to visit the compiled Java files and check for violations of the type system rules

Subclasses must specify the set of type qualifiers they support. See AnnotatedTypeFactory.createSupportedTypeQualifiers().

If the specified type qualifiers are meta-annotated with SubtypeOf, this implementation will automatically construct the type qualifier hierarchy. Otherwise, or if this behavior must be overridden, the subclass may override the AnnotatedTypeFactory.createQualifierHierarchy() method.

See the Checker Framework Manual:
The checker class
  • Field Details

  • Constructor Details

    • BaseTypeChecker

      protected BaseTypeChecker()
      Create a new BaseTypeChecker.
  • Method Details

    • createSourceVisitor

      protected BaseTypeVisitor<?> createSourceVisitor()
      Returns the appropriate visitor that type-checks the compilation unit according to the type system rules.

      This implementation uses the checker naming convention to create the appropriate visitor. If no visitor is found, it returns an instance of BaseTypeVisitor. It reflectively invokes the constructor that accepts this checker and the compilation unit tree (in that order) as arguments.

      Subclasses have to override this method to create the appropriate visitor if they do not follow the checker naming convention.

      Specified by:
      createSourceVisitor in class SourceChecker
      Returns:
      the type-checking visitor
    • createSourceVisitorPublic

      public BaseTypeVisitor<?> createSourceVisitorPublic()
      A public variant of createSourceVisitor(). Only use this if you know what you are doing.
      Returns:
      the type-checking visitor
    • getVisitor

      public BaseTypeVisitor<?> getVisitor()
      Description copied from class: SourceChecker
      Returns the SourceVisitor associated with this.
      Overrides:
      getVisitor in class SourceChecker
      Returns:
      the SourceVisitor associated with this
    • getTypeFactory

      public GenericAnnotatedTypeFactory<?,?,?,?> getTypeFactory()
      Return the type factory associated with this checker.
      Returns:
      the type factory associated with this checker
    • getAnnotationProvider

      public AnnotationProvider getAnnotationProvider()
      Description copied from class: SourceChecker
      Returns the AnnotationProvider (the type factory) associated with this.
      Overrides:
      getAnnotationProvider in class SourceChecker
      Returns:
      the AnnotationProvider (the type factory) associated with this
    • getTypeFactoryOfSubcheckerOrNull

      public <T extends GenericAnnotatedTypeFactory<?, ?, ?, ?>> @Nullable T getTypeFactoryOfSubcheckerOrNull(Class<? extends BaseTypeChecker> subCheckerClass)
      Returns the type factory used by a subchecker. Returns null if no matching subchecker was found or if the type factory is null. The caller must know the exact checker class to request.

      Because the visitor state is copied, call this method each time a subfactory is needed rather than store the returned subfactory in a field.

      Type Parameters:
      T - the type of subCheckerClass's AnnotatedTypeFactory
      Parameters:
      subCheckerClass - the class of the subchecker
      Returns:
      the type factory of the requested subchecker or null if not found
    • processErrorMessageArg

      protected Object processErrorMessageArg(Object arg)
      Description copied from class: SourceChecker
      Process an argument to an error message before it is passed to String.format.

      This implementation expands the argument if it is exactly a message key.

      By contrast, SourceChecker.fullMessageOf(java.lang.String, java.lang.String) processes the message key itself but not the arguments, and tries suffixes.

      Overrides:
      processErrorMessageArg in class SourceChecker
      Parameters:
      arg - the argument
      Returns:
      the result after processing
    • shouldAddShutdownHook

      protected boolean shouldAddShutdownHook()
      Description copied from class: SourceChecker
      Return true to indicate that method SourceChecker.shutdownHook() should be added as a shutdownHook of the JVM.
      Overrides:
      shouldAddShutdownHook in class SourceChecker
      Returns:
      true to add SourceChecker.shutdownHook() as a shutdown hook of the JVM
    • shutdownHook

      protected void shutdownHook()
      Description copied from class: SourceChecker
      Method that gets called exactly once at shutdown time of the JVM. Checkers can override this method to customize the behavior.

      If you override this, you must also override SourceChecker.shouldAddShutdownHook() to return true.

      Overrides:
      shutdownHook in class SourceChecker
    • createSupportedLintOptions

      protected Set<String> createSupportedLintOptions()
      Description copied from class: SourceChecker
      Compute the set of supported lint options for this checker and its subcheckers.
      Overrides:
      createSupportedLintOptions in class SourceChecker
      Returns:
      the set of supported lint options for this checker and its subcheckers
    • getUltimateParentChecker

      public BaseTypeChecker getUltimateParentChecker()
      Finds the ultimate parent checker of this checker. The ultimate parent checker is the checker that the user actually requested, i.e. the one with no parent. The ultimate parent might be this checker itself.
      Returns:
      the first checker in the parent checker chain with no parent checker of its own, i.e., the ultimate parent checker
    • invokeConstructorFor

      public static <T> @Nullable T invokeConstructorFor(@ClassGetName String className, Class<?>[] paramTypes, Object[] args)
      Invokes the constructor belonging to the class named by name having the given parameter types on the given arguments. Returns null if the class cannot be found. Otherwise, throws an exception if there is trouble with the constructor invocation.
      Type Parameters:
      T - the type to which the constructor belongs
      Parameters:
      className - the name of the class to which the constructor belongs
      paramTypes - the types of the constructor's parameters
      args - the arguments on which to invoke the constructor
      Returns:
      the result of the constructor invocation on args, or null if the class does not exist