Class InterningVisitor

  • All Implemented Interfaces:
    com.sun.source.tree.TreeVisitor<java.lang.Void,​java.lang.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 argument is InternedDistinct.
    2. the receiver and argument for a call to an equals method are both Interned (optional warning "unnecessary.equals")
    See Also:
    BaseTypeVisitor
    • Constructor Detail

      • InterningVisitor

        public InterningVisitor​(BaseTypeChecker checker)
        Create an InterningVisitor.
    • Method Detail

      • visitBinary

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

        public java.lang.Void visitMethodInvocation​(com.sun.source.tree.MethodInvocationTree tree,
                                                    java.lang.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<java.lang.Void,​java.lang.Void>
        Overrides:
        visitMethodInvocation in class BaseTypeVisitor<InterningAnnotatedTypeFactory>
      • visitMethod

        public java.lang.Void visitMethod​(com.sun.source.tree.MethodTree tree,
                                          java.lang.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<java.lang.Void,​java.lang.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:
        SourceVisitor.visitClass(com.sun.source.tree.ClassTree, java.lang.Object)
      • 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
        Returns:
        true if the tree is a valid type
      • isInvocationOfEquals

        public static boolean isInvocationOfEquals​(com.sun.source.tree.MethodInvocationTree tree)
        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:
        tree - a method invocation tree
        Returns:
        true iff tree is a invocation of equals()