| 1 | /******************************************************************************* |
|---|---|
| 2 | * Copyright (c) 2013, 2014 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 | import java.util.ArrayList; |
| 17 | import java.util.List; |
| 18 | |
| 19 | /** |
| 20 | * AST node for an array dimension (added in JLS8 API). |
| 21 | * <p> |
| 22 | * A dimension, represented as <b>[]</b>, can have type annotations. The dimension node is used for: |
| 23 | * </p> |
| 24 | * <ul> |
| 25 | * <li>the dimensions of an {@link ArrayType}</li> |
| 26 | * <li>extra dimension in the following node types: |
| 27 | * {@link SingleVariableDeclaration}, {@link VariableDeclarationFragment}, {@link MethodDeclaration}</li> |
| 28 | * </ul> |
| 29 | * |
| 30 | * <pre> |
| 31 | * Dimension: |
| 32 | * { Annotation } <b>[]</b> |
| 33 | * </pre> |
| 34 | * |
| 35 | * @since 3.10 |
| 36 | * @noinstantiate This class is not intended to be instantiated by clients. |
| 37 | */ |
| 38 | @SuppressWarnings({"rawtypes", "unchecked"}) |
| 39 | public class Dimension extends ASTNode { |
| 40 | |
| 41 | /** |
| 42 | * The "annotations" structural property of this node type (element type: {@link Annotation}). |
| 43 | */ |
| 44 | public static final ChildListPropertyDescriptor ANNOTATIONS_PROPERTY = |
| 45 | new ChildListPropertyDescriptor(Dimension.class, "annotations", Annotation.class, NO_CYCLE_RISK); //$NON-NLS-1$ |
| 46 | |
| 47 | /** |
| 48 | * A list of property descriptors (element type: |
| 49 | * {@link StructuralPropertyDescriptor}), |
| 50 | * or null if uninitialized. |
| 51 | */ |
| 52 | private static final List PROPERTY_DESCRIPTORS_8_0; |
| 53 | |
| 54 | static { |
| 55 | List propertyList = new ArrayList(2); |
| 56 | createPropertyList(Dimension.class, propertyList); |
| 57 | addProperty(ANNOTATIONS_PROPERTY, propertyList); |
| 58 | PROPERTY_DESCRIPTORS_8_0 = reapPropertyList(propertyList); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Returns a list of structural property descriptors for this node type. |
| 63 | * Clients must not modify the result. |
| 64 | * |
| 65 | * @param apiLevel the API level; one of the |
| 66 | * <code>AST.JLS*</code> constants |
| 67 | * @return a list of property descriptors (element type: |
| 68 | * {@link StructuralPropertyDescriptor}) |
| 69 | */ |
| 70 | public static List propertyDescriptors(int apiLevel) { |
| 71 | return PROPERTY_DESCRIPTORS_8_0; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * The list of annotations for this dimension (element type: {@link Annotation}). |
| 76 | * Defaults to an empty list. |
| 77 | */ |
| 78 | private ASTNode.NodeList annotations = new ASTNode.NodeList(ANNOTATIONS_PROPERTY); |
| 79 | |
| 80 | /** |
| 81 | * Creates a new dimension node (supported only in level JLS8 or above). |
| 82 | * <p> |
| 83 | * N.B. This constructor is package-private. |
| 84 | * </p> |
| 85 | * |
| 86 | * @param ast the AST that is to own this node |
| 87 | * @exception UnsupportedOperationException if this operation is used |
| 88 | * in a JLS2, JLS3 or JLS4 AST |
| 89 | */ |
| 90 | Dimension(AST ast) { |
| 91 | super(ast); |
| 92 | unsupportedIn2_3_4(); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | final List internalStructuralPropertiesForType(int apiLevel) { |
| 97 | return propertyDescriptors(apiLevel); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | final List internalGetChildListProperty(ChildListPropertyDescriptor property) { |
| 102 | if (property == ANNOTATIONS_PROPERTY) { |
| 103 | return annotations(); |
| 104 | } |
| 105 | // allow default implementation to flag the error |
| 106 | return super.internalGetChildListProperty(property); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | final int getNodeType0() { |
| 111 | return DIMENSION; |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | ASTNode clone0(AST target) { |
| 116 | Dimension result = new Dimension(target); |
| 117 | result.annotations().addAll( |
| 118 | ASTNode.copySubtrees(target, annotations())); |
| 119 | return result; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
| 124 | // dispatch to correct overloaded match method |
| 125 | return matcher.match(this, other); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | void accept0(ASTVisitor visitor) { |
| 130 | boolean visitChildren = visitor.visit(this); |
| 131 | if (visitChildren) { |
| 132 | // visit children in normal left to right reading order |
| 133 | acceptChildren(visitor, this.annotations); |
| 134 | } |
| 135 | visitor.endVisit(this); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Returns the live ordered list of annotations for this dimension. |
| 140 | * |
| 141 | * @return the live list of annotations (element type: {@link Annotation}) |
| 142 | */ |
| 143 | public List annotations() { |
| 144 | return this.annotations; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | int memSize() { |
| 149 | return BASE_NODE_SIZE + 1 * 4; |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | int treeSize() { |
| 154 | return |
| 155 | memSize() |
| 156 | + this.annotations.listSize(); |
| 157 | } |
| 158 | } |
| 159 |
Members