KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > jdom > DOMType


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 package org.eclipse.jdt.internal.core.jdom;
12
13 import java.util.Enumeration JavaDoc;
14
15 import org.eclipse.jdt.core.ICompilationUnit;
16 import org.eclipse.jdt.core.IJavaElement;
17 import org.eclipse.jdt.core.IType;
18 import org.eclipse.jdt.core.compiler.CharOperation;
19 import org.eclipse.jdt.core.compiler.InvalidInputException;
20 import org.eclipse.jdt.core.jdom.*;
21 import org.eclipse.jdt.internal.compiler.parser.Scanner;
22 import org.eclipse.jdt.internal.compiler.parser.TerminalTokens;
23 import org.eclipse.jdt.internal.core.util.CharArrayBuffer;
24 import org.eclipse.jdt.internal.core.util.Messages;
25 /**
26  * DOMType provides an implementation of IDOMType.
27  *
28  * @see IDOMType
29  * @see DOMNode
30  * @deprecated The JDOM was made obsolete by the addition in 2.0 of the more
31  * powerful, fine-grained DOM/AST API found in the
32  * org.eclipse.jdt.core.dom package.
33  */

34 // TODO (jerome) - add implementation support for 1.5 features
35
/* package */ class DOMType extends DOMMember implements IDOMType {
36     /**
37      * The 'class' or 'interface' keyword if altered
38      * from the documents contents, otherwise <code>null</code>.
39      */

40     protected String JavaDoc fTypeKeyword;
41
42     /**
43      * The original inclusive source range of the 'class'
44      * or 'interface' keyword in the document.
45      */

46     protected int[] fTypeRange;
47
48     /**
49      * The superclass name for the class declaration
50      * if altered from the document's contents, otherwise
51      * <code>null</code>. Also <code>null</code> when this
52      * type represents an interface.
53      */

54     protected String JavaDoc fSuperclass;
55
56     /**
57      * The original inclusive source range of the superclass
58      * name in the document, or -1's of no superclass was
59      * specified in the document.
60      */

61     protected int[] fSuperclassRange;
62
63
64     /**
65      * The original inclusive souce range of the 'extends' keyword
66      * in the document, including surrounding whitespace, or -1's if
67      * the keyword was not present in the document.
68      */

69     protected int[] fExtendsRange;
70
71     /**
72      * The original inclusive souce range of the 'implements' keyword
73      * in the document, including surrounding whitespace, or -1's if
74      * the keyword was not present in the document.
75      */

76     protected int[] fImplementsRange;
77
78     /**
79      * The comma delimited list of interfaces this type implements
80      * or extends, if altered from the document's contents, otherwise
81      * <code>null</code>. Also <code>null</code> if this type does
82      * not implement or extend any interfaces.
83      */

84     protected char[] fInterfaces;
85
86     /**
87      * The original inclusive source range of the list of interfaces this
88      * type implements or extends, not including any surrouding whitespace.
89      * If the document did not specify interfaces, this array contains -1's.
90      */

91     protected int[] fInterfacesRange;
92
93     
94
95     /**
96      * The original source range of the first character following the
97      * type name superclass name, or interface list, up to and including
98      * the first character before the first type member.
99      */

100     protected int[] fOpenBodyRange;
101
102     /**
103      * The original source range of the first new line or non whitespace
104      * character preceding the close brace of the type's body, up to the
105      * and including the first character before the next node (if there are
106      * no following nodes, the range ends at the position of the last
107      * character in the document).
108      */

109     protected int[] fCloseBodyRange;
110
111     /**
112      * A list of interfaces this type extends or implements.
113      * <code>null</code> when this type does not extend
114      * or implement any interfaces.
115      */

116     protected String JavaDoc[] fSuperInterfaces= CharOperation.NO_STRINGS;
117     
118     /**
119      * The formal type parameters.
120      * @since 3.0
121      */

122     protected String JavaDoc[] fTypeParameters = CharOperation.NO_STRINGS;
123
124     /**
125      * Indicates this type is an enum class.
126      * @since 3.0
127      */

128     protected boolean fIsEnum= false;
129     
130     /**
131      * Indicates this type is an annotatation type (interface).
132      * @since 3.0
133      */

134     protected boolean fIsAnnotation= false;
135     
136     /**
137      * This position is the position of the end of the last line separator before the closing brace starting
138      * position of the receiver.
139      */

140 // protected int fInsertionPosition;
141

142 /**
143  * Constructs an empty type node.
144  */

145 DOMType() {
146     // Constructs an empty type node
147
}
148 /**
149  * Creates a new detailed TYPE document fragment on the given range of the document.
150  *
151  * @param document - the document containing this node's original contents
152  * @param sourceRange - a two element array of integers describing the
153  * entire inclusive source range of this node within its document.
154  * Contents start on and include the character at the first position.
155  * Contents end on and include the character at the last position.
156  * An array of -1's indicates this node's contents do not exist
157  * in the document.
158  * @param name - the identifier portion of the name of this node, or
159  * <code>null</code> if this node does not have a name
160  * @param nameRange - a two element array of integers describing the
161  * entire inclusive source range of this node's name within its document,
162  * including any array qualifiers that might immediately follow the name
163  * or -1's if this node does not have a name.
164  * @param commentRange - a two element array describing the comments that precede
165  * the member declaration. The first matches the start of this node's
166  * sourceRange, and the second is the new-line or first non-whitespace
167  * character following the last comment. If no comments are present,
168  * this array contains two -1's.
169  * @param flags - an integer representing the modifiers for this member. The
170  * integer can be analyzed with org.eclipse.jdt.core.Flags
171  * @param modifierRange - a two element array describing the location of
172  * modifiers for this member within its source range. The first integer
173  * is the first character of the first modifier for this member, and
174  * the second integer is the last whitespace character preceeding the
175  * next part of this member declaration. If there are no modifiers present
176  * in this node's source code (that is, package default visibility), this array
177  * contains two -1's.
178  * @param typeRange - a two element array describing the location of the 'class'
179  * or 'interface' keyword in the type declaration - first and last character
180  * positions.
181  * @param superclassRange - a two element array describing the location of the
182  * superclass name in the type declaration - first and last character
183  * positions or two -1's if a superclass is not present in the document.
184  * @param extendsRange - a two element array describing the location of the
185  * 'extends' keyword in the type declaration, including any surrounding
186  * whitespace, or -1's if the 'extends' keyword is not present in the document.
187  * @param implementsList - an array of names of the interfaces this type implements
188  * or extends, or <code>null</code> if this type does not implement or extend
189  * any interfaces.
190  * @param implementsRange - a two element array describing the location of the
191  * comment delimited list of interfaces this type implements or extends,
192  * not including any surrounding whitespace, or -1's if no interface list
193  * is present in the document.
194  * @param implementsKeywordRange - a two element array describing the location of the
195  * 'implements' keyword, including any surrounding whitespace, or -1's if no
196  * 'implements' keyword is present in the document.
197  * @param openBodyRange - a two element array describing the location of the
198  * open brace of the type's body and whitespace following the type declaration
199  * and preceeding the first member in the type.
200  * @param closeBodyRange - a two element array describing the source range of the
201  * first new line or non whitespace character preceeding the close brace of the
202  * type's body, up to the close brace
203  * @param isClass - true is the type is a class, false if it is an interface
204  */

205 DOMType(char[] document, int[] sourceRange, String JavaDoc name, int[] nameRange, int[] commentRange, int flags, int[] modifierRange, int[] typeRange, int[] superclassRange, int[] extendsRange, String JavaDoc[] implementsList, int[] implementsRange, int[] implementsKeywordRange, int[] openBodyRange, int[] closeBodyRange, boolean isClass) {
206     super(document, sourceRange, name, nameRange, commentRange, flags, modifierRange);
207
208     fTypeRange= typeRange;
209     setMask(MASK_TYPE_IS_CLASS, isClass);
210
211     fExtendsRange= extendsRange;
212     fImplementsRange= implementsKeywordRange;
213     fSuperclassRange= superclassRange;
214     fInterfacesRange= implementsRange;
215     fCloseBodyRange= closeBodyRange;
216     setMask(MASK_TYPE_HAS_SUPERCLASS, superclassRange[0] > 0);
217     setMask(MASK_TYPE_HAS_INTERFACES, implementsList != null);
218     fSuperInterfaces= implementsList;
219     fOpenBodyRange= openBodyRange;
220     fCloseBodyRange= closeBodyRange;
221     setMask(MASK_DETAILED_SOURCE_INDEXES, true);
222
223 }
224 /**
225  * Creates a new simple TYPE document fragment on the given range of the document.
226  *
227  * @param document - the document containing this node's original contents
228  * @param sourceRange - a two element array of integers describing the
229  * entire inclusive source range of this node within its document.
230  * Contents start on and include the character at the first position.
231  * Contents end on and include the character at the last position.
232  * An array of -1's indicates this node's contents do not exist
233  * in the document.
234  * @param name - the identifier portion of the name of this node, or
235  * <code>null</code> if this node does not have a name
236  * @param nameRange - a two element array of integers describing the
237  * entire inclusive source range of this node's name within its document,
238  * including any array qualifiers that might immediately follow the name
239  * or -1's if this node does not have a name.
240  * @param flags - an integer representing the modifiers for this member. The
241  * integer can be analyzed with org.eclipse.jdt.core.Flags
242  * @param implementsList - an array of names of the interfaces this type implements
243  * or extends, or <code>null</code> if this type does not implement or extend
244  * any interfaces.
245  * @param isClass - true is the type is a class, false if it is an interface
246  */

247 DOMType(char[] document, int[] sourceRange, String JavaDoc name, int[] nameRange, int flags, String JavaDoc[] implementsList, boolean isClass) {
248     this(document, sourceRange, name, nameRange, new int[] {-1, -1}, flags,
249         new int[] {-1, -1}, new int[] {-1, -1}, new int[] {-1, -1}, new int[] {-1, -1},
250         implementsList, new int[] {-1, -1}, new int[] {-1, -1}, new int[] {-1, -1}, new int[] {sourceRange[1], sourceRange[1]}, isClass);
251     setMask(MASK_DETAILED_SOURCE_INDEXES, false);
252 }
253 /**
254  * @see IDOMType#addSuperInterface(String)
255  */

256 public void addSuperInterface(String JavaDoc name) throws IllegalArgumentException JavaDoc {
257     if (name == null) {
258         throw new IllegalArgumentException JavaDoc(Messages.dom_addNullInterface);
259     }
260     if (fSuperInterfaces == null) {
261         fSuperInterfaces= new String JavaDoc[1];
262         fSuperInterfaces[0]= name;
263     } else {
264         fSuperInterfaces= appendString(fSuperInterfaces, name);
265     }
266     setSuperInterfaces(fSuperInterfaces);
267 }
268 /**
269  * @see DOMMember#appendMemberBodyContents(CharArrayBuffer)
270  */

271 protected void appendMemberBodyContents(CharArrayBuffer buffer) {
272     buffer.append(fDocument, fOpenBodyRange[0], fOpenBodyRange[1] + 1 - fOpenBodyRange[0]);
273     appendContentsOfChildren(buffer);
274     buffer.append(fDocument, fCloseBodyRange[0], fCloseBodyRange[1] + 1 - fCloseBodyRange[0]);
275     buffer.append(fDocument, fCloseBodyRange[1] + 1, fSourceRange[1] - fCloseBodyRange[1]);
276 }
277 /**
278  * @see DOMMember#appendMemberDeclarationContents(CharArrayBuffer )
279  */

280 protected void appendMemberDeclarationContents(CharArrayBuffer buffer) {
281     
282     if (fTypeKeyword != null) {
283         buffer.append(fTypeKeyword);
284         buffer.append(fDocument, fTypeRange[1], fNameRange[0] - fTypeRange[1] );
285     } else {
286         buffer.append(fDocument, fTypeRange[0], fTypeRange[1] + 1 - fTypeRange[0]);
287     }
288
289     buffer.append(getName());
290
291     if (isClass()) {
292         boolean hasInterfaces = false;
293         if (getMask(MASK_TYPE_HAS_SUPERCLASS)) {
294             if (fExtendsRange[0] < 0) {
295                 buffer.append(" extends "); //$NON-NLS-1$
296
} else {
297                 buffer.append(fDocument, fExtendsRange[0], fExtendsRange[1] + 1 - fExtendsRange[0]);
298             }
299             if (fSuperclass != null) {
300                 buffer.append(fSuperclass);
301             } else {
302                 buffer.append(fDocument, fSuperclassRange[0], fSuperclassRange[1] + 1 - fSuperclassRange[0]);
303             }
304         }
305         if (getMask(MASK_TYPE_HAS_INTERFACES)) {
306             hasInterfaces = true;
307             if (fImplementsRange[0] < 0) {
308                 buffer.append(" implements "); //$NON-NLS-1$
309
} else {
310                 buffer.append(fDocument, fImplementsRange[0], fImplementsRange[1] + 1 - fImplementsRange[0]);
311             }
312             if (fInterfaces != null) {
313                 buffer.append(fInterfaces);
314             } else {
315                 buffer.append(fDocument, fInterfacesRange[0], fInterfacesRange[1] + 1 - fInterfacesRange[0]);
316             }
317         }
318         if (hasInterfaces) {
319             if (fImplementsRange[0] < 0) {
320                 buffer.append(' ');
321             } else {
322                 buffer.append(fDocument, fInterfacesRange[1] + 1, fOpenBodyRange[0] - fInterfacesRange[1] - 1);
323             }
324         } else {
325             if (fSuperclassRange[0] < 0) {
326                 buffer.append(' ');
327             } else if (fImplementsRange[0] > 0) {
328                 buffer.append(fDocument, fSuperclassRange[1] + 1, fImplementsRange[0] - fSuperclassRange[1] - 1);
329                 buffer.append(fDocument, fInterfacesRange[1] + 1, fOpenBodyRange[0] - fInterfacesRange[1] - 1);
330             } else {
331                 buffer.append(fDocument, fSuperclassRange[1] + 1, fOpenBodyRange[0] - fSuperclassRange[1] - 1);
332             }
333         }
334     } else {
335         if (getMask(MASK_TYPE_HAS_INTERFACES)) {
336             if (fExtendsRange[0] < 0) {
337                 buffer.append(" extends "); //$NON-NLS-1$
338
} else {
339                 buffer.append(fDocument, fExtendsRange[0], fExtendsRange[1] + 1 - fExtendsRange[0]);
340             }
341             if (fInterfaces != null) {
342                 buffer.append(fInterfaces);
343                 buffer.append(' ');
344             } else {
345                 buffer.append(fDocument, fInterfacesRange[0], fInterfacesRange[1] + 1 - fInterfacesRange[0]);
346             }
347         } else {
348             if (fImplementsRange[0] < 0) {
349                 buffer.append(' ');
350             } else {
351                 buffer.append(fDocument, fNameRange[1] + 1, fOpenBodyRange[0] - fNameRange[1] - 1);
352             }
353         }
354     }
355     
356 }
357 /**
358  * @see DOMMember#appendSimpleContents(CharArrayBuffer)
359  */

360 protected void appendSimpleContents(CharArrayBuffer buffer) {
361     // append eveything before my name
362
buffer.append(fDocument, fSourceRange[0], fNameRange[0] - fSourceRange[0]);
363     // append my name
364
buffer.append(fName);
365
366     
367     // append everything after my name and before my first child
368
buffer.append(fDocument, fNameRange[1] + 1, fOpenBodyRange[1] - fNameRange[1]);
369     // append my children
370
appendContentsOfChildren(buffer);
371     // append from my last child to my end
372
buffer.append(fDocument, fCloseBodyRange[0], fSourceRange[1] - fCloseBodyRange[0] + 1);
373
374
375 }
376 /**
377  * @see IDOMNode#canHaveChildren()
378  */

379 public boolean canHaveChildren() {
380     return true;
381 }
382 /**
383  * Returns the position of the closing brace for the body of this type.
384  * This value this method returns is only valid before the type has
385  * been normalized and is present only for normalization.
386  */

387 int getCloseBodyPosition() {
388     return fCloseBodyRange[0];
389 }
390 /**
391  * @see DOMNode#getDetailedNode()
392  */

393 protected DOMNode getDetailedNode() {
394     return (DOMNode)getFactory().createType(getContents());
395 }
396 /**
397  * @see DOMNode#getInsertionPosition()
398  */

399 public int getInsertionPosition() {
400     // this should return the position of the end of the last line separator before the closing brace of the type
401
// See PR 1GELSDQ: ITPJUI:WINNT - JDOM: IType.createMethod does not insert nicely for inner types
402
return fInsertionPosition;
403 }
404 /**
405  * @see IDOMNode#getJavaElement
406  */

407 public IJavaElement getJavaElement(IJavaElement parent) throws IllegalArgumentException JavaDoc {
408     switch (parent.getElementType()) {
409         case IJavaElement.COMPILATION_UNIT:
410             return ((ICompilationUnit)parent).getType(getName());
411         case IJavaElement.TYPE:
412             return ((IType)parent).getType(getName());
413         // Note: creating local/anonymous type is not supported
414
default:
415             throw new IllegalArgumentException JavaDoc(Messages.element_illegalParent);
416     }
417 }
418 /**
419  * @see DOMMember#getMemberDeclarationStartPosition()
420  */

421 protected int getMemberDeclarationStartPosition() {
422     return fTypeRange[0];
423 }
424 /**
425  * @see IDOMNode#getNodeType()
426  */

427 public int getNodeType() {
428     return IDOMNode.TYPE;
429 }
430 /**
431  * Answers the open body range end position.
432  */

433 int getOpenBodyEnd() {
434     return fOpenBodyRange[1];
435 }
436 /**
437  * @see IDOMType#getSuperclass()
438  */

439 public String JavaDoc getSuperclass() {
440     becomeDetailed();
441     if (getMask(MASK_TYPE_HAS_SUPERCLASS)) {
442         if (fSuperclass != null) {
443             return fSuperclass;
444         } else {
445             return new String JavaDoc(fDocument, fSuperclassRange[0], fSuperclassRange[1] + 1 - fSuperclassRange[0]);
446         }
447     } else {
448         return null;
449     }
450 }
451 /**
452  * @see IDOMType#getSuperInterfaces()
453  */

454 public String JavaDoc[] getSuperInterfaces() {
455     return fSuperInterfaces;
456 }
457 /**
458  * @see IDOMNode
459  */

460 public boolean isAllowableChild(IDOMNode node) {
461     if (node != null) {
462         int type= node.getNodeType();
463         return type == IDOMNode.TYPE || type == IDOMNode.FIELD|| type == IDOMNode.METHOD ||
464             type == IDOMNode.INITIALIZER;
465     } else {
466         return false;
467     }
468     
469 }
470 /**
471  * @see IDOMType#isClass()
472  */

473 public boolean isClass() {
474     return getMask(MASK_TYPE_IS_CLASS);
475 }
476 /**
477  * @see DOMNode
478  */

479 protected DOMNode newDOMNode() {
480     return new DOMType();
481 }
482 /**
483  * Normalizes this <code>DOMNode</code>'s source positions to include whitespace preceeding
484  * the node on the line on which the node starts, and all whitespace after the node up to
485  * the next node's start
486  */

487 void normalize(ILineStartFinder finder) {
488     // perform final changes to the open and close body ranges
489
int openBodyEnd, openBodyStart, closeBodyStart, closeBodyEnd;
490     DOMNode first = (DOMNode) getFirstChild();
491     DOMNode lastNode = null;
492     // look for the open body
493
Scanner scanner = new Scanner();
494     scanner.setSource(fDocument);
495     scanner.resetTo(fNameRange[1] + 1, fDocument.length);
496     
497     try {
498         int currentToken = scanner.getNextToken();
499         while(currentToken != TerminalTokens.TokenNameLBRACE &&
500                 currentToken != TerminalTokens.TokenNameEOF) {
501             currentToken = scanner.getNextToken();
502         }
503         if(currentToken == TerminalTokens.TokenNameLBRACE) {
504             openBodyEnd = scanner.currentPosition - 1;
505             openBodyStart = scanner.startPosition;
506         } else {
507             openBodyEnd = fDocument.length;
508             openBodyStart = fDocument.length;
509         }
510     } catch(InvalidInputException e) {
511         openBodyEnd = fDocument.length;
512         openBodyStart = fDocument.length;
513     }
514     if (first != null) {
515         int lineStart = finder.getLineStart(first.getStartPosition());
516         if (lineStart > openBodyEnd) {
517             openBodyEnd = lineStart - 1;
518         } else {
519             openBodyEnd = first.getStartPosition() - 1;
520         }
521         lastNode = (DOMNode) first.getNextNode();
522         if (lastNode == null) {
523             lastNode = first;
524         } else {
525             while (lastNode.getNextNode() != null) {
526                 lastNode = (DOMNode) lastNode.getNextNode();
527             }
528         }
529         scanner.setSource(fDocument);
530         scanner.resetTo(lastNode.getEndPosition() + 1, fDocument.length);
531         try {
532             int currentToken = scanner.getNextToken();
533             while(currentToken != TerminalTokens.TokenNameRBRACE &&
534                     currentToken != TerminalTokens.TokenNameEOF) {
535                 currentToken = scanner.getNextToken();
536             }
537             if(currentToken == TerminalTokens.TokenNameRBRACE) {
538                 closeBodyStart = scanner.startPosition;
539                 closeBodyEnd = scanner.currentPosition - 1;
540             } else {
541                 closeBodyStart = fDocument.length;
542                 closeBodyEnd = fDocument.length;
543             }
544         } catch(InvalidInputException e) {
545             closeBodyStart = fDocument.length;
546             closeBodyEnd = fDocument.length;
547         }
548     } else {
549         scanner.resetTo(openBodyEnd, fDocument.length);
550         try {
551             int currentToken = scanner.getNextToken();
552             while(currentToken != TerminalTokens.TokenNameRBRACE &&
553                     currentToken != TerminalTokens.TokenNameEOF) {
554                 currentToken = scanner.getNextToken();
555             }
556             if(currentToken == TerminalTokens.TokenNameRBRACE) {
557                 closeBodyStart = scanner.startPosition;
558                 closeBodyEnd = scanner.currentPosition - 1;
559             } else {
560                 closeBodyStart = fDocument.length;
561                 closeBodyEnd = fDocument.length;
562             }
563         } catch(InvalidInputException e) {
564             closeBodyStart = fDocument.length;
565             closeBodyEnd = fDocument.length;
566         }
567         openBodyEnd = closeBodyEnd - 1;
568     }
569     setOpenBodyRangeEnd(openBodyEnd);
570     setOpenBodyRangeStart(openBodyStart);
571     setCloseBodyRangeStart(closeBodyStart);
572     setCloseBodyRangeEnd(closeBodyEnd);
573     fInsertionPosition = finder.getLineStart(closeBodyStart);
574     if (lastNode != null && fInsertionPosition < lastNode.getEndPosition()) {
575         fInsertionPosition = getCloseBodyPosition();
576     }
577     if (fInsertionPosition <= openBodyEnd) {
578         fInsertionPosition = getCloseBodyPosition();
579     }
580     super.normalize(finder);
581 }
582
583 /**
584  * Normalizes this <code>DOMNode</code>'s end position.
585  */

586 void normalizeEndPosition(ILineStartFinder finder, DOMNode next) {
587     if (next == null) {
588         // this node's end position includes all of the characters up
589
// to the end of the enclosing node
590
DOMNode parent = (DOMNode) getParent();
591         if (parent == null || parent instanceof DOMCompilationUnit) {
592             setSourceRangeEnd(fDocument.length - 1);
593         } else {
594             // parent is a type
595
setSourceRangeEnd(((DOMType)parent).getCloseBodyPosition() - 1);
596         }
597     } else {
598         // this node's end position is just before the start of the next node
599
next.normalizeStartPosition(getEndPosition(), finder);
600         setSourceRangeEnd(next.getStartPosition() - 1);
601     }
602 }
603
604 /**
605  * Offsets all the source indexes in this node by the given amount.
606  */

607 protected void offset(int offset) {
608     super.offset(offset);
609     offsetRange(fCloseBodyRange, offset);
610     offsetRange(fExtendsRange, offset);
611     offsetRange(fImplementsRange, offset);
612     offsetRange(fInterfacesRange, offset);
613     offsetRange(fOpenBodyRange, offset);
614     offsetRange(fSuperclassRange, offset);
615     offsetRange(fTypeRange, offset);
616 }
617 /**
618  * @see IDOMType#setClass(boolean)
619  */

620 public void setClass(boolean b) {
621     becomeDetailed();
622     fragment();
623     setMask(MASK_TYPE_IS_CLASS, b);
624     if (b) {
625         fTypeKeyword= "class"; //$NON-NLS-1$
626
} else {
627         fTypeKeyword= "interface"; //$NON-NLS-1$
628
setSuperclass(null);
629     }
630 }
631 /**
632  * Sets the end of the close body range
633  */

634 void setCloseBodyRangeEnd(int end) {
635     fCloseBodyRange[1] = end;
636 }
637 /**
638  * Sets the start of the close body range
639  */

640 void setCloseBodyRangeStart(int start) {
641     fCloseBodyRange[0] = start;
642 }
643 /**
644  * Sets the name of this node.
645  *
646  * <p>When the name of a type is set, all of its constructors must be marked
647  * as fragmented, since the names of the constructors must reflect the name
648  * of this type.
649  *
650  * @see IDOMNode#setName(String)
651  */

652 public void setName(String JavaDoc name) throws IllegalArgumentException JavaDoc {
653     if (name == null) {
654         throw new IllegalArgumentException JavaDoc(Messages.element_nullName);
655     }
656     super.setName(name);
657     Enumeration JavaDoc children= getChildren();
658     while (children.hasMoreElements()) {
659         IDOMNode child= (IDOMNode)children.nextElement();
660         if (child.getNodeType() == IDOMNode.METHOD && ((IDOMMethod)child).isConstructor()) {
661             ((DOMNode)child).fragment();
662         }
663     }
664 }
665 /**
666  * Sets the end of the open body range
667  */

668 void setOpenBodyRangeEnd(int end) {
669     fOpenBodyRange[1] = end;
670 }
671 /**
672  * Sets the start of the open body range
673  */

674 void setOpenBodyRangeStart(int start) {
675     fOpenBodyRange[0] = start;
676 }
677 /**
678  * @see IDOMType#setSuperclass(String)
679  */

680 public void setSuperclass(String JavaDoc superclassName) {
681     becomeDetailed();
682     fragment();
683     fSuperclass= superclassName;
684     setMask(MASK_TYPE_HAS_SUPERCLASS, superclassName != null);
685 }
686 /**
687  * @see IDOMType#setSuperInterfaces(String[])
688  */

689 public void setSuperInterfaces(String JavaDoc[] names) {
690     becomeDetailed();
691     if (names == null) {
692         throw new IllegalArgumentException JavaDoc(Messages.dom_nullInterfaces);
693     }
694     fragment();
695     fSuperInterfaces= names;
696     if (names.length == 0) {
697         fInterfaces= null;
698         fSuperInterfaces= CharOperation.NO_STRINGS;
699         setMask(MASK_TYPE_HAS_INTERFACES, false);
700     } else {
701         setMask(MASK_TYPE_HAS_INTERFACES, true);
702         CharArrayBuffer buffer = new CharArrayBuffer();
703         for (int i = 0; i < names.length; i++) {
704             if (i > 0) {
705                 buffer.append(", "); //$NON-NLS-1$
706
}
707             buffer.append(names[i]);
708         }
709         fInterfaces = buffer.getContents();
710     }
711 }
712 /**
713  * Sets the type keyword
714  */

715 void setTypeKeyword(String JavaDoc keyword) {
716     fTypeKeyword = keyword;
717 }
718 /**
719  * @see DOMNode#shareContents(DOMNode)
720  */

721 protected void shareContents(DOMNode node) {
722     super.shareContents(node);
723     DOMType type= (DOMType)node;
724     fCloseBodyRange= rangeCopy(type.fCloseBodyRange);
725     fExtendsRange= type.fExtendsRange;
726     fImplementsRange= rangeCopy(type.fImplementsRange);
727     fInterfaces= type.fInterfaces;
728     fInterfacesRange= rangeCopy(type.fInterfacesRange);
729     fOpenBodyRange= rangeCopy(type.fOpenBodyRange);
730     fSuperclass= type.fSuperclass;
731     fSuperclassRange= rangeCopy(type.fSuperclassRange);
732     fSuperInterfaces= type.fSuperInterfaces;
733     fTypeKeyword= type.fTypeKeyword;
734     fTypeRange= rangeCopy(type.fTypeRange);
735 }
736 /**
737  * @see IDOMNode#toString()
738  */

739 public String JavaDoc toString() {
740     return "TYPE: " + getName(); //$NON-NLS-1$
741
}
742
743 /**
744  * @see IDOMType#getTypeParameters()
745  * @since 3.0
746  */

747 public String JavaDoc[] getTypeParameters() {
748     return this.fTypeParameters;
749 }
750
751 /**
752  * @see IDOMType#isEnum()
753  * @since 3.0
754  */

755 public boolean isEnum() {
756     return this.fIsEnum;
757 }
758
759 /**
760  * @see IDOMType#isAnnotation()
761  * @since 3.0
762  */

763 public boolean isAnnotation() {
764     return this.fIsAnnotation;
765 }
766
767 /**
768  * @see IDOMType#setEnum(boolean)
769  * @since 3.0
770  */

771 public void setEnum(boolean b) {
772     this.fIsEnum = b;
773     if (this.fIsEnum) {
774         // enums are always classes with no superclass
775
setClass(true);
776         setSuperclass(null);
777     }
778 }
779
780 /**
781  * @see IDOMType#setAnnotation(boolean)
782  * @since 3.0
783  */

784 public void setAnnotation(boolean b) {
785     this.fIsAnnotation= b;
786     if (this.fIsAnnotation) {
787         // annotation types are always interface with no superclass or superinterfaces
788
setClass(false);
789         setSuperclass(null);
790         setSuperInterfaces(CharOperation.NO_STRINGS);
791     }
792 }
793
794 /**
795  * @see IDOMType#setTypeParameters(java.lang.String[])
796  * @since 3.0
797  */

798 public void setTypeParameters(String JavaDoc[] typeParameters) {
799     this.fTypeParameters = typeParameters;
800 }
801 }
802
Popular Tags