8015955: ObjectNode.elements should be stronger typed
authorattila
Wed, 05 Jun 2013 10:44:32 +0200
changeset 17981 9b8e085aa1fe
parent 17979 adae4d39ee07
child 17982 03696682b6e5
8015955: ObjectNode.elements should be stronger typed Reviewed-by: lagergren, sundar
nashorn/src/jdk/nashorn/internal/codegen/CodeGenerator.java
nashorn/src/jdk/nashorn/internal/ir/BlockLexicalContext.java
nashorn/src/jdk/nashorn/internal/ir/ObjectNode.java
nashorn/src/jdk/nashorn/internal/parser/JSONParser.java
nashorn/src/jdk/nashorn/internal/parser/Parser.java
nashorn/src/jdk/nashorn/internal/runtime/JSONFunctions.java
--- a/nashorn/src/jdk/nashorn/internal/codegen/CodeGenerator.java	Wed Jun 05 12:08:49 2013 +0530
+++ b/nashorn/src/jdk/nashorn/internal/codegen/CodeGenerator.java	Wed Jun 05 10:44:32 2013 +0200
@@ -1326,8 +1326,7 @@
 
     @Override
     public boolean enterObjectNode(final ObjectNode objectNode) {
-        final List<Node> elements = objectNode.getElements();
-        final int        size     = elements.size();
+        final List<PropertyNode> elements = objectNode.getElements();
 
         final List<String> keys    = new ArrayList<>();
         final List<Symbol> symbols = new ArrayList<>();
@@ -1335,8 +1334,7 @@
 
         boolean hasGettersSetters = false;
 
-        for (int i = 0; i < size; i++) {
-            final PropertyNode propertyNode = (PropertyNode)elements.get(i);
+        for (PropertyNode propertyNode: elements) {
             final Node         value        = propertyNode.getValue();
             final String       key          = propertyNode.getKeyName();
             final Symbol       symbol       = value == null ? null : propertyNode.getSymbol();
--- a/nashorn/src/jdk/nashorn/internal/ir/BlockLexicalContext.java	Wed Jun 05 12:08:49 2013 +0530
+++ b/nashorn/src/jdk/nashorn/internal/ir/BlockLexicalContext.java	Wed Jun 05 10:44:32 2013 +0200
@@ -63,7 +63,6 @@
         return sstack.pop();
     }
 
-    @SuppressWarnings("unchecked")
     @Override
     public <T extends LexicalContextNode> T pop(final T node) {
         T expected = node;
--- a/nashorn/src/jdk/nashorn/internal/ir/ObjectNode.java	Wed Jun 05 12:08:49 2013 +0530
+++ b/nashorn/src/jdk/nashorn/internal/ir/ObjectNode.java	Wed Jun 05 10:44:32 2013 +0200
@@ -27,7 +27,6 @@
 
 import java.util.Collections;
 import java.util.List;
-
 import jdk.nashorn.internal.ir.annotations.Immutable;
 import jdk.nashorn.internal.ir.visitor.NodeVisitor;
 
@@ -38,7 +37,7 @@
 public final class ObjectNode extends Node {
 
     /** Literal elements. */
-    private final List<Node> elements;
+    private final List<PropertyNode> elements;
 
     /**
      * Constructor
@@ -47,12 +46,12 @@
      * @param finish   finish
      * @param elements the elements used to initialize this ObjectNode
      */
-    public ObjectNode(final long token, final int finish, final List<Node> elements) {
+    public ObjectNode(final long token, final int finish, final List<PropertyNode> elements) {
         super(token, finish);
         this.elements = elements;
     }
 
-    private ObjectNode(final ObjectNode objectNode, final List<Node> elements) {
+    private ObjectNode(final ObjectNode objectNode, final List<PropertyNode> elements) {
         super(objectNode);
         this.elements = elements;
     }
@@ -60,7 +59,7 @@
     @Override
     public Node accept(final NodeVisitor<? extends LexicalContext> visitor) {
         if (visitor.enterObjectNode(this)) {
-            return visitor.leaveObjectNode(setElements(Node.accept(visitor, Node.class, elements)));
+            return visitor.leaveObjectNode(setElements(Node.accept(visitor, PropertyNode.class, elements)));
         }
 
         return this;
@@ -92,11 +91,11 @@
      * Get the elements of this literal node
      * @return a list of elements
      */
-    public List<Node> getElements() {
+    public List<PropertyNode> getElements() {
         return Collections.unmodifiableList(elements);
     }
 
-    private ObjectNode setElements(final List<Node> elements) {
+    private ObjectNode setElements(final List<PropertyNode> elements) {
         if (this.elements == elements) {
             return this;
         }
--- a/nashorn/src/jdk/nashorn/internal/parser/JSONParser.java	Wed Jun 05 12:08:49 2013 +0530
+++ b/nashorn/src/jdk/nashorn/internal/parser/JSONParser.java	Wed Jun 05 10:44:32 2013 +0200
@@ -282,7 +282,7 @@
         next();
 
         // Prepare to accumulate elements.
-        final List<Node> elements = new ArrayList<>();
+        final List<PropertyNode> elements = new ArrayList<>();
 
         // Create a block for the object literal.
 loop:
@@ -298,7 +298,7 @@
 
             default:
                 // Get and add the next property.
-                final Node property = propertyAssignment();
+                final PropertyNode property = propertyAssignment();
                 elements.add(property);
 
                 // Comma between property assigments is mandatory in JSON.
@@ -317,7 +317,7 @@
      * Parse a property assignment from the token stream
      * @return the property assignment as a Node
      */
-    private Node propertyAssignment() {
+    private PropertyNode propertyAssignment() {
         // Capture firstToken.
         final long propertyToken = token;
         LiteralNode<?> name = null;
--- a/nashorn/src/jdk/nashorn/internal/parser/Parser.java	Wed Jun 05 12:08:49 2013 +0530
+++ b/nashorn/src/jdk/nashorn/internal/parser/Parser.java	Wed Jun 05 10:44:32 2013 +0200
@@ -59,7 +59,6 @@
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
-
 import jdk.nashorn.internal.codegen.CompilerConstants;
 import jdk.nashorn.internal.codegen.Namespace;
 import jdk.nashorn.internal.ir.AccessNode;
@@ -2028,7 +2027,7 @@
             }
         }
 
-        return new ObjectNode(objectToken, finish, new ArrayList<Node>(map.values()));
+        return new ObjectNode(objectToken, finish, new ArrayList<>(map.values()));
     }
 
     /**
--- a/nashorn/src/jdk/nashorn/internal/runtime/JSONFunctions.java	Wed Jun 05 12:08:49 2013 +0530
+++ b/nashorn/src/jdk/nashorn/internal/runtime/JSONFunctions.java	Wed Jun 05 10:44:32 2013 +0200
@@ -25,9 +25,11 @@
 
 package jdk.nashorn.internal.runtime;
 
+import static jdk.nashorn.internal.runtime.arrays.ArrayIndex.getArrayIndexNoThrow;
+import static jdk.nashorn.internal.runtime.arrays.ArrayIndex.isValidArrayIndex;
+
 import java.lang.invoke.MethodHandle;
 import java.util.Iterator;
-import java.util.List;
 import jdk.nashorn.internal.ir.LiteralNode;
 import jdk.nashorn.internal.ir.Node;
 import jdk.nashorn.internal.ir.ObjectNode;
@@ -36,8 +38,6 @@
 import jdk.nashorn.internal.parser.JSONParser;
 import jdk.nashorn.internal.parser.TokenType;
 import jdk.nashorn.internal.runtime.linker.Bootstrap;
-import static jdk.nashorn.internal.runtime.arrays.ArrayIndex.getArrayIndexNoThrow;
-import static jdk.nashorn.internal.runtime.arrays.ArrayIndex.isValidArrayIndex;
 
 /**
  * Utilities used by "JSON" object implementation.
@@ -171,10 +171,8 @@
             final ObjectNode   objNode  = (ObjectNode) node;
             final ScriptObject object   = ((GlobalObject)global).newObject();
             final boolean      strict   = global.isStrictContext();
-            final List<Node>   elements = objNode.getElements();
 
-            for (final Node elem : elements) {
-                final PropertyNode pNode     = (PropertyNode) elem;
+            for (final PropertyNode pNode: objNode.getElements()) {
                 final Node         valueNode = pNode.getValue();
 
                 final String name = pNode.getKeyName();