EclipseJDT Source Viewer

Home|eclipse_jdt/src/org/eclipse/jdt/core/dom/Statement.java
1/*******************************************************************************
2 * Copyright (c) 2000, 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
15package org.eclipse.jdt.core.dom;
16
17import org.eclipse.jdt.core.compiler.InvalidInputException;
18import org.eclipse.jdt.internal.compiler.parser.Scanner;
19import org.eclipse.jdt.internal.compiler.parser.TerminalTokens;
20
21/**
22 * Abstract base class of AST nodes that represent statements.
23 * There are many kinds of statements.
24 * <p>
25 * The grammar combines both Statement and BlockStatement.</p>
26 * <pre>
27 * Statement:
28 *    {@link AssertStatement},
29 *    {@link Block},
30 *    {@link BreakStatement},
31 *    {@link ConstructorInvocation},
32 *    {@link ContinueStatement},
33 *    {@link DoStatement},
34 *    {@link EmptyStatement},
35 *    {@link EnhancedForStatement}
36 *    {@link ExpressionStatement},
37 *    {@link ForStatement},
38 *    {@link IfStatement},
39 *    {@link LabeledStatement},
40 *    {@link ReturnStatement},
41 *    {@link SuperConstructorInvocation},
42 *    {@link SwitchCase},
43 *    {@link SwitchStatement},
44 *    {@link SynchronizedStatement},
45 *    {@link ThrowStatement},
46 *    {@link TryStatement},
47 *    {@link TypeDeclarationStatement},
48 *    {@link VariableDeclarationStatement},
49 *    {@link WhileStatement}
50 * </pre>
51 *
52 * @since 2.0
53 */
54public abstract class Statement extends ASTNode {
55
56    /**
57     * The leading comment, or <code>null</code> if none.
58     * Defaults to none.
59     *
60     * @deprecated The leading comment feature was removed in 2.1.
61     */
62    private String optionalLeadingComment = null;
63
64    /**
65     * Creates a new AST node for a statement owned by the given AST.
66     * <p>
67     * N.B. This constructor is package-private.
68     * </p>
69     *
70     * @param ast the AST that is to own this node
71     */
72    Statement(AST ast) {
73        super(ast);
74    }
75
76    /**
77     * Returns the leading comment string, including the starting
78     * and ending comment delimiters, and any embedded line breaks.
79     * <p>
80     * A leading comment is a comment that appears before the statement.
81     * It may be either a traditional comment or an end-of-line comment.
82     * Traditional comments must begin with "/&#42;, may contain line breaks,
83     * and must end with "&#42;/. End-of-line comments must begin with "//",
84     * must end with a line delimiter (as per JLS 3.7), and must not contain
85     * line breaks.
86     * </p>
87     *
88     * @return the comment string, or <code>null</code> if none
89     * @deprecated This feature was removed in the 2.1 release because it was
90     * only a partial, and inadequate, solution to the issue of associating
91     * comments with statements. Furthermore, AST.parseCompilationUnit did not
92     * associate leading comments, making this moot. Clients that need to access
93     * comments preceding a statement should either consult the compilation
94     * unit's {@linkplain CompilationUnit#getCommentList() comment table}
95     * or use a scanner to reanalyze the source text immediately preceding
96     * the statement's source range.
97     */
98    public String getLeadingComment() {
99        return this.optionalLeadingComment;
100    }
101
102    /**
103     * Sets or clears the leading comment string. The comment
104     * string must include the starting and ending comment delimiters,
105     * and any embedded linebreaks.
106     * <p>
107     * A leading comment is a comment that appears before the statement.
108     * It may be either a traditional comment or an end-of-line comment.
109     * Traditional comments must begin with "/&#42;, may contain line breaks,
110     * and must end with "&#42;/. End-of-line comments must begin with "//"
111     * (as per JLS 3.7), and must not contain line breaks.
112     * </p>
113     * <p>
114     * Examples:
115     * <pre>
116     * <code>
117     * setLeadingComment("/&#42; traditional comment &#42;/");  // correct
118     * setLeadingComment("missing comment delimiters");  // wrong
119     * setLeadingComment("/&#42; unterminated traditional comment ");  // wrong
120     * setLeadingComment("/&#42; broken\n traditional comment &#42;/");  // correct
121     * setLeadingComment("// end-of-line comment\n");  // correct
122     * setLeadingComment("// end-of-line comment without line terminator");  // correct
123     * setLeadingComment("// broken\n end-of-line comment\n");  // wrong
124     * </code>
125     * </pre>
126     *
127     * @param comment the comment string, or <code>null</code> if none
128     * @exception IllegalArgumentException if the comment string is invalid
129     * @deprecated This feature was removed in the 2.1 release because it was
130     * only a partial, and inadequate, solution to the issue of associating
131     * comments with statements.
132     */
133    public void setLeadingComment(String comment) {
134        if (comment != null) {
135            char[] source = comment.toCharArray();
136            Scanner scanner = this.ast.scanner;
137            scanner.resetTo(0source.length);
138            scanner.setSource(source);
139            try {
140                int token;
141                boolean onlyOneComment = false;
142                while ((token = scanner.getNextToken()) != TerminalTokens.TokenNameEOF) {
143                    switch(token) {
144                        case TerminalTokens.TokenNameCOMMENT_BLOCK :
145                        case TerminalTokens.TokenNameCOMMENT_JAVADOC :
146                        case TerminalTokens.TokenNameCOMMENT_LINE :
147                            if (onlyOneComment) {
148                                throw new IllegalArgumentException();
149                            }
150                            onlyOneComment = true;
151                            break;
152                        default:
153                            onlyOneComment = false;
154                    }
155                }
156                if (!onlyOneComment) {
157                    throw new IllegalArgumentException();
158                }
159            } catch (InvalidInputException e) {
160                throw new IllegalArgumentException(e);
161            }
162        }
163        // we do not consider the obsolete comment as a structureal property
164        // but we protect them nevertheless
165        checkModifiable();
166        this.optionalLeadingComment = comment;
167    }
168
169    /**
170     * Copies the leading comment from the given statement.
171     *
172     * @param source the statement that supplies the leading comment
173     * @since 2.1
174     */
175    void copyLeadingComment(Statement source) {
176        setLeadingComment(source.getLeadingComment());
177    }
178
179    @Override
180    int memSize() {
181        int size = BASE_NODE_SIZE + 1 * 4 + stringSize(getLeadingComment());
182        return size;
183    }
184}
185
186
MembersX
Statement:copyLeadingComment
Statement:setLeadingComment:Block:Block:source
Statement:setLeadingComment:Block:Block:scanner
Statement:setLeadingComment:Block:Block:Block:onlyOneComment
Statement:getLeadingComment
Statement:memSize:Block:size
Statement:memSize
Statement:optionalLeadingComment
Statement:setLeadingComment
Statement:Statement
Statement:setLeadingComment:Block:Block:Block:token
Members
X