1 | /******************************************************************************* |
---|---|
2 | * Copyright (c) 2004, 2013 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 | |
15 | package org.eclipse.jdt.core.dom; |
16 | |
17 | import java.util.ArrayList; |
18 | import java.util.List; |
19 | |
20 | import org.eclipse.jdt.internal.compiler.util.Util; |
21 | |
22 | /** |
23 | * AST node for a text element within a doc comment. |
24 | * <pre> |
25 | * TextElement: |
26 | * Sequence of characters not including a close comment delimiter <b>*</b><b>/</b> |
27 | * </pre> |
28 | * |
29 | * @see Javadoc |
30 | * @since 3.0 |
31 | * @noinstantiate This class is not intended to be instantiated by clients. |
32 | */ |
33 | @SuppressWarnings("rawtypes") |
34 | public final class TextElement extends ASTNode implements IDocElement { |
35 | |
36 | /** |
37 | * The "text" structural property of this node type (type: {@link String}). |
38 | * |
39 | * @since 3.0 |
40 | */ |
41 | public static final SimplePropertyDescriptor TEXT_PROPERTY = |
42 | new SimplePropertyDescriptor(TextElement.class, "text", String.class, MANDATORY); //$NON-NLS-1$ |
43 | |
44 | /** |
45 | * A list of property descriptors (element type: |
46 | * {@link StructuralPropertyDescriptor}), |
47 | * or null if uninitialized. |
48 | * @since 3.0 |
49 | */ |
50 | private static final List PROPERTY_DESCRIPTORS; |
51 | |
52 | static { |
53 | List propertyList = new ArrayList(2); |
54 | createPropertyList(TextElement.class, propertyList); |
55 | addProperty(TEXT_PROPERTY, propertyList); |
56 | PROPERTY_DESCRIPTORS = reapPropertyList(propertyList); |
57 | } |
58 | |
59 | /** |
60 | * Returns a list of structural property descriptors for this node type. |
61 | * Clients must not modify the result. |
62 | * |
63 | * @param apiLevel the API level; one of the |
64 | * <code>AST.JLS*</code> constants |
65 | * @return a list of property descriptors (element type: |
66 | * {@link StructuralPropertyDescriptor}) |
67 | * @since 3.0 |
68 | */ |
69 | public static List propertyDescriptors(int apiLevel) { |
70 | return PROPERTY_DESCRIPTORS; |
71 | } |
72 | |
73 | /** |
74 | * The text element; defaults to the empty string. |
75 | */ |
76 | private String text = Util.EMPTY_STRING; |
77 | |
78 | /** |
79 | * Creates a new AST node for a text element owned by the given AST. |
80 | * The new node has an empty text string. |
81 | * <p> |
82 | * N.B. This constructor is package-private; all subclasses must be |
83 | * declared in the same package; clients are unable to declare |
84 | * additional subclasses. |
85 | * </p> |
86 | * |
87 | * @param ast the AST that is to own this node |
88 | */ |
89 | TextElement(AST ast) { |
90 | super(ast); |
91 | } |
92 | |
93 | @Override |
94 | final List internalStructuralPropertiesForType(int apiLevel) { |
95 | return propertyDescriptors(apiLevel); |
96 | } |
97 | |
98 | @Override |
99 | final Object internalGetSetObjectProperty(SimplePropertyDescriptor property, boolean get, Object value) { |
100 | if (property == TEXT_PROPERTY) { |
101 | if (get) { |
102 | return getText(); |
103 | } else { |
104 | setText((String) value); |
105 | return null; |
106 | } |
107 | } |
108 | // allow default implementation to flag the error |
109 | return super.internalGetSetObjectProperty(property, get, value); |
110 | } |
111 | |
112 | @Override |
113 | final int getNodeType0() { |
114 | return TEXT_ELEMENT; |
115 | } |
116 | |
117 | @Override |
118 | ASTNode clone0(AST target) { |
119 | TextElement result = new TextElement(target); |
120 | result.setSourceRange(getStartPosition(), getLength()); |
121 | result.setText(getText()); |
122 | return result; |
123 | } |
124 | |
125 | @Override |
126 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
127 | // dispatch to correct overloaded match method |
128 | return matcher.match(this, other); |
129 | } |
130 | |
131 | @Override |
132 | void accept0(ASTVisitor visitor) { |
133 | visitor.visit(this); |
134 | visitor.endVisit(this); |
135 | } |
136 | |
137 | /** |
138 | * Returns this node's text. |
139 | * |
140 | * @return the text of this node |
141 | */ |
142 | public String getText() { |
143 | return this.text; |
144 | } |
145 | |
146 | /** |
147 | * Sets the text of this node to the given value. |
148 | * <p> |
149 | * The text element typically includes leading and trailing |
150 | * whitespace that separates it from the immediately preceding |
151 | * or following elements. The text element must not include |
152 | * a block comment closing delimiter "*"+"/". |
153 | * </p> |
154 | * |
155 | * @param text the text of this node |
156 | * @exception IllegalArgumentException if the text is null |
157 | * or contains a block comment closing delimiter |
158 | */ |
159 | public void setText(String text) { |
160 | if (text == null) { |
161 | throw new IllegalArgumentException(); |
162 | } |
163 | if (text.indexOf("*/") > 0) { //$NON-NLS-1$ |
164 | throw new IllegalArgumentException(); |
165 | } |
166 | preValueChange(TEXT_PROPERTY); |
167 | this.text = text; |
168 | postValueChange(TEXT_PROPERTY); |
169 | } |
170 | |
171 | @Override |
172 | int memSize() { |
173 | int size = BASE_NODE_SIZE + 1 * 4; |
174 | if (this.text != Util.EMPTY_STRING) { |
175 | // everything but our empty string costs |
176 | size += stringSize(this.text); |
177 | } |
178 | return size; |
179 | } |
180 | |
181 | @Override |
182 | int treeSize() { |
183 | return memSize(); |
184 | } |
185 | } |
186 | |
187 |
Members