Package org.codehaus.groovy.ast
Class MethodInvocationTrap
java.lang.Object
org.codehaus.groovy.ast.CodeVisitorSupport
org.codehaus.groovy.ast.MethodInvocationTrap
- All Implemented Interfaces:
GroovyCodeVisitor
Abstract base class for AST visitors that trap and intercept specific method invocations.
Primarily used to detect AstBuilder "from code" invocations and convert them to the equivalent
"from string" approach during compilation.
Subclasses implement isBuildInvocation() to identify target method calls and
handleTargetMethodCallExpression() to transform them. The base class provides utility methods
for error reporting and closure-to-source conversion.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ReaderSourceThe source reader for extracting closure source code.protected final SourceUnitThe source unit for reporting errors and accessing compilation context. -
Constructor Summary
ConstructorsConstructorDescriptionMethodInvocationTrap(ReaderSource source, SourceUnit sourceUnit) Creates a method invocation trap with the given source and source unit. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidReports an error back to the source unit with line and column information.protected StringconvertClosureToSource(ClosureExpression expression) Converts a closure expression into its source code equivalent using the source reader.protected abstract booleanHandles a target method call expression by transforming or validating it.protected abstract booleanDetermines whether a method call expression is a target invocation for special handling.voidVisits a method call expression, checking if it is a target build invocation.Methods inherited from class org.codehaus.groovy.ast.CodeVisitorSupport
afterSwitchCaseStatementsVisited, 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, visitMethodPointerExpression, visitMethodReferenceExpression, visitNotExpression, visitPostfixExpression, visitPrefixExpression, visitPropertyExpression, visitRangeExpression, visitReturnStatement, visitShortTernaryExpression, visitSpreadExpression, visitSpreadMapExpression, visitStaticMethodCallExpression, visitSwitch, visitSynchronizedStatement, visitTernaryExpression, visitThrowStatement, visitTryCatchFinally, visitTupleExpression, visitUnaryMinusExpression, visitUnaryPlusExpression, visitVariableExpression, visitWhileLoopMethods 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
-
Field Details
-
source
The source reader for extracting closure source code. -
sourceUnit
The source unit for reporting errors and accessing compilation context.
-
-
Constructor Details
-
MethodInvocationTrap
Creates a method invocation trap with the given source and source unit. Both arguments are required;nullvalues will raiseIllegalArgumentException.- Parameters:
source- theReaderSourcefor extracting source code from closures, cannot benullsourceUnit- theSourceUnitfor error reporting, cannot benull- Throws:
IllegalArgumentException- if either parameter isnull
-
-
Method Details
-
visitMethodCallExpression
Visits a method call expression, checking if it is a target build invocation. If identified as a target invocation, callshandleTargetMethodCallExpression(). If that method returnstrue, resumes normal tree walking; otherwise stops. For non-target method calls, continues normal tree walking regardless.- Specified by:
visitMethodCallExpressionin interfaceGroovyCodeVisitor- Overrides:
visitMethodCallExpressionin classCodeVisitorSupport- Parameters:
call- the method call expression that may or may not be an AstBuilder 'from code' invocation
-
addError
Reports an error back to the source unit with line and column information. The error is collected and processing continues.- Parameters:
msg- the error messageexpr- the expression that caused the error
-
convertClosureToSource
Converts a closure expression into its source code equivalent using the source reader. If an error occurs during conversion, adds an error to the source unit and returnsnull.- Parameters:
expression- the closure expression to convert- Returns:
- the source code string of the closure, or
nullif conversion fails
-
isBuildInvocation
Determines whether a method call expression is a target invocation for special handling. Subclasses override to identify invocations matching their specific criteria.- Parameters:
call- the method call expression to check- Returns:
trueif this call is a target invocation requiring special handling
-
handleTargetMethodCallExpression
Handles a target method call expression by transforming or validating it. Subclasses override to implement the transformation logic.- Parameters:
call- the target method call expression- Returns:
trueto continue tree walking,falseto stop
-