| 1 | /******************************************************************************* |
|---|---|
| 2 | * Copyright (c) 2000, 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 | /** |
| 21 | * Block statement AST node type. |
| 22 | * |
| 23 | * <pre> |
| 24 | * Block: |
| 25 | * <b>{</b> { Statement } <b>}</b> |
| 26 | * </pre> |
| 27 | * |
| 28 | * @since 2.0 |
| 29 | * @noinstantiate This class is not intended to be instantiated by clients. |
| 30 | */ |
| 31 | @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 32 | public class Block extends Statement { |
| 33 | |
| 34 | /** |
| 35 | * The "statements" structural property of this node type (element type: |
| 36 | * {@link Statement}). |
| 37 | * |
| 38 | * @since 3.0 |
| 39 | */ |
| 40 | public static final ChildListPropertyDescriptor STATEMENTS_PROPERTY = new ChildListPropertyDescriptor(Block.class, |
| 41 | "statements", Statement.class, CYCLE_RISK); //$NON-NLS-1$ |
| 42 | |
| 43 | /** |
| 44 | * A list of property descriptors (element type: |
| 45 | * {@link StructuralPropertyDescriptor}), |
| 46 | * or null if uninitialized. |
| 47 | */ |
| 48 | private static final List PROPERTY_DESCRIPTORS; |
| 49 | |
| 50 | static { |
| 51 | List properyList = new ArrayList(2); |
| 52 | createPropertyList(Block.class, properyList); |
| 53 | addProperty(STATEMENTS_PROPERTY, properyList); |
| 54 | PROPERTY_DESCRIPTORS = reapPropertyList(properyList); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns a list of structural property descriptors for this node type. |
| 59 | * Clients must not modify the result. |
| 60 | * |
| 61 | * @param apiLevel the API level; one of the |
| 62 | * <code>AST.JLS*</code> constants |
| 63 | * @return a list of property descriptors (element type: |
| 64 | * {@link StructuralPropertyDescriptor}) |
| 65 | * @since 3.0 |
| 66 | */ |
| 67 | public static List propertyDescriptors(int apiLevel) { |
| 68 | return PROPERTY_DESCRIPTORS; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * The list of statements (element type: {@link Statement}). |
| 73 | * Defaults to an empty list. |
| 74 | */ |
| 75 | private ASTNode.NodeList statements = new ASTNode.NodeList(STATEMENTS_PROPERTY); |
| 76 | |
| 77 | /** |
| 78 | * Creates a new unparented block node owned by the given AST. |
| 79 | * By default, the block is empty. |
| 80 | * <p> |
| 81 | * N.B. This constructor is package-private. |
| 82 | * </p> |
| 83 | * |
| 84 | * @param ast the AST that is to own this node |
| 85 | */ |
| 86 | Block(AST ast) { |
| 87 | super(ast); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | final List internalStructuralPropertiesForType(int apiLevel) { |
| 92 | return propertyDescriptors(apiLevel); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | final List internalGetChildListProperty(ChildListPropertyDescriptor property) { |
| 97 | if (property == STATEMENTS_PROPERTY) { |
| 98 | return statements(); |
| 99 | } |
| 100 | // allow default implementation to flag the error |
| 101 | return super.internalGetChildListProperty(property); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | final int getNodeType0() { |
| 106 | return BLOCK; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | ASTNode clone0(AST target) { |
| 111 | Block result = new Block(target); |
| 112 | result.setSourceRange(getStartPosition(), getLength()); |
| 113 | result.copyLeadingComment(this); |
| 114 | result.statements().addAll( |
| 115 | ASTNode.copySubtrees(target, statements())); |
| 116 | return result; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
| 121 | // dispatch to correct overloaded match method |
| 122 | return matcher.match(this, other); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | void accept0(ASTVisitor visitor) { |
| 127 | boolean visitChildren = visitor.visit(this); |
| 128 | if (visitChildren) { |
| 129 | visitor.pushScope("Block"); |
| 130 | acceptChildren(visitor, this.statements); |
| 131 | visitor.popScope(); |
| 132 | } |
| 133 | visitor.endVisit(this); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Returns the live list of statements in this block. Adding and |
| 138 | * removing nodes from this list affects this node dynamically. |
| 139 | * All nodes in this list must be <code>Statement</code>s; |
| 140 | * attempts to add any other type of node will trigger an |
| 141 | * exception. |
| 142 | * |
| 143 | * @return the live list of statements in this block |
| 144 | * (element type: {@link Statement}) |
| 145 | */ |
| 146 | public List statements() { |
| 147 | return this.statements; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | int memSize() { |
| 152 | return super.memSize() + 1 * 4; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | int treeSize() { |
| 157 | return memSize() + this.statements.listSize(); |
| 158 | } |
| 159 | } |
| 160 |
Members