public class GenericsVisitor
extends ClassCodeVisitorSupport
Verify correct usage of generics. This includes:
<> usage| Constructor and description |
|---|
GenericsVisitor(SourceUnit source)Creates a visitor that validates generic type usage for one source unit. |
| 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.
|
Creates a visitor that validates generic type usage for one source unit.
source - the source unit being verifiedReturns the source unit currently being verified.
Validates generic usage for array element types.
expression - the array expression to inspect 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.
Validates generic usage for cast target types.
expression - the cast expression to inspectJLS 14.20: a catch type must be a reifiable class or union; a type parameter is not reifiable and may not be caught.
Validates generic usage in a class header and its contents.
node - the class to inspect 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.
Validates generic usage on constructor call types.
expression - the constructor call to inspectValidates generic usage for a constructor or method signature.
node - the executable member to inspectisConstructor - whether node is a constructorValidates generic usage in declared variable types.
expression - the declaration expression to inspectValidates generic usage for a field declaration.
node - the field to inspect 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().
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).
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.
Copyright © 2003-2026 The Apache Software Foundation. All rights reserved.