public void acceptVisitor(final IAstVisitor<? super T, ?> visitor) { AstNode next; for (AstNode current = _node.getFirstChild(); current != null; current = next) { assert current.getParent() == _node; next = current.getNextSibling(); if (current.getRole() == _role) { current.acceptVisitor(visitor, null); } } }
protected final <T extends AstNode> void setChildByRole(final Role<T> role, final T newChild) { final T oldChild = getChildByRole(role); if (oldChild.isNull()) { addChild(newChild, role); } else { oldChild.replaceWith(newChild); } }
@Override public EntityDeclaration clone() { final EntityDeclaration copy = (EntityDeclaration) super.clone(); copy._anyModifiers = _anyModifiers; return copy; }
private void recurse(final AstNode replacement) { final AstNode parent = replacement.getParent(); if (parent != null) { parent.acceptVisitor(this, null); } else { replacement.acceptVisitor(this, null); } } }
labelInfo.gotoStatements.size() == 1 && label.getParent() instanceof BlockStatement && label.getParent().getParent() instanceof SwitchSection && label.getParent().getParent().getParent() == commonAncestor) { s.getParent().getParent() instanceof SwitchSection && s.getParent().getParent().getParent() == commonAncestor) { final SwitchSection targetSection = (SwitchSection) label.getParent().getParent(); final BlockStatement fallThroughBlock = (BlockStatement) s.getParent(); final SwitchSection fallThroughSection = (SwitchSection) fallThroughBlock.getParent(); for (; current != null && !remainingNodes.isEmpty(); current = current.getNextSibling()) { if (current instanceof Statement) { orderedNodes.addLast(current); current = startNode.getPreviousSibling(); for (; current != null && !remainingNodes.isEmpty(); current = current.getPreviousSibling()) { if (current instanceof Statement) { orderedNodes.addFirst(current); final AstNode insertBefore = orderedNodes.getLast().getNextSibling(); final AstNode insertAfter = orderedNodes.getFirst().getPreviousSibling(); node.remove(); blockStatements.add((Statement) node); if (!AstNode.isUnconditionalBranch(lastOrDefault(newBlock.getStatements()))) {
isNull() ? "Cannot replace null nodes." : "Cannot replace the root node." ); final Role oldRole = this.getRole(); remove(); if (replacement != null && !replacement.isNull()) { if (replacement.getParent() != null) { throw new IllegalStateException("replace function must return the root of a tree"); oldParent.insertChildBeforeUnsafe(oldSuccessor, replacement, oldRole); oldParent.addChildUnsafe(replacement, oldRole);
public final <T extends AstNode> void insertChildBefore(final AstNode nextSibling, final T child, final Role<T> role) { VerifyArgument.notNull(role, "role"); if (nextSibling == null || nextSibling.isNull()) { addChild(child, role); return; } if (child == null || child.isNull()) { return; } verifyNotFrozen(); if (((AstNode)child)._parent != null) { throw new IllegalArgumentException("Node belongs to another tree."); } if (child.isFrozen()) { throw new IllegalArgumentException("Cannot add a frozen node."); } if (nextSibling._parent != this) { throw new IllegalArgumentException("Next sibling is not a child of this node."); } insertChildBeforeUnsafe(nextSibling, child, role); }
protected boolean checkResolveAfterRemoveCast(final AstNode parent) { final AstNode grandParent = parent.getParent(); if (grandParent == null || parent.getRole() != Roles.ARGUMENT) { return true; final Expression expression = (Expression) grandParent.clone(); final AstNodeCollection<Expression> arguments = expression.getChildrenByRole(Roles.ARGUMENT); final List<TypeReference> argumentTypes = getTypes(arguments); MemberReference memberReference = grandParent.getUserData(Keys.MEMBER_REFERENCE); if (!(memberReference instanceof MethodReference) && grandParent.getParent() != null) { memberReference = grandParent.getParent().getUserData(Keys.MEMBER_REFERENCE); final int argumentIndex = indexOf(grandParent.getChildrenByRole(Roles.ARGUMENT), (Expression) parent); final Expression toReplace = get(arguments, argumentIndex);
@Override protected AstNode visitChildren(final AstNode node, final Void data) { AstNode next; for (AstNode child = node.getFirstChild(); child != null; child = next) { next = child.getNextSibling(); final AstNode childResult = child.acceptVisitor(this, data); if (childResult != null && childResult != child) { next = childResult; } } return node; }
public final <T extends AstNode> void addChild(final T child, final Role<? extends T> role) { VerifyArgument.notNull(role, "role"); if (child == null || child.isNull()) { return; } verifyNotFrozen(); if (((AstNode)child)._parent != null) { throw new IllegalArgumentException("Node belongs to another tree."); } if (child.isFrozen()) { throw new IllegalArgumentException("Cannot add a frozen node."); } addChildUnsafe(child, role); }
@Override @SuppressWarnings("CloneDoesntDeclareCloneNotSupportedException") public AstNode clone() { try { final AstNode clone = (AstNode) super.clone(); clone._parent = null; clone._firstChild = null; clone._lastChild = null; clone._previousSibling = null; clone._nextSibling = null; clone.flags &= ~FROZEN_BIT; for (final Key<?> key : Keys.ALL_KEYS) { copyKey(this, clone, key); } for (AstNode current = _firstChild; current != null; current = current._nextSibling) { clone.addChildUnsafe(current.clone(), current.getRole()); } // clone.cloneAnnotations(); return clone; } catch (CloneNotSupportedException e) { throw new UndeclaredThrowableException(e); } }
public static boolean isCastRedundant(final Function<AstNode, ResolveResult> resolver, final CastExpression cast) { AstNode parent = skipParenthesesUp(cast.getParent()); if (parent == null) { return false; } if (parent.getRole() == Roles.ARGUMENT || parent.isReference()) { parent = parent.getParent(); } final IsRedundantVisitor visitor = new IsRedundantVisitor(resolver, false); parent.acceptVisitor(visitor, null); return visitor.isRedundant(); }
private void writeSpecials(final AstNode start, final AstNode end) { for (AstNode current = start; current != end; current = current.getNextSibling()) { if (current.getRole() == Roles.COMMENT || current.getRole() == Roles.NEW_LINE) { current.acceptVisitor(this, null); } } }
@Override public void run(final AstNode compilationUnit) { compilationUnit.acceptVisitor(this, null); } }
public void run(final AstNode root) { if (!(root instanceof MethodDeclaration)) { throw new IllegalArgumentException("InliningVisitor must be run against a MethodDeclaration."); } final MethodDeclaration clone = (MethodDeclaration) root.clone(); super.run(clone); final BlockStatement body = clone.getBody(); final AstNodeCollection<Statement> statements = body.getStatements(); if (statements.size() == 1) { final Statement firstStatement = statements.firstOrNullObject(); if (firstStatement instanceof ExpressionStatement || firstStatement instanceof ReturnStatement) { _result = firstStatement.getChildByRole(Roles.EXPRESSION); _result.remove(); return; } } _result = body; _result.remove(); }
public String getText(final JavaFormattingOptions options) { if (isNull()) { return StringUtilities.EMPTY; } final ITextOutput output = new PlainTextOutput(); final JavaOutputVisitor visitor = new JavaOutputVisitor(output, DecompilerSettings.javaDefaults()); acceptVisitor(visitor, null); return output.toString(); }
@Override public boolean add(final T t) { _node.addChild(t, _role); return true; }
@Override protected void freezeCore() { for (AstNode child = _firstChild; child != null; child = child._nextSibling) { child.freezeIfUnfrozen(); } flags |= FROZEN_BIT; }
public void copyUserDataFrom(final AstNode source) { VerifyArgument.notNull(source, "source"); for (final Key<?> key : Keys.ALL_KEYS) { copyKey(source, this, key); } }