| 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 | package org.eclipse.jdt.core.dom; |
| 15 | |
| 16 | import java.util.ArrayList; |
| 17 | import java.util.List; |
| 18 | |
| 19 | /** |
| 20 | * Anonymous class declaration AST node type. For JLS2, this type of node appears |
| 21 | * only as a child on a class instance creation expression. |
| 22 | * For JLS3, this type of node appears may also appear as the child of |
| 23 | * an enum constant declaration. |
| 24 | * |
| 25 | * <pre> |
| 26 | * AnonymousClassDeclaration: |
| 27 | * <b>{</b> ClassBodyDeclaration <b>}</b> |
| 28 | * </pre> |
| 29 | * |
| 30 | * @see ClassInstanceCreation |
| 31 | * @see EnumConstantDeclaration |
| 32 | * @since 2.0 |
| 33 | * @noinstantiate This class is not intended to be instantiated by clients. |
| 34 | */ |
| 35 | @SuppressWarnings({"rawtypes", "unchecked"}) |
| 36 | public class AnonymousClassDeclaration extends ASTNode { |
| 37 | |
| 38 | /** |
| 39 | * The "bodyDeclarations" structural property of this node type (element type: {@link BodyDeclaration}). |
| 40 | * @since 3.0 |
| 41 | */ |
| 42 | public static final ChildListPropertyDescriptor BODY_DECLARATIONS_PROPERTY = |
| 43 | new ChildListPropertyDescriptor(AnonymousClassDeclaration.class, "bodyDeclarations", BodyDeclaration.class, CYCLE_RISK); //$NON-NLS-1$ |
| 44 | |
| 45 | /** |
| 46 | * A list of property descriptors (element type: |
| 47 | * {@link StructuralPropertyDescriptor}), |
| 48 | * or null if uninitialized. |
| 49 | */ |
| 50 | private static final List PROPERTY_DESCRIPTORS; |
| 51 | |
| 52 | static { |
| 53 | List properyList = new ArrayList(2); |
| 54 | createPropertyList(AnonymousClassDeclaration.class, properyList); |
| 55 | addProperty(BODY_DECLARATIONS_PROPERTY, properyList); |
| 56 | PROPERTY_DESCRIPTORS = reapPropertyList(properyList); |
| 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 | |
| 66 | * @return a list of property descriptors (element type: |
| 67 | * {@link StructuralPropertyDescriptor}) |
| 68 | * @since 3.0 |
| 69 | */ |
| 70 | public static List propertyDescriptors(int apiLevel) { |
| 71 | return PROPERTY_DESCRIPTORS; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * The body declarations (element type: {@link BodyDeclaration}). |
| 76 | * Defaults to none. |
| 77 | */ |
| 78 | private ASTNode.NodeList bodyDeclarations = |
| 79 | new ASTNode.NodeList(BODY_DECLARATIONS_PROPERTY); |
| 80 | |
| 81 | /** |
| 82 | * Creates a new AST node for an anonymous class declaration owned |
| 83 | * by the given AST. By default, the list of body declarations is empty. |
| 84 | * <p> |
| 85 | * N.B. This constructor is package-private; all subclasses must be |
| 86 | * declared in the same package; clients are unable to declare |
| 87 | * additional subclasses. |
| 88 | * </p> |
| 89 | * |
| 90 | * @param ast the AST that is to own this node |
| 91 | */ |
| 92 | AnonymousClassDeclaration(AST ast) { |
| 93 | super(ast); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | final List internalStructuralPropertiesForType(int apiLevel) { |
| 98 | return propertyDescriptors(apiLevel); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | final List internalGetChildListProperty(ChildListPropertyDescriptor property) { |
| 103 | if (property == BODY_DECLARATIONS_PROPERTY) { |
| 104 | return bodyDeclarations(); |
| 105 | } |
| 106 | // allow default implementation to flag the error |
| 107 | return super.internalGetChildListProperty(property); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | final int getNodeType0() { |
| 112 | return ANONYMOUS_CLASS_DECLARATION; |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | ASTNode clone0(AST target) { |
| 117 | AnonymousClassDeclaration result = new AnonymousClassDeclaration(target); |
| 118 | result.setSourceRange(getStartPosition(), getLength()); |
| 119 | result.bodyDeclarations().addAll( |
| 120 | ASTNode.copySubtrees(target, bodyDeclarations())); |
| 121 | return result; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
| 126 | // dispatch to correct overloaded match method |
| 127 | return matcher.match(this, other); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | void accept0(ASTVisitor visitor) { |
| 132 | boolean visitChildren = visitor.visit(this); |
| 133 | if (visitChildren) { |
| 134 | // visit children in normal left to right reading order |
| 135 | acceptChildren(visitor, this.bodyDeclarations); |
| 136 | } |
| 137 | visitor.endVisit(this); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Returns the live ordered list of body declarations of this |
| 142 | * anonymous class declaration. |
| 143 | * |
| 144 | * @return the live list of body declarations |
| 145 | * (element type: {@link BodyDeclaration}) |
| 146 | */ |
| 147 | public List bodyDeclarations() { |
| 148 | return this.bodyDeclarations; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Resolves and returns the binding for the anonymous class declared in |
| 153 | * this declaration. |
| 154 | * <p> |
| 155 | * Note that bindings are generally unavailable unless requested when the |
| 156 | * AST is being built. |
| 157 | * </p> |
| 158 | * |
| 159 | * @return the binding, or <code>null</code> if the binding cannot be |
| 160 | * resolved |
| 161 | */ |
| 162 | public ITypeBinding resolveBinding() { |
| 163 | return this.ast.getBindingResolver().resolveType(this); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | int memSize() { |
| 168 | // treat Code as free |
| 169 | return BASE_NODE_SIZE + 4; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | int treeSize() { |
| 174 | return |
| 175 | memSize() |
| 176 | + this.bodyDeclarations.listSize(); |
| 177 | } |
| 178 | } |
| 179 |
Members