Lines 11-16
Link Here
|
11 |
|
11 |
|
12 |
package org.eclipse.jdt.core.dom; |
12 |
package org.eclipse.jdt.core.dom; |
13 |
|
13 |
|
|
|
14 |
import java.util.AbstractList; |
14 |
import java.util.Iterator; |
15 |
import java.util.Iterator; |
15 |
import java.util.List; |
16 |
import java.util.List; |
16 |
|
17 |
|
Lines 24-34
Link Here
|
24 |
* InterfaceDeclaration |
25 |
* InterfaceDeclaration |
25 |
* ClassDeclaration: |
26 |
* ClassDeclaration: |
26 |
* [ Javadoc ] { Modifier } <b>class</b> Identifier |
27 |
* [ Javadoc ] { Modifier } <b>class</b> Identifier |
27 |
* [ <b>extends</b> Type] |
28 |
* [ <b><</b> TypeParameter { <b>,</b> TypeParameter } <b>></b> ] |
|
|
29 |
* [ <b>extends</b> Type ] |
28 |
* [ <b>implements</b> Type { <b>,</b> Type } ] |
30 |
* [ <b>implements</b> Type { <b>,</b> Type } ] |
29 |
* <b>{</b> { ClassBodyDeclaration | <b>;</b> } <b>}</b> |
31 |
* <b>{</b> { ClassBodyDeclaration | <b>;</b> } <b>}</b> |
30 |
* InterfaceDeclaration: |
32 |
* InterfaceDeclaration: |
31 |
* [ Javadoc ] { Modifier } <b>interface</b> Identifier |
33 |
* [ Javadoc ] { Modifier } <b>interface</b> Identifier |
|
|
34 |
* [ <b><</b> TypeParameter { <b>,</b> TypeParameter } <b>></b> ] |
32 |
* [ <b>extends</b> Type { <b>,</b> Type } ] |
35 |
* [ <b>extends</b> Type { <b>,</b> Type } ] |
33 |
* <b>{</b> { InterfaceBodyDeclaration | <b>;</b> } <b>}</b> |
36 |
* <b>{</b> { InterfaceBodyDeclaration | <b>;</b> } <b>}</b> |
34 |
* </pre> |
37 |
* </pre> |
Lines 42-47
Link Here
|
42 |
* no body), or the last character of the "}" token following the body |
45 |
* no body), or the last character of the "}" token following the body |
43 |
* declarations. |
46 |
* declarations. |
44 |
* </p> |
47 |
* </p> |
|
|
48 |
* <p> |
49 |
* Note: Support for generic types is an experimental language feature |
50 |
* under discussion in JSR-014 and under consideration for inclusion |
51 |
* in the 1.5 release of J2SE. The support here is therefore tentative |
52 |
* and subject to change. |
53 |
* </p> |
45 |
* |
54 |
* |
46 |
* @since 2.0 |
55 |
* @since 2.0 |
47 |
*/ |
56 |
*/ |
Lines 74-91
Link Here
|
74 |
private SimpleName typeName = null; |
83 |
private SimpleName typeName = null; |
75 |
|
84 |
|
76 |
/** |
85 |
/** |
77 |
* The optional superclass name; <code>null</code> if none. |
86 |
* The type paramters (element type: <code>TypeParameter</code>). |
|
|
87 |
* Defaults to an empty list. |
88 |
* @since 2.2 |
89 |
*/ |
90 |
private ASTNode.NodeList typeParameters = |
91 |
new ASTNode.NodeList(false, TypeParameter.class); |
92 |
|
93 |
/** |
94 |
* The optional superclass type; <code>null</code> if none. |
78 |
* Defaults to none. Note that this field is not used for |
95 |
* Defaults to none. Note that this field is not used for |
79 |
* interface declarations. |
96 |
* interface declarations. |
|
|
97 |
* @since 2.2 |
80 |
*/ |
98 |
*/ |
81 |
private Name optionalSuperclassName = null; |
99 |
private Type optionalSuperclassType = null; |
82 |
|
100 |
|
83 |
/** |
101 |
/** |
84 |
* The superinterface names (element type: <code>Name</code>). |
102 |
* The superinterface types (element type: <code>Type</code>). |
85 |
* Defaults to an empty list. |
103 |
* Defaults to an empty list. |
|
|
104 |
* @since 2.2 |
86 |
*/ |
105 |
*/ |
87 |
private ASTNode.NodeList superInterfaceNames = |
106 |
private ASTNode.NodeList superInterfaceTypes = |
88 |
new ASTNode.NodeList(false, Name.class); |
107 |
new ASTNode.NodeList(false, Type.class); |
89 |
|
108 |
|
90 |
/** |
109 |
/** |
91 |
* The body declarations (element type: <code>BodyDeclaration</code>). |
110 |
* The body declarations (element type: <code>BodyDeclaration</code>). |
Lines 98-105
Link Here
|
98 |
* Creates a new AST node for a type declaration owned by the given |
117 |
* Creates a new AST node for a type declaration owned by the given |
99 |
* AST. By default, the type declaration is for a class of an |
118 |
* AST. By default, the type declaration is for a class of an |
100 |
* unspecified, but legal, name; no modifiers; no javadoc; |
119 |
* unspecified, but legal, name; no modifiers; no javadoc; |
101 |
* no superclass or superinterfaces; and an empty list of body |
120 |
* no type parameters; no superclass or superinterfaces; and an empty list |
102 |
* declarations. |
121 |
* of body declarations. |
103 |
* <p> |
122 |
* <p> |
104 |
* N.B. This constructor is package-private; all subclasses must be |
123 |
* N.B. This constructor is package-private; all subclasses must be |
105 |
* declared in the same package; clients are unable to declare |
124 |
* declared in the same package; clients are unable to declare |
Lines 130-139
Link Here
|
130 |
(Javadoc) ASTNode.copySubtree(target,(ASTNode) getJavadoc())); |
149 |
(Javadoc) ASTNode.copySubtree(target,(ASTNode) getJavadoc())); |
131 |
result.setInterface(isInterface()); |
150 |
result.setInterface(isInterface()); |
132 |
result.setName((SimpleName) getName().clone(target)); |
151 |
result.setName((SimpleName) getName().clone(target)); |
133 |
result.setSuperclass( |
152 |
result.typeParameters().addAll( |
134 |
(Name) ASTNode.copySubtree(target,(ASTNode) getSuperclass())); |
153 |
ASTNode.copySubtrees(target, typeParameters())); |
135 |
result.superInterfaces().addAll( |
154 |
result.setSuperclassType( |
136 |
ASTNode.copySubtrees(target, superInterfaces())); |
155 |
(Type) ASTNode.copySubtree(target,(ASTNode) getSuperclassType())); |
|
|
156 |
result.superInterfaceTypes().addAll( |
157 |
ASTNode.copySubtrees(target, superInterfaceTypes())); |
137 |
result.bodyDeclarations().addAll( |
158 |
result.bodyDeclarations().addAll( |
138 |
ASTNode.copySubtrees(target, bodyDeclarations())); |
159 |
ASTNode.copySubtrees(target, bodyDeclarations())); |
139 |
return result; |
160 |
return result; |
Lines 156-163
Link Here
|
156 |
// visit children in normal left to right reading order |
177 |
// visit children in normal left to right reading order |
157 |
acceptChild(visitor, getJavadoc()); |
178 |
acceptChild(visitor, getJavadoc()); |
158 |
acceptChild(visitor, getName()); |
179 |
acceptChild(visitor, getName()); |
159 |
acceptChild(visitor, getSuperclass()); |
180 |
acceptChildren(visitor, typeParameters); |
160 |
acceptChildren(visitor, superInterfaceNames); |
181 |
acceptChild(visitor, getSuperclassType()); |
|
|
182 |
acceptChildren(visitor, superInterfaceTypes); |
161 |
acceptChildren(visitor, bodyDeclarations); |
183 |
acceptChildren(visitor, bodyDeclarations); |
162 |
} |
184 |
} |
163 |
visitor.endVisit(this); |
185 |
visitor.endVisit(this); |
Lines 257-267
Link Here
|
257 |
this.typeName = typeName; |
279 |
this.typeName = typeName; |
258 |
} |
280 |
} |
259 |
|
281 |
|
260 |
// JSR-014 feature |
282 |
/** |
261 |
// public List<TypeParameter> typeParameters() { |
283 |
* Returns the live ordered list of type parameters of this type |
262 |
// throw RuntimeException("not implemented yet"); |
284 |
* declaration. This list is non-empty for parameterized types. |
263 |
// } |
285 |
* <p> |
264 |
|
286 |
* Note: Support for generic types is an experimental language feature |
|
|
287 |
* under discussion in JSR-014 and under consideration for inclusion |
288 |
* in the 1.5 release of J2SE. The support here is therefore tentative |
289 |
* and subject to change. |
290 |
* </p> |
291 |
* |
292 |
* @return the live list of type parameters |
293 |
* (element type: <code>TypeParameter</code>) |
294 |
* @since 2.2 |
295 |
*/ |
296 |
public List typeParameters() { |
297 |
return typeParameters; |
298 |
} |
299 |
|
265 |
/** |
300 |
/** |
266 |
* Returns the name of the superclass declared in this type |
301 |
* Returns the name of the superclass declared in this type |
267 |
* declaration, or <code>null</code> if there is none. |
302 |
* declaration, or <code>null</code> if there is none. |
Lines 272-280
Link Here
|
272 |
* |
307 |
* |
273 |
* @return the superclass name node, or <code>null</code> if |
308 |
* @return the superclass name node, or <code>null</code> if |
274 |
* there is none |
309 |
* there is none |
|
|
310 |
* @deprecated Replaced by <code>getSuperclassType</code>, which returns |
311 |
* a <code>Type</code> instead of a <code>Name</code>. |
275 |
*/ |
312 |
*/ |
276 |
public Name getSuperclass() { |
313 |
public Name getSuperclass() { |
277 |
return optionalSuperclassName; |
314 |
// implement deprecated method in terms of get/setSuperclassType |
|
|
315 |
Type superclassType = getSuperclassType(); |
316 |
if (superclassType == null) { |
317 |
// return null if no superclass type |
318 |
return null; |
319 |
} else if (superclassType instanceof SimpleType) { |
320 |
// no problem - extract name from SimpleType |
321 |
SimpleType t = (SimpleType) superclassType; |
322 |
return t.getName(); |
323 |
} else if ((superclassType instanceof ParameterizedType) |
324 |
|| (superclassType instanceof QualifiedType)) { |
325 |
// compatibility issue |
326 |
// back-level clients know nothing of new node types added in 2.1 |
327 |
// take this opportunity to inform client of problem |
328 |
throw new RuntimeException("Deprecated AST API method cannot handle newer node types"); //$NON-NLS-1$ |
329 |
} else { |
330 |
// compatibility issue |
331 |
// AST is bogus - illegal for type to be array or primitive type |
332 |
// take this opportunity to inform client of problem |
333 |
throw new RuntimeException("Deprecated AST API method cannot handle malformed AST"); //$NON-NLS-1$ |
334 |
} |
278 |
} |
335 |
} |
279 |
|
336 |
|
280 |
/** |
337 |
/** |
Lines 292-303
Link Here
|
292 |
* <li>the node belongs to a different AST</li> |
349 |
* <li>the node belongs to a different AST</li> |
293 |
* <li>the node already has a parent</li> |
350 |
* <li>the node already has a parent</li> |
294 |
* </ul> |
351 |
* </ul> |
|
|
352 |
* @deprecated Replaced by <code>setSuperclassType</code>, which expects |
353 |
* a <code>Type</code> instead of a <code>Name</code>. |
295 |
*/ |
354 |
*/ |
296 |
public void setSuperclass(Name superclassName) { |
355 |
public void setSuperclass(Name superclassName) { |
297 |
replaceChild( |
356 |
// implement deprecated method in terms of get/setSuperclassType |
298 |
(ASTNode) this.optionalSuperclassName, |
357 |
if (superclassName == null) { |
299 |
(ASTNode) superclassName, false); |
358 |
setSuperclassType(null); |
300 |
this.optionalSuperclassName = superclassName; |
359 |
} else { |
|
|
360 |
Type superclassType = getSuperclassType(); |
361 |
if (superclassType instanceof SimpleType) { |
362 |
// if possible edit name in SimpleType |
363 |
SimpleType s = (SimpleType) superclassType; |
364 |
s.setName(superclassName); |
365 |
// give type node same range as name node |
366 |
s.setSourceRange( |
367 |
superclassName.getStartPosition(), |
368 |
superclassName.getLength()); |
369 |
// note that only s will be modified(), not the TypeDecl node |
370 |
} else { |
371 |
// all other cases - wrap name in a SimpleType and replace superclassType |
372 |
Type newT = getAST().newSimpleType(superclassName); |
373 |
// give new type node same range as name node |
374 |
newT.setSourceRange( |
375 |
superclassName.getStartPosition(), |
376 |
superclassName.getLength()); |
377 |
setSuperclassType(newT); |
378 |
} |
379 |
} |
380 |
} |
381 |
|
382 |
/** |
383 |
* Returns the superclass declared in this type |
384 |
* declaration, or <code>null</code> if there is none. |
385 |
* <p> |
386 |
* Note that this child is not relevant for interface declarations |
387 |
* (although it does still figure in subtree equality comparisons). |
388 |
* </p> |
389 |
* |
390 |
* @return the superclass type node, or <code>null</code> if |
391 |
* there is none |
392 |
* @since 2.2 |
393 |
*/ |
394 |
public Type getSuperclassType() { |
395 |
return this.optionalSuperclassType; |
396 |
} |
397 |
|
398 |
/** |
399 |
* Sets or clears the superclass declared in this type |
400 |
* declaration. |
401 |
* <p> |
402 |
* Note that this child is not relevant for interface declarations |
403 |
* (although it does still figure in subtree equality comparisons). |
404 |
* </p> |
405 |
* |
406 |
* @param superclassType the superclass type node, or <code>null</code> if |
407 |
* there is none |
408 |
* @exception IllegalArgumentException if: |
409 |
* <ul> |
410 |
* <li>the node belongs to a different AST</li> |
411 |
* <li>the node already has a parent</li> |
412 |
* </ul> |
413 |
* @since 2.2 |
414 |
*/ |
415 |
public void setSuperclassType(Type superclassType) { |
416 |
replaceChild(this.optionalSuperclassType, superclassType, true); |
417 |
this.optionalSuperclassType = superclassType; |
301 |
} |
418 |
} |
302 |
|
419 |
|
303 |
/** |
420 |
/** |
Lines 308-316
Link Here
|
308 |
* |
425 |
* |
309 |
* @return the live list of interface names |
426 |
* @return the live list of interface names |
310 |
* (element type: <code>Name</code>) |
427 |
* (element type: <code>Name</code>) |
|
|
428 |
* @deprecated Replaced by <code>superInterfaceTypes</code>, which contains |
429 |
* a list of <code>Type</code>s instead of <code>Name</code>s. |
311 |
*/ |
430 |
*/ |
312 |
public List superInterfaces() { |
431 |
public List superInterfaces() { |
313 |
return superInterfaceNames; |
432 |
// implement deprecated method in terms of superInterfaceTypes() |
|
|
433 |
// return special implementation of List<Name> in terms of List<Type> |
434 |
return new AbstractList() { |
435 |
/** |
436 |
* @see java.util.AbstractCollection#size() |
437 |
*/ |
438 |
public int size() { |
439 |
return superInterfaceTypes().size(); |
440 |
} |
441 |
|
442 |
/** |
443 |
* @see AbstractList#get(int) |
444 |
*/ |
445 |
public Object get(int index) { |
446 |
Type t = (Type) superInterfaceTypes().get(index); |
447 |
if (t instanceof SimpleType) { |
448 |
// old client reading an old style element |
449 |
SimpleType s = (SimpleType) t; |
450 |
return s.getName(); |
451 |
} else if ((t instanceof ParameterizedType) |
452 |
|| (t instanceof QualifiedType)) { |
453 |
// compatibility issue |
454 |
// back-level clients know nothing of new node types added in 2.1 |
455 |
// take this opportunity to inform client of problem |
456 |
throw new RuntimeException("Deprecated AST API method (TypeDeclaration.superinterfaces()) cannot handle newer node types"); //$NON-NLS-1$ |
457 |
} else { |
458 |
// compatibility issue |
459 |
// AST is bogus - illegal for type to be array or primitive type |
460 |
// take this opportunity to inform client of problem |
461 |
throw new RuntimeException("Deprecated AST API method (TypeDeclaration.superinterfaces()) cannot handle malformed AST"); //$NON-NLS-1$ |
462 |
} |
463 |
} |
464 |
|
465 |
/** |
466 |
* @see List#set(int, java.lang.Object) |
467 |
*/ |
468 |
public Object set(int index, Object element) { |
469 |
if (!(element instanceof Name)) { |
470 |
throw new IllegalArgumentException(); |
471 |
} |
472 |
Type oldType = (Type) superInterfaceTypes().get(index); |
473 |
Name newName = (Name) element; |
474 |
if (oldType instanceof SimpleType) { |
475 |
// old client operating on old style element |
476 |
SimpleType s = (SimpleType) oldType; |
477 |
Name oldName = s.getName(); |
478 |
if (oldName != element) { |
479 |
s.setName(newName); |
480 |
// give type node same range as name node |
481 |
s.setSourceRange( |
482 |
newName.getStartPosition(), |
483 |
newName.getLength()); |
484 |
} |
485 |
return oldName; |
486 |
} else { |
487 |
// old client replaced a new-fangled element |
488 |
Type newType = getAST().newSimpleType(newName); |
489 |
// give new type node same range as name node |
490 |
newType.setSourceRange( |
491 |
newName.getStartPosition(), |
492 |
newName.getLength()); |
493 |
superInterfaceTypes().set(index, newType); |
494 |
// no choice but to return old new-fangled element |
495 |
return oldType; |
496 |
} |
497 |
} |
498 |
|
499 |
/** |
500 |
* @see List#add(int, java.lang.Object) |
501 |
*/ |
502 |
public void add(int index, Object element) { |
503 |
if (!(element instanceof Name)) { |
504 |
throw new IllegalArgumentException(); |
505 |
} |
506 |
Name newName = (Name) element; |
507 |
Type newType = getAST().newSimpleType(newName); |
508 |
// give new type node same range as name node |
509 |
newType.setSourceRange( |
510 |
newName.getStartPosition(), |
511 |
newName.getLength()); |
512 |
superInterfaceTypes().add(index, newType); |
513 |
} |
514 |
|
515 |
/** |
516 |
* @see List#remove(int) |
517 |
*/ |
518 |
public Object remove(int index) { |
519 |
Object result = superInterfaceTypes().remove(index); |
520 |
if (result instanceof SimpleType) { |
521 |
// old client operating on old style element |
522 |
SimpleType s = (SimpleType) result; |
523 |
Name oldName = s.getName(); |
524 |
// make sure that oldName has no parent afterwards |
525 |
s.setName(getAST().newSimpleName("deleted")); //$NON-NLS-1$ |
526 |
return oldName; |
527 |
} else { |
528 |
// old client removing a new-fangled element |
529 |
// take a chance that they ignore result |
530 |
return result; |
531 |
} |
532 |
} |
533 |
}; |
534 |
} |
535 |
|
536 |
/** |
537 |
* Returns the live ordered list of superinterfaces of this type |
538 |
* declaration. For a class declaration, these are the interfaces |
539 |
* that this class implements; for an interface declaration, |
540 |
* these are the interfaces that this interface extends. |
541 |
* |
542 |
* @return the live list of interface types |
543 |
* (element type: <code>Type</code>) |
544 |
* @since 2.2 |
545 |
*/ |
546 |
public List superInterfaceTypes() { |
547 |
return superInterfaceTypes; |
314 |
} |
548 |
} |
315 |
|
549 |
|
316 |
/** |
550 |
/** |
Lines 506-512
Link Here
|
506 |
* Method declared on ASTNode. |
740 |
* Method declared on ASTNode. |
507 |
*/ |
741 |
*/ |
508 |
int memSize() { |
742 |
int memSize() { |
509 |
return super.memSize() + 6 * 4; |
743 |
return super.memSize() + 7 * 4; |
510 |
} |
744 |
} |
511 |
|
745 |
|
512 |
/* (omit javadoc for this method) |
746 |
/* (omit javadoc for this method) |
Lines 517-524
Link Here
|
517 |
memSize() |
751 |
memSize() |
518 |
+ (getJavadoc() == null ? 0 : getJavadoc().treeSize()) |
752 |
+ (getJavadoc() == null ? 0 : getJavadoc().treeSize()) |
519 |
+ (typeName == null ? 0 : getName().treeSize()) |
753 |
+ (typeName == null ? 0 : getName().treeSize()) |
520 |
+ (optionalSuperclassName == null ? 0 : getSuperclass().treeSize()) |
754 |
+ typeParameters.listSize() |
521 |
+ superInterfaceNames.listSize() |
755 |
+ (optionalSuperclassType == null ? 0 : getSuperclassType().treeSize()) |
|
|
756 |
+ superInterfaceTypes.listSize() |
522 |
+ bodyDeclarations.listSize(); |
757 |
+ bodyDeclarations.listSize(); |
523 |
} |
758 |
} |
524 |
} |
759 |
} |