| 1 | /******************************************************************************* |
|---|---|
| 2 | * Copyright (c) 2004, 2019 IBM Corporation and others. |
| 3 | * |
| 4 | * This program and the accompanying materials |
| 5 | * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | * which accompanies this distribution, and is available at |
| 7 | * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | * |
| 9 | * SPDX-License-Identifier: EPL-2.0 |
| 10 | * |
| 11 | * Contributors: |
| 12 | * IBM Corporation - initial API and implementation |
| 13 | *******************************************************************************/ |
| 14 | package org.eclipse.jdt.core.dom; |
| 15 | |
| 16 | /** |
| 17 | * Abstract base class for all AST nodes that represent comments. |
| 18 | * There are exactly three kinds of comment: |
| 19 | * line comments ({@link LineComment}), |
| 20 | * block comments ({@link BlockComment}), and |
| 21 | * doc comments ({@link Javadoc}). |
| 22 | * <pre> |
| 23 | * Comment: |
| 24 | * LineComment |
| 25 | * BlockComment |
| 26 | * Javadoc |
| 27 | * </pre> |
| 28 | * |
| 29 | * @since 3.0 |
| 30 | */ |
| 31 | public abstract class Comment extends ASTNode { |
| 32 | |
| 33 | /** |
| 34 | * Alternate root node, or <code>null</code> if none. |
| 35 | * Initially <code>null</code>. |
| 36 | */ |
| 37 | private ASTNode alternateRoot = null; |
| 38 | |
| 39 | /** |
| 40 | * Creates a new AST node for a comment owned by the given AST. |
| 41 | * <p> |
| 42 | * N.B. This constructor is package-private. |
| 43 | * </p> |
| 44 | * |
| 45 | * @param ast the AST that is to own this node |
| 46 | */ |
| 47 | Comment(AST ast) { |
| 48 | super(ast); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Returns whether this comment is a block comment |
| 53 | * (<code>BlockComment</code>). |
| 54 | * |
| 55 | * @return <code>true</code> if this is a block comment, and |
| 56 | * <code>false</code> otherwise |
| 57 | */ |
| 58 | public final boolean isBlockComment() { |
| 59 | return (this instanceof BlockComment); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Returns whether this comment is a line comment |
| 64 | * (<code>LineComment</code>). |
| 65 | * |
| 66 | * @return <code>true</code> if this is a line comment, and |
| 67 | * <code>false</code> otherwise |
| 68 | */ |
| 69 | public final boolean isLineComment() { |
| 70 | return (this instanceof LineComment); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns whether this comment is a doc comment |
| 75 | * (<code>Javadoc</code>). |
| 76 | * |
| 77 | * @return <code>true</code> if this is a doc comment, and |
| 78 | * <code>false</code> otherwise |
| 79 | */ |
| 80 | public final boolean isDocComment() { |
| 81 | return (this instanceof Javadoc); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Returns the root AST node that this comment occurs |
| 86 | * within, or <code>null</code> if none (or not recorded). |
| 87 | * <p> |
| 88 | * Typically, the comment nodes created while parsing a compilation |
| 89 | * unit are not considered descendents of the normal AST |
| 90 | * root, namely an {@link CompilationUnit}. Instead, these |
| 91 | * comment nodes exist outside the normal AST and each is |
| 92 | * a root in its own right. This optional property provides |
| 93 | * a well-known way to navigate from the comment to the |
| 94 | * compilation unit in such cases. Note that the alternate root |
| 95 | * property is not one of the comment node's children. It is simply a |
| 96 | * reference to a node. |
| 97 | * </p> |
| 98 | * |
| 99 | * @return the alternate root node, or <code>null</code> |
| 100 | * if none |
| 101 | * @see #setAlternateRoot(ASTNode) |
| 102 | */ |
| 103 | public final ASTNode getAlternateRoot() { |
| 104 | return this.alternateRoot; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Returns the root AST node that this comment occurs |
| 109 | * within, or <code>null</code> if none (or not recorded). |
| 110 | * |
| 111 | * @param root the alternate root node, or <code>null</code> |
| 112 | * if none |
| 113 | * @see #getAlternateRoot() |
| 114 | */ |
| 115 | public final void setAlternateRoot(ASTNode root) { |
| 116 | // alternate root is *not* considered a structural property |
| 117 | // but we protect them nevertheless |
| 118 | checkModifiable(); |
| 119 | this.alternateRoot = root; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | int memSize() { |
| 124 | return BASE_NODE_SIZE + 1 * 4; |
| 125 | } |
| 126 | } |
| 127 |
Members