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
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
ConstructorsConstructorDescriptionGenericsVisitor(SourceUnit source) Creates a visitor that validates generic type usage for one source unit. -
Method Summary
Modifier and TypeMethodDescriptionprotected SourceUnitReturns the source unit currently being verified.voidvisitArrayExpression(ArrayExpression expression) Validates generic usage for array element types.voidvisitBinaryExpression(BinaryExpression expression) Groovy represents the type operand ofinstanceofas aClassExpression, but it is not a class literal (T.class).voidvisitCastExpression(CastExpression expression) Validates generic usage for cast target types.voidvisitCatchStatement(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.voidvisitClass(ClassNode node) Validates generic usage in a class header and its contents.voidvisitClassExpression(ClassExpression expression) JLS 15.8.2: a class literal may not name a type variable or a parameterized type (T.class,Cell<String>.class).voidValidates generic usage on constructor call types.protected voidvisitConstructorOrMethod(MethodNode node, boolean isConstructor) Validates generic usage for a constructor or method signature.voidvisitDeclarationExpression(DeclarationExpression expression) Validates generic usage in declared variable types.voidvisitField(FieldNode node) Validates generic usage for a field declaration.voidJLS 4.5.2: a static member of a generic type must be referred to by the generic type name, not a parameterization such asCell<String>.id().voidvisitMethodPointerExpression(MethodPointerExpression expression) JLS 15.13:TypeName::mandTypeName::neware method or constructor references, not class literals.voidvisitPropertyExpression(PropertyExpression expression) JLS 4.5.2 also applies to static fields accessed asCell<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, visitYieldStatementMethods 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, visitVariableExpressionMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.codehaus.groovy.ast.GroovyCodeVisitor
visit, visit, visitEmptyExpression, visitListOfExpressions
-
Constructor Details
-
GenericsVisitor
Creates a visitor that validates generic type usage for one source unit.- Parameters:
source- the source unit being verified
-
-
Method Details
-
getSourceUnit
Returns the source unit currently being verified.- Specified by:
getSourceUnitin classClassCodeVisitorSupport- Returns:
- the active source unit
-
visitClass
Validates generic usage in a class header and its contents.- Specified by:
visitClassin interfaceGroovyClassVisitor- Overrides:
visitClassin classClassCodeVisitorSupport- Parameters:
node- the class to inspect
-
visitField
Validates generic usage for a field declaration.- Specified by:
visitFieldin interfaceGroovyClassVisitor- Overrides:
visitFieldin classClassCodeVisitorSupport- Parameters:
node- the field to inspect
-
visitCatchStatement
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:
visitCatchStatementin interfaceGroovyCodeVisitor- Overrides:
visitCatchStatementin classClassCodeVisitorSupport- Parameters:
statement- the catch statement to visit
-
visitConstructorOrMethod
Validates generic usage for a constructor or method signature.- Overrides:
visitConstructorOrMethodin classClassCodeVisitorSupport- Parameters:
node- the executable member to inspectisConstructor- whethernodeis a constructor
-
visitConstructorCallExpression
Validates generic usage on constructor call types.- Specified by:
visitConstructorCallExpressionin interfaceGroovyCodeVisitor- Overrides:
visitConstructorCallExpressionin classCodeVisitorSupport- Parameters:
expression- the constructor call to inspect
-
visitDeclarationExpression
Validates generic usage in declared variable types.- Specified by:
visitDeclarationExpressionin interfaceGroovyCodeVisitor- Overrides:
visitDeclarationExpressionin classClassCodeVisitorSupport- Parameters:
expression- the declaration expression to inspect
-
visitArrayExpression
Validates generic usage for array element types.- Specified by:
visitArrayExpressionin interfaceGroovyCodeVisitor- Overrides:
visitArrayExpressionin classCodeVisitorSupport- Parameters:
expression- the array expression to inspect
-
visitClassExpression
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.classis aClassExpression;Foo<String>.classmay still be aPropertyExpressionwhose object is that class expression — seevisitPropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression).ClassExpressionis also the TypeName of a static qualifier,instanceofoperand, 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:
visitClassExpressionin interfaceGroovyCodeVisitor- Overrides:
visitClassExpressionin classCodeVisitorSupport- Parameters:
expression- the class expression
-
visitBinaryExpression
Groovy represents the type operand ofinstanceofas aClassExpression, but it is not a class literal (T.class). Do not visit that operand as a class expression (that would rejectinstanceof TasT.class). Still runcheckGenericsUsage(ClassNode)so nested-type well-formedness applies (Outer<?>.Innerwith a genericInner,Outer.Inner<?>). Parameterizedinstanceofis handled at parse (AstBuilder#rejectNonReifiableInstanceof): the grammar allows type arguments soinstanceof List<?>can parse, whileinstanceof Map<String,Integer>is rejected (JLS 15.20.2).InstanceOfVerifierthen checks primitives and type-parameter targets.- Specified by:
visitBinaryExpressionin interfaceGroovyCodeVisitor- Overrides:
visitBinaryExpressionin classCodeVisitorSupport- Parameters:
expression- the binary expression
-
visitMethodCallExpression
JLS 4.5.2: a static member of a generic type must be referred to by the generic type name, not a parameterization such asCell<String>.id().- Specified by:
visitMethodCallExpressionin interfaceGroovyCodeVisitor- Overrides:
visitMethodCallExpressionin classCodeVisitorSupport- Parameters:
call- the method call expression
-
visitPropertyExpression
JLS 4.5.2 also applies to static fields accessed asCell<String>.ID. JLS 15.8.2:Cell<String>.classis a class literal, not a static member selection — do not use the 4.5.2 diagnostic for.class.- Specified by:
visitPropertyExpressionin interfaceGroovyCodeVisitor- Overrides:
visitPropertyExpressionin classCodeVisitorSupport- Parameters:
expression- the property expression
-
visitMethodPointerExpression
JLS 15.13:TypeName::mandTypeName::neware method or constructor references, not class literals. A parameterization ofTypeNameis legal for an instance method or constructor reference (Iterable<String>::asCollection,HashMap<String,Integer>::new).- Specified by:
visitMethodPointerExpressionin interfaceGroovyCodeVisitor- Overrides:
visitMethodPointerExpressionin classCodeVisitorSupport- Parameters:
expression- the method pointer expression
-
visitCastExpression
Validates generic usage for cast target types.- Specified by:
visitCastExpressionin interfaceGroovyCodeVisitor- Overrides:
visitCastExpressionin classCodeVisitorSupport- Parameters:
expression- the cast expression to inspect
-