changeset 2396:3f3cc8d3f13c

8011677: EndPosTables should avoid hidden references to Parser Reviewed-by: mcimadamore
author jjg
date Mon, 08 Apr 2013 11:57:37 -0700
parents b402b93cbe38
children eb134c8e931d 1f19b84efa6d
files src/share/classes/com/sun/tools/javac/parser/JavacParser.java
diffstat 1 files changed, 26 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/parser/JavacParser.java	Mon Apr 08 11:54:26 2013 -0700
+++ b/src/share/classes/com/sun/tools/javac/parser/JavacParser.java	Mon Apr 08 11:57:37 2013 -0700
@@ -171,8 +171,8 @@
 
     protected AbstractEndPosTable newEndPosTable(boolean keepEndPositions) {
         return  keepEndPositions
-                ? new SimpleEndPosTable()
-                : new EmptyEndPosTable();
+                ? new SimpleEndPosTable(this)
+                : new EmptyEndPosTable(this);
     }
 
     protected DocCommentTable newDocCommentTable(boolean keepDocComments, ParserFactory fac) {
@@ -3088,6 +3088,7 @@
             toplevel.docComments = docComments;
         if (keepLineMap)
             toplevel.lineMap = S.getLineMap();
+        this.endPosTable.setParser(null); // remove reference to parser
         toplevel.endPositions = this.endPosTable;
         return toplevel;
     }
@@ -4003,11 +4004,12 @@
     /*
      * a functional source tree and end position mappings
      */
-    protected class SimpleEndPosTable extends AbstractEndPosTable {
+    protected static class SimpleEndPosTable extends AbstractEndPosTable {
 
         private final Map<JCTree, Integer> endPosMap;
 
-        SimpleEndPosTable() {
+        SimpleEndPosTable(JavacParser parser) {
+            super(parser);
             endPosMap = new HashMap<JCTree, Integer>();
         }
 
@@ -4016,12 +4018,12 @@
         }
 
         protected <T extends JCTree> T to(T t) {
-            storeEnd(t, token.endPos);
+            storeEnd(t, parser.token.endPos);
             return t;
         }
 
         protected <T extends JCTree> T toP(T t) {
-            storeEnd(t, S.prevToken().endPos);
+            storeEnd(t, parser.S.prevToken().endPos);
             return t;
         }
 
@@ -4043,7 +4045,11 @@
     /*
      * a default skeletal implementation without any mapping overhead.
      */
-    protected class EmptyEndPosTable extends AbstractEndPosTable {
+    protected static class EmptyEndPosTable extends AbstractEndPosTable {
+
+        EmptyEndPosTable(JavacParser parser) {
+            super(parser);
+        }
 
         protected void storeEnd(JCTree tree, int endpos) { /* empty */ }
 
@@ -4065,13 +4071,21 @@
 
     }
 
-    protected abstract class AbstractEndPosTable implements EndPosTable {
+    protected static abstract class AbstractEndPosTable implements EndPosTable {
+        /**
+         * The current parser.
+         */
+        protected JavacParser parser;
 
         /**
          * Store the last error position.
          */
         protected int errorEndPos;
 
+        public AbstractEndPosTable(JavacParser parser) {
+            this.parser = parser;
+        }
+
         /**
          * Store ending position for a tree, the value of which is the greater
          * of last error position and the given ending position.
@@ -4106,5 +4120,9 @@
                 errorEndPos = errPos;
             }
         }
+
+        protected void setParser(JavacParser parser) {
+            this.parser = parser;
+        }
     }
 }