src/share/classes/com/sun/tools/javac/tree/TreeCopier.java

Thu, 02 Oct 2008 19:58:40 -0700

author
xdono
date
Thu, 02 Oct 2008 19:58:40 -0700
changeset 117
24a47c3062fe
parent 104
5e89c4ca637c
child 308
03944ee4fac4
permissions
-rw-r--r--

6754988: Update copyright year
Summary: Update for files that have been modified starting July 2008
Reviewed-by: ohair, tbell

     1 /*
     2  * Copyright 2006-2008 Sun Microsystems, Inc.  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.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    26 package com.sun.tools.javac.tree;
    28 import com.sun.source.tree.*;
    29 import com.sun.tools.javac.tree.JCTree.*;
    30 import com.sun.tools.javac.util.List;
    31 import com.sun.tools.javac.util.ListBuffer;
    33 /**
    34  * Creates a copy of a tree, using a given TreeMaker.
    35  * Names, literal values, etc are shared with the original.
    36  *
    37  *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    38  *  you write code that depends on this, you do so at your own risk.
    39  *  This code and its internal interfaces are subject to change or
    40  *  deletion without notice.</b>
    41  */
    42 public class TreeCopier<P> implements TreeVisitor<JCTree,P> {
    43     private TreeMaker M;
    45     /** Creates a new instance of TreeCopier */
    46     public TreeCopier(TreeMaker M) {
    47         this.M = M;
    48     }
    50     public <T extends JCTree> T copy(T tree) {
    51         return copy(tree, null);
    52     }
    54     @SuppressWarnings("unchecked")
    55     public <T extends JCTree> T copy(T tree, P p) {
    56         if (tree == null)
    57             return null;
    58         return (T) (tree.accept(this, p));
    59     }
    61     public <T extends JCTree> List<T> copy(List<T> trees) {
    62         return copy(trees, null);
    63     }
    65     public <T extends JCTree> List<T> copy(List<T> trees, P p) {
    66         if (trees == null)
    67             return null;
    68         ListBuffer<T> lb = new ListBuffer<T>();
    69         for (T tree: trees)
    70             lb.append(copy(tree, p));
    71         return lb.toList();
    72     }
    74     public JCTree visitAnnotation(AnnotationTree node, P p) {
    75         JCAnnotation t = (JCAnnotation) node;
    76         JCTree annotationType = copy(t.annotationType, p);
    77         List<JCExpression> args = copy(t.args, p);
    78         return M.at(t.pos).Annotation(annotationType, args);
    79     }
    81     public JCTree visitAssert(AssertTree node, P p) {
    82         JCAssert t = (JCAssert) node;
    83         JCExpression cond = copy(t.cond, p);
    84         JCExpression detail = copy(t.detail, p);
    85         return M.at(t.pos).Assert(cond, detail);
    86     }
    88     public JCTree visitAssignment(AssignmentTree node, P p) {
    89         JCAssign t = (JCAssign) node;
    90         JCExpression lhs = copy(t.lhs, p);
    91         JCExpression rhs = copy(t.rhs, p);
    92         return M.at(t.pos).Assign(lhs, rhs);
    93     }
    95     public JCTree visitCompoundAssignment(CompoundAssignmentTree node, P p) {
    96         JCAssignOp t = (JCAssignOp) node;
    97         JCTree lhs = copy(t.lhs, p);
    98         JCTree rhs = copy(t.rhs, p);
    99         return M.at(t.pos).Assignop(t.getTag(), lhs, rhs);
   100     }
   102     public JCTree visitBinary(BinaryTree node, P p) {
   103         JCBinary t = (JCBinary) node;
   104         JCExpression lhs = copy(t.lhs, p);
   105         JCExpression rhs = copy(t.rhs, p);
   106         return M.at(t.pos).Binary(t.getTag(), lhs, rhs);
   107     }
   109     public JCTree visitBlock(BlockTree node, P p) {
   110         JCBlock t = (JCBlock) node;
   111         List<JCStatement> stats = copy(t.stats, p);
   112         return M.at(t.pos).Block(t.flags, stats);
   113     }
   115     public JCTree visitBreak(BreakTree node, P p) {
   116         JCBreak t = (JCBreak) node;
   117         return M.at(t.pos).Break(t.label);
   118     }
   120     public JCTree visitCase(CaseTree node, P p) {
   121         JCCase t = (JCCase) node;
   122         JCExpression pat = copy(t.pat, p);
   123         List<JCStatement> stats = copy(t.stats, p);
   124         return M.at(t.pos).Case(pat, stats);
   125     }
   127     public JCTree visitCatch(CatchTree node, P p) {
   128         JCCatch t = (JCCatch) node;
   129         JCVariableDecl param = copy(t.param, p);
   130         JCBlock body = copy(t.body, p);
   131         return M.at(t.pos).Catch(param, body);
   132     }
   134     public JCTree visitClass(ClassTree node, P p) {
   135         JCClassDecl t = (JCClassDecl) node;
   136         JCModifiers mods = copy(t.mods, p);
   137         List<JCTypeParameter> typarams = copy(t.typarams, p);
   138         JCTree extending = copy(t.extending, p);
   139         List<JCExpression> implementing = copy(t.implementing, p);
   140         List<JCTree> defs = copy(t.defs, p);
   141         return M.at(t.pos).ClassDef(mods, t.name, typarams, extending, implementing, defs);
   142     }
   144     public JCTree visitConditionalExpression(ConditionalExpressionTree node, P p) {
   145         JCConditional t = (JCConditional) node;
   146         JCExpression cond = copy(t.cond, p);
   147         JCExpression truepart = copy(t.truepart, p);
   148         JCExpression falsepart = copy(t.falsepart, p);
   149         return M.at(t.pos).Conditional(cond, truepart, falsepart);
   150     }
   152     public JCTree visitContinue(ContinueTree node, P p) {
   153         JCContinue t = (JCContinue) node;
   154         return M.at(t.pos).Continue(t.label);
   155     }
   157     public JCTree visitDoWhileLoop(DoWhileLoopTree node, P p) {
   158         JCDoWhileLoop t = (JCDoWhileLoop) node;
   159         JCStatement body = copy(t.body, p);
   160         JCExpression cond = copy(t.cond, p);
   161         return M.at(t.pos).DoLoop(body, cond);
   162     }
   164     public JCTree visitErroneous(ErroneousTree node, P p) {
   165         JCErroneous t = (JCErroneous) node;
   166         List<? extends JCTree> errs = copy(t.errs, p);
   167         return M.at(t.pos).Erroneous(errs);
   168     }
   170     public JCTree visitExpressionStatement(ExpressionStatementTree node, P p) {
   171         JCExpressionStatement t = (JCExpressionStatement) node;
   172         JCExpression expr = copy(t.expr, p);
   173         return M.at(t.pos).Exec(expr);
   174     }
   176     public JCTree visitEnhancedForLoop(EnhancedForLoopTree node, P p) {
   177         JCEnhancedForLoop t = (JCEnhancedForLoop) node;
   178         JCVariableDecl var = copy(t.var, p);
   179         JCExpression expr = copy(t.expr, p);
   180         JCStatement body = copy(t.body, p);
   181         return M.at(t.pos).ForeachLoop(var, expr, body);
   182     }
   184     public JCTree visitForLoop(ForLoopTree node, P p) {
   185         JCForLoop t = (JCForLoop) node;
   186         List<JCStatement> init = copy(t.init, p);
   187         JCExpression cond = copy(t.cond, p);
   188         List<JCExpressionStatement> step = copy(t.step, p);
   189         JCStatement body = copy(t.body, p);
   190         return M.at(t.pos).ForLoop(init, cond, step, body);
   191     }
   193     public JCTree visitIdentifier(IdentifierTree node, P p) {
   194         JCIdent t = (JCIdent) node;
   195         return M.at(t.pos).Ident(t.name);
   196     }
   198     public JCTree visitIf(IfTree node, P p) {
   199         JCIf t = (JCIf) node;
   200         JCExpression cond = copy(t.cond, p);
   201         JCStatement thenpart = copy(t.thenpart, p);
   202         JCStatement elsepart = copy(t.elsepart, p);
   203         return M.at(t.pos).If(cond, thenpart, elsepart);
   204     }
   206     public JCTree visitImport(ImportTree node, P p) {
   207         JCImport t = (JCImport) node;
   208         JCTree qualid = copy(t.qualid, p);
   209         return M.at(t.pos).Import(qualid, t.staticImport);
   210     }
   212     public JCTree visitArrayAccess(ArrayAccessTree node, P p) {
   213         JCArrayAccess t = (JCArrayAccess) node;
   214         JCExpression indexed = copy(t.indexed, p);
   215         JCExpression index = copy(t.index, p);
   216         return M.at(t.pos).Indexed(indexed, index);
   217     }
   219     public JCTree visitLabeledStatement(LabeledStatementTree node, P p) {
   220         JCLabeledStatement t = (JCLabeledStatement) node;
   221         JCStatement body = copy(t.body, p);
   222         return M.at(t.pos).Labelled(t.label, t.body);
   223     }
   225     public JCTree visitLiteral(LiteralTree node, P p) {
   226         JCLiteral t = (JCLiteral) node;
   227         return M.at(t.pos).Literal(t.typetag, t.value);
   228     }
   230     public JCTree visitMethod(MethodTree node, P p) {
   231         JCMethodDecl t  = (JCMethodDecl) node;
   232         JCModifiers mods = copy(t.mods, p);
   233         JCExpression restype = copy(t.restype, p);
   234         List<JCTypeParameter> typarams = copy(t.typarams, p);
   235         List<JCVariableDecl> params = copy(t.params, p);
   236         List<JCExpression> thrown = copy(t.thrown, p);
   237         JCBlock body = copy(t.body, p);
   238         JCExpression defaultValue = copy(t.defaultValue, p);
   239         return M.at(t.pos).MethodDef(mods, t.name, restype, typarams, params, thrown, body, defaultValue);
   240     }
   242     public JCTree visitMethodInvocation(MethodInvocationTree node, P p) {
   243         JCMethodInvocation t = (JCMethodInvocation) node;
   244         List<JCExpression> typeargs = copy(t.typeargs, p);
   245         JCExpression meth = copy(t.meth, p);
   246         List<JCExpression> args = copy(t.args, p);
   247         return M.at(t.pos).Apply(typeargs, meth, args);
   248     }
   250     public JCTree visitModifiers(ModifiersTree node, P p) {
   251         JCModifiers t = (JCModifiers) node;
   252         List<JCAnnotation> annotations = copy(t.annotations, p);
   253         return M.at(t.pos).Modifiers(t.flags, annotations);
   254     }
   256     public JCTree visitNewArray(NewArrayTree node, P p) {
   257         JCNewArray t = (JCNewArray) node;
   258         JCExpression elemtype = copy(t.elemtype, p);
   259         List<JCExpression> dims = copy(t.dims, p);
   260         List<JCExpression> elems = copy(t.elems, p);
   261         return M.at(t.pos).NewArray(elemtype, dims, elems);
   262     }
   264     public JCTree visitNewClass(NewClassTree node, P p) {
   265         JCNewClass t = (JCNewClass) node;
   266         JCExpression encl = copy(t.encl, p);
   267         List<JCExpression> typeargs = copy(t.typeargs, p);
   268         JCExpression clazz = copy(t.clazz, p);
   269         List<JCExpression> args = copy(t.args, p);
   270         JCClassDecl def = copy(t.def, p);
   271         return M.at(t.pos).NewClass(encl, typeargs, clazz, args, def);
   272     }
   274     public JCTree visitParenthesized(ParenthesizedTree node, P p) {
   275         JCParens t = (JCParens) node;
   276         JCExpression expr = copy(t.expr, p);
   277         return M.at(t.pos).Parens(expr);
   278     }
   280     public JCTree visitReturn(ReturnTree node, P p) {
   281         JCReturn t = (JCReturn) node;
   282         JCExpression expr = copy(t.expr, p);
   283         return M.at(t.pos).Return(expr);
   284     }
   286     public JCTree visitMemberSelect(MemberSelectTree node, P p) {
   287         JCFieldAccess t = (JCFieldAccess) node;
   288         JCExpression selected = copy(t.selected, p);
   289         return M.at(t.pos).Select(selected, t.name);
   290     }
   292     public JCTree visitEmptyStatement(EmptyStatementTree node, P p) {
   293         JCSkip t = (JCSkip) node;
   294         return M.at(t.pos).Skip();
   295     }
   297     public JCTree visitSwitch(SwitchTree node, P p) {
   298         JCSwitch t = (JCSwitch) node;
   299         JCExpression selector = copy(t.selector, p);
   300         List<JCCase> cases = copy(t.cases, p);
   301         return M.at(t.pos).Switch(selector, cases);
   302     }
   304     public JCTree visitSynchronized(SynchronizedTree node, P p) {
   305         JCSynchronized t = (JCSynchronized) node;
   306         JCExpression lock = copy(t.lock, p);
   307         JCBlock body = copy(t.body, p);
   308         return M.at(t.pos).Synchronized(lock, body);
   309     }
   311     public JCTree visitThrow(ThrowTree node, P p) {
   312         JCThrow t = (JCThrow) node;
   313         JCTree expr = copy(t.expr, p);
   314         return M.at(t.pos).Throw(expr);
   315     }
   317     public JCTree visitCompilationUnit(CompilationUnitTree node, P p) {
   318         JCCompilationUnit t = (JCCompilationUnit) node;
   319         List<JCAnnotation> packageAnnotations = copy(t.packageAnnotations, p);
   320         JCExpression pid = copy(t.pid, p);
   321         List<JCTree> defs = copy(t.defs, p);
   322         return M.at(t.pos).TopLevel(packageAnnotations, pid, defs);
   323     }
   325     public JCTree visitTry(TryTree node, P p) {
   326         JCTry t = (JCTry) node;
   327         JCBlock body = copy(t.body, p);
   328         List<JCCatch> catchers = copy(t.catchers, p);
   329         JCBlock finalizer = copy(t.finalizer, p);
   330         return M.at(t.pos).Try(body, catchers, finalizer);
   331     }
   333     public JCTree visitParameterizedType(ParameterizedTypeTree node, P p) {
   334         JCTypeApply t = (JCTypeApply) node;
   335         JCExpression clazz = copy(t.clazz, p);
   336         List<JCExpression> arguments = copy(t.arguments, p);
   337         return M.at(t.pos).TypeApply(clazz, arguments);
   338     }
   340     public JCTree visitArrayType(ArrayTypeTree node, P p) {
   341         JCArrayTypeTree t = (JCArrayTypeTree) node;
   342         JCExpression elemtype = copy(t.elemtype, p);
   343         return M.at(t.pos).TypeArray(elemtype);
   344     }
   346     public JCTree visitTypeCast(TypeCastTree node, P p) {
   347         JCTypeCast t = (JCTypeCast) node;
   348         JCTree clazz = copy(t.clazz, p);
   349         JCExpression expr = copy(t.expr, p);
   350         return M.at(t.pos).TypeCast(clazz, expr);
   351     }
   353     public JCTree visitPrimitiveType(PrimitiveTypeTree node, P p) {
   354         JCPrimitiveTypeTree t = (JCPrimitiveTypeTree) node;
   355         return M.at(t.pos).TypeIdent(t.typetag);
   356     }
   358     public JCTree visitTypeParameter(TypeParameterTree node, P p) {
   359         JCTypeParameter t = (JCTypeParameter) node;
   360         List<JCExpression> bounds = copy(t.bounds, p);
   361         return M.at(t.pos).TypeParameter(t.name, t.bounds);
   362     }
   364     public JCTree visitInstanceOf(InstanceOfTree node, P p) {
   365         JCInstanceOf t = (JCInstanceOf) node;
   366         JCExpression expr = copy(t.expr, p);
   367         JCTree clazz = copy(t.clazz, p);
   368         return M.at(t.pos).TypeTest(expr, clazz);
   369     }
   371     public JCTree visitUnary(UnaryTree node, P p) {
   372         JCUnary t = (JCUnary) node;
   373         JCExpression arg = copy(t.arg, p);
   374         return M.at(t.pos).Unary(t.getTag(), arg);
   375     }
   377     public JCTree visitVariable(VariableTree node, P p) {
   378         JCVariableDecl t = (JCVariableDecl) node;
   379         JCModifiers mods = copy(t.mods, p);
   380         JCExpression vartype = copy(t.vartype, p);
   381         JCExpression init = copy(t.init, p);
   382         return M.at(t.pos).VarDef(mods, t.name, vartype, init);
   383     }
   385     public JCTree visitWhileLoop(WhileLoopTree node, P p) {
   386         JCWhileLoop t = (JCWhileLoop) node;
   387         JCStatement body = copy(t.body, p);
   388         JCExpression cond = copy(t.cond, p);
   389         return M.at(t.pos).WhileLoop(cond, body);
   390     }
   392     public JCTree visitWildcard(WildcardTree node, P p) {
   393         JCWildcard t = (JCWildcard) node;
   394         TypeBoundKind kind = M.at(t.kind.pos).TypeBoundKind(t.kind.kind);
   395         JCTree inner = copy(t.inner, p);
   396         return M.at(t.pos).Wildcard(kind, inner);
   397     }
   399     public JCTree visitOther(Tree node, P p) {
   400         JCTree tree = (JCTree) node;
   401         switch (tree.getTag()) {
   402             case JCTree.LETEXPR: {
   403                 LetExpr t = (LetExpr) node;
   404                 List<JCVariableDecl> defs = copy(t.defs, p);
   405                 JCTree expr = copy(t.expr, p);
   406                 return M.at(t.pos).LetExpr(defs, expr);
   407             }
   408             default:
   409                 throw new AssertionError("unknown tree tag: " + tree.getTag());
   410         }
   411     }
   413 }

mercurial