Package com.github.javaparser.ast.expr
Class InstanceOfExpr
- java.lang.Object
-
- com.github.javaparser.ast.Node
-
- com.github.javaparser.ast.expr.Expression
-
- com.github.javaparser.ast.expr.InstanceOfExpr
-
- All Implemented Interfaces:
NodeWithExpression<InstanceOfExpr>,NodeWithRange<Node>,NodeWithTokenRange<Node>,NodeWithType<InstanceOfExpr,ReferenceType>,Observable,Visitable,HasParentNode<Node>,Cloneable
public class InstanceOfExpr extends Expression implements NodeWithType<InstanceOfExpr,ReferenceType>, NodeWithExpression<InstanceOfExpr>
The instanceof statement
Java ?? to 13
Theinstanceofexpression is a relational operator, evaluating to true if the object on the left hand side is an instance of the type (ReferenceType) on the right hand side.
Example:tool instanceof Drill
This is then used wherever a conditional/boolean value can be used. For example:
if (obj instanceof String) { String s = (String) obj; // use s }
Java 14
Since JDK14, it is possible to bind a variable that is cast to the type being tested against. This is referred to as aPatternwithin JEP305, and avoids the need to cast to the desired type.
Example:tool instanceof Drill d
This is then used wherever a conditional/boolean value can be used. The scope of the variable created can vary, and is defined further within JEP305.
if (obj instanceof String s) { // can use s here } else { // can't use s here }
JDK14 Grammar
Per JEP305:
RelationalExpression: ... RelationalExpression instanceof ReferenceType RelationalExpression instanceof Pattern Pattern: ReferenceType Identifier- Author:
- Julio Vilmar Gesser
- See Also:
PatternExpr, JEP305: https://bugs.openjdk.java.net/browse/JDK-8181287, https://docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-15.20
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.github.javaparser.ast.Node
Node.BreadthFirstIterator, Node.DirectChildrenIterator, Node.ObserverRegistrationMode, Node.ParentsVisitor, Node.Parsedness, Node.PostOrderIterator, Node.PreOrderIterator, Node.TreeTraversal
-
-
Field Summary
-
Fields inherited from class com.github.javaparser.ast.expr.Expression
EXCLUDE_ENCLOSED_EXPR, IS_NOT_ENCLOSED_EXPR
-
Fields inherited from class com.github.javaparser.ast.Node
ABSOLUTE_BEGIN_LINE, ABSOLUTE_END_LINE, LINE_SEPARATOR_KEY, NODE_BY_BEGIN_POSITION, PHANTOM_KEY, prettyPrinterNoCommentsConfiguration, PRINTER_KEY, SYMBOL_RESOLVER_KEY
-
-
Constructor Summary
Constructors Constructor Description InstanceOfExpr()InstanceOfExpr(Expression expression, ReferenceType type)InstanceOfExpr(Expression expression, ReferenceType type, PatternExpr pattern)InstanceOfExpr(TokenRange tokenRange, Expression expression, ReferenceType type, PatternExpr pattern)This constructor is used by the parser and is considered private.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <R,A>
Raccept(GenericVisitor<R,A> v, A arg)Accept method for visitor support.<A> voidaccept(VoidVisitor<A> v, A arg)Accept method for visitor support.InstanceOfExprasInstanceOfExpr()InstanceOfExprclone()ExpressiongetExpression()InstanceOfExprMetaModelgetMetaModel()Optional<SimpleName>getName()Helper method which, if this is an expression with a type pattern, returns the identifier/name.Optional<PatternExpr>getPattern()ReferenceTypegetType()Gets the typevoidifInstanceOfExpr(Consumer<InstanceOfExpr> action)booleanisInstanceOfExpr()booleanremove(Node node)InstanceOfExprremovePattern()booleanreplace(Node node, Node replacementNode)InstanceOfExprsetExpression(Expression expression)InstanceOfExprsetPattern(PatternExpr pattern)InstanceOfExprsetType(ReferenceType type)Sets the typeOptional<InstanceOfExpr>toInstanceOfExpr()-
Methods inherited from class com.github.javaparser.ast.expr.Expression
appearsInAssignmentContext, appearsInInvocationContext, asAnnotationExpr, asArrayAccessExpr, asArrayCreationExpr, asArrayInitializerExpr, asAssignExpr, asBinaryExpr, asBooleanLiteralExpr, asCastExpr, asCharLiteralExpr, asClassExpr, asConditionalExpr, asDoubleLiteralExpr, asEnclosedExpr, asFieldAccessExpr, asIntegerLiteralExpr, asLambdaExpr, asLiteralExpr, asLiteralStringValueExpr, asLongLiteralExpr, asMarkerAnnotationExpr, asMethodCallExpr, asMethodReferenceExpr, asNameExpr, asNormalAnnotationExpr, asNullLiteralExpr, asObjectCreationExpr, asPatternExpr, asRecordPatternExpr, asSingleMemberAnnotationExpr, asStringLiteralExpr, asSuperExpr, asSwitchExpr, asTextBlockLiteralExpr, asThisExpr, asTypeExpr, asTypePatternExpr, asUnaryExpr, asVariableDeclarationExpr, calculateResolvedType, elidesTypeArguments, ifAnnotationExpr, ifArrayAccessExpr, ifArrayCreationExpr, ifArrayInitializerExpr, ifAssignExpr, ifBinaryExpr, ifBooleanLiteralExpr, ifCastExpr, ifCharLiteralExpr, ifClassExpr, ifConditionalExpr, ifDoubleLiteralExpr, ifEnclosedExpr, ifFieldAccessExpr, ifIntegerLiteralExpr, ifLambdaExpr, ifLiteralExpr, ifLiteralStringValueExpr, ifLongLiteralExpr, ifMarkerAnnotationExpr, ifMethodCallExpr, ifMethodReferenceExpr, ifNameExpr, ifNormalAnnotationExpr, ifNullLiteralExpr, ifObjectCreationExpr, ifPatternExpr, ifRecordPatternExpr, ifSingleMemberAnnotationExpr, ifStringLiteralExpr, ifSuperExpr, ifSwitchExpr, ifTextBlockLiteralExpr, ifThisExpr, ifTypeExpr, ifTypePatternExpr, ifUnaryExpr, ifVariableDeclarationExpr, isAnnotationExpr, isArrayAccessExpr, isArrayCreationExpr, isArrayInitializerExpr, isAssignExpr, isAssignmentContext, isBinaryExpr, isBooleanLiteralExpr, isCastExpr, isCharLiteralExpr, isClassExpr, isConditionalExpr, isDoubleLiteralExpr, isEnclosedExpr, isFieldAccessExpr, isIntegerLiteralExpr, isInvocationContext, isLambdaExpr, isLiteralExpr, isLiteralStringValueExpr, isLongLiteralExpr, isMarkerAnnotationExpr, isMethodCallExpr, isMethodReferenceExpr, isNameExpr, isNormalAnnotationExpr, isNullLiteralExpr, isObjectCreationExpr, isPatternExpr, isPolyExpression, isQualified, isRecordPatternExpr, isSingleMemberAnnotationExpr, isStandaloneExpression, isStringLiteralExpr, isSuperExpr, isSwitchExpr, isTextBlockLiteralExpr, isThisExpr, isTypeExpr, isTypePatternExpr, isUnaryExpr, isVariableDeclarationExpr, toAnnotationExpr, toArrayAccessExpr, toArrayCreationExpr, toArrayInitializerExpr, toAssignExpr, toBinaryExpr, toBooleanLiteralExpr, toCastExpr, toCharLiteralExpr, toClassExpr, toConditionalExpr, toDoubleLiteralExpr, toEnclosedExpr, toFieldAccessExpr, toIntegerLiteralExpr, toLambdaExpr, toLiteralExpr, toLiteralStringValueExpr, toLongLiteralExpr, toMarkerAnnotationExpr, toMethodCallExpr, toMethodReferenceExpr, toNameExpr, toNormalAnnotationExpr, toNullLiteralExpr, toObjectCreationExpr, toPatternExpr, toRecordPatternExpr, toSingleMemberAnnotationExpr, toStringLiteralExpr, toSuperExpr, toSwitchExpr, toTextBlockLiteralExpr, toThisExpr, toTypeExpr, toTypePatternExpr, toUnaryExpr, toVariableDeclarationExpr
-
Methods inherited from class com.github.javaparser.ast.Node
addOrphanComment, containsData, createDefaultPrinter, createDefaultPrinter, customInitialization, equals, findAll, findAll, findAll, findByRange, findCompilationUnit, findData, findFirst, findFirst, findFirst, findRootNode, getAllContainedComments, getChildNodes, getChildNodesByType, getComment, getData, getDataKeys, getDefaultPrinterConfiguration, getLineEndingStyle, getLineEndingStyleOrDefault, getNodesByType, getOrphanComments, getParentNode, getParentNodeForChildren, getParsed, getPrinter, getPrinter, getRange, getSymbolResolver, getTokenRange, hashCode, hasScope, isAncestorOf, isPhantom, isRegistered, notifyPropertyChange, register, register, registerForSubtree, remove, removeComment, removeData, removeForced, removeOrphanComment, replace, setAsParentNodeOf, setAsParentNodeOf, setBlockComment, setComment, setData, setLineComment, setParentNode, setParsed, setRange, setTokenRange, stream, stream, toString, toString, tryAddImportToParentCompilationUnit, unregister, walk, walk, walk
-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.github.javaparser.HasParentNode
findAncestor, findAncestor, findAncestor, hasParentNode, isDescendantOf
-
Methods inherited from interface com.github.javaparser.ast.nodeTypes.NodeWithExpression
setExpression
-
Methods inherited from interface com.github.javaparser.ast.nodeTypes.NodeWithRange
containsWithin, containsWithinRange, getBegin, getEnd, hasRange
-
Methods inherited from interface com.github.javaparser.ast.nodeTypes.NodeWithType
getTypeAsString, setType, setType, tryAddImportToParentCompilationUnit
-
-
-
-
Constructor Detail
-
InstanceOfExpr
public InstanceOfExpr()
-
InstanceOfExpr
public InstanceOfExpr(Expression expression, ReferenceType type)
-
InstanceOfExpr
public InstanceOfExpr(Expression expression, ReferenceType type, PatternExpr pattern)
-
InstanceOfExpr
public InstanceOfExpr(TokenRange tokenRange, Expression expression, ReferenceType type, PatternExpr pattern)
This constructor is used by the parser and is considered private.
-
-
Method Detail
-
getName
public Optional<SimpleName> getName()
Helper method which, if this is an expression with a type pattern, returns the identifier/name.
For example:obj instanceof String stringName
In this example,getName()returnsstringName
-
accept
public <R,A> R accept(GenericVisitor<R,A> v, A arg)
Description copied from interface:VisitableAccept method for visitor support.- Specified by:
acceptin interfaceVisitable- Type Parameters:
R- the type of the return value of the visitorA- the type the user argument passed to the visitor- Parameters:
v- the visitor implementationarg- the argument passed to the visitor (of type A)- Returns:
- the result of the visit (of type R)
-
accept
public <A> void accept(VoidVisitor<A> v, A arg)
Description copied from interface:VisitableAccept method for visitor support.
-
asInstanceOfExpr
public InstanceOfExpr asInstanceOfExpr()
- Overrides:
asInstanceOfExprin classExpression
-
clone
public InstanceOfExpr clone()
- Overrides:
clonein classExpression
-
getExpression
public Expression getExpression()
- Specified by:
getExpressionin interfaceNodeWithExpression<InstanceOfExpr>
-
getMetaModel
public InstanceOfExprMetaModel getMetaModel()
- Overrides:
getMetaModelin classExpression- Returns:
- get JavaParser specific node introspection information.
-
getPattern
public Optional<PatternExpr> getPattern()
-
getType
public ReferenceType getType()
Description copied from interface:NodeWithTypeGets the type- Specified by:
getTypein interfaceNodeWithType<InstanceOfExpr,ReferenceType>- Returns:
- the type
-
ifInstanceOfExpr
public void ifInstanceOfExpr(Consumer<InstanceOfExpr> action)
- Overrides:
ifInstanceOfExprin classExpression
-
isInstanceOfExpr
public boolean isInstanceOfExpr()
- Overrides:
isInstanceOfExprin classExpression
-
removePattern
public InstanceOfExpr removePattern()
-
setExpression
public InstanceOfExpr setExpression(Expression expression)
- Specified by:
setExpressionin interfaceNodeWithExpression<InstanceOfExpr>
-
setPattern
public InstanceOfExpr setPattern(PatternExpr pattern)
-
setType
public InstanceOfExpr setType(ReferenceType type)
Description copied from interface:NodeWithTypeSets the type- Specified by:
setTypein interfaceNodeWithType<InstanceOfExpr,ReferenceType>- Parameters:
type- the type- Returns:
- this
-
toInstanceOfExpr
public Optional<InstanceOfExpr> toInstanceOfExpr()
- Overrides:
toInstanceOfExprin classExpression
-
-