KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > dom > TypeLiteral


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
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 JavaDoc;
15 import java.util.List JavaDoc;
16
17 /**
18  * Type literal AST node type.
19  *
20  * <pre>
21  * TypeLiteral:
22  * ( Type | <b>void</b> ) <b>.</b> <b>class</b>
23  * </pre>
24  *
25  * @since 2.0
26  */

27 public class TypeLiteral extends Expression {
28
29     /**
30      * The "type" structural property of this node type.
31      * @since 3.0
32      */

33     public static final ChildPropertyDescriptor TYPE_PROPERTY =
34         new ChildPropertyDescriptor(TypeLiteral.class, "type", Type.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
35

36     /**
37      * A list of property descriptors (element type:
38      * {@link StructuralPropertyDescriptor}),
39      * or null if uninitialized.
40      */

41     private static final List JavaDoc PROPERTY_DESCRIPTORS;
42     
43     static {
44         List JavaDoc propertyList = new ArrayList JavaDoc(2);
45         createPropertyList(TypeLiteral.class, propertyList);
46         addProperty(TYPE_PROPERTY, propertyList);
47         PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
48     }
49
50     /**
51      * Returns a list of structural property descriptors for this node type.
52      * Clients must not modify the result.
53      *
54      * @param apiLevel the API level; one of the
55      * <code>AST.JLS*</code> constants
56      * @return a list of property descriptors (element type:
57      * {@link StructuralPropertyDescriptor})
58      * @since 3.0
59      */

60     public static List JavaDoc propertyDescriptors(int apiLevel) {
61         return PROPERTY_DESCRIPTORS;
62     }
63             
64     /**
65      * The type; lazily initialized; defaults to a unspecified,
66      * legal type.
67      */

68     private Type type = null;
69
70     /**
71      * Creates a new AST node for a type literal owned by the given
72      * AST. By default, the expression has an unspecified (but legal) type.
73      * <p>
74      * N.B. This constructor is package-private.
75      * </p>
76      *
77      * @param ast the AST that is to own this node
78      */

79     TypeLiteral(AST ast) {
80         super(ast);
81     }
82
83     /* (omit javadoc for this method)
84      * Method declared on ASTNode.
85      */

86     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
87         return propertyDescriptors(apiLevel);
88     }
89     
90     /* (omit javadoc for this method)
91      * Method declared on ASTNode.
92      */

93     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
94         if (property == TYPE_PROPERTY) {
95             if (get) {
96                 return getType();
97             } else {
98                 setType((Type) child);
99                 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.setSourceRange(this.getStartPosition(), this.getLength());
119         result.setType((Type) getType().clone(target));
120         return result;
121     }
122
123     /* (omit javadoc for this method)
124      * Method declared on ASTNode.
125      */

126     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
127         // dispatch to correct overloaded match method
128
return matcher.match(this, other);
129     }
130
131     /* (omit javadoc for this method)
132      * Method declared on ASTNode.
133      */

134     void accept0(ASTVisitor visitor) {
135         boolean visitChildren = visitor.visit(this);
136         if (visitChildren) {
137             acceptChild(visitor, getType());
138         }
139         visitor.endVisit(this);
140     }
141     
142     /**
143      * Returns the type in this type literal expression.
144      *
145      * @return the type
146      */

147     public Type getType() {
148         if (this.type == null) {
149             // lazy init must be thread-safe for readers
150
synchronized (this) {
151                 if (this.type == null) {
152                     preLazyInit();
153                     this.type = this.ast.newPrimitiveType(PrimitiveType.INT);
154                     postLazyInit(this.type, TYPE_PROPERTY);
155                 }
156             }
157         }
158         return this.type;
159     }
160
161     /**
162      * Sets the type in this type literal expression to the given type.
163      *
164      * @param type the new type
165      * @exception IllegalArgumentException if:
166      * <ul>
167      * <li>the node belongs to a different AST</li>
168      * <li>the node already has a parent</li>
169      * </ul>
170      */

171     public void setType(Type type) {
172         if (type == null) {
173             throw new IllegalArgumentException JavaDoc();
174         }
175         ASTNode oldChild = this.type;
176         preReplaceChild(oldChild, type, TYPE_PROPERTY);
177         this.type = type;
178         postReplaceChild(oldChild, type, TYPE_PROPERTY);
179     }
180
181     /* (omit javadoc for this method)
182      * Method declared on ASTNode.
183      */

184     int memSize() {
185         // treat Operator as free
186
return BASE_NODE_SIZE + 1 * 4;
187     }
188     
189     /* (omit javadoc for this method)
190      * Method declared on ASTNode.
191      */

192     int treeSize() {
193         return
194             memSize()
195             + (this.type == null ? 0 : getType().treeSize());
196     }
197 }
198
199
Popular Tags