| 1 | /* |
|---|---|
| 2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
| 3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
| 4 | * |
| 5 | * This file is part of JavaParser. |
| 6 | * |
| 7 | * JavaParser can be used either under the terms of |
| 8 | * a) the GNU Lesser General Public License as published by |
| 9 | * the Free Software Foundation, either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * b) the terms of the Apache License |
| 12 | * |
| 13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
| 14 | * LICENCE.APACHE. Please refer to those files for details. |
| 15 | * |
| 16 | * JavaParser is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU Lesser General Public License for more details. |
| 20 | */ |
| 21 | package com.github.javaparser.ast.comments; |
| 22 | |
| 23 | import com.github.javaparser.ast.AllFieldsConstructor; |
| 24 | import com.github.javaparser.ast.Node; |
| 25 | import com.github.javaparser.ast.observer.ObservableProperty; |
| 26 | import java.util.Optional; |
| 27 | import static com.github.javaparser.utils.Utils.assertNotNull; |
| 28 | import com.github.javaparser.ast.visitor.CloneVisitor; |
| 29 | import com.github.javaparser.metamodel.CommentMetaModel; |
| 30 | import com.github.javaparser.metamodel.InternalProperty; |
| 31 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
| 32 | import com.github.javaparser.TokenRange; |
| 33 | import com.github.javaparser.ast.Generated; |
| 34 | import java.util.function.Consumer; |
| 35 | import static com.github.javaparser.utils.CodeGenerationUtils.f; |
| 36 | |
| 37 | /** |
| 38 | * Abstract class for all AST nodes that represent comments. |
| 39 | * |
| 40 | * @author Julio Vilmar Gesser |
| 41 | * @see BlockComment |
| 42 | * @see LineComment |
| 43 | * @see JavadocComment |
| 44 | */ |
| 45 | public abstract class Comment extends Node { |
| 46 | |
| 47 | private String content; |
| 48 | |
| 49 | @InternalProperty |
| 50 | private Node commentedNode; |
| 51 | |
| 52 | @AllFieldsConstructor |
| 53 | public Comment(String content) { |
| 54 | this(null, content); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * This constructor is used by the parser and is considered private. |
| 59 | */ |
| 60 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
| 61 | public Comment(TokenRange tokenRange, String content) { |
| 62 | super(tokenRange); |
| 63 | setContent(content); |
| 64 | customInitialization(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Return the text of the comment. |
| 69 | * |
| 70 | * @return text of the comment |
| 71 | */ |
| 72 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 73 | public String getContent() { |
| 74 | return content; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Sets the text of the comment. |
| 79 | * |
| 80 | * @param content the text of the comment to set |
| 81 | */ |
| 82 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 83 | public Comment setContent(final String content) { |
| 84 | assertNotNull(content); |
| 85 | if (content == this.content) { |
| 86 | return (Comment) this; |
| 87 | } |
| 88 | notifyPropertyChange(ObservableProperty.CONTENT, this.content, content); |
| 89 | this.content = content; |
| 90 | return this; |
| 91 | } |
| 92 | |
| 93 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 94 | public boolean isLineComment() { |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 99 | public LineComment asLineComment() { |
| 100 | throw new IllegalStateException(f("%s is not an LineComment", this)); |
| 101 | } |
| 102 | |
| 103 | public Optional<Node> getCommentedNode() { |
| 104 | return Optional.ofNullable(this.commentedNode); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Sets the commentedNode |
| 109 | * |
| 110 | * @param commentedNode the commentedNode, can be null |
| 111 | * @return this, the Comment |
| 112 | */ |
| 113 | public Comment setCommentedNode(Node commentedNode) { |
| 114 | notifyPropertyChange(ObservableProperty.COMMENTED_NODE, this.commentedNode, commentedNode); |
| 115 | if (commentedNode == null) { |
| 116 | this.commentedNode = null; |
| 117 | return this; |
| 118 | } |
| 119 | if (commentedNode == this) { |
| 120 | throw new IllegalArgumentException(); |
| 121 | } |
| 122 | if (commentedNode instanceof Comment) { |
| 123 | throw new IllegalArgumentException(); |
| 124 | } |
| 125 | this.commentedNode = commentedNode; |
| 126 | return this; |
| 127 | } |
| 128 | |
| 129 | public boolean isOrphan() { |
| 130 | return this.commentedNode == null; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public Node setComment(final Comment comment) { |
| 135 | // comments on comments are not allowed, so we override setComment(Comment) here |
| 136 | if (comment != null) { |
| 137 | throw new IllegalArgumentException("A comment cannot be commented."); |
| 138 | } |
| 139 | return super.setComment(comment); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public boolean remove() { |
| 144 | // the other are orphan comments and remove should work with them |
| 145 | if (this.commentedNode != null) { |
| 146 | this.commentedNode.setComment(null); |
| 147 | return true; |
| 148 | } else if (this.getParentNode().isPresent()) { |
| 149 | return this.getParentNode().get().removeOrphanComment(this); |
| 150 | } else { |
| 151 | return false; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public Node findRootNode() { |
| 157 | // (Non-orphan) comments are not integrated into the normal AST; we need to get the commented node first. |
| 158 | Node n = getCommentedNode().orElse(this); |
| 159 | while (n.getParentNode().isPresent()) { |
| 160 | n = n.getParentNode().get(); |
| 161 | } |
| 162 | return n; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
| 167 | public boolean remove(Node node) { |
| 168 | if (node == null) |
| 169 | return false; |
| 170 | return super.remove(node); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
| 175 | public Comment clone() { |
| 176 | return (Comment) accept(new CloneVisitor(), null); |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
| 181 | public CommentMetaModel getMetaModel() { |
| 182 | return JavaParserMetaModel.commentMetaModel; |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
| 187 | public boolean replace(Node node, Node replacementNode) { |
| 188 | if (node == null) |
| 189 | return false; |
| 190 | return super.replace(node, replacementNode); |
| 191 | } |
| 192 | |
| 193 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 194 | public boolean isBlockComment() { |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 199 | public BlockComment asBlockComment() { |
| 200 | throw new IllegalStateException(f("%s is not an BlockComment", this)); |
| 201 | } |
| 202 | |
| 203 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 204 | public boolean isJavadocComment() { |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 209 | public JavadocComment asJavadocComment() { |
| 210 | throw new IllegalStateException(f("%s is not an JavadocComment", this)); |
| 211 | } |
| 212 | |
| 213 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 214 | public void ifBlockComment(Consumer<BlockComment> action) { |
| 215 | } |
| 216 | |
| 217 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 218 | public void ifJavadocComment(Consumer<JavadocComment> action) { |
| 219 | } |
| 220 | |
| 221 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 222 | public void ifLineComment(Consumer<LineComment> action) { |
| 223 | } |
| 224 | |
| 225 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 226 | public Optional<BlockComment> toBlockComment() { |
| 227 | return Optional.empty(); |
| 228 | } |
| 229 | |
| 230 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 231 | public Optional<JavadocComment> toJavadocComment() { |
| 232 | return Optional.empty(); |
| 233 | } |
| 234 | |
| 235 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 236 | public Optional<LineComment> toLineComment() { |
| 237 | return Optional.empty(); |
| 238 | } |
| 239 | } |
| 240 |
Members