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

Sun, 17 Feb 2013 16:44:55 -0500

author
dholmes
date
Sun, 17 Feb 2013 16:44:55 -0500
changeset 1571
af8417e590f4
parent 1521
71f35e4b93a5
child 1755
ddb4a2bfcd82
permissions
-rw-r--r--

Merge

     1 /*
     2  * Copyright (c) 2006, 2013, 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  */
    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 supported API.
    38  *  If 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 visitAnnotatedType(AnnotatedTypeTree node, P p) {
    75         JCAnnotatedType t = (JCAnnotatedType) node;
    76         List<JCAnnotation> annotations = copy(t.annotations, p);
    77         JCExpression underlyingType = copy(t.underlyingType, p);
    78         return M.at(t.pos).AnnotatedType(annotations, underlyingType);
    79     }
    81     public JCTree visitAnnotation(AnnotationTree node, P p) {
    82         JCAnnotation t = (JCAnnotation) node;
    83         JCTree annotationType = copy(t.annotationType, p);
    84         List<JCExpression> args = copy(t.args, p);
    85         if (t.getKind() == Tree.Kind.TYPE_ANNOTATION) {
    86             JCAnnotation newTA = M.at(t.pos).TypeAnnotation(annotationType, args);
    87             newTA.attribute = t.attribute;
    88             return newTA;
    89         } else {
    90             JCAnnotation newT = M.at(t.pos).Annotation(annotationType, args);
    91             newT.attribute = t.attribute;
    92             return newT;
    93         }
    94     }
    96     public JCTree visitAssert(AssertTree node, P p) {
    97         JCAssert t = (JCAssert) node;
    98         JCExpression cond = copy(t.cond, p);
    99         JCExpression detail = copy(t.detail, p);
   100         return M.at(t.pos).Assert(cond, detail);
   101     }
   103     public JCTree visitAssignment(AssignmentTree node, P p) {
   104         JCAssign t = (JCAssign) node;
   105         JCExpression lhs = copy(t.lhs, p);
   106         JCExpression rhs = copy(t.rhs, p);
   107         return M.at(t.pos).Assign(lhs, rhs);
   108     }
   110     public JCTree visitCompoundAssignment(CompoundAssignmentTree node, P p) {
   111         JCAssignOp t = (JCAssignOp) node;
   112         JCTree lhs = copy(t.lhs, p);
   113         JCTree rhs = copy(t.rhs, p);
   114         return M.at(t.pos).Assignop(t.getTag(), lhs, rhs);
   115     }
   117     public JCTree visitBinary(BinaryTree node, P p) {
   118         JCBinary t = (JCBinary) node;
   119         JCExpression lhs = copy(t.lhs, p);
   120         JCExpression rhs = copy(t.rhs, p);
   121         return M.at(t.pos).Binary(t.getTag(), lhs, rhs);
   122     }
   124     public JCTree visitBlock(BlockTree node, P p) {
   125         JCBlock t = (JCBlock) node;
   126         List<JCStatement> stats = copy(t.stats, p);
   127         return M.at(t.pos).Block(t.flags, stats);
   128     }
   130     public JCTree visitBreak(BreakTree node, P p) {
   131         JCBreak t = (JCBreak) node;
   132         return M.at(t.pos).Break(t.label);
   133     }
   135     public JCTree visitCase(CaseTree node, P p) {
   136         JCCase t = (JCCase) node;
   137         JCExpression pat = copy(t.pat, p);
   138         List<JCStatement> stats = copy(t.stats, p);
   139         return M.at(t.pos).Case(pat, stats);
   140     }
   142     public JCTree visitCatch(CatchTree node, P p) {
   143         JCCatch t = (JCCatch) node;
   144         JCVariableDecl param = copy(t.param, p);
   145         JCBlock body = copy(t.body, p);
   146         return M.at(t.pos).Catch(param, body);
   147     }
   149     public JCTree visitClass(ClassTree node, P p) {
   150         JCClassDecl t = (JCClassDecl) node;
   151         JCModifiers mods = copy(t.mods, p);
   152         List<JCTypeParameter> typarams = copy(t.typarams, p);
   153         JCExpression extending = copy(t.extending, p);
   154         List<JCExpression> implementing = copy(t.implementing, p);
   155         List<JCTree> defs = copy(t.defs, p);
   156         return M.at(t.pos).ClassDef(mods, t.name, typarams, extending, implementing, defs);
   157     }
   159     public JCTree visitConditionalExpression(ConditionalExpressionTree node, P p) {
   160         JCConditional t = (JCConditional) node;
   161         JCExpression cond = copy(t.cond, p);
   162         JCExpression truepart = copy(t.truepart, p);
   163         JCExpression falsepart = copy(t.falsepart, p);
   164         return M.at(t.pos).Conditional(cond, truepart, falsepart);
   165     }
   167     public JCTree visitContinue(ContinueTree node, P p) {
   168         JCContinue t = (JCContinue) node;
   169         return M.at(t.pos).Continue(t.label);
   170     }
   172     public JCTree visitDoWhileLoop(DoWhileLoopTree node, P p) {
   173         JCDoWhileLoop t = (JCDoWhileLoop) node;
   174         JCStatement body = copy(t.body, p);
   175         JCExpression cond = copy(t.cond, p);
   176         return M.at(t.pos).DoLoop(body, cond);
   177     }
   179     public JCTree visitErroneous(ErroneousTree node, P p) {
   180         JCErroneous t = (JCErroneous) node;
   181         List<? extends JCTree> errs = copy(t.errs, p);
   182         return M.at(t.pos).Erroneous(errs);
   183     }
   185     public JCTree visitExpressionStatement(ExpressionStatementTree node, P p) {
   186         JCExpressionStatement t = (JCExpressionStatement) node;
   187         JCExpression expr = copy(t.expr, p);
   188         return M.at(t.pos).Exec(expr);
   189     }
   191     public JCTree visitEnhancedForLoop(EnhancedForLoopTree node, P p) {
   192         JCEnhancedForLoop t = (JCEnhancedForLoop) node;
   193         JCVariableDecl var = copy(t.var, p);
   194         JCExpression expr = copy(t.expr, p);
   195         JCStatement body = copy(t.body, p);
   196         return M.at(t.pos).ForeachLoop(var, expr, body);
   197     }
   199     public JCTree visitForLoop(ForLoopTree node, P p) {
   200         JCForLoop t = (JCForLoop) node;
   201         List<JCStatement> init = copy(t.init, p);
   202         JCExpression cond = copy(t.cond, p);
   203         List<JCExpressionStatement> step = copy(t.step, p);
   204         JCStatement body = copy(t.body, p);
   205         return M.at(t.pos).ForLoop(init, cond, step, body);
   206     }
   208     public JCTree visitIdentifier(IdentifierTree node, P p) {
   209         JCIdent t = (JCIdent) node;
   210         return M.at(t.pos).Ident(t.name);
   211     }
   213     public JCTree visitIf(IfTree node, P p) {
   214         JCIf t = (JCIf) node;
   215         JCExpression cond = copy(t.cond, p);
   216         JCStatement thenpart = copy(t.thenpart, p);
   217         JCStatement elsepart = copy(t.elsepart, p);
   218         return M.at(t.pos).If(cond, thenpart, elsepart);
   219     }
   221     public JCTree visitImport(ImportTree node, P p) {
   222         JCImport t = (JCImport) node;
   223         JCTree qualid = copy(t.qualid, p);
   224         return M.at(t.pos).Import(qualid, t.staticImport);
   225     }
   227     public JCTree visitArrayAccess(ArrayAccessTree node, P p) {
   228         JCArrayAccess t = (JCArrayAccess) node;
   229         JCExpression indexed = copy(t.indexed, p);
   230         JCExpression index = copy(t.index, p);
   231         return M.at(t.pos).Indexed(indexed, index);
   232     }
   234     public JCTree visitLabeledStatement(LabeledStatementTree node, P p) {
   235         JCLabeledStatement t = (JCLabeledStatement) node;
   236         JCStatement body = copy(t.body, p);
   237         return M.at(t.pos).Labelled(t.label, t.body);
   238     }
   240     public JCTree visitLiteral(LiteralTree node, P p) {
   241         JCLiteral t = (JCLiteral) node;
   242         return M.at(t.pos).Literal(t.typetag, t.value);
   243     }
   245     public JCTree visitMethod(MethodTree node, P p) {
   246         JCMethodDecl t  = (JCMethodDecl) node;
   247         JCModifiers mods = copy(t.mods, p);
   248         JCExpression restype = copy(t.restype, p);
   249         List<JCTypeParameter> typarams = copy(t.typarams, p);
   250         List<JCVariableDecl> params = copy(t.params, p);
   251         JCVariableDecl recvparam = copy(t.recvparam, p);
   252         List<JCExpression> thrown = copy(t.thrown, p);
   253         JCBlock body = copy(t.body, p);
   254         JCExpression defaultValue = copy(t.defaultValue, p);
   255         return M.at(t.pos).MethodDef(mods, t.name, restype, typarams, recvparam, params, thrown, body, defaultValue);
   256     }
   258     public JCTree visitMethodInvocation(MethodInvocationTree node, P p) {
   259         JCMethodInvocation t = (JCMethodInvocation) node;
   260         List<JCExpression> typeargs = copy(t.typeargs, p);
   261         JCExpression meth = copy(t.meth, p);
   262         List<JCExpression> args = copy(t.args, p);
   263         return M.at(t.pos).Apply(typeargs, meth, args);
   264     }
   266     public JCTree visitModifiers(ModifiersTree node, P p) {
   267         JCModifiers t = (JCModifiers) node;
   268         List<JCAnnotation> annotations = copy(t.annotations, p);
   269         return M.at(t.pos).Modifiers(t.flags, annotations);
   270     }
   272     public JCTree visitNewArray(NewArrayTree node, P p) {
   273         JCNewArray t = (JCNewArray) node;
   274         JCExpression elemtype = copy(t.elemtype, p);
   275         List<JCExpression> dims = copy(t.dims, p);
   276         List<JCExpression> elems = copy(t.elems, p);
   277         return M.at(t.pos).NewArray(elemtype, dims, elems);
   278     }
   280     public JCTree visitNewClass(NewClassTree node, P p) {
   281         JCNewClass t = (JCNewClass) node;
   282         JCExpression encl = copy(t.encl, p);
   283         List<JCExpression> typeargs = copy(t.typeargs, p);
   284         JCExpression clazz = copy(t.clazz, p);
   285         List<JCExpression> args = copy(t.args, p);
   286         JCClassDecl def = copy(t.def, p);
   287         return M.at(t.pos).NewClass(encl, typeargs, clazz, args, def);
   288     }
   290     public JCTree visitLambdaExpression(LambdaExpressionTree node, P p) {
   291         JCLambda t = (JCLambda) node;
   292         List<JCVariableDecl> params = copy(t.params, p);
   293         JCTree body = copy(t.body, p);
   294         return M.at(t.pos).Lambda(params, body);
   295     }
   297     public JCTree visitParenthesized(ParenthesizedTree node, P p) {
   298         JCParens t = (JCParens) node;
   299         JCExpression expr = copy(t.expr, p);
   300         return M.at(t.pos).Parens(expr);
   301     }
   303     public JCTree visitReturn(ReturnTree node, P p) {
   304         JCReturn t = (JCReturn) node;
   305         JCExpression expr = copy(t.expr, p);
   306         return M.at(t.pos).Return(expr);
   307     }
   309     public JCTree visitMemberSelect(MemberSelectTree node, P p) {
   310         JCFieldAccess t = (JCFieldAccess) node;
   311         JCExpression selected = copy(t.selected, p);
   312         return M.at(t.pos).Select(selected, t.name);
   313     }
   315     public JCTree visitMemberReference(MemberReferenceTree node, P p) {
   316         JCMemberReference t = (JCMemberReference) node;
   317         JCExpression expr = copy(t.expr, p);
   318         List<JCExpression> typeargs = copy(t.typeargs, p);
   319         return M.at(t.pos).Reference(t.mode, t.name, expr, typeargs);
   320     }
   322     public JCTree visitEmptyStatement(EmptyStatementTree node, P p) {
   323         JCSkip t = (JCSkip) node;
   324         return M.at(t.pos).Skip();
   325     }
   327     public JCTree visitSwitch(SwitchTree node, P p) {
   328         JCSwitch t = (JCSwitch) node;
   329         JCExpression selector = copy(t.selector, p);
   330         List<JCCase> cases = copy(t.cases, p);
   331         return M.at(t.pos).Switch(selector, cases);
   332     }
   334     public JCTree visitSynchronized(SynchronizedTree node, P p) {
   335         JCSynchronized t = (JCSynchronized) node;
   336         JCExpression lock = copy(t.lock, p);
   337         JCBlock body = copy(t.body, p);
   338         return M.at(t.pos).Synchronized(lock, body);
   339     }
   341     public JCTree visitThrow(ThrowTree node, P p) {
   342         JCThrow t = (JCThrow) node;
   343         JCTree expr = copy(t.expr, p);
   344         return M.at(t.pos).Throw(expr);
   345     }
   347     public JCTree visitCompilationUnit(CompilationUnitTree node, P p) {
   348         JCCompilationUnit t = (JCCompilationUnit) node;
   349         List<JCAnnotation> packageAnnotations = copy(t.packageAnnotations, p);
   350         JCExpression pid = copy(t.pid, p);
   351         List<JCTree> defs = copy(t.defs, p);
   352         return M.at(t.pos).TopLevel(packageAnnotations, pid, defs);
   353     }
   355     public JCTree visitTry(TryTree node, P p) {
   356         JCTry t = (JCTry) node;
   357         List<JCTree> resources = copy(t.resources, p);
   358         JCBlock body = copy(t.body, p);
   359         List<JCCatch> catchers = copy(t.catchers, p);
   360         JCBlock finalizer = copy(t.finalizer, p);
   361         return M.at(t.pos).Try(resources, body, catchers, finalizer);
   362     }
   364     public JCTree visitParameterizedType(ParameterizedTypeTree node, P p) {
   365         JCTypeApply t = (JCTypeApply) node;
   366         JCExpression clazz = copy(t.clazz, p);
   367         List<JCExpression> arguments = copy(t.arguments, p);
   368         return M.at(t.pos).TypeApply(clazz, arguments);
   369     }
   371     public JCTree visitUnionType(UnionTypeTree node, P p) {
   372         JCTypeUnion t = (JCTypeUnion) node;
   373         List<JCExpression> components = copy(t.alternatives, p);
   374         return M.at(t.pos).TypeUnion(components);
   375     }
   377     public JCTree visitIntersectionType(IntersectionTypeTree node, P p) {
   378         JCTypeIntersection t = (JCTypeIntersection) node;
   379         List<JCExpression> bounds = copy(t.bounds, p);
   380         return M.at(t.pos).TypeIntersection(bounds);
   381     }
   383     public JCTree visitArrayType(ArrayTypeTree node, P p) {
   384         JCArrayTypeTree t = (JCArrayTypeTree) node;
   385         JCExpression elemtype = copy(t.elemtype, p);
   386         return M.at(t.pos).TypeArray(elemtype);
   387     }
   389     public JCTree visitTypeCast(TypeCastTree node, P p) {
   390         JCTypeCast t = (JCTypeCast) node;
   391         JCTree clazz = copy(t.clazz, p);
   392         JCExpression expr = copy(t.expr, p);
   393         return M.at(t.pos).TypeCast(clazz, expr);
   394     }
   396     public JCTree visitPrimitiveType(PrimitiveTypeTree node, P p) {
   397         JCPrimitiveTypeTree t = (JCPrimitiveTypeTree) node;
   398         return M.at(t.pos).TypeIdent(t.typetag);
   399     }
   401     public JCTree visitTypeParameter(TypeParameterTree node, P p) {
   402         JCTypeParameter t = (JCTypeParameter) node;
   403         List<JCAnnotation> annos = copy(t.annotations, p);
   404         List<JCExpression> bounds = copy(t.bounds, p);
   405         return M.at(t.pos).TypeParameter(t.name, bounds, annos);
   406     }
   408     public JCTree visitInstanceOf(InstanceOfTree node, P p) {
   409         JCInstanceOf t = (JCInstanceOf) node;
   410         JCExpression expr = copy(t.expr, p);
   411         JCTree clazz = copy(t.clazz, p);
   412         return M.at(t.pos).TypeTest(expr, clazz);
   413     }
   415     public JCTree visitUnary(UnaryTree node, P p) {
   416         JCUnary t = (JCUnary) node;
   417         JCExpression arg = copy(t.arg, p);
   418         return M.at(t.pos).Unary(t.getTag(), arg);
   419     }
   421     public JCTree visitVariable(VariableTree node, P p) {
   422         JCVariableDecl t = (JCVariableDecl) node;
   423         JCModifiers mods = copy(t.mods, p);
   424         JCExpression vartype = copy(t.vartype, p);
   425         JCExpression init = copy(t.init, p);
   426         return M.at(t.pos).VarDef(mods, t.name, vartype, init);
   427     }
   429     public JCTree visitWhileLoop(WhileLoopTree node, P p) {
   430         JCWhileLoop t = (JCWhileLoop) node;
   431         JCStatement body = copy(t.body, p);
   432         JCExpression cond = copy(t.cond, p);
   433         return M.at(t.pos).WhileLoop(cond, body);
   434     }
   436     public JCTree visitWildcard(WildcardTree node, P p) {
   437         JCWildcard t = (JCWildcard) node;
   438         TypeBoundKind kind = M.at(t.kind.pos).TypeBoundKind(t.kind.kind);
   439         JCTree inner = copy(t.inner, p);
   440         return M.at(t.pos).Wildcard(kind, inner);
   441     }
   443     public JCTree visitOther(Tree node, P p) {
   444         JCTree tree = (JCTree) node;
   445         switch (tree.getTag()) {
   446             case LETEXPR: {
   447                 LetExpr t = (LetExpr) node;
   448                 List<JCVariableDecl> defs = copy(t.defs, p);
   449                 JCTree expr = copy(t.expr, p);
   450                 return M.at(t.pos).LetExpr(defs, expr);
   451             }
   452             default:
   453                 throw new AssertionError("unknown tree tag: " + tree.getTag());
   454         }
   455     }
   457 }

mercurial