1. Tim Vernum
  2. syntactic

Commits

Tim Vernum  committed a3679eb

Convert 'this' references

  • Participants
  • Parent commits 42bb618
  • Branches default

Comments (0)

Files changed (2)

File convert/source/java/main/org/adjective/syntactic/convert/j7to8/Convert7To8Visitor.java

View file
 import org.adjective.syntactic.parser.util.JavaType;
 import org.adjective.syntactic.parser.util.ModifierSet;
 
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Stack;
 
 public class Convert7To8Visitor extends DefaultVisitor<Object, Object>
 {
+    private final Stack<String> className;
     // TODO Support nested scopes
     private Map<String, HasModifiers> vars;
 
+    public Convert7To8Visitor()
+    {
+        className = new Stack<String>();
+    }
+
+    @Override
+    public Object visit(final ASTClassOrInterfaceDeclaration node, final Object data)
+    {
+        className.push(node.getName().getIdentifier());
+        try
+        {
+            return super.visit(node, data);
+        }
+        finally
+        {
+            className.pop();
+        }
+    }
+
     @Override
     public Object visit(final ASTMethodDeclaration node, final Object data)
     {
                             modifiers.getModifiers().set(ModifierSet.Modifier.FINAL);
                         }
                     }
-                    else if (prefix.is(ASTThis.class))
-                    {
-                        // TODO convert references to "this" to enclosing class
-                    }
                 }
-
-                return data;
+                if (prefix.is(ASTThis.class))
+                {
+                    final ASTQualifiedIdentifier newPrefix = new ASTQualifiedIdentifier(className.peek());
+                    final ASTMemberSuffix newSuffix = new ASTMemberSuffix(prefix.as(ASTThis.class));
+                    node.setTo(newPrefix, merge(newSuffix, node.getSuffixes()));
+                }
+                return super.visit(node, data);
             }
 
         }, null);
     }
 
+    private <T> Iterable<T> merge(final T first, final Iterable<T> rest)
+    {
+        final ArrayList<T> list = new ArrayList<T>();
+        list.add(first);
+        for (T t : rest)
+        {
+            list.add(t);
+        }
+        return list;
+    }
+
     private JavaType getType(ASTLocalVariableDeclaration declaration, ASTVariableDeclarator variable)
     {
         return BaseNode.makeType(declaration.getTypeNode(), variable.getArraySuffix());

File convert/source/java/samples/sample/LambdaThis.java

View file
+/*
+ * ------------------------------------------------------------------------
+ *  Copyright 2013 Tim Vernum
+ *  ------------------------------------------------------------------------
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *  ------------------------------------------------------------------------
+ */
+package sample;
+
+import java.util.Date;
+
+public class LambdaThis {
+
+    public Runnable getRunnable()
+    {
+        Runnable r =  () -> { this.printTime(System.currentTimeMillis(), this); };
+        return r;
+    }
+
+    private void printTime(long time, final LambdaThis ignore) {
+        System.out.println(new Date(time));
+    }
+}