001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.core.dom;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: /**
016: * Anonymous class declaration AST node type. For JLS2, this type of node appears
017: * only as a child on a class instance creation expression.
018: * For JLS3, this type of node appears may also appear as the child of
019: * an enum constant declaration.
020: *
021: * <pre>
022: * AnonymousClassDeclaration:
023: * <b>{</b> ClassBodyDeclaration <b>}</b>
024: * </pre>
025: *
026: * @see ClassInstanceCreation
027: * @see EnumConstantDeclaration
028: * @since 2.0
029: */
030: public class AnonymousClassDeclaration extends ASTNode {
031:
032: /**
033: * The "bodyDeclarations" structural property of this node type.
034: * @since 3.0
035: */
036: public static final ChildListPropertyDescriptor BODY_DECLARATIONS_PROPERTY = new ChildListPropertyDescriptor(
037: AnonymousClassDeclaration.class,
038: "bodyDeclarations", BodyDeclaration.class, CYCLE_RISK); //$NON-NLS-1$
039:
040: /**
041: * A list of property descriptors (element type:
042: * {@link StructuralPropertyDescriptor}),
043: * or null if uninitialized.
044: */
045: private static final List PROPERTY_DESCRIPTORS;
046:
047: static {
048: List properyList = new ArrayList(2);
049: createPropertyList(AnonymousClassDeclaration.class, properyList);
050: addProperty(BODY_DECLARATIONS_PROPERTY, properyList);
051: PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
052: }
053:
054: /**
055: * Returns a list of structural property descriptors for this node type.
056: * Clients must not modify the result.
057: *
058: * @param apiLevel the API level; one of the
059: * <code>AST.JLS*</code> constants
060:
061: * @return a list of property descriptors (element type:
062: * {@link StructuralPropertyDescriptor})
063: * @since 3.0
064: */
065: public static List propertyDescriptors(int apiLevel) {
066: return PROPERTY_DESCRIPTORS;
067: }
068:
069: /**
070: * The body declarations (element type: <code>BodyDeclaration</code>).
071: * Defaults to none.
072: */
073: private ASTNode.NodeList bodyDeclarations = new ASTNode.NodeList(
074: BODY_DECLARATIONS_PROPERTY);
075:
076: /**
077: * Creates a new AST node for an anonymous class declaration owned
078: * by the given AST. By default, the list of body declarations is empty.
079: * <p>
080: * N.B. This constructor is package-private; all subclasses must be
081: * declared in the same package; clients are unable to declare
082: * additional subclasses.
083: * </p>
084: *
085: * @param ast the AST that is to own this node
086: */
087: AnonymousClassDeclaration(AST ast) {
088: super (ast);
089: }
090:
091: /* (omit javadoc for this method)
092: * Method declared on ASTNode.
093: */
094: final List internalStructuralPropertiesForType(int apiLevel) {
095: return propertyDescriptors(apiLevel);
096: }
097:
098: /* (omit javadoc for this method)
099: * Method declared on ASTNode.
100: */
101: final List internalGetChildListProperty(
102: 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: /* (omit javadoc for this method)
111: * Method declared on ASTNode.
112: */
113: final int getNodeType0() {
114: return ANONYMOUS_CLASS_DECLARATION;
115: }
116:
117: /* (omit javadoc for this method)
118: * Method declared on ASTNode.
119: */
120: ASTNode clone0(AST target) {
121: AnonymousClassDeclaration result = new AnonymousClassDeclaration(
122: target);
123: result
124: .setSourceRange(this .getStartPosition(), this
125: .getLength());
126: result.bodyDeclarations().addAll(
127: ASTNode.copySubtrees(target, bodyDeclarations()));
128: return result;
129: }
130:
131: /* (omit javadoc for this method)
132: * Method declared on ASTNode.
133: */
134: final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
135: // dispatch to correct overloaded match method
136: return matcher.match(this , other);
137: }
138:
139: /* (omit javadoc for this method)
140: * Method declared on ASTNode.
141: */
142: void accept0(ASTVisitor visitor) {
143: boolean visitChildren = visitor.visit(this );
144: if (visitChildren) {
145: // visit children in normal left to right reading order
146: acceptChildren(visitor, bodyDeclarations);
147: }
148: visitor.endVisit(this );
149: }
150:
151: /**
152: * Returns the live ordered list of body declarations of this
153: * anonymous class declaration.
154: *
155: * @return the live list of body declarations
156: * (element type: <code>BodyDeclaration</code>)
157: */
158: public List bodyDeclarations() {
159: return this .bodyDeclarations;
160: }
161:
162: /**
163: * Resolves and returns the binding for the anonymous class declared in
164: * this declaration.
165: * <p>
166: * Note that bindings are generally unavailable unless requested when the
167: * AST is being built.
168: * </p>
169: *
170: * @return the binding, or <code>null</code> if the binding cannot be
171: * resolved
172: */
173: public ITypeBinding resolveBinding() {
174: return this .ast.getBindingResolver().resolveType(this );
175: }
176:
177: /* (omit javadoc for this method)
178: * Method declared on ASTNode.
179: */
180: int memSize() {
181: // treat Code as free
182: return BASE_NODE_SIZE + 4;
183: }
184:
185: /* (omit javadoc for this method)
186: * Method declared on ASTNode.
187: */
188: int treeSize() {
189: return memSize() + this.bodyDeclarations.listSize();
190: }
191: }
|