Package org.codehaus.groovy.control

# Class GenericsVisitor

java.lang.Object
org.codehaus.groovy.ast.CodeVisitorSupport
org.codehaus.groovy.ast.ClassCodeVisitorSupport
org.codehaus.groovy.control.GenericsVisitor

All Implemented Interfaces:
  `GroovyClassVisitor`, `GroovyCodeVisitor`, `ErrorCollecting`

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
  Description
  `GenericsVisitor(SourceUnit source)`
  Creates a visitor that validates generic type usage for one source unit.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected SourceUnit`
  `getSourceUnit()`
  Returns the source unit currently being verified.
  `void`
  `visitArrayExpression(ArrayExpression expression)`
  Validates generic usage for array element types.
  `void`
  `visitBinaryExpression(BinaryExpression expression)`
  Groovy represents the type operand of `instanceof` as a `ClassExpression`, but it is not a class literal (`T.class`).
  `void`
  `visitCastExpression(CastExpression expression)`
  Validates generic usage for cast target types.
  `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.
  `void`
  `visitClass(ClassNode node)`
  Validates generic usage in a class header and its contents.
  `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`).
  `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.
  `void`
  `visitDeclarationExpression(DeclarationExpression expression)`
  Validates generic usage in declared variable types.
  `void`
  `visitField(FieldNode node)`
  Validates generic usage for a field declaration.
  `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()`.
  `void`
  `visitMethodPointerExpression(MethodPointerExpression expression)`
  JLS 15.13: `TypeName::m` and `TypeName::new` are method or constructor references, not class literals.
  `void`
  `visitPropertyExpression(PropertyExpression expression)`
  JLS 4.5.2 also applies to static fields accessed as `Cell<String>.ID`.

  ### Methods inherited from class org.codehaus.groovy.ast.ClassCodeVisitorSupport

  `addError, visitAnnotation, visitAnnotations, visitAnnotations, visitAssertStatement, visitBlockStatement, visitBreakStatement, visitCaseStatement, visitClassCodeContainer, visitClosureExpression, visitConstructor, visitContinueStatement, visitDoWhileLoop, visitExpressionStatement, visitForLoop, visitIfElse, visitImports, visitMethod, visitObjectInitializerStatements, visitPackage, visitProperty, visitReturnStatement, visitStatement, visitStatementAnnotations, visitSwitch, visitSynchronizedStatement, visitThrowStatement, visitTryCatchFinally, visitWhileLoop, visitYieldStatement`

  ### Methods inherited from class org.codehaus.groovy.ast.CodeVisitorSupport

  `afterSwitchCaseStatementsVisited, afterSwitchCaseStatementsVisited, afterSwitchConditionExpressionVisited, afterSwitchConditionExpressionVisited, visitArgumentlistExpression, visitAttributeExpression, visitBitwiseNegationExpression, visitBooleanExpression, visitBytecodeExpression, visitClosureListExpression, visitConstantExpression, visitEmptyStatement, visitFieldExpression, visitGStringExpression, visitLambdaExpression, visitListExpression, visitMapEntryExpression, visitMapExpression, visitMethodReferenceExpression, visitNotExpression, visitPostfixExpression, visitPrefixExpression, visitRangeExpression, visitShortTernaryExpression, visitSpreadExpression, visitSpreadMapExpression, visitStaticMethodCallExpression, visitSwitchExpression, visitTernaryExpression, visitTupleExpression, visitUnaryMinusExpression, visitUnaryPlusExpression, visitVariableExpression`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

  ### Methods inherited from interface org.codehaus.groovy.ast.GroovyCodeVisitor

  `visit, visit, visitEmptyExpression, visitListOfExpressions`

- ## Constructor Details

  - ### GenericsVisitor

    public GenericsVisitor(SourceUnit source)
    Creates a visitor that validates generic type usage for one source unit.
    Parameters:
      `source` - the source unit being verified

- ## Method Details

  - ### getSourceUnit

    protected SourceUnit getSourceUnit()
    Returns the source unit currently being verified.
    Specified by:
      `getSourceUnit` in class `ClassCodeVisitorSupport`
    Returns:
      the active source unit

  - ### visitClass

    public void visitClass(ClassNode node)
    Validates generic usage in a class header and its contents.
    Specified by:
      `visitClass` in interface `GroovyClassVisitor`
    Overrides:
      `visitClass` in class `ClassCodeVisitorSupport`
    Parameters:
      `node` - the class to inspect

  - ### visitField

    public void visitField(FieldNode node)
    Validates generic usage for a field declaration.
    Specified by:
      `visitField` in interface `GroovyClassVisitor`
    Overrides:
      `visitField` in class `ClassCodeVisitorSupport`
    Parameters:
      `node` - the field to inspect

  - ### visitCatchStatement

    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.
    Specified by:
      `visitCatchStatement` in interface `GroovyCodeVisitor`
    Overrides:
      `visitCatchStatement` in class `ClassCodeVisitorSupport`
    Parameters:
      `statement` - the catch statement to visit

  - ### visitConstructorOrMethod

    protected void visitConstructorOrMethod(MethodNode node, boolean isConstructor)
    Validates generic usage for a constructor or method signature.
    Overrides:
      `visitConstructorOrMethod` in class `ClassCodeVisitorSupport`
    Parameters:
      `node` - the executable member to inspect
      `isConstructor` - whether `node` is a constructor

  - ### visitConstructorCallExpression

    public void visitConstructorCallExpression(ConstructorCallExpression expression)
    Validates generic usage on constructor call types.
    Specified by:
      `visitConstructorCallExpression` in interface `GroovyCodeVisitor`
    Overrides:
      `visitConstructorCallExpression` in class `CodeVisitorSupport`
    Parameters:
      `expression` - the constructor call to inspect

  - ### visitDeclarationExpression

    public void visitDeclarationExpression(DeclarationExpression expression)
    Validates generic usage in declared variable types.
    Specified by:
      `visitDeclarationExpression` in interface `GroovyCodeVisitor`
    Overrides:
      `visitDeclarationExpression` in class `ClassCodeVisitorSupport`
    Parameters:
      `expression` - the declaration expression to inspect

  - ### visitArrayExpression

    public void visitArrayExpression(ArrayExpression expression)
    Validates generic usage for array element types.
    Specified by:
      `visitArrayExpression` in interface `GroovyCodeVisitor`
    Overrides:
      `visitArrayExpression` in class `CodeVisitorSupport`
    Parameters:
      `expression` - the array expression to inspect

  - ### visitClassExpression

    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(org.codehaus.groovy.ast.expr.PropertyExpression)`. `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.
    Specified by:
      `visitClassExpression` in interface `GroovyCodeVisitor`
    Overrides:
      `visitClassExpression` in class `CodeVisitorSupport`
    Parameters:
      `expression` - the class expression

  - ### visitBinaryExpression

    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.
    Specified by:
      `visitBinaryExpression` in interface `GroovyCodeVisitor`
    Overrides:
      `visitBinaryExpression` in class `CodeVisitorSupport`
    Parameters:
      `expression` - the binary expression

  - ### visitMethodCallExpression

    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()`.
    Specified by:
      `visitMethodCallExpression` in interface `GroovyCodeVisitor`
    Overrides:
      `visitMethodCallExpression` in class `CodeVisitorSupport`
    Parameters:
      `call` - the method call expression

  - ### visitPropertyExpression

    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`.
    Specified by:
      `visitPropertyExpression` in interface `GroovyCodeVisitor`
    Overrides:
      `visitPropertyExpression` in class `CodeVisitorSupport`
    Parameters:
      `expression` - the property expression

  - ### visitMethodPointerExpression

    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`).
    Specified by:
      `visitMethodPointerExpression` in interface `GroovyCodeVisitor`
    Overrides:
      `visitMethodPointerExpression` in class `CodeVisitorSupport`
    Parameters:
      `expression` - the method pointer expression

  - ### visitCastExpression

    public void visitCastExpression(CastExpression expression)
    Validates generic usage for cast target types.
    Specified by:
      `visitCastExpression` in interface `GroovyCodeVisitor`
    Overrides:
      `visitCastExpression` in class `CodeVisitorSupport`
    Parameters:
      `expression` - the cast expression to inspect