| 1 | /******************************************************************************* |
|---|---|
| 2 | * Copyright (c) 2004, 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 | package org.eclipse.jdt.core.dom; |
| 15 | |
| 16 | import java.util.ArrayList; |
| 17 | import java.util.List; |
| 18 | |
| 19 | /** |
| 20 | * Normal annotation node (added in JLS3 API). |
| 21 | * <pre> |
| 22 | * NormalAnnotation: |
| 23 | * <b>@</b> TypeName <b>(</b> [ MemberValuePair { <b>,</b> MemberValuePair } ] <b>)</b> |
| 24 | * </pre> |
| 25 | * |
| 26 | * @since 3.1 |
| 27 | * @noinstantiate This class is not intended to be instantiated by clients. |
| 28 | */ |
| 29 | @SuppressWarnings({"rawtypes", "unchecked"}) |
| 30 | public final class NormalAnnotation extends Annotation { |
| 31 | |
| 32 | /** |
| 33 | * The "typeName" structural property of this node type (child type: {@link Name}). |
| 34 | */ |
| 35 | public static final ChildPropertyDescriptor TYPE_NAME_PROPERTY = |
| 36 | internalTypeNamePropertyFactory(NormalAnnotation.class); |
| 37 | |
| 38 | /** |
| 39 | * The "values" structural property of this node type (element type: {@link MemberValuePair}). |
| 40 | */ |
| 41 | public static final ChildListPropertyDescriptor VALUES_PROPERTY = |
| 42 | new ChildListPropertyDescriptor(NormalAnnotation.class, "values", MemberValuePair.class, CYCLE_RISK); //$NON-NLS-1$ |
| 43 | |
| 44 | /** |
| 45 | * A list of property descriptors (element type: |
| 46 | * {@link StructuralPropertyDescriptor}), |
| 47 | * or null if uninitialized. |
| 48 | */ |
| 49 | private static final List PROPERTY_DESCRIPTORS; |
| 50 | |
| 51 | static { |
| 52 | List propertyList = new ArrayList(3); |
| 53 | createPropertyList(NormalAnnotation.class, propertyList); |
| 54 | addProperty(TYPE_NAME_PROPERTY, propertyList); |
| 55 | addProperty(VALUES_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 AST.JLS* constants |
| 64 | * @return a list of property descriptors (element type: |
| 65 | * {@link StructuralPropertyDescriptor}) |
| 66 | */ |
| 67 | public static List propertyDescriptors(int apiLevel) { |
| 68 | return PROPERTY_DESCRIPTORS; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * The list of member value pairs (element type: |
| 73 | * {@link MemberValuePair}). Defaults to an empty list. |
| 74 | */ |
| 75 | private ASTNode.NodeList values = |
| 76 | new ASTNode.NodeList(VALUES_PROPERTY); |
| 77 | |
| 78 | /** |
| 79 | * Creates a new unparented normal annotation node owned |
| 80 | * by the given AST. By default, the annotation has an |
| 81 | * unspecified type name and an empty list of member value |
| 82 | * pairs. |
| 83 | * <p> |
| 84 | * N.B. This constructor is package-private. |
| 85 | * </p> |
| 86 | * |
| 87 | * @param ast the AST that is to own this node |
| 88 | */ |
| 89 | NormalAnnotation(AST ast) { |
| 90 | super(ast); |
| 91 | unsupportedIn2(); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | final List internalStructuralPropertiesForType(int apiLevel) { |
| 96 | return propertyDescriptors(apiLevel); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
| 101 | if (property == TYPE_NAME_PROPERTY) { |
| 102 | if (get) { |
| 103 | return getTypeName(); |
| 104 | } else { |
| 105 | setTypeName((Name) child); |
| 106 | return null; |
| 107 | } |
| 108 | } |
| 109 | // allow default implementation to flag the error |
| 110 | return super.internalGetSetChildProperty(property, get, child); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | final List internalGetChildListProperty(ChildListPropertyDescriptor property) { |
| 115 | if (property == VALUES_PROPERTY) { |
| 116 | return values(); |
| 117 | } |
| 118 | // allow default implementation to flag the error |
| 119 | return super.internalGetChildListProperty(property); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | final ChildPropertyDescriptor internalTypeNameProperty() { |
| 124 | return TYPE_NAME_PROPERTY; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | final int getNodeType0() { |
| 129 | return NORMAL_ANNOTATION; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | ASTNode clone0(AST target) { |
| 134 | NormalAnnotation result = new NormalAnnotation(target); |
| 135 | result.setSourceRange(getStartPosition(), getLength()); |
| 136 | result.setTypeName((Name) ASTNode.copySubtree(target, getTypeName())); |
| 137 | result.values().addAll(ASTNode.copySubtrees(target, values())); |
| 138 | return result; |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
| 143 | // dispatch to correct overloaded match method |
| 144 | return matcher.match(this, other); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | void accept0(ASTVisitor visitor) { |
| 149 | boolean visitChildren = visitor.visit(this); |
| 150 | if (visitChildren) { |
| 151 | // visit children in normal left to right reading order |
| 152 | acceptChild(visitor, getTypeName()); |
| 153 | acceptChildren(visitor, this.values); |
| 154 | } |
| 155 | visitor.endVisit(this); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Returns the live list of member value pairs in this annotation. |
| 160 | * Adding and removing nodes from this list affects this node |
| 161 | * dynamically. All nodes in this list must be |
| 162 | * {@link MemberValuePair}s; attempts to add any other |
| 163 | * type of node will trigger an exception. |
| 164 | * |
| 165 | * @return the live list of member value pairs in this |
| 166 | * annotation (element type: {@link MemberValuePair}) |
| 167 | */ |
| 168 | public List values() { |
| 169 | return this.values; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | int memSize() { |
| 174 | return super.memSize() + 1 * 4; |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | int treeSize() { |
| 179 | return |
| 180 | memSize() |
| 181 | + (this.typeName == null ? 0 : getTypeName().treeSize()) |
| 182 | + this.values.listSize(); |
| 183 | } |
| 184 | } |
| 185 |
Members