1 /*******************************************************************************
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.core.dom;
14 import java.util.ArrayList;
15 import java.util.List;
18 * Type literal AST node type.
22 * ( Type | <b>void</b> ) <b>.</b> <b>class</b>
26 * @noinstantiate This class is not intended to be instantiated by clients.
28 public class TypeLiteral extends Expression {
31 * The "type" structural property of this node type.
34 public static final ChildPropertyDescriptor TYPE_PROPERTY =
35 new ChildPropertyDescriptor(TypeLiteral.class, "type", Type.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
38 * A list of property descriptors (element type:
39 * {@link StructuralPropertyDescriptor}),
40 * or null if uninitialized.
42 private static final List PROPERTY_DESCRIPTORS;
45 List propertyList = new ArrayList(2);
46 createPropertyList(TypeLiteral.class, propertyList);
47 addProperty(TYPE_PROPERTY, propertyList);
48 PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
52 * Returns a list of structural property descriptors for this node type.
53 * Clients must not modify the result.
55 * @param apiLevel the API level; one of the
56 * <code>AST.JLS*</code> constants
57 * @return a list of property descriptors (element type:
58 * {@link StructuralPropertyDescriptor})
61 public static List propertyDescriptors(int apiLevel) {
62 return PROPERTY_DESCRIPTORS;
66 * The type; lazily initialized; defaults to a unspecified,
69 private Type type = null;
72 * Creates a new AST node for a type literal owned by the given
73 * AST. By default, the expression has an unspecified (but legal) type.
75 * N.B. This constructor is package-private.
78 * @param ast the AST that is to own this node
80 TypeLiteral(AST ast) {
84 /* (omit javadoc for this method)
85 * Method declared on ASTNode.
87 final List internalStructuralPropertiesForType(int apiLevel) {
88 return propertyDescriptors(apiLevel);
91 /* (omit javadoc for this method)
92 * Method declared on ASTNode.
94 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
95 if (property == TYPE_PROPERTY) {
99 setType((Type) child);
103 // allow default implementation to flag the error
104 return super.internalGetSetChildProperty(property, get, child);
107 /* (omit javadoc for this method)
108 * Method declared on ASTNode.
110 final int getNodeType0() {
114 /* (omit javadoc for this method)
115 * Method declared on ASTNode.
117 ASTNode clone0(AST target) {
118 TypeLiteral result = new TypeLiteral(target);
119 result.setSourceRange(this.getStartPosition(), this.getLength());
120 result.setType((Type) getType().clone(target));
124 /* (omit javadoc for this method)
125 * Method declared on ASTNode.
127 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
128 // dispatch to correct overloaded match method
129 return matcher.match(this, other);
132 /* (omit javadoc for this method)
133 * Method declared on ASTNode.
135 void accept0(ASTVisitor visitor) {
136 boolean visitChildren = visitor.visit(this);
138 acceptChild(visitor, getType());
140 visitor.endVisit(this);
144 * Returns the type in this type literal expression.
148 public Type getType() {
149 if (this.type == null) {
150 // lazy init must be thread-safe for readers
151 synchronized (this) {
152 if (this.type == null) {
154 this.type = this.ast.newPrimitiveType(PrimitiveType.INT);
155 postLazyInit(this.type, TYPE_PROPERTY);
163 * Sets the type in this type literal expression to the given type.
165 * @param type the new type
166 * @exception IllegalArgumentException if:
168 * <li>the node belongs to a different AST</li>
169 * <li>the node already has a parent</li>
172 public void setType(Type type) {
174 throw new IllegalArgumentException();
176 ASTNode oldChild = this.type;
177 preReplaceChild(oldChild, type, TYPE_PROPERTY);
179 postReplaceChild(oldChild, type, TYPE_PROPERTY);
182 /* (omit javadoc for this method)
183 * Method declared on ASTNode.
186 // treat Operator as free
187 return BASE_NODE_SIZE + 1 * 4;
190 /* (omit javadoc for this method)
191 * Method declared on ASTNode.
196 + (this.type == null ? 0 : getType().treeSize());