Home » openjdk-7 » com.sun.tools » javac » tree » [javadoc | source]

    1   /*
    2    * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
    3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    4    *
    5    * This code is free software; you can redistribute it and/or modify it
    6    * under the terms of the GNU General Public License version 2 only, as
    7    * published by the Free Software Foundation.  Oracle designates this
    8    * particular file as subject to the "Classpath" exception as provided
    9    * by Oracle in the LICENSE file that accompanied this code.
   10    *
   11    * This code is distributed in the hope that it will be useful, but WITHOUT
   12    * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   13    * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   14    * version 2 for more details (a copy is included in the LICENSE file that
   15    * accompanied this code).
   16    *
   17    * You should have received a copy of the GNU General Public License version
   18    * 2 along with this work; if not, write to the Free Software Foundation,
   19    * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   20    *
   21    * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   22    * or visit www.oracle.com if you need additional information or have any
   23    * questions.
   24    */
   25   
   26   package com.sun.tools.javac.tree;
   27   
   28   import com.sun.tools.javac.util;
   29   import com.sun.tools.javac.tree.JCTree;
   30   
   31   /** A subclass of Tree.Visitor, this class defines
   32    *  a general tree translator pattern. Translation proceeds recursively in
   33    *  left-to-right order down a tree, constructing translated nodes by
   34    *  overwriting existing ones. There is one visitor method in this class
   35    *  for every possible kind of tree node.  To obtain a specific
   36    *  translator, it suffices to override those visitor methods which
   37    *  do some interesting work. The translator class itself takes care of all
   38    *  navigational aspects.
   39    *
   40    *  <p><b>This is NOT part of any supported API.
   41    *  If you write code that depends on this, you do so at your own risk.
   42    *  This code and its internal interfaces are subject to change or
   43    *  deletion without notice.</b>
   44    */
   45   public class TreeTranslator extends JCTree.Visitor {
   46   
   47       /** Visitor result field: a tree
   48        */
   49       protected JCTree result;
   50   
   51       /** Visitor method: Translate a single node.
   52        */
   53       @SuppressWarnings("unchecked")
   54       public <T extends JCTree> T translate(T tree) {
   55           if (tree == null) {
   56               return null;
   57           } else {
   58               tree.accept(this);
   59               JCTree result = this.result;
   60               this.result = null;
   61               return (T)result; // XXX cast
   62           }
   63       }
   64   
   65       /** Visitor method: translate a list of nodes.
   66        */
   67       public <T extends JCTree> List<T> translate(List<T> trees) {
   68           if (trees == null) return null;
   69           for (List<T> l = trees; l.nonEmpty(); l = l.tail)
   70               l.head = translate(l.head);
   71           return trees;
   72       }
   73   
   74       /**  Visitor method: translate a list of variable definitions.
   75        */
   76       public List<JCVariableDecl> translateVarDefs(List<JCVariableDecl> trees) {
   77           for (List<JCVariableDecl> l = trees; l.nonEmpty(); l = l.tail)
   78               l.head = translate(l.head);
   79           return trees;
   80       }
   81   
   82       /**  Visitor method: translate a list of type parameters.
   83        */
   84       public List<JCTypeParameter> translateTypeParams(List<JCTypeParameter> trees) {
   85           for (List<JCTypeParameter> l = trees; l.nonEmpty(); l = l.tail)
   86               l.head = translate(l.head);
   87           return trees;
   88       }
   89   
   90       /**  Visitor method: translate a list of case parts of switch statements.
   91        */
   92       public List<JCCase> translateCases(List<JCCase> trees) {
   93           for (List<JCCase> l = trees; l.nonEmpty(); l = l.tail)
   94               l.head = translate(l.head);
   95           return trees;
   96       }
   97   
   98       /**  Visitor method: translate a list of catch clauses in try statements.
   99        */
  100       public List<JCCatch> translateCatchers(List<JCCatch> trees) {
  101           for (List<JCCatch> l = trees; l.nonEmpty(); l = l.tail)
  102               l.head = translate(l.head);
  103           return trees;
  104       }
  105   
  106       /**  Visitor method: translate a list of catch clauses in try statements.
  107        */
  108       public List<JCAnnotation> translateAnnotations(List<JCAnnotation> trees) {
  109           for (List<JCAnnotation> l = trees; l.nonEmpty(); l = l.tail)
  110               l.head = translate(l.head);
  111           return trees;
  112       }
  113   
  114   /* ***************************************************************************
  115    * Visitor methods
  116    ****************************************************************************/
  117   
  118       public void visitTopLevel(JCCompilationUnit tree) {
  119           tree.pid = translate(tree.pid);
  120           tree.defs = translate(tree.defs);
  121           result = tree;
  122       }
  123   
  124       public void visitImport(JCImport tree) {
  125           tree.qualid = translate(tree.qualid);
  126           result = tree;
  127       }
  128   
  129       public void visitClassDef(JCClassDecl tree) {
  130           tree.mods = translate(tree.mods);
  131           tree.typarams = translateTypeParams(tree.typarams);
  132           tree.extending = translate(tree.extending);
  133           tree.implementing = translate(tree.implementing);
  134           tree.defs = translate(tree.defs);
  135           result = tree;
  136       }
  137   
  138       public void visitMethodDef(JCMethodDecl tree) {
  139           tree.mods = translate(tree.mods);
  140           tree.restype = translate(tree.restype);
  141           tree.typarams = translateTypeParams(tree.typarams);
  142           tree.params = translateVarDefs(tree.params);
  143           tree.thrown = translate(tree.thrown);
  144           tree.body = translate(tree.body);
  145           result = tree;
  146       }
  147   
  148       public void visitVarDef(JCVariableDecl tree) {
  149           tree.mods = translate(tree.mods);
  150           tree.vartype = translate(tree.vartype);
  151           tree.init = translate(tree.init);
  152           result = tree;
  153       }
  154   
  155       public void visitSkip(JCSkip tree) {
  156           result = tree;
  157       }
  158   
  159       public void visitBlock(JCBlock tree) {
  160           tree.stats = translate(tree.stats);
  161           result = tree;
  162       }
  163   
  164       public void visitDoLoop(JCDoWhileLoop tree) {
  165           tree.body = translate(tree.body);
  166           tree.cond = translate(tree.cond);
  167           result = tree;
  168       }
  169   
  170       public void visitWhileLoop(JCWhileLoop tree) {
  171           tree.cond = translate(tree.cond);
  172           tree.body = translate(tree.body);
  173           result = tree;
  174       }
  175   
  176       public void visitForLoop(JCForLoop tree) {
  177           tree.init = translate(tree.init);
  178           tree.cond = translate(tree.cond);
  179           tree.step = translate(tree.step);
  180           tree.body = translate(tree.body);
  181           result = tree;
  182       }
  183   
  184       public void visitForeachLoop(JCEnhancedForLoop tree) {
  185           tree.var = translate(tree.var);
  186           tree.expr = translate(tree.expr);
  187           tree.body = translate(tree.body);
  188           result = tree;
  189       }
  190   
  191       public void visitLabelled(JCLabeledStatement tree) {
  192           tree.body = translate(tree.body);
  193           result = tree;
  194       }
  195   
  196       public void visitSwitch(JCSwitch tree) {
  197           tree.selector = translate(tree.selector);
  198           tree.cases = translateCases(tree.cases);
  199           result = tree;
  200       }
  201   
  202       public void visitCase(JCCase tree) {
  203           tree.pat = translate(tree.pat);
  204           tree.stats = translate(tree.stats);
  205           result = tree;
  206       }
  207   
  208       public void visitSynchronized(JCSynchronized tree) {
  209           tree.lock = translate(tree.lock);
  210           tree.body = translate(tree.body);
  211           result = tree;
  212       }
  213   
  214       public void visitTry(JCTry tree) {
  215           tree.resources = translate(tree.resources);
  216           tree.body = translate(tree.body);
  217           tree.catchers = translateCatchers(tree.catchers);
  218           tree.finalizer = translate(tree.finalizer);
  219           result = tree;
  220       }
  221   
  222       public void visitCatch(JCCatch tree) {
  223           tree.param = translate(tree.param);
  224           tree.body = translate(tree.body);
  225           result = tree;
  226       }
  227   
  228       public void visitConditional(JCConditional tree) {
  229           tree.cond = translate(tree.cond);
  230           tree.truepart = translate(tree.truepart);
  231           tree.falsepart = translate(tree.falsepart);
  232           result = tree;
  233       }
  234   
  235       public void visitIf(JCIf tree) {
  236           tree.cond = translate(tree.cond);
  237           tree.thenpart = translate(tree.thenpart);
  238           tree.elsepart = translate(tree.elsepart);
  239           result = tree;
  240       }
  241   
  242       public void visitExec(JCExpressionStatement tree) {
  243           tree.expr = translate(tree.expr);
  244           result = tree;
  245       }
  246   
  247       public void visitBreak(JCBreak tree) {
  248           result = tree;
  249       }
  250   
  251       public void visitContinue(JCContinue tree) {
  252           result = tree;
  253       }
  254   
  255       public void visitReturn(JCReturn tree) {
  256           tree.expr = translate(tree.expr);
  257           result = tree;
  258       }
  259   
  260       public void visitThrow(JCThrow tree) {
  261           tree.expr = translate(tree.expr);
  262           result = tree;
  263       }
  264   
  265       public void visitAssert(JCAssert tree) {
  266           tree.cond = translate(tree.cond);
  267           tree.detail = translate(tree.detail);
  268           result = tree;
  269       }
  270   
  271       public void visitApply(JCMethodInvocation tree) {
  272           tree.meth = translate(tree.meth);
  273           tree.args = translate(tree.args);
  274           result = tree;
  275       }
  276   
  277       public void visitNewClass(JCNewClass tree) {
  278           tree.encl = translate(tree.encl);
  279           tree.clazz = translate(tree.clazz);
  280           tree.args = translate(tree.args);
  281           tree.def = translate(tree.def);
  282           result = tree;
  283       }
  284   
  285       public void visitNewArray(JCNewArray tree) {
  286           tree.elemtype = translate(tree.elemtype);
  287           tree.dims = translate(tree.dims);
  288           tree.elems = translate(tree.elems);
  289           result = tree;
  290       }
  291   
  292       public void visitParens(JCParens tree) {
  293           tree.expr = translate(tree.expr);
  294           result = tree;
  295       }
  296   
  297       public void visitAssign(JCAssign tree) {
  298           tree.lhs = translate(tree.lhs);
  299           tree.rhs = translate(tree.rhs);
  300           result = tree;
  301       }
  302   
  303       public void visitAssignop(JCAssignOp tree) {
  304           tree.lhs = translate(tree.lhs);
  305           tree.rhs = translate(tree.rhs);
  306           result = tree;
  307       }
  308   
  309       public void visitUnary(JCUnary tree) {
  310           tree.arg = translate(tree.arg);
  311           result = tree;
  312       }
  313   
  314       public void visitBinary(JCBinary tree) {
  315           tree.lhs = translate(tree.lhs);
  316           tree.rhs = translate(tree.rhs);
  317           result = tree;
  318       }
  319   
  320       public void visitTypeCast(JCTypeCast tree) {
  321           tree.clazz = translate(tree.clazz);
  322           tree.expr = translate(tree.expr);
  323           result = tree;
  324       }
  325   
  326       public void visitTypeTest(JCInstanceOf tree) {
  327           tree.expr = translate(tree.expr);
  328           tree.clazz = translate(tree.clazz);
  329           result = tree;
  330       }
  331   
  332       public void visitIndexed(JCArrayAccess tree) {
  333           tree.indexed = translate(tree.indexed);
  334           tree.index = translate(tree.index);
  335           result = tree;
  336       }
  337   
  338       public void visitSelect(JCFieldAccess tree) {
  339           tree.selected = translate(tree.selected);
  340           result = tree;
  341       }
  342   
  343       public void visitIdent(JCIdent tree) {
  344           result = tree;
  345       }
  346   
  347       public void visitLiteral(JCLiteral tree) {
  348           result = tree;
  349       }
  350   
  351       public void visitTypeIdent(JCPrimitiveTypeTree tree) {
  352           result = tree;
  353       }
  354   
  355       public void visitTypeArray(JCArrayTypeTree tree) {
  356           tree.elemtype = translate(tree.elemtype);
  357           result = tree;
  358       }
  359   
  360       public void visitTypeApply(JCTypeApply tree) {
  361           tree.clazz = translate(tree.clazz);
  362           tree.arguments = translate(tree.arguments);
  363           result = tree;
  364       }
  365   
  366       public void visitTypeUnion(JCTypeUnion tree) {
  367           tree.alternatives = translate(tree.alternatives);
  368           result = tree;
  369       }
  370   
  371       public void visitTypeParameter(JCTypeParameter tree) {
  372           tree.bounds = translate(tree.bounds);
  373           result = tree;
  374       }
  375   
  376       @Override
  377       public void visitWildcard(JCWildcard tree) {
  378           tree.kind = translate(tree.kind);
  379           tree.inner = translate(tree.inner);
  380           result = tree;
  381       }
  382   
  383       @Override
  384       public void visitTypeBoundKind(TypeBoundKind tree) {
  385           result = tree;
  386       }
  387   
  388       public void visitErroneous(JCErroneous tree) {
  389           result = tree;
  390       }
  391   
  392       public void visitLetExpr(LetExpr tree) {
  393           tree.defs = translateVarDefs(tree.defs);
  394           tree.expr = translate(tree.expr);
  395           result = tree;
  396       }
  397   
  398       public void visitModifiers(JCModifiers tree) {
  399           tree.annotations = translateAnnotations(tree.annotations);
  400           result = tree;
  401       }
  402   
  403       public void visitAnnotation(JCAnnotation tree) {
  404           tree.annotationType = translate(tree.annotationType);
  405           tree.args = translate(tree.args);
  406           result = tree;
  407       }
  408   
  409       public void visitTree(JCTree tree) {
  410           throw new AssertionError(tree);
  411       }
  412   }

Home » openjdk-7 » com.sun.tools » javac » tree » [javadoc | source]