Stephan Herrmann | 7b7062f | 2010-04-01 19:56:59 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
Stephan Herrmann | 66cc2b6 | 2010-09-26 15:38:59 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2000, 2010 IBM Corporation and others. |
Stephan Herrmann | 7b7062f | 2010-04-01 19:56:59 +0000 | [diff] [blame] | 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | *******************************************************************************/ |
| 11 | |
| 12 | package org.eclipse.jdt.core.dom; |
| 13 | |
| 14 | import java.util.ArrayList; |
| 15 | import java.util.List; |
| 16 | |
| 17 | /** |
| 18 | * Array initializer AST node type. |
| 19 | * |
| 20 | * <pre> |
| 21 | * ArrayInitializer: |
| 22 | * <b>{</b> [ Expression { <b>,</b> Expression} [ <b>,</b> ]] <b>}</b> |
| 23 | * </pre> |
Stephan Herrmann | ab1b976 | 2010-09-26 14:31:00 +0000 | [diff] [blame] | 24 | * |
Stephan Herrmann | 7b7062f | 2010-04-01 19:56:59 +0000 | [diff] [blame] | 25 | * @since 2.0 |
| 26 | * @noinstantiate This class is not intended to be instantiated by clients. |
| 27 | */ |
| 28 | public class ArrayInitializer extends Expression { |
Stephan Herrmann | ab1b976 | 2010-09-26 14:31:00 +0000 | [diff] [blame] | 29 | |
Stephan Herrmann | 7b7062f | 2010-04-01 19:56:59 +0000 | [diff] [blame] | 30 | /** |
Stephan Herrmann | 66cc2b6 | 2010-09-26 15:38:59 +0000 | [diff] [blame^] | 31 | * The "expressions" structural property of this node type (element type: {@link Expression}). |
Stephan Herrmann | 7b7062f | 2010-04-01 19:56:59 +0000 | [diff] [blame] | 32 | * @since 3.0 |
| 33 | */ |
Stephan Herrmann | ab1b976 | 2010-09-26 14:31:00 +0000 | [diff] [blame] | 34 | public static final ChildListPropertyDescriptor EXPRESSIONS_PROPERTY = |
Stephan Herrmann | 7b7062f | 2010-04-01 19:56:59 +0000 | [diff] [blame] | 35 | new ChildListPropertyDescriptor(ArrayInitializer.class, "expressions", Expression.class, CYCLE_RISK); //$NON-NLS-1$ |
| 36 | |
| 37 | /** |
Stephan Herrmann | ab1b976 | 2010-09-26 14:31:00 +0000 | [diff] [blame] | 38 | * A list of property descriptors (element type: |
Stephan Herrmann | 7b7062f | 2010-04-01 19:56:59 +0000 | [diff] [blame] | 39 | * {@link StructuralPropertyDescriptor}), |
| 40 | * or null if uninitialized. |
| 41 | */ |
| 42 | private static final List PROPERTY_DESCRIPTORS; |
Stephan Herrmann | ab1b976 | 2010-09-26 14:31:00 +0000 | [diff] [blame] | 43 | |
Stephan Herrmann | 7b7062f | 2010-04-01 19:56:59 +0000 | [diff] [blame] | 44 | static { |
| 45 | List properyList = new ArrayList(2); |
| 46 | createPropertyList(ArrayInitializer.class, properyList); |
| 47 | addProperty(EXPRESSIONS_PROPERTY, properyList); |
| 48 | PROPERTY_DESCRIPTORS = reapPropertyList(properyList); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Returns a list of structural property descriptors for this node type. |
| 53 | * Clients must not modify the result. |
Stephan Herrmann | ab1b976 | 2010-09-26 14:31:00 +0000 | [diff] [blame] | 54 | * |
Stephan Herrmann | 7b7062f | 2010-04-01 19:56:59 +0000 | [diff] [blame] | 55 | * @param apiLevel the API level; one of the |
| 56 | * <code>AST.JLS*</code> constants |
| 57 | |
Stephan Herrmann | ab1b976 | 2010-09-26 14:31:00 +0000 | [diff] [blame] | 58 | * @return a list of property descriptors (element type: |
Stephan Herrmann | 7b7062f | 2010-04-01 19:56:59 +0000 | [diff] [blame] | 59 | * {@link StructuralPropertyDescriptor}) |
| 60 | * @since 3.0 |
| 61 | */ |
| 62 | public static List propertyDescriptors(int apiLevel) { |
| 63 | return PROPERTY_DESCRIPTORS; |
| 64 | } |
Stephan Herrmann | ab1b976 | 2010-09-26 14:31:00 +0000 | [diff] [blame] | 65 | |
Stephan Herrmann | 7b7062f | 2010-04-01 19:56:59 +0000 | [diff] [blame] | 66 | /** |
| 67 | * The list of expressions (element type: |
Stephan Herrmann | 66cc2b6 | 2010-09-26 15:38:59 +0000 | [diff] [blame^] | 68 | * {@link Expression}). Defaults to an empty list. |
Stephan Herrmann | 7b7062f | 2010-04-01 19:56:59 +0000 | [diff] [blame] | 69 | */ |
| 70 | private ASTNode.NodeList expressions = |
| 71 | new ASTNode.NodeList(EXPRESSIONS_PROPERTY); |
| 72 | |
| 73 | /** |
Stephan Herrmann | ab1b976 | 2010-09-26 14:31:00 +0000 | [diff] [blame] | 74 | * Creates a new AST node for an array initializer owned by the |
Stephan Herrmann | 7b7062f | 2010-04-01 19:56:59 +0000 | [diff] [blame] | 75 | * given AST. By default, the list of expressions is empty. |
Stephan Herrmann | ab1b976 | 2010-09-26 14:31:00 +0000 | [diff] [blame] | 76 | * |
Stephan Herrmann | 7b7062f | 2010-04-01 19:56:59 +0000 | [diff] [blame] | 77 | * @param ast the AST that is to own this node |
| 78 | */ |
| 79 | ArrayInitializer(AST ast) { |
Stephan Herrmann | ab1b976 | 2010-09-26 14:31:00 +0000 | [diff] [blame] | 80 | super(ast); |
Stephan Herrmann | 7b7062f | 2010-04-01 19:56:59 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /* (omit javadoc for this method) |
| 84 | * Method declared on ASTNode. |
| 85 | */ |
| 86 | final List internalStructuralPropertiesForType(int apiLevel) { |
| 87 | return propertyDescriptors(apiLevel); |
| 88 | } |
Stephan Herrmann | ab1b976 | 2010-09-26 14:31:00 +0000 | [diff] [blame] | 89 | |
Stephan Herrmann | 7b7062f | 2010-04-01 19:56:59 +0000 | [diff] [blame] | 90 | /* (omit javadoc for this method) |
| 91 | * Method declared on ASTNode. |
| 92 | */ |
| 93 | final List internalGetChildListProperty(ChildListPropertyDescriptor property) { |
| 94 | if (property == EXPRESSIONS_PROPERTY) { |
| 95 | return expressions(); |
| 96 | } |
| 97 | // allow default implementation to flag the error |
| 98 | return super.internalGetChildListProperty(property); |
| 99 | } |
| 100 | |
| 101 | /* (omit javadoc for this method) |
| 102 | * Method declared on ASTNode. |
| 103 | */ |
| 104 | final int getNodeType0() { |
| 105 | return ARRAY_INITIALIZER; |
| 106 | } |
| 107 | |
| 108 | /* (omit javadoc for this method) |
| 109 | * Method declared on ASTNode. |
| 110 | */ |
| 111 | ASTNode clone0(AST target) { |
| 112 | ArrayInitializer result = new ArrayInitializer(target); |
| 113 | result.setSourceRange(getStartPosition(), getLength()); |
| 114 | result.expressions().addAll(ASTNode.copySubtrees(target, expressions())); |
| 115 | return result; |
| 116 | } |
| 117 | |
| 118 | /* (omit javadoc for this method) |
| 119 | * Method declared on ASTNode. |
| 120 | */ |
| 121 | final boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
| 122 | // dispatch to correct overloaded match method |
| 123 | return matcher.match(this, other); |
| 124 | } |
| 125 | |
| 126 | /* (omit javadoc for this method) |
| 127 | * Method declared on ASTNode. |
| 128 | */ |
| 129 | void accept0(ASTVisitor visitor) { |
| 130 | boolean visitChildren = visitor.visit(this); |
| 131 | if (visitChildren) { |
| 132 | acceptChildren(visitor, this.expressions); |
| 133 | } |
| 134 | visitor.endVisit(this); |
| 135 | } |
Stephan Herrmann | ab1b976 | 2010-09-26 14:31:00 +0000 | [diff] [blame] | 136 | |
Stephan Herrmann | 7b7062f | 2010-04-01 19:56:59 +0000 | [diff] [blame] | 137 | /** |
| 138 | * Returns the live ordered list of expressions in this array initializer. |
Stephan Herrmann | ab1b976 | 2010-09-26 14:31:00 +0000 | [diff] [blame] | 139 | * |
| 140 | * @return the live list of expressions |
Stephan Herrmann | 66cc2b6 | 2010-09-26 15:38:59 +0000 | [diff] [blame^] | 141 | * (element type: {@link Expression}) |
Stephan Herrmann | ab1b976 | 2010-09-26 14:31:00 +0000 | [diff] [blame] | 142 | */ |
Stephan Herrmann | 7b7062f | 2010-04-01 19:56:59 +0000 | [diff] [blame] | 143 | public List expressions() { |
| 144 | return this.expressions; |
| 145 | } |
Stephan Herrmann | ab1b976 | 2010-09-26 14:31:00 +0000 | [diff] [blame] | 146 | |
Stephan Herrmann | 7b7062f | 2010-04-01 19:56:59 +0000 | [diff] [blame] | 147 | /* (omit javadoc for this method) |
| 148 | * Method declared on ASTNode. |
| 149 | */ |
| 150 | int memSize() { |
| 151 | return BASE_NODE_SIZE + 1 * 4; |
| 152 | } |
Stephan Herrmann | ab1b976 | 2010-09-26 14:31:00 +0000 | [diff] [blame] | 153 | |
Stephan Herrmann | 7b7062f | 2010-04-01 19:56:59 +0000 | [diff] [blame] | 154 | /* (omit javadoc for this method) |
| 155 | * Method declared on ASTNode. |
| 156 | */ |
| 157 | int treeSize() { |
| 158 | return memSize() + this.expressions.listSize(); |
| 159 | } |
| 160 | } |
| 161 | |