8079362: Enforce best practices for Node token API usage
Reviewed-by: hannesw, sundar
diff --git a/nashorn/src/jdk/nashorn/internal/codegen/AssignSymbols.java b/nashorn/src/jdk/nashorn/internal/codegen/AssignSymbols.java
index 87084d5..ebdf3b7 100644
--- a/nashorn/src/jdk/nashorn/internal/codegen/AssignSymbols.java
+++ b/nashorn/src/jdk/nashorn/internal/codegen/AssignSymbols.java
@@ -84,6 +84,7 @@
import jdk.nashorn.internal.ir.VarNode;
import jdk.nashorn.internal.ir.WithNode;
import jdk.nashorn.internal.ir.visitor.NodeVisitor;
+import jdk.nashorn.internal.parser.TokenType;
import jdk.nashorn.internal.runtime.Context;
import jdk.nashorn.internal.runtime.ECMAErrors;
import jdk.nashorn.internal.runtime.ErrorManager;
@@ -714,12 +715,10 @@
@Override
public Node leaveBinaryNode(final BinaryNode binaryNode) {
- switch (binaryNode.tokenType()) {
- case ASSIGN:
+ if (binaryNode.isTokenType(TokenType.ASSIGN)) {
return leaveASSIGN(binaryNode);
- default:
- return super.leaveBinaryNode(binaryNode);
}
+ return super.leaveBinaryNode(binaryNode);
}
private Node leaveASSIGN(final BinaryNode binaryNode) {
diff --git a/nashorn/src/jdk/nashorn/internal/codegen/LocalVariableTypesCalculator.java b/nashorn/src/jdk/nashorn/internal/codegen/LocalVariableTypesCalculator.java
index 67adc4a..2645f24 100644
--- a/nashorn/src/jdk/nashorn/internal/codegen/LocalVariableTypesCalculator.java
+++ b/nashorn/src/jdk/nashorn/internal/codegen/LocalVariableTypesCalculator.java
@@ -459,7 +459,7 @@
// NOTE: regardless of operator's lexical associativity, lhs is always evaluated first.
final Expression lhs = binaryNode.lhs();
final LvarType lhsType;
- if (!(lhs instanceof IdentNode && binaryNode.tokenType() == TokenType.ASSIGN)) {
+ if (!(lhs instanceof IdentNode && binaryNode.isTokenType(TokenType.ASSIGN))) {
lhsType = visitExpression(lhs);
} else {
// Can't visit IdentNode on LHS of a simple assignment, as visits imply use, and this is def.
diff --git a/nashorn/src/jdk/nashorn/internal/codegen/Lower.java b/nashorn/src/jdk/nashorn/internal/codegen/Lower.java
index b7f95aa..90afc9b 100644
--- a/nashorn/src/jdk/nashorn/internal/codegen/Lower.java
+++ b/nashorn/src/jdk/nashorn/internal/codegen/Lower.java
@@ -200,7 +200,7 @@
final String name = getConstantPropertyName(indexNode.getIndex());
if (name != null) {
// If index node is a constant property name convert index node to access node.
- assert Token.descType(indexNode.getToken()) == TokenType.LBRACKET;
+ assert indexNode.isIndex();
return new AccessNode(indexNode.getToken(), indexNode.getFinish(), indexNode.getBase(), name);
}
return super.leaveIndexNode(indexNode);
diff --git a/nashorn/src/jdk/nashorn/internal/ir/AccessNode.java b/nashorn/src/jdk/nashorn/internal/ir/AccessNode.java
index b8b6482..315ee39 100644
--- a/nashorn/src/jdk/nashorn/internal/ir/AccessNode.java
+++ b/nashorn/src/jdk/nashorn/internal/ir/AccessNode.java
@@ -28,8 +28,6 @@
import jdk.nashorn.internal.codegen.types.Type;
import jdk.nashorn.internal.ir.annotations.Immutable;
import jdk.nashorn.internal.ir.visitor.NodeVisitor;
-import jdk.nashorn.internal.parser.Token;
-import jdk.nashorn.internal.parser.TokenType;
/**
* IR representation of a property access (period operator.)
@@ -103,14 +101,6 @@
return property;
}
- /**
- * Return true if this node represents an index operation normally represented as {@link IndexNode}.
- * @return true if an index access.
- */
- public boolean isIndex() {
- return Token.descType(getToken()) == TokenType.LBRACKET;
- }
-
private AccessNode setBase(final Expression base) {
if (this.base == base) {
return this;
diff --git a/nashorn/src/jdk/nashorn/internal/ir/BaseNode.java b/nashorn/src/jdk/nashorn/internal/ir/BaseNode.java
index 9c4156e..4e59753 100644
--- a/nashorn/src/jdk/nashorn/internal/ir/BaseNode.java
+++ b/nashorn/src/jdk/nashorn/internal/ir/BaseNode.java
@@ -29,6 +29,7 @@
import jdk.nashorn.internal.codegen.types.Type;
import jdk.nashorn.internal.ir.annotations.Immutable;
+import jdk.nashorn.internal.parser.TokenType;
/**
* IR base for accessing/indexing nodes.
@@ -122,6 +123,14 @@
}
/**
+ * Return true if this node represents an index operation normally represented as {@link IndexNode}.
+ * @return true if an index access.
+ */
+ public boolean isIndex() {
+ return isTokenType(TokenType.LBRACKET);
+ }
+
+ /**
* Mark this node as being the callee operand of a {@link CallNode}.
* @return a base node identical to this one in all aspects except with its function flag set.
*/
diff --git a/nashorn/src/jdk/nashorn/internal/ir/BinaryNode.java b/nashorn/src/jdk/nashorn/internal/ir/BinaryNode.java
index 8a1762a..9f2efa3 100644
--- a/nashorn/src/jdk/nashorn/internal/ir/BinaryNode.java
+++ b/nashorn/src/jdk/nashorn/internal/ir/BinaryNode.java
@@ -312,7 +312,7 @@
@Override
public boolean isSelfModifying() {
- return isAssignment() && tokenType() != TokenType.ASSIGN;
+ return isAssignment() && !isTokenType(TokenType.ASSIGN);
}
@Override
@@ -529,7 +529,7 @@
final TokenType tokenType = tokenType();
if(tokenType == TokenType.ADD || tokenType == TokenType.ASSIGN_ADD) {
return OPTIMISTIC_UNDECIDED_TYPE;
- } else if (CAN_OVERFLOW.contains(tokenType())) {
+ } else if (CAN_OVERFLOW.contains(tokenType)) {
return Type.INT;
}
return getMostPessimisticType();
diff --git a/nashorn/src/jdk/nashorn/internal/ir/Node.java b/nashorn/src/jdk/nashorn/internal/ir/Node.java
index d6c5765..0f96e8d 100644
--- a/nashorn/src/jdk/nashorn/internal/ir/Node.java
+++ b/nashorn/src/jdk/nashorn/internal/ir/Node.java
@@ -220,26 +220,28 @@
}
/**
- * Return token tokenType from a token descriptor.
+ * Returns this node's token's type. If you want to check for the node having a specific token type,
+ * consider using {@link #isTokenType(TokenType)} instead.
*
- * @return Type of token.
+ * @return type of token.
*/
public TokenType tokenType() {
return Token.descType(token);
}
/**
- * Test token tokenType.
+ * Tests if this node has the specific token type.
*
- * @param type a type to check this token against
+ * @param type a token type to check this node's token type against
* @return true if token types match.
*/
public boolean isTokenType(final TokenType type) {
- return Token.descType(token) == type;
+ return tokenType() == type;
}
/**
- * Get the token for this location
+ * Get the token for this node. If you want to retrieve the token's type, consider using
+ * {@link #tokenType()} or {@link #isTokenType(TokenType)} instead.
* @return the token
*/
public long getToken() {
diff --git a/nashorn/src/jdk/nashorn/internal/ir/VarNode.java b/nashorn/src/jdk/nashorn/internal/ir/VarNode.java
index 1cee8cb..ec91261 100644
--- a/nashorn/src/jdk/nashorn/internal/ir/VarNode.java
+++ b/nashorn/src/jdk/nashorn/internal/ir/VarNode.java
@@ -27,7 +27,6 @@
import jdk.nashorn.internal.ir.annotations.Immutable;
import jdk.nashorn.internal.ir.visitor.NodeVisitor;
-import jdk.nashorn.internal.parser.Token;
/**
* Node represents a var/let declaration.
@@ -182,7 +181,7 @@
@Override
public void toString(final StringBuilder sb, final boolean printType) {
- sb.append(Token.descType(getToken()).getName()).append(' ');
+ sb.append(tokenType().getName()).append(' ');
name.toString(sb, printType);
if (init != null) {
diff --git a/nashorn/src/jdk/nashorn/internal/parser/Parser.java b/nashorn/src/jdk/nashorn/internal/parser/Parser.java
index 12ad59e..611ecb4 100644
--- a/nashorn/src/jdk/nashorn/internal/parser/Parser.java
+++ b/nashorn/src/jdk/nashorn/internal/parser/Parser.java
@@ -607,7 +607,7 @@
* @return whether the ident can be used as L-value
*/
private static boolean checkIdentLValue(final IdentNode ident) {
- return Token.descType(ident.getToken()).getKind() != TokenKind.KEYWORD;
+ return ident.tokenType().getKind() != TokenKind.KEYWORD;
}
/**