001    /*
002     * JastAddJ is covered by the modified BSD License. You should have received a copy of the
003     * modified BSD license with this compiler.
004     * 
005     * Copyright (c) 2011, Jesper Öqvist <jesper.oqvist@cs.lth.se>
006     * All rights reserved.
007     */
008    
009    /**
010     * Extensions to the generated Abstract Syntax Tree.
011     */
012    aspect JastAddExtensions {
013    
014        /**
015         * Create a deep copy of this subtree.
016         * The copy is dangling, i.e. has no parent.
017         *
018         * @return a dangling copy of the subtree at this node
019         */
020        public ASTNode ASTNode.cloneSubtree() {
021                try {
022                        ASTNode tree = (ASTNode) clone();
023                        tree.setParent(null);// make dangling
024                        if (children != null) {
025                                tree.children = new ASTNode[children.length];
026                                for (int i = 0; i < children.length; ++i) {
027                                        if (children[i] == null) {
028                                                tree.children[i] = null;
029                                        } else {
030                                                tree.children[i] = children[i].cloneSubtree();
031                                                tree.children[i].setParent(tree);
032                                        }
033                                }
034                        }
035                        return tree;
036                } catch (CloneNotSupportedException e) {
037                        throw new Error("Error: clone not supported for " +
038                                        getClass().getName());
039                }
040        }
041    }
042