Class InterningVisitor

java.lang.Object
com.sun.source.util.TreeScanner<R,P>
com.sun.source.util.TreePathScanner<R,P>
All Implemented Interfaces:
com.sun.source.tree.TreeVisitor<Void,Void>

public final class InterningVisitor extends BaseTypeVisitor<InterningAnnotatedTypeFactory>
Typechecks source code for interning violations. A type is considered interned if its primary annotation is Interned or InternedDistinct. This visitor reports errors or warnings for violations for the following cases:
  1. either argument to a "==" or "!=" comparison is not Interned (error "not.interned"). As a special case, the comparison is permitted if either arugment is InternedDistinct.
  2. the receiver and argument for a call to an equals method are both Interned (optional warning "unnecessary.equals")
See Also:
  • Constructor Details

    • InterningVisitor

      public InterningVisitor(BaseTypeChecker checker)
      Create an InterningVisitor.
  • Method Details

    • visitBinary

      public Void visitBinary(com.sun.source.tree.BinaryTree node, Void p)
      Checks comparison operators, == and !=, for INTERNING violations.
      Specified by:
      visitBinary in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitBinary in class com.sun.source.util.TreeScanner<Void,Void>
    • visitMethodInvocation

      public Void visitMethodInvocation(com.sun.source.tree.MethodInvocationTree node, Void p)
      If lint option "dotequals" is specified, warn if the .equals method is used where reference equality is safe.
      Specified by:
      visitMethodInvocation in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitMethodInvocation in class BaseTypeVisitor<InterningAnnotatedTypeFactory>
    • visitMethod

      public Void visitMethod(com.sun.source.tree.MethodTree node, Void p)
      Description copied from class: BaseTypeVisitor
      Checks that the method obeys override and subtype rules to all overridden methods. (Uses the pseudo-assignment logic to do so.)

      The override rule specifies that a method, m1, may override a method m2 only if:

      • m1 return type is a subtype of m2
      • m1 receiver type is a supertype of m2
      • m1 parameters are supertypes of corresponding m2 parameters
      Also, it issues a "missing.this" error for static method annotated receivers.
      Specified by:
      visitMethod in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitMethod in class BaseTypeVisitor<InterningAnnotatedTypeFactory>
    • processClassTree

      public void processClassTree(com.sun.source.tree.ClassTree classTree)
      Method to implement the @UsesObjectEquals functionality. If a class is annotated with @UsesObjectEquals, it must:
      • not override .equals(Object) and be a subclass of a class annotated with @UsesObjectEquals, or
      • override equals(Object) with body "this == arg"
      If a class is not annotated with @UsesObjectEquals, it must:
      • not have a superclass annotated with @UsesObjectEquals
      Overrides:
      processClassTree in class BaseTypeVisitor<InterningAnnotatedTypeFactory>
      Parameters:
      classTree - class to check
      See Also:
    • checkConstructorResult

      protected void checkConstructorResult(AnnotatedTypeMirror.AnnotatedExecutableType constructorType, ExecutableElement constructorElement)
      Description copied from class: BaseTypeVisitor
      Issue a warning if the result type of the constructor is not top. If it is a supertype of the class, then a conflicting.annos error will also be issued by BaseTypeVisitor.isValidUse(AnnotatedTypeMirror.AnnotatedDeclaredType,AnnotatedTypeMirror.AnnotatedDeclaredType,Tree).
      Overrides:
      checkConstructorResult in class BaseTypeVisitor<InterningAnnotatedTypeFactory>
      Parameters:
      constructorType - AnnotatedExecutableType for the constructor
      constructorElement - element that declares the constructor
    • validateTypeOf

      public boolean validateTypeOf(com.sun.source.tree.Tree tree)
      Description copied from class: BaseTypeVisitor
      Tests whether the tree expressed by the passed type tree is a valid type, and emits an error if that is not the case (e.g. '@Mutable String'). If the tree is a method or constructor, check the return type.
      Overrides:
      validateTypeOf in class BaseTypeVisitor<InterningAnnotatedTypeFactory>
      Parameters:
      tree - the AST type supplied by the user
    • isInvocationOfEquals

      public static boolean isInvocationOfEquals(com.sun.source.tree.MethodInvocationTree node)
      Tests whether a method invocation is an invocation of Object.equals(java.lang.Object) with one argument.

      Returns true even if a method overloads Object.equals(Object), because of the common idiom of writing an equals method with a non-Object parameter, in addition to the equals method that overrides Object.equals(Object).

      Parameters:
      node - a method invocation node
      Returns:
      true iff node is a invocation of equals()
    • isTypeCastSafe

      protected boolean isTypeCastSafe(AnnotatedTypeMirror castType, AnnotatedTypeMirror exprType)
      Description copied from class: BaseTypeVisitor
      Returns true if the cast is safe.

      Only primary qualifiers are checked unless the command line option "checkCastElementType" is supplied.

      Overrides:
      isTypeCastSafe in class BaseTypeVisitor<InterningAnnotatedTypeFactory>
      Parameters:
      castType - annotated type of the cast
      exprType - annotated type of the casted expression
      Returns:
      true if the type cast is safe, false otherwise