I have a class hierarchy representing different language constructs:
Expression <- NumericLiteral
UnaryExpression
BinaryExpression
IndexingExpression
IteratedExpression
...
The objects of these classes form complex tree hierarchies on which I have to perform various structural checks, e.g. if the node is an IteratedExpression then its first child should be an IndexingExpression. If the check only involves one level I can use the Visitor pattern, but in more complex cases as in the example below I am using instanceof.
void visit(IteratedExpression node) {
if (!(node.getChild(0) instanceof IndexingExpression)) {
// report error
}
}
Is it a proper use of instanceof or I have a flaw in my design? What are the alternatives?
Since there were some alternatives suggested I would like to emphasize the first part of the question:
Is it a proper use of instanceof or I have a flaw in my design?