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: * Type literal AST node type.
017: *
018: * <pre>
019: * TypeLiteral:
020: * ( Type | <b>void</b> ) <b>.</b> <b>class</b>
021: * </pre>
022: *
023: * @since 2.0
024: */
025: public class TypeLiteral extends Expression {
026:
027: /**
028: * The "type" structural property of this node type.
029: * @since 3.0
030: */
031: public static final ChildPropertyDescriptor TYPE_PROPERTY = new ChildPropertyDescriptor(
032: TypeLiteral.class,
033: "type", Type.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
034:
035: /**
036: * A list of property descriptors (element type:
037: * {@link StructuralPropertyDescriptor}),
038: * or null if uninitialized.
039: */
040: private static final List PROPERTY_DESCRIPTORS;
041:
042: static {
043: List propertyList = new ArrayList(2);
044: createPropertyList(TypeLiteral.class, propertyList);
045: addProperty(TYPE_PROPERTY, propertyList);
046: PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
047: }
048:
049: /**
050: * Returns a list of structural property descriptors for this node type.
051: * Clients must not modify the result.
052: *
053: * @param apiLevel the API level; one of the
054: * <code>AST.JLS*</code> constants
055: * @return a list of property descriptors (element type:
056: * {@link StructuralPropertyDescriptor})
057: * @since 3.0
058: */
059: public static List propertyDescriptors(int apiLevel) {
060: return PROPERTY_DESCRIPTORS;
061: }
062:
063: /**
064: * The type; lazily initialized; defaults to a unspecified,
065: * legal type.
066: */
067: private Type type = null;
068:
069: /**
070: * Creates a new AST node for a type literal owned by the given
071: * AST. By default, the expression has an unspecified (but legal) type.
072: * <p>
073: * N.B. This constructor is package-private.
074: * </p>
075: *
076: * @param ast the AST that is to own this node
077: */
078: TypeLiteral(AST ast) {
079: super (ast);
080: }
081:
082: /* (omit javadoc for this method)
083: * Method declared on ASTNode.
084: */
085: final List internalStructuralPropertiesForType(int apiLevel) {
086: return propertyDescriptors(apiLevel);
087: }
088:
089: /* (omit javadoc for this method)
090: * Method declared on ASTNode.
091: */
092: final ASTNode internalGetSetChildProperty(
093: ChildPropertyDescriptor property, boolean get, ASTNode child) {
094: if (property == TYPE_PROPERTY) {
095: if (get) {
096: return getType();
097: } else {
098: setType((Type) child);
099: return null;
100: }
101: }
102: // allow default implementation to flag the error
103: return super .internalGetSetChildProperty(property, get, child);
104: }
105:
106: /* (omit javadoc for this method)
107: * Method declared on ASTNode.
108: */
109: final int getNodeType0() {
110: return TYPE_LITERAL;
111: }
112:
113: /* (omit javadoc for this method)
114: * Method declared on ASTNode.
115: */
116: ASTNode clone0(AST target) {
117: TypeLiteral result = new TypeLiteral(target);
118: result
119: .setSourceRange(this .getStartPosition(), this
120: .getLength());
121: result.setType((Type) getType().clone(target));
122: return result;
123: }
124:
125: /* (omit javadoc for this method)
126: * Method declared on ASTNode.
127: */
128: final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
129: // dispatch to correct overloaded match method
130: return matcher.match(this , other);
131: }
132:
133: /* (omit javadoc for this method)
134: * Method declared on ASTNode.
135: */
136: void accept0(ASTVisitor visitor) {
137: boolean visitChildren = visitor.visit(this );
138: if (visitChildren) {
139: acceptChild(visitor, getType());
140: }
141: visitor.endVisit(this );
142: }
143:
144: /**
145: * Returns the type in this type literal expression.
146: *
147: * @return the type
148: */
149: public Type getType() {
150: if (this .type == null) {
151: // lazy init must be thread-safe for readers
152: synchronized (this ) {
153: if (this .type == null) {
154: preLazyInit();
155: this .type = this .ast
156: .newPrimitiveType(PrimitiveType.INT);
157: postLazyInit(this .type, TYPE_PROPERTY);
158: }
159: }
160: }
161: return this .type;
162: }
163:
164: /**
165: * Sets the type in this type literal expression to the given type.
166: *
167: * @param type the new type
168: * @exception IllegalArgumentException if:
169: * <ul>
170: * <li>the node belongs to a different AST</li>
171: * <li>the node already has a parent</li>
172: * </ul>
173: */
174: public void setType(Type type) {
175: if (type == null) {
176: throw new IllegalArgumentException();
177: }
178: ASTNode oldChild = this .type;
179: preReplaceChild(oldChild, type, TYPE_PROPERTY);
180: this .type = type;
181: postReplaceChild(oldChild, type, TYPE_PROPERTY);
182: }
183:
184: /* (omit javadoc for this method)
185: * Method declared on ASTNode.
186: */
187: int memSize() {
188: // treat Operator as free
189: return BASE_NODE_SIZE + 1 * 4;
190: }
191:
192: /* (omit javadoc for this method)
193: * Method declared on ASTNode.
194: */
195: int treeSize() {
196: return memSize()
197: + (this .type == null ? 0 : getType().treeSize());
198: }
199: }
|