KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2003, 2005 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 node for a wildcard type (added in JLS3 API).
19  * <pre>
20  * WildcardType:
21  * <b>?</b> [ ( <b>extends</b> | <b>super</b>) Type ]
22  * </pre>
23  * <p>
24  * Not all node arrangements will represent legal Java constructs. In particular,
25  * it is nonsense if a wildcard type node appears anywhere other than as an
26  * argument of a <code>ParameterizedType</code> node.
27  * </p>
28  *
29  * @since 3.1
30  */

31 public class WildcardType extends Type {
32     
33     /**
34      * The "bound" structural property of this node type.
35      */

36     public static final ChildPropertyDescriptor BOUND_PROPERTY =
37         new ChildPropertyDescriptor(WildcardType.class, "bound", Type.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
38

39     /**
40      * The "upperBound" structural property of this node type.
41      */

42     public static final SimplePropertyDescriptor UPPER_BOUND_PROPERTY =
43         new SimplePropertyDescriptor(WildcardType.class, "upperBound", boolean.class, MANDATORY); //$NON-NLS-1$
44

45     /**
46      * A list of property descriptors (element type:
47      * {@link StructuralPropertyDescriptor}),
48      * or null if uninitialized.
49      */

50     private static final List JavaDoc PROPERTY_DESCRIPTORS;
51     
52     static {
53         List JavaDoc propertyList = new ArrayList JavaDoc(3);
54         createPropertyList(WildcardType.class, propertyList);
55         addProperty(BOUND_PROPERTY, propertyList);
56         addProperty(UPPER_BOUND_PROPERTY, propertyList);
57         PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
58     }
59
60     /**
61      * Returns a list of structural property descriptors for this node type.
62      * Clients must not modify the result.
63      *
64      * @param apiLevel the API level; one of the
65      * <code>AST.JLS&ast;</code> constants
66
67      * @return a list of property descriptors (element type:
68      * {@link StructuralPropertyDescriptor})
69      */

70     public static List JavaDoc propertyDescriptors(int apiLevel) {
71         return PROPERTY_DESCRIPTORS;
72     }
73             
74     /**
75      * The optional type bound node; <code>null</code> if none;
76      * defaults to none.
77      */

78     private Type optionalBound = null;
79     
80     /**
81      * Indicates whether the wildcard bound is an upper bound
82      * ("extends") as opposed to a lower bound ("super").
83      * Defaults to <code>true</code> initially.
84      */

85     private boolean isUpperBound = true;
86
87     /**
88      * Creates a new unparented node for a wildcard type owned by the
89      * given AST. By default, no upper bound.
90      * <p>
91      * N.B. This constructor is package-private.
92      * </p>
93      *
94      * @param ast the AST that is to own this node
95      */

96     WildcardType(AST ast) {
97         super(ast);
98         unsupportedIn2();
99     }
100
101     /* (omit javadoc for this method)
102      * Method declared on ASTNode.
103      */

104     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
105         return propertyDescriptors(apiLevel);
106     }
107     
108     /* (omit javadoc for this method)
109      * Method declared on ASTNode.
110      */

111     final boolean internalGetSetBooleanProperty(SimplePropertyDescriptor property, boolean get, boolean value) {
112         if (property == UPPER_BOUND_PROPERTY) {
113             if (get) {
114                 return isUpperBound();
115             } else {
116                 setUpperBound(value);
117                 return false;
118             }
119         }
120         // allow default implementation to flag the error
121
return super.internalGetSetBooleanProperty(property, get, value);
122     }
123     
124     /* (omit javadoc for this method)
125      * Method declared on ASTNode.
126      */

127     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
128         if (property == BOUND_PROPERTY) {
129             if (get) {
130                 return getBound();
131             } else {
132                 setBound((Type) child);
133                 return null;
134             }
135         }
136         // allow default implementation to flag the error
137
return super.internalGetSetChildProperty(property, get, child);
138     }
139     
140     /* (omit javadoc for this method)
141      * Method declared on ASTNode.
142      */

143     final int getNodeType0() {
144         return WILDCARD_TYPE;
145     }
146
147     /* (omit javadoc for this method)
148      * Method declared on ASTNode.
149      */

150     ASTNode clone0(AST target) {
151         WildcardType result = new WildcardType(target);
152         result.setSourceRange(this.getStartPosition(), this.getLength());
153         result.setBound((Type) ASTNode.copySubtree(target, getBound()), isUpperBound());
154         return result;
155     }
156
157     /* (omit javadoc for this method)
158      * Method declared on ASTNode.
159      */

160     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
161         // dispatch to correct overloaded match method
162
return matcher.match(this, other);
163     }
164
165     /* (omit javadoc for this method)
166      * Method declared on ASTNode.
167      */

168     void accept0(ASTVisitor visitor) {
169         boolean visitChildren = visitor.visit(this);
170         if (visitChildren) {
171             // visit children in normal left to right reading order
172
acceptChild(visitor, getBound());
173         }
174         visitor.endVisit(this);
175     }
176     
177     /**
178      * Returns whether this wildcard type is an upper bound
179      * ("extends") as opposed to a lower bound ("super").
180      * <p>
181      * Note that this property is irrelevant for wildcards
182      * that do not have a bound.
183      * </p>
184      *
185      * @return <code>true</code> if an upper bound,
186      * and <code>false</code> if a lower bound
187      * @see #setBound(Type)
188      */

189     public boolean isUpperBound() {
190         return this.isUpperBound;
191     }
192     
193     /**
194      * Returns the bound of this wildcard type if it has one.
195      * If {@link #isUpperBound isUpperBound} returns true, this
196      * is an upper bound ("? extends B"); if it returns false, this
197      * is a lower bound ("? super B").
198      *
199      * @return the bound of this wildcard type, or <code>null</code>
200      * if none
201      * @see #setBound(Type)
202      */

203     public Type getBound() {
204         return this.optionalBound;
205     }
206     
207     /**
208      * Sets the bound of this wildcard type to the given type and
209      * marks it as an upper or a lower bound. The method is
210      * equivalent to calling <code>setBound(type); setUpperBound(isUpperBound)</code>.
211      *
212      * @param type the new bound of this wildcard type, or <code>null</code>
213      * if none
214      * @param isUpperBound <code>true</code> for an upper bound ("? extends B"),
215      * and <code>false</code> for a lower bound ("? super B")
216      * @exception IllegalArgumentException if:
217      * <ul>
218      * <li>the node belongs to a different AST</li>
219      * <li>the node already has a parent</li>
220      * </ul>
221      * @see #getBound()
222      * @see #isUpperBound()
223      */

224     public void setBound(Type type, boolean isUpperBound) {
225         setBound(type);
226         setUpperBound(isUpperBound);
227     }
228
229     /**
230      * Sets the bound of this wildcard type to the given type.
231      *
232      * @param type the new bound of this wildcard type, or <code>null</code>
233      * if none
234      * @exception IllegalArgumentException if:
235      * <ul>
236      * <li>the node belongs to a different AST</li>
237      * <li>the node already has a parent</li>
238      * </ul>
239      * @see #getBound()
240      */

241     public void setBound(Type type) {
242         ASTNode oldChild = this.optionalBound;
243         preReplaceChild(oldChild, type, BOUND_PROPERTY);
244         this.optionalBound = type;
245         postReplaceChild(oldChild, type, BOUND_PROPERTY);
246     }
247
248     /**
249      * Sets whether this wildcard type is an upper bound
250      * ("extends") as opposed to a lower bound ("super").
251      *
252      * @param isUpperBound <code>true</code> if an upper bound,
253      * and <code>false</code> if a lower bound
254      * @see #isUpperBound()
255      */

256     public void setUpperBound(boolean isUpperBound) {
257         preValueChange(UPPER_BOUND_PROPERTY);
258         this.isUpperBound = isUpperBound;
259         postValueChange(UPPER_BOUND_PROPERTY);
260     }
261     
262     /* (omit javadoc for this method)
263      * Method declared on ASTNode.
264      */

265     int memSize() {
266         return BASE_NODE_SIZE + 2 * 4;
267     }
268     
269     /* (omit javadoc for this method)
270      * Method declared on ASTNode.
271      */

272     int treeSize() {
273         return
274         memSize()
275         + (this.optionalBound == null ? 0 : getBound().treeSize());
276     }
277 }
278
279
Popular Tags