treecopier.java

来自「是一款用JAVA 编写的编译器 具有很强的编译功能」· Java 代码 · 共 416 行 · 第 1/2 页

JAVA
416
字号
/* * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Sun designates this * particular file as subject to the "Classpath" exception as provided * by Sun in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * CA 95054 USA or visit www.sun.com if you need additional information or * have any questions. */package com.sun.tools.javac.tree;import com.sun.source.tree.Tree;import com.sun.source.tree.*;import com.sun.tools.javac.tree.JCTree.*;import com.sun.tools.javac.util.List;import com.sun.tools.javac.util.ListBuffer;import java.util.Map;/** * Creates a copy of a tree, using a given TreeMaker. * Names, literal values, etc are shared with the original. * *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If *  you write code that depends on this, you do so at your own risk. *  This code and its internal interfaces are subject to change or *  deletion without notice.</b> */public class TreeCopier<P> implements TreeVisitor<JCTree,P> {    private TreeMaker M;        /** Creates a new instance of TreeCopier */    public TreeCopier(TreeMaker M) {        this.M = M;    }        public <T extends JCTree> T copy(T tree) {        return copy(tree, null);    }        @SuppressWarnings("unchecked")    public <T extends JCTree> T copy(T tree, P p) {        if (tree == null)            return null;        return (T) (tree.accept(this, p));    }        public <T extends JCTree> List<T> copy(List<T> trees) {        return copy(trees, null);    }        public <T extends JCTree> List<T> copy(List<T> trees, P p) {        if (trees == null)            return null;        ListBuffer<T> lb = new ListBuffer<T>();        for (T tree: trees)            lb.append(copy(tree, p));        return lb.toList();    }    public JCTree visitAnnotation(AnnotationTree node, P p) {        JCAnnotation t = (JCAnnotation) node;        JCTree annotationType = copy(t.annotationType, p);        List<JCExpression> args = copy(t.args, p);        return M.at(t.pos).Annotation(annotationType, args);    }    public JCTree visitAssert(AssertTree node, P p) {        JCAssert t = (JCAssert) node;        JCExpression cond = copy(t.cond, p);        JCExpression detail = copy(t.detail, p);        return M.at(t.pos).Assert(cond, detail);    }    public JCTree visitAssignment(AssignmentTree node, P p) {        JCAssign t = (JCAssign) node;        JCExpression lhs = copy(t.lhs, p);        JCExpression rhs = copy(t.rhs, p);        return M.at(t.pos).Assign(lhs, rhs);    }    public JCTree visitCompoundAssignment(CompoundAssignmentTree node, P p) {        JCAssignOp t = (JCAssignOp) node;        JCTree lhs = copy(t.lhs, p);        JCTree rhs = copy(t.rhs, p);        return M.at(t.pos).Assignop(t.getTag(), lhs, rhs);    }    public JCTree visitBinary(BinaryTree node, P p) {        JCBinary t = (JCBinary) node;        JCExpression lhs = copy(t.lhs, p);        JCExpression rhs = copy(t.rhs, p);        return M.at(t.pos).Binary(t.getTag(), lhs, rhs);    }    public JCTree visitBlock(BlockTree node, P p) {        JCBlock t = (JCBlock) node;        List<JCStatement> stats = copy(t.stats, p);        return M.at(t.pos).Block(t.flags, stats);    }    public JCTree visitBreak(BreakTree node, P p) {        JCBreak t = (JCBreak) node;        return M.at(t.pos).Break(t.label);    }    public JCTree visitCase(CaseTree node, P p) {        JCCase t = (JCCase) node;        JCExpression pat = copy(t.pat, p);        List<JCStatement> stats = copy(t.stats, p);        return M.at(t.pos).Case(pat, stats);    }    public JCTree visitCatch(CatchTree node, P p) {        JCCatch t = (JCCatch) node;        JCVariableDecl param = copy(t.param, p);        JCBlock body = copy(t.body, p);        return M.at(t.pos).Catch(param, body);    }    public JCTree visitClass(ClassTree node, P p) {        JCClassDecl t = (JCClassDecl) node;        JCModifiers mods = copy(t.mods, p);        List<JCTypeParameter> typarams = copy(t.typarams, p);        JCTree extending = copy(t.extending, p);        List<JCExpression> implementing = copy(t.implementing, p);        List<JCTree> defs = copy(t.defs, p);        return M.at(t.pos).ClassDef(mods, t.name, typarams, extending, implementing, defs);    }    public JCTree visitConditionalExpression(ConditionalExpressionTree node, P p) {        JCConditional t = (JCConditional) node;        JCExpression cond = copy(t.cond, p);        JCExpression truepart = copy(t.truepart, p);        JCExpression falsepart = copy(t.falsepart, p);        return M.at(t.pos).Conditional(cond, truepart, falsepart);    }    public JCTree visitContinue(ContinueTree node, P p) {        JCContinue t = (JCContinue) node;        return M.at(t.pos).Continue(t.label);    }    public JCTree visitDoWhileLoop(DoWhileLoopTree node, P p) {        JCDoWhileLoop t = (JCDoWhileLoop) node;        JCStatement body = copy(t.body, p);        JCExpression cond = copy(t.cond, p);        return M.at(t.pos).DoLoop(body, cond);    }    public JCTree visitErroneous(ErroneousTree node, P p) {        JCErroneous t = (JCErroneous) node;        List<? extends JCTree> errs = copy(t.errs, p);        return M.at(t.pos).Erroneous(errs);    }    public JCTree visitExpressionStatement(ExpressionStatementTree node, P p) {        JCExpressionStatement t = (JCExpressionStatement) node;        JCExpression expr = copy(t.expr, p);        return M.at(t.pos).Exec(expr);    }    public JCTree visitEnhancedForLoop(EnhancedForLoopTree node, P p) {        JCEnhancedForLoop t = (JCEnhancedForLoop) node;        JCVariableDecl var = copy(t.var, p);        JCExpression expr = copy(t.expr, p);        JCStatement body = copy(t.body, p);        return M.at(t.pos).ForeachLoop(var, expr, body);    }    public JCTree visitForLoop(ForLoopTree node, P p) {        JCForLoop t = (JCForLoop) node;        List<JCStatement> init = copy(t.init, p);        JCExpression cond = copy(t.cond, p);        List<JCExpressionStatement> step = copy(t.step, p);        JCStatement body = copy(t.body, p);        return M.at(t.pos).ForLoop(init, cond, step, body);    }    public JCTree visitIdentifier(IdentifierTree node, P p) {        JCIdent t = (JCIdent) node;        return M.at(t.pos).Ident(t.name);    }    public JCTree visitIf(IfTree node, P p) {        JCIf t = (JCIf) node;        JCExpression cond = copy(t.cond, p);        JCStatement thenpart = copy(t.thenpart, p);        JCStatement elsepart = copy(t.elsepart, p);        return M.at(t.pos).If(cond, thenpart, elsepart);    }    public JCTree visitImport(ImportTree node, P p) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?