Class AbstractExpressionHandler
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.checks.indentation.AbstractExpressionHandler
-
- Direct Known Subclasses:
BlockParentHandler
,CaseHandler
,ImportHandler
,IndexHandler
,LabelHandler
,LambdaHandler
,MemberDefHandler
,MethodCallHandler
,NewHandler
,PackageDefHandler
,PrimordialHandler
,SwitchRuleHandler
,YieldHandler
public abstract class AbstractExpressionHandler extends Object
Abstract base class for all handlers.
-
-
Field Summary
Fields Modifier and Type Field Description private IndentLevel
indent
Indentation amount for this handler.private IndentationCheck
indentCheck
The instance ofIndentationCheck
using this handler.private DetailAST
mainAst
The AST which is handled by this handler.private AbstractExpressionHandler
parent
Containing AST handler.private String
typeName
Name used during output to user.
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractExpressionHandler(IndentationCheck indentCheck, String typeName, DetailAST expr, AbstractExpressionHandler parent)
Construct an instance of this handler with the given indentation check, name, abstract syntax tree, and parent handler.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
checkChildren(DetailAST parentNode, int[] tokenTypes, IndentLevel startIndent, boolean firstLineMatches, boolean allowNesting)
Check the indent level of the children of the specified parent expression.protected void
checkExpressionSubtree(DetailAST tree, IndentLevel indentLevel, boolean firstLineMatches, boolean allowNesting)
Check the indentation level for an expression subtree.abstract void
checkIndentation()
Check the indentation of the expression we are handling.protected void
checkLeftParen(DetailAST lparen)
Check the indentation of the left parenthesis.private void
checkLineIndent(DetailAST ast, IndentLevel indentLevel, boolean mustMatch)
Check the indentation for a single-line.private void
checkLinesIndent(DetailAstSet astSet, IndentLevel indentLevel, boolean firstLineMatches, int firstLine, boolean allowNesting)
Check the indentation for a set of lines.protected void
checkModifiers()
Check the indentation level of modifiers.protected void
checkRightParen(DetailAST lparen, DetailAST rparen)
Check the indentation of the right parenthesis.protected void
checkWrappingIndentation(DetailAST firstNode, DetailAST lastNode)
Checks indentation on wrapped lines between and includingfirstNode
andlastNode
.protected void
checkWrappingIndentation(DetailAST firstNode, DetailAST lastNode, int wrappedIndentLevel, int startIndent, boolean ignoreFirstLine)
Checks indentation on wrapped lines between and includingfirstNode
andlastNode
.protected int
expandedTabsColumnNo(DetailAST ast)
Get the column number for the start of a given expression, expanding tabs out into spaces in the process.protected void
findSubtreeAst(DetailAstSet astSet, DetailAST tree, boolean allowNesting)
Find the set of abstract syntax tree for a given subtree.protected int
getBasicOffset()
A shortcut forIndentationCheck
property.protected int
getBraceAdjustment()
A shortcut forIndentationCheck
property.protected static DetailAST
getFirstAstNode(DetailAST ast)
Get the first ast for given expression.protected static int
getFirstLine(DetailAST tree)
Get the first line number for given expression.static DetailAST
getFirstToken(DetailAST ast)
Searches in given subtree (including given node) for the token which represents first symbol for this subtree in file.IndentLevel
getIndent()
Get the indentation amount for this handler.protected IndentationCheck
getIndentCheck()
Accessor for the IndentCheck attribute.protected IndentLevel
getIndentImpl()
Compute the indentation amount for this handler.protected int
getLineStart(int lineNo)
Get the start of the line for the given line number.protected int
getLineStart(DetailAST ast)
Get the start of the line for the given expression.private int
getLineStart(String line)
Get the start of the specified line.protected DetailAST
getMainAst()
Accessor for the MainAst attribute.protected AbstractExpressionHandler
getParent()
Accessor for the Parent attribute.IndentLevel
getSuggestedChildIndent(AbstractExpressionHandler child)
Indentation level suggested for a child element.protected boolean
isOnStartOfLine(DetailAST ast)
Determines if the given expression is at the start of a line.private void
logChildError(DetailAST ast, int actualIndent, IndentLevel expectedIndent)
Log child indentation error.protected void
logError(DetailAST ast, String subtypeName, int actualIndent)
Log an indentation error.protected void
logError(DetailAST ast, String subtypeName, int actualIndent, IndentLevel expectedIndent)
Log an indentation error.protected boolean
shouldIncreaseIndent()
Checks that indentation should be increased after first line in checkLinesIndent().
-
-
-
Field Detail
-
indentCheck
private final IndentationCheck indentCheck
The instance ofIndentationCheck
using this handler.
-
parent
private final AbstractExpressionHandler parent
Containing AST handler.
-
indent
private IndentLevel indent
Indentation amount for this handler.
-
-
Constructor Detail
-
AbstractExpressionHandler
protected AbstractExpressionHandler(IndentationCheck indentCheck, String typeName, DetailAST expr, AbstractExpressionHandler parent)
Construct an instance of this handler with the given indentation check, name, abstract syntax tree, and parent handler.- Parameters:
indentCheck
- the indentation checktypeName
- the name of the handlerexpr
- the abstract syntax treeparent
- the parent handler
-
-
Method Detail
-
checkIndentation
public abstract void checkIndentation()
Check the indentation of the expression we are handling.
-
getIndent
public final IndentLevel getIndent()
Get the indentation amount for this handler. For performance reasons, this value is cached. The first time this method is called, the indentation amount is computed and stored. On further calls, the stored value is returned.- Returns:
- the expected indentation amount
-
getIndentImpl
protected IndentLevel getIndentImpl()
Compute the indentation amount for this handler.- Returns:
- the expected indentation amount
-
getSuggestedChildIndent
public IndentLevel getSuggestedChildIndent(AbstractExpressionHandler child)
Indentation level suggested for a child element. Children don't have to respect this, but most do.- Parameters:
child
- child AST (so suggestion level can differ based on child type)- Returns:
- suggested indentation for child
-
logError
protected final void logError(DetailAST ast, String subtypeName, int actualIndent)
Log an indentation error.- Parameters:
ast
- the expression that caused the errorsubtypeName
- the type of the expressionactualIndent
- the actual indent level of the expression
-
logError
protected final void logError(DetailAST ast, String subtypeName, int actualIndent, IndentLevel expectedIndent)
Log an indentation error.- Parameters:
ast
- the expression that caused the errorsubtypeName
- the type of the expressionactualIndent
- the actual indent level of the expressionexpectedIndent
- the expected indent level of the expression
-
logChildError
private void logChildError(DetailAST ast, int actualIndent, IndentLevel expectedIndent)
Log child indentation error.- Parameters:
ast
- the abstract syntax tree that causes the erroractualIndent
- the actual indent level of the expressionexpectedIndent
- the expected indent level of the expression
-
isOnStartOfLine
protected final boolean isOnStartOfLine(DetailAST ast)
Determines if the given expression is at the start of a line.- Parameters:
ast
- the expression to check- Returns:
- true if it is, false otherwise
-
getFirstToken
public static DetailAST getFirstToken(DetailAST ast)
Searches in given subtree (including given node) for the token which represents first symbol for this subtree in file.- Parameters:
ast
- a root of subtree in which the search should be performed.- Returns:
- a token which occurs first in the file.
-
getLineStart
protected final int getLineStart(DetailAST ast)
Get the start of the line for the given expression.- Parameters:
ast
- the expression to find the start of the line for- Returns:
- the start of the line for the given expression
-
getLineStart
protected final int getLineStart(int lineNo)
Get the start of the line for the given line number.- Parameters:
lineNo
- the line number to find the start for- Returns:
- the start of the line for the given expression
-
getLineStart
private int getLineStart(String line)
Get the start of the specified line.- Parameters:
line
- the specified line number- Returns:
- the start of the specified line
-
shouldIncreaseIndent
protected boolean shouldIncreaseIndent()
Checks that indentation should be increased after first line in checkLinesIndent().- Returns:
- true if indentation should be increased after first line in checkLinesIndent() false otherwise
-
checkLinesIndent
private void checkLinesIndent(DetailAstSet astSet, IndentLevel indentLevel, boolean firstLineMatches, int firstLine, boolean allowNesting)
Check the indentation for a set of lines.- Parameters:
astSet
- the set of abstract syntax tree to checkindentLevel
- the indentation levelfirstLineMatches
- whether or not the first line has to matchfirstLine
- first line of whole expressionallowNesting
- whether or not subtree nesting is allowed
-
checkLineIndent
private void checkLineIndent(DetailAST ast, IndentLevel indentLevel, boolean mustMatch)
Check the indentation for a single-line.- Parameters:
ast
- the abstract syntax tree to checkindentLevel
- the indentation levelmustMatch
- whether or not the indentation level must match
-
checkWrappingIndentation
protected void checkWrappingIndentation(DetailAST firstNode, DetailAST lastNode)
Checks indentation on wrapped lines between and includingfirstNode
andlastNode
.- Parameters:
firstNode
- First node to start examining.lastNode
- Last node to examine inclusively.
-
checkWrappingIndentation
protected void checkWrappingIndentation(DetailAST firstNode, DetailAST lastNode, int wrappedIndentLevel, int startIndent, boolean ignoreFirstLine)
Checks indentation on wrapped lines between and includingfirstNode
andlastNode
.- Parameters:
firstNode
- First node to start examining.lastNode
- Last node to examine inclusively.wrappedIndentLevel
- Indentation all wrapped lines should use.startIndent
- Indentation first line before wrapped lines used.ignoreFirstLine
- Test if first line's indentation should be checked or not.
-
checkChildren
protected final void checkChildren(DetailAST parentNode, int[] tokenTypes, IndentLevel startIndent, boolean firstLineMatches, boolean allowNesting)
Check the indent level of the children of the specified parent expression.- Parameters:
parentNode
- the parent whose children we are checkingtokenTypes
- the token types to checkstartIndent
- the starting indent levelfirstLineMatches
- whether or not the first line needs to matchallowNesting
- whether or not nested children are allowed
-
checkExpressionSubtree
protected final void checkExpressionSubtree(DetailAST tree, IndentLevel indentLevel, boolean firstLineMatches, boolean allowNesting)
Check the indentation level for an expression subtree.- Parameters:
tree
- the expression subtree to checkindentLevel
- the indentation levelfirstLineMatches
- whether or not the first line has to matchallowNesting
- whether or not subtree nesting is allowed
-
getFirstLine
protected static int getFirstLine(DetailAST tree)
Get the first line number for given expression.- Parameters:
tree
- the expression to find the first line for- Returns:
- the first line of expression
-
getFirstAstNode
protected static DetailAST getFirstAstNode(DetailAST ast)
Get the first ast for given expression.- Parameters:
ast
- the abstract syntax tree for which the starting ast is to be found- Returns:
- the first ast of the expression
-
expandedTabsColumnNo
protected final int expandedTabsColumnNo(DetailAST ast)
Get the column number for the start of a given expression, expanding tabs out into spaces in the process.- Parameters:
ast
- the expression to find the start of- Returns:
- the column number for the start of the expression
-
findSubtreeAst
protected final void findSubtreeAst(DetailAstSet astSet, DetailAST tree, boolean allowNesting)
Find the set of abstract syntax tree for a given subtree.- Parameters:
astSet
- the set of ast to addtree
- the subtree to examineallowNesting
- whether or not to allow nested subtrees
-
checkModifiers
protected void checkModifiers()
Check the indentation level of modifiers.
-
getIndentCheck
protected final IndentationCheck getIndentCheck()
Accessor for the IndentCheck attribute.- Returns:
- the IndentCheck attribute
-
getMainAst
protected final DetailAST getMainAst()
Accessor for the MainAst attribute.- Returns:
- the MainAst attribute
-
getParent
protected final AbstractExpressionHandler getParent()
Accessor for the Parent attribute.- Returns:
- the Parent attribute
-
getBasicOffset
protected final int getBasicOffset()
A shortcut forIndentationCheck
property.- Returns:
- value of basicOffset property of
IndentationCheck
-
getBraceAdjustment
protected final int getBraceAdjustment()
A shortcut forIndentationCheck
property.- Returns:
- value of braceAdjustment property
of
IndentationCheck
-
checkRightParen
protected final void checkRightParen(DetailAST lparen, DetailAST rparen)
Check the indentation of the right parenthesis.- Parameters:
lparen
- left parenthesis associated with aRparenrparen
- parenthesis to check
-
checkLeftParen
protected final void checkLeftParen(DetailAST lparen)
Check the indentation of the left parenthesis.- Parameters:
lparen
- parenthesis to check
-
-