0N/A/*
814N/A * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
553N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
553N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.tools.javac.tree;
0N/A
0N/Aimport com.sun.source.tree.*;
0N/Aimport com.sun.tools.javac.tree.JCTree.*;
0N/Aimport com.sun.tools.javac.util.List;
0N/Aimport com.sun.tools.javac.util.ListBuffer;
0N/A
0N/A/**
0N/A * Creates a copy of a tree, using a given TreeMaker.
0N/A * Names, literal values, etc are shared with the original.
0N/A *
580N/A * <p><b>This is NOT part of any supported API.
580N/A * If you write code that depends on this, you do so at your own risk.
0N/A * This code and its internal interfaces are subject to change or
0N/A * deletion without notice.</b>
0N/A */
0N/Apublic class TreeCopier<P> implements TreeVisitor<JCTree,P> {
0N/A private TreeMaker M;
0N/A
0N/A /** Creates a new instance of TreeCopier */
0N/A public TreeCopier(TreeMaker M) {
0N/A this.M = M;
0N/A }
0N/A
0N/A public <T extends JCTree> T copy(T tree) {
0N/A return copy(tree, null);
0N/A }
0N/A
0N/A @SuppressWarnings("unchecked")
0N/A public <T extends JCTree> T copy(T tree, P p) {
0N/A if (tree == null)
0N/A return null;
0N/A return (T) (tree.accept(this, p));
0N/A }
0N/A
0N/A public <T extends JCTree> List<T> copy(List<T> trees) {
0N/A return copy(trees, null);
0N/A }
0N/A
0N/A public <T extends JCTree> List<T> copy(List<T> trees, P p) {
0N/A if (trees == null)
0N/A return null;
0N/A ListBuffer<T> lb = new ListBuffer<T>();
0N/A for (T tree: trees)
0N/A lb.append(copy(tree, p));
0N/A return lb.toList();
0N/A }
0N/A
0N/A public JCTree visitAnnotation(AnnotationTree node, P p) {
0N/A JCAnnotation t = (JCAnnotation) node;
0N/A JCTree annotationType = copy(t.annotationType, p);
0N/A List<JCExpression> args = copy(t.args, p);
0N/A return M.at(t.pos).Annotation(annotationType, args);
0N/A }
0N/A
0N/A public JCTree visitAssert(AssertTree node, P p) {
0N/A JCAssert t = (JCAssert) node;
0N/A JCExpression cond = copy(t.cond, p);
0N/A JCExpression detail = copy(t.detail, p);
0N/A return M.at(t.pos).Assert(cond, detail);
0N/A }
0N/A
0N/A public JCTree visitAssignment(AssignmentTree node, P p) {
0N/A JCAssign t = (JCAssign) node;
0N/A JCExpression lhs = copy(t.lhs, p);
0N/A JCExpression rhs = copy(t.rhs, p);
0N/A return M.at(t.pos).Assign(lhs, rhs);
0N/A }
0N/A
0N/A public JCTree visitCompoundAssignment(CompoundAssignmentTree node, P p) {
0N/A JCAssignOp t = (JCAssignOp) node;
0N/A JCTree lhs = copy(t.lhs, p);
0N/A JCTree rhs = copy(t.rhs, p);
0N/A return M.at(t.pos).Assignop(t.getTag(), lhs, rhs);
0N/A }
0N/A
0N/A public JCTree visitBinary(BinaryTree node, P p) {
0N/A JCBinary t = (JCBinary) node;
0N/A JCExpression lhs = copy(t.lhs, p);
0N/A JCExpression rhs = copy(t.rhs, p);
0N/A return M.at(t.pos).Binary(t.getTag(), lhs, rhs);
0N/A }
0N/A
0N/A public JCTree visitBlock(BlockTree node, P p) {
0N/A JCBlock t = (JCBlock) node;
0N/A List<JCStatement> stats = copy(t.stats, p);
0N/A return M.at(t.pos).Block(t.flags, stats);
0N/A }
0N/A
0N/A public JCTree visitBreak(BreakTree node, P p) {
0N/A JCBreak t = (JCBreak) node;
0N/A return M.at(t.pos).Break(t.label);
0N/A }
0N/A
0N/A public JCTree visitCase(CaseTree node, P p) {
0N/A JCCase t = (JCCase) node;
0N/A JCExpression pat = copy(t.pat, p);
0N/A List<JCStatement> stats = copy(t.stats, p);
0N/A return M.at(t.pos).Case(pat, stats);
0N/A }
0N/A
0N/A public JCTree visitCatch(CatchTree node, P p) {
0N/A JCCatch t = (JCCatch) node;
0N/A JCVariableDecl param = copy(t.param, p);
0N/A JCBlock body = copy(t.body, p);
0N/A return M.at(t.pos).Catch(param, body);
0N/A }
0N/A
0N/A public JCTree visitClass(ClassTree node, P p) {
0N/A JCClassDecl t = (JCClassDecl) node;
0N/A JCModifiers mods = copy(t.mods, p);
0N/A List<JCTypeParameter> typarams = copy(t.typarams, p);
903N/A JCExpression extending = copy(t.extending, p);
0N/A List<JCExpression> implementing = copy(t.implementing, p);
0N/A List<JCTree> defs = copy(t.defs, p);
0N/A return M.at(t.pos).ClassDef(mods, t.name, typarams, extending, implementing, defs);
0N/A }
0N/A
0N/A public JCTree visitConditionalExpression(ConditionalExpressionTree node, P p) {
0N/A JCConditional t = (JCConditional) node;
0N/A JCExpression cond = copy(t.cond, p);
0N/A JCExpression truepart = copy(t.truepart, p);
0N/A JCExpression falsepart = copy(t.falsepart, p);
0N/A return M.at(t.pos).Conditional(cond, truepart, falsepart);
0N/A }
0N/A
0N/A public JCTree visitContinue(ContinueTree node, P p) {
0N/A JCContinue t = (JCContinue) node;
0N/A return M.at(t.pos).Continue(t.label);
0N/A }
0N/A
0N/A public JCTree visitDoWhileLoop(DoWhileLoopTree node, P p) {
0N/A JCDoWhileLoop t = (JCDoWhileLoop) node;
0N/A JCStatement body = copy(t.body, p);
0N/A JCExpression cond = copy(t.cond, p);
0N/A return M.at(t.pos).DoLoop(body, cond);
0N/A }
0N/A
0N/A public JCTree visitErroneous(ErroneousTree node, P p) {
0N/A JCErroneous t = (JCErroneous) node;
0N/A List<? extends JCTree> errs = copy(t.errs, p);
0N/A return M.at(t.pos).Erroneous(errs);
0N/A }
0N/A
0N/A public JCTree visitExpressionStatement(ExpressionStatementTree node, P p) {
0N/A JCExpressionStatement t = (JCExpressionStatement) node;
0N/A JCExpression expr = copy(t.expr, p);
0N/A return M.at(t.pos).Exec(expr);
0N/A }
0N/A
0N/A public JCTree visitEnhancedForLoop(EnhancedForLoopTree node, P p) {
0N/A JCEnhancedForLoop t = (JCEnhancedForLoop) node;
0N/A JCVariableDecl var = copy(t.var, p);
0N/A JCExpression expr = copy(t.expr, p);
0N/A JCStatement body = copy(t.body, p);
0N/A return M.at(t.pos).ForeachLoop(var, expr, body);
0N/A }
0N/A
0N/A public JCTree visitForLoop(ForLoopTree node, P p) {
0N/A JCForLoop t = (JCForLoop) node;
0N/A List<JCStatement> init = copy(t.init, p);
0N/A JCExpression cond = copy(t.cond, p);
0N/A List<JCExpressionStatement> step = copy(t.step, p);
0N/A JCStatement body = copy(t.body, p);
0N/A return M.at(t.pos).ForLoop(init, cond, step, body);
0N/A }
0N/A
0N/A public JCTree visitIdentifier(IdentifierTree node, P p) {
0N/A JCIdent t = (JCIdent) node;
0N/A return M.at(t.pos).Ident(t.name);
0N/A }
0N/A
0N/A public JCTree visitIf(IfTree node, P p) {
0N/A JCIf t = (JCIf) node;
0N/A JCExpression cond = copy(t.cond, p);
0N/A JCStatement thenpart = copy(t.thenpart, p);
0N/A JCStatement elsepart = copy(t.elsepart, p);
0N/A return M.at(t.pos).If(cond, thenpart, elsepart);
0N/A }
0N/A
0N/A public JCTree visitImport(ImportTree node, P p) {
0N/A JCImport t = (JCImport) node;
0N/A JCTree qualid = copy(t.qualid, p);
0N/A return M.at(t.pos).Import(qualid, t.staticImport);
0N/A }
0N/A
0N/A public JCTree visitArrayAccess(ArrayAccessTree node, P p) {
0N/A JCArrayAccess t = (JCArrayAccess) node;
0N/A JCExpression indexed = copy(t.indexed, p);
0N/A JCExpression index = copy(t.index, p);
0N/A return M.at(t.pos).Indexed(indexed, index);
0N/A }
0N/A
0N/A public JCTree visitLabeledStatement(LabeledStatementTree node, P p) {
0N/A JCLabeledStatement t = (JCLabeledStatement) node;
0N/A JCStatement body = copy(t.body, p);
0N/A return M.at(t.pos).Labelled(t.label, t.body);
0N/A }
0N/A
0N/A public JCTree visitLiteral(LiteralTree node, P p) {
0N/A JCLiteral t = (JCLiteral) node;
0N/A return M.at(t.pos).Literal(t.typetag, t.value);
0N/A }
0N/A
0N/A public JCTree visitMethod(MethodTree node, P p) {
0N/A JCMethodDecl t = (JCMethodDecl) node;
0N/A JCModifiers mods = copy(t.mods, p);
0N/A JCExpression restype = copy(t.restype, p);
0N/A List<JCTypeParameter> typarams = copy(t.typarams, p);
0N/A List<JCVariableDecl> params = copy(t.params, p);
0N/A List<JCExpression> thrown = copy(t.thrown, p);
0N/A JCBlock body = copy(t.body, p);
0N/A JCExpression defaultValue = copy(t.defaultValue, p);
814N/A return M.at(t.pos).MethodDef(mods, t.name, restype, typarams, params, thrown, body, defaultValue);
0N/A }
0N/A
0N/A public JCTree visitMethodInvocation(MethodInvocationTree node, P p) {
0N/A JCMethodInvocation t = (JCMethodInvocation) node;
0N/A List<JCExpression> typeargs = copy(t.typeargs, p);
0N/A JCExpression meth = copy(t.meth, p);
0N/A List<JCExpression> args = copy(t.args, p);
0N/A return M.at(t.pos).Apply(typeargs, meth, args);
0N/A }
0N/A
0N/A public JCTree visitModifiers(ModifiersTree node, P p) {
0N/A JCModifiers t = (JCModifiers) node;
0N/A List<JCAnnotation> annotations = copy(t.annotations, p);
0N/A return M.at(t.pos).Modifiers(t.flags, annotations);
0N/A }
0N/A
0N/A public JCTree visitNewArray(NewArrayTree node, P p) {
0N/A JCNewArray t = (JCNewArray) node;
0N/A JCExpression elemtype = copy(t.elemtype, p);
0N/A List<JCExpression> dims = copy(t.dims, p);
0N/A List<JCExpression> elems = copy(t.elems, p);
0N/A return M.at(t.pos).NewArray(elemtype, dims, elems);
0N/A }
0N/A
0N/A public JCTree visitNewClass(NewClassTree node, P p) {
0N/A JCNewClass t = (JCNewClass) node;
0N/A JCExpression encl = copy(t.encl, p);
0N/A List<JCExpression> typeargs = copy(t.typeargs, p);
0N/A JCExpression clazz = copy(t.clazz, p);
0N/A List<JCExpression> args = copy(t.args, p);
0N/A JCClassDecl def = copy(t.def, p);
0N/A return M.at(t.pos).NewClass(encl, typeargs, clazz, args, def);
0N/A }
0N/A
0N/A public JCTree visitParenthesized(ParenthesizedTree node, P p) {
0N/A JCParens t = (JCParens) node;
0N/A JCExpression expr = copy(t.expr, p);
0N/A return M.at(t.pos).Parens(expr);
0N/A }
0N/A
0N/A public JCTree visitReturn(ReturnTree node, P p) {
0N/A JCReturn t = (JCReturn) node;
0N/A JCExpression expr = copy(t.expr, p);
0N/A return M.at(t.pos).Return(expr);
0N/A }
0N/A
0N/A public JCTree visitMemberSelect(MemberSelectTree node, P p) {
0N/A JCFieldAccess t = (JCFieldAccess) node;
0N/A JCExpression selected = copy(t.selected, p);
0N/A return M.at(t.pos).Select(selected, t.name);
0N/A }
0N/A
0N/A public JCTree visitEmptyStatement(EmptyStatementTree node, P p) {
0N/A JCSkip t = (JCSkip) node;
0N/A return M.at(t.pos).Skip();
0N/A }
0N/A
0N/A public JCTree visitSwitch(SwitchTree node, P p) {
0N/A JCSwitch t = (JCSwitch) node;
0N/A JCExpression selector = copy(t.selector, p);
0N/A List<JCCase> cases = copy(t.cases, p);
0N/A return M.at(t.pos).Switch(selector, cases);
0N/A }
0N/A
0N/A public JCTree visitSynchronized(SynchronizedTree node, P p) {
0N/A JCSynchronized t = (JCSynchronized) node;
0N/A JCExpression lock = copy(t.lock, p);
0N/A JCBlock body = copy(t.body, p);
0N/A return M.at(t.pos).Synchronized(lock, body);
0N/A }
0N/A
0N/A public JCTree visitThrow(ThrowTree node, P p) {
0N/A JCThrow t = (JCThrow) node;
0N/A JCTree expr = copy(t.expr, p);
0N/A return M.at(t.pos).Throw(expr);
0N/A }
0N/A
0N/A public JCTree visitCompilationUnit(CompilationUnitTree node, P p) {
0N/A JCCompilationUnit t = (JCCompilationUnit) node;
0N/A List<JCAnnotation> packageAnnotations = copy(t.packageAnnotations, p);
0N/A JCExpression pid = copy(t.pid, p);
0N/A List<JCTree> defs = copy(t.defs, p);
0N/A return M.at(t.pos).TopLevel(packageAnnotations, pid, defs);
0N/A }
0N/A
0N/A public JCTree visitTry(TryTree node, P p) {
0N/A JCTry t = (JCTry) node;
608N/A List<JCTree> resources = copy(t.resources, p);
0N/A JCBlock body = copy(t.body, p);
0N/A List<JCCatch> catchers = copy(t.catchers, p);
0N/A JCBlock finalizer = copy(t.finalizer, p);
608N/A return M.at(t.pos).Try(resources, body, catchers, finalizer);
0N/A }
0N/A
0N/A public JCTree visitParameterizedType(ParameterizedTypeTree node, P p) {
0N/A JCTypeApply t = (JCTypeApply) node;
0N/A JCExpression clazz = copy(t.clazz, p);
0N/A List<JCExpression> arguments = copy(t.arguments, p);
0N/A return M.at(t.pos).TypeApply(clazz, arguments);
0N/A }
0N/A
968N/A public JCTree visitUnionType(UnionTypeTree node, P p) {
968N/A JCTypeUnion t = (JCTypeUnion) node;
723N/A List<JCExpression> components = copy(t.alternatives, p);
968N/A return M.at(t.pos).TypeUnion(components);
549N/A }
549N/A
0N/A public JCTree visitArrayType(ArrayTypeTree node, P p) {
0N/A JCArrayTypeTree t = (JCArrayTypeTree) node;
0N/A JCExpression elemtype = copy(t.elemtype, p);
0N/A return M.at(t.pos).TypeArray(elemtype);
0N/A }
0N/A
0N/A public JCTree visitTypeCast(TypeCastTree node, P p) {
0N/A JCTypeCast t = (JCTypeCast) node;
0N/A JCTree clazz = copy(t.clazz, p);
0N/A JCExpression expr = copy(t.expr, p);
0N/A return M.at(t.pos).TypeCast(clazz, expr);
0N/A }
0N/A
0N/A public JCTree visitPrimitiveType(PrimitiveTypeTree node, P p) {
0N/A JCPrimitiveTypeTree t = (JCPrimitiveTypeTree) node;
0N/A return M.at(t.pos).TypeIdent(t.typetag);
0N/A }
0N/A
0N/A public JCTree visitTypeParameter(TypeParameterTree node, P p) {
0N/A JCTypeParameter t = (JCTypeParameter) node;
0N/A List<JCExpression> bounds = copy(t.bounds, p);
814N/A return M.at(t.pos).TypeParameter(t.name, bounds);
0N/A }
0N/A
0N/A public JCTree visitInstanceOf(InstanceOfTree node, P p) {
0N/A JCInstanceOf t = (JCInstanceOf) node;
0N/A JCExpression expr = copy(t.expr, p);
0N/A JCTree clazz = copy(t.clazz, p);
0N/A return M.at(t.pos).TypeTest(expr, clazz);
0N/A }
0N/A
0N/A public JCTree visitUnary(UnaryTree node, P p) {
0N/A JCUnary t = (JCUnary) node;
0N/A JCExpression arg = copy(t.arg, p);
0N/A return M.at(t.pos).Unary(t.getTag(), arg);
0N/A }
0N/A
0N/A public JCTree visitVariable(VariableTree node, P p) {
0N/A JCVariableDecl t = (JCVariableDecl) node;
0N/A JCModifiers mods = copy(t.mods, p);
0N/A JCExpression vartype = copy(t.vartype, p);
0N/A JCExpression init = copy(t.init, p);
0N/A return M.at(t.pos).VarDef(mods, t.name, vartype, init);
0N/A }
0N/A
0N/A public JCTree visitWhileLoop(WhileLoopTree node, P p) {
0N/A JCWhileLoop t = (JCWhileLoop) node;
0N/A JCStatement body = copy(t.body, p);
0N/A JCExpression cond = copy(t.cond, p);
0N/A return M.at(t.pos).WhileLoop(cond, body);
0N/A }
0N/A
0N/A public JCTree visitWildcard(WildcardTree node, P p) {
0N/A JCWildcard t = (JCWildcard) node;
0N/A TypeBoundKind kind = M.at(t.kind.pos).TypeBoundKind(t.kind.kind);
0N/A JCTree inner = copy(t.inner, p);
0N/A return M.at(t.pos).Wildcard(kind, inner);
0N/A }
0N/A
0N/A public JCTree visitOther(Tree node, P p) {
0N/A JCTree tree = (JCTree) node;
0N/A switch (tree.getTag()) {
0N/A case JCTree.LETEXPR: {
0N/A LetExpr t = (LetExpr) node;
0N/A List<JCVariableDecl> defs = copy(t.defs, p);
0N/A JCTree expr = copy(t.expr, p);
0N/A return M.at(t.pos).LetExpr(defs, expr);
0N/A }
0N/A default:
0N/A throw new AssertionError("unknown tree tag: " + tree.getTag());
0N/A }
0N/A }
0N/A
0N/A}