Package: org.codehaus.groovy.control

## [Java] Class GenericsVisitor

- org.codehaus.groovy.control.GenericsVisitor

-     public class GenericsVisitor
      extends ClassCodeVisitorSupport

  Verify correct usage of generics. This includes:

  - class header (class and superclass declaration)
  - arity of type parameters for fields, parameters, local variables
  - invalid diamond `<>` usage
  - JLS well-formedness of type arguments, bounds, array creation, and type parameters

- - ### Constructor Summary

    Constructors
    Constructor and description
    `GenericsVisitor(SourceUnit source)` Creates a visitor that validates generic type usage for one source unit.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `protected SourceUnit` | `getSourceUnit()` Returns the source unit currently being verified.
    `` | `public void` | `visitArrayExpression(ArrayExpression expression)` Validates generic usage for array element types.
    `` | `public void` | `visitBinaryExpression(BinaryExpression expression)` Groovy represents the type operand of `instanceof` as a ClassExpression, but it is not a class literal (`T.class`).
    `` | `public void` | `visitCastExpression(CastExpression expression)` Validates generic usage for cast target types.
    `` | `public void` | `visitCatchStatement(CatchStatement statement)` JLS 14.20: a catch type must be a reifiable class or union; a type parameter is not reifiable and may not be caught.
    `` | `public void` | `visitClass(ClassNode node)` Validates generic usage in a class header and its contents.
    `` | `public void` | `visitClassExpression(ClassExpression expression)` JLS 15.8.2: a class literal may not name a type variable or a parameterized type (`T.class`, `Cell<String>.class`).
    `` | `public void` | `visitConstructorCallExpression(ConstructorCallExpression expression)` Validates generic usage on constructor call types.
    `` | `protected void` | `visitConstructorOrMethod(MethodNode node, boolean isConstructor)` Validates generic usage for a constructor or method signature.
    `` | `public void` | `visitDeclarationExpression(DeclarationExpression expression)` Validates generic usage in declared variable types.
    `` | `public void` | `visitField(FieldNode node)` Validates generic usage for a field declaration.
    `` | `public void` | `visitMethodCallExpression(MethodCallExpression call)` JLS 4.5.2: a static member of a generic type must be referred to by the generic type name, not a parameterization such as `Cell<String>.id()`.
    `` | `public void` | `visitMethodPointerExpression(MethodPointerExpression expression)` JLS 15.13: `TypeName::m` and `TypeName::new` are method or constructor references, not class literals.
    `` | `public void` | `visitPropertyExpression(PropertyExpression expression)` JLS 4.5.2 also applies to static fields accessed as `Cell<String>.ID`.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class ClassCodeVisitorSupport` | `addError, getSourceUnit, visitAnnotation, visitAnnotations, visitAnnotations, visitAssertStatement, visitBlockStatement, visitBreakStatement, visitCaseStatement, visitCatchStatement, visitClass, visitClassCodeContainer, visitClosureExpression, visitConstructor, visitConstructorOrMethod, visitContinueStatement, visitDeclarationExpression, visitDoWhileLoop, visitExpressionStatement, visitField, visitForLoop, visitIfElse, visitImports, visitMethod, visitObjectInitializerStatements, visitPackage, visitProperty, visitReturnStatement, visitStatement, visitStatementAnnotations, visitSwitch, visitSynchronizedStatement, visitThrowStatement, visitTryCatchFinally, visitWhileLoop, visitYieldStatement`
    `class CodeVisitorSupport` | `afterSwitchCaseStatementsVisited, afterSwitchCaseStatementsVisited, afterSwitchConditionExpressionVisited, afterSwitchConditionExpressionVisited, visitArgumentlistExpression, visitArrayExpression, visitAssertStatement, visitAttributeExpression, visitBinaryExpression, visitBitwiseNegationExpression, visitBlockStatement, visitBooleanExpression, visitBreakStatement, visitBytecodeExpression, visitCaseStatement, visitCastExpression, visitCatchStatement, visitClassExpression, visitClosureExpression, visitClosureListExpression, visitConstantExpression, visitConstructorCallExpression, visitContinueStatement, visitDeclarationExpression, visitDoWhileLoop, visitEmptyStatement, visitExpressionStatement, visitFieldExpression, visitForLoop, visitGStringExpression, visitIfElse, visitLambdaExpression, visitListExpression, visitMapEntryExpression, visitMapExpression, visitMethodCallExpression, visitMethodPointerExpression, visitMethodReferenceExpression, visitNotExpression, visitPostfixExpression, visitPrefixExpression, visitPropertyExpression, visitRangeExpression, visitReturnStatement, visitShortTernaryExpression, visitSpreadExpression, visitSpreadMapExpression, visitStaticMethodCallExpression, visitSwitch, visitSwitchExpression, visitSynchronizedStatement, visitTernaryExpression, visitThrowStatement, visitTryCatchFinally, visitTupleExpression, visitUnaryMinusExpression, visitUnaryPlusExpression, visitVariableExpression, visitWhileLoop, visitYieldStatement`

- - ### Constructor Detail

    - #### public GenericsVisitor(SourceUnit source)

      Creates a visitor that validates generic type usage for one source unit.

      Parameters:
        `source` - the source unit being verified

  - ### Method Detail

    - #### protected SourceUnit getSourceUnit()

      Returns the source unit currently being verified.

      Returns:
        the active source unit

    - #### public void visitArrayExpression(ArrayExpression expression)

      Validates generic usage for array element types.

      Parameters:
        `expression` - the array expression to inspect

    - #### public void visitBinaryExpression(BinaryExpression expression)

      Groovy represents the type operand of `instanceof` as a ClassExpression, but it is not a class literal (`T.class`). Do not visit that operand as a class expression (that would reject `instanceof T` as `T.class`). Still run checkGenericsUsage(ClassNode) so nested-type well-formedness applies (`Outer<?>.Inner` with a generic `Inner`, `Outer.Inner<?>`). Parameterized `instanceof` is handled at parse (`AstBuilder#rejectNonReifiableInstanceof`): the grammar allows type arguments so `instanceof List<?>` can parse, while `instanceof Map<String,Integer>` is rejected (JLS 15.20.2). InstanceOfVerifier then checks primitives and type-parameter targets.

    - #### public void visitCastExpression(CastExpression expression)

      Validates generic usage for cast target types.

      Parameters:
        `expression` - the cast expression to inspect

    - #### public void visitCatchStatement(CatchStatement statement)

      JLS 14.20: a catch type must be a reifiable class or union; a type parameter is not reifiable and may not be caught.

    - #### public void visitClass(ClassNode node)

      Validates generic usage in a class header and its contents.

      Parameters:
        `node` - the class to inspect

    - #### public void visitClassExpression(ClassExpression expression)

      JLS 15.8.2: a class literal may not name a type variable or a parameterized type (`T.class`, `Cell<String>.class`). After resolve, `Foo.class` is a ClassExpression; `Foo<String>.class` may still be a PropertyExpression whose object is that class expression — see visitPropertyExpression. ClassExpression is also the TypeName of a static qualifier, `instanceof` operand, or method reference; those parents visit the type without entering this method, so a class-literal check here is only for an actual class literal.

    - #### public void visitConstructorCallExpression(ConstructorCallExpression expression)

      Validates generic usage on constructor call types.

      Parameters:
        `expression` - the constructor call to inspect

    - #### protected void visitConstructorOrMethod(MethodNode node, boolean isConstructor)

      Validates generic usage for a constructor or method signature.

      Parameters:
        `node` - the executable member to inspect
        `isConstructor` - whether `node` is a constructor

    - #### public void visitDeclarationExpression(DeclarationExpression expression)

      Validates generic usage in declared variable types.

      Parameters:
        `expression` - the declaration expression to inspect

    - #### public void visitField(FieldNode node)

      Validates generic usage for a field declaration.

      Parameters:
        `node` - the field to inspect

    - #### public void visitMethodCallExpression(MethodCallExpression call)

      JLS 4.5.2: a static member of a generic type must be referred to by the generic type name, not a parameterization such as `Cell<String>.id()`.

    - #### public void visitMethodPointerExpression(MethodPointerExpression expression)

      JLS 15.13: `TypeName::m` and `TypeName::new` are method or constructor references, not class literals. A parameterization of `TypeName` is legal for an instance method or constructor reference (`Iterable<String>::asCollection`, `HashMap<String,Integer>::new`).

    - #### public void visitPropertyExpression(PropertyExpression expression)

      JLS 4.5.2 also applies to static fields accessed as `Cell<String>.ID`. JLS 15.8.2: `Cell<String>.class` is a class literal, not a static member selection — do not use the 4.5.2 diagnostic for `.class`.