1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core.jdom;
13 import java.util.Enumeration;
15 import net.sourceforge.phpdt.core.IJavaElement;
16 import net.sourceforge.phpdt.core.IType;
17 import net.sourceforge.phpdt.core.jdom.DOMException;
18 import net.sourceforge.phpdt.core.jdom.IDOMField;
19 import net.sourceforge.phpdt.core.jdom.IDOMNode;
20 import net.sourceforge.phpdt.internal.compiler.util.Util;
21 import net.sourceforge.phpdt.internal.core.util.CharArrayBuffer;
22 import net.sourceforge.phpdt.internal.core.util.CharArrayOps;
25 * DOMField provides an implementation of IDOMField.
30 class DOMField extends DOMMember implements IDOMField {
33 * Contains the type of the field when the type
34 * has been altered from the contents in the
35 * document, otherwise <code>null</code>.
37 protected String fType;
40 * The original inclusive source range of the
41 * field's type in the document.
43 protected int[] fTypeRange;
46 * The contents of the initializer when the
47 * initializer has been altered from the
48 * original state in the document, otherwise
51 protected String fInitializer;
54 * The original inclusive source range of the
55 * initializer in the document.
57 protected int[] fInitializerRange;
60 * Constructs an empty field node.
65 * Creates a new detailed FIELD document fragment on the given range of the document.
67 * @param document - the document containing this node's original contents
68 * @param sourceRange - a two element array of integers describing the
69 * entire inclusive source range of this node within its document.
70 * Contents start on and include the character at the first position.
71 * Contents end on and include the character at the last position.
72 * An array of -1's indicates this node's contents do not exist
74 * @param name - the identifier portion of the name of this field,
75 * corresponding to VariableDeclaratorId (JLS 8.3).
76 * @param nameRange - a two element array of integers describing the
77 * entire inclusive source range of this node's name within its document,
78 * including any array qualifiers that might follow the name.
79 * @param commentRange - a two element array describing the comments that precede
80 * the member declaration. The first matches the start of this node's
81 * sourceRange, and the second is the new-line or first non-whitespace
82 * character following the last comment. If no comments are present,
83 * this array contains two -1's.
84 * @param flags - an integer representing the modifiers for this member. The
85 * integer can be analyzed with org.eclipse.jdt.core.Flags
86 * @param modifierRange - a two element array describing the location of
87 * modifiers for this member within its source range. The first integer
88 * is the first character of the first modifier for this member, and
89 * the second integer is the last whitespace character preceeding the
90 * next part of this member declaration. If there are no modifiers present
91 * in this node's source code (that is, package default visibility), this array
93 * @param typeRange- a two element array describing the location of the
94 * typeName in the document - the positions of the first and last characters
96 * @param type - the type of the field, in normalized form, as defined in
97 * Type in Field Declaration (JLS 8.3)
98 * @param hasInitializer - true if this field declaration includes an initializer,
100 * @param initRange - a two element array describing the location of the initializer
101 * in the declaration. The first integer is the position of the character
102 * following the equals sign, and the position of the last character is the last
103 * in the initializer. If this field has no initializer, this array contains
105 * @param isVariableDeclarator - true if the field is a seconday variable declarator
106 * for a previous field declaration.
108 DOMField(char[] document, int[] sourceRange, String name, int[] nameRange, int[] commentRange, int flags, int[] modifierRange, int[] typeRange, String type, boolean hasInitializer, int[] initRange, boolean isVariableDeclarator) {
109 super(document, sourceRange, name, nameRange, commentRange, flags, modifierRange);
112 fTypeRange= typeRange;
113 setHasInitializer(hasInitializer);
114 fInitializerRange= initRange;
115 setIsVariableDeclarator(isVariableDeclarator);
116 setMask(MASK_DETAILED_SOURCE_INDEXES, true);
120 * Creates a new simple FIELD document fragment on the given range of the document.
122 * @param document - the document containing this node's original contents
123 * @param sourceRange - a two element array of integers describing the
124 * entire inclusive source range of this node within its document.
125 * Contents start on and include the character at the first position.
126 * Contents end on and include the character at the last position.
127 * An array of -1's indicates this node's contents do not exist
129 * @param name - the identifier portion of the name of this field,
130 * corresponding to VariableDeclaratorId (JLS 8.3).
131 * @param nameRange - a two element array of integers describing the
132 * entire inclusive source range of this node's name within its document,
133 * including any array qualifiers that might follow the name.
134 * @param flags - an integer representing the modifiers for this member. The
135 * integer can be analyzed with org.eclipse.jdt.core.Flags
136 * @param type - the type of the field, in normalized form, as defined in
137 * Type in Field Declaration (JLS 8.3)
138 * @param isVariableDeclarator - true if the field is a seconday variable declarator
139 * for a previous field declaration.
141 DOMField(char[] document, int[] sourceRange, String name, int[] nameRange, int flags, String type, boolean isVariableDeclarator) {
142 this(document, sourceRange, name, nameRange, new int[] {-1, -1}, flags, new int[] {-1, -1}, new int[] {-1, -1}, type, false, new int[] {-1, -1}, isVariableDeclarator);
143 setMask(MASK_DETAILED_SOURCE_INDEXES, false);
146 * Appends this member's body contents to the given CharArrayBuffer.
147 * Body contents include the member body and any trailing whitespace.
149 * <p>A field does not have a body.
151 * @see DOMMember#appendMemberBodyContents(CharArrayBuffer)
153 protected void appendMemberBodyContents(CharArrayBuffer buffer) {}
155 * @see DOMMember#appendMemberDeclarationContents(CharArrayBuffer)
157 protected void appendMemberDeclarationContents(CharArrayBuffer buffer) {
159 if (isVariableDeclarator()) {
160 buffer.append(fDocument, fSourceRange[0], fNameRange[0] - fSourceRange[0]);
163 .append(getTypeContents())
164 .append(fDocument, fTypeRange[1] + 1, fNameRange[0] - fTypeRange[1] - 1);
167 buffer.append(getNameContents());
168 if (hasInitializer()) {
169 if (fInitializerRange[0] < 0) {
172 .append(fInitializer)
173 .append(fDocument, fNameRange[1] + 1, fSourceRange[1] - fNameRange[1]);
176 .append(fDocument, fNameRange[1] + 1, fInitializerRange[0] - fNameRange[1] - 1)
177 .append(getInitializer())
178 .append(fDocument, fInitializerRange[1] + 1, fSourceRange[1] - fInitializerRange[1]);
181 if (fInitializerRange[0] < 0) {
182 buffer.append(fDocument, fNameRange[1] + 1, fSourceRange[1] - fNameRange[1]);
184 buffer.append(fDocument, fInitializerRange[1] + 1, fSourceRange[1] - fInitializerRange[1]);
190 * Appends this member's header contents to the given CharArrayBuffer.
191 * Header contents include any preceding comments and modifiers.
193 * <p>If this field is a secondary variable declarator, there is no header.
195 * @see DOMMember#appendMemberHeaderFragment(CharArrayBuffer)
197 protected void appendMemberHeaderFragment(CharArrayBuffer buffer) {
199 if (isVariableDeclarator()) {
202 super.appendMemberHeaderFragment(buffer);
207 * @see DOMMember#appendSimpleContents(CharArrayBuffer)
209 protected void appendSimpleContents(CharArrayBuffer buffer) {
210 // append eveything before my name
211 buffer.append(fDocument, fSourceRange[0], fNameRange[0] - fSourceRange[0]);
213 buffer.append(fName);
214 // append everything after my name
215 buffer.append(fDocument, fNameRange[1] + 1, fSourceRange[1] - fNameRange[1]);
218 * Generates detailed source indexes for this node if possible.
220 * @exception DOMException if unable to generate detailed source indexes
223 //protected void becomeDetailed() throws DOMException {
224 // if (!isDetailed()) {
225 // if (isVariableDeclarator() || hasMultipleVariableDeclarators()) {
226 // DOMNode first = getFirstFieldDeclaration();
227 // DOMNode last = getLastFieldDeclaration();
228 // DOMNode node= first;
229 // String source= first.getContents();
230 // while (node != last) {
231 // node= node.fNextNode;
232 // source+=node.getContents();
234 // DOMBuilder builder = new DOMBuilder();
235 // IDOMField[] details= builder.createFields(source.toCharArray());
236 // if (details.length == 0) {
237 // throw new DOMException(Util.bind("dom.cannotDetail")); //$NON-NLS-1$
240 // for (int i= 0; i < details.length; i++) {
241 // node.shareContents((DOMNode)details[i]);
242 // node= node.fNextNode;
246 // super.becomeDetailed();
252 * @see IDOMNode#clone()
254 public Object clone() {
255 // if (isVariableDeclarator() || hasMultipleVariableDeclarators()) {
256 // return getFactory().createField(new String(getSingleVariableDeclaratorContents()));
258 return super.clone();
262 * Expands all variable declarators in this field declaration into
263 * stand-alone field declarations.
265 protected void expand() {
266 if (isVariableDeclarator() || hasMultipleVariableDeclarators()) {
267 Enumeration siblings= new SiblingEnumeration(getFirstFieldDeclaration());
268 DOMField field= (DOMField)siblings.nextElement();
269 DOMNode next= field.fNextNode;
270 while (siblings.hasMoreElements() && (next instanceof DOMField) && (((DOMField)next).isVariableDeclarator())) {
271 field.localizeContents();
272 if (field.fParent != null) {
273 field.fParent.fragment();
275 field= (DOMField)siblings.nextElement();
276 next= field.fNextNode;
278 field.localizeContents();
282 * @see DOMNode#getDetailedNode()
284 //protected DOMNode getDetailedNode() {
285 // if (isVariableDeclarator() || hasMultipleVariableDeclarators()) {
286 // return (DOMNode)getFactory().createField(new String(getSingleVariableDeclaratorContents()));
288 // return (DOMNode)getFactory().createField(getContents());
292 * Returns the first field document fragment that defines
293 * the type for this variable declarator.
295 protected DOMField getFirstFieldDeclaration() {
296 if (isVariableDeclarator()) {
297 return ((DOMField)fPreviousNode).getFirstFieldDeclaration();
303 * @see IDOMField#getInitializer()
305 public String getInitializer() {
307 if (hasInitializer()) {
308 if (fInitializer != null) {
311 return CharArrayOps.substring(fDocument, fInitializerRange[0], fInitializerRange[1] + 1 - fInitializerRange[0]);
318 * @see IDOMNode#getJavaElement
320 public IJavaElement getJavaElement(IJavaElement parent) throws IllegalArgumentException {
321 if (parent.getElementType() == IJavaElement.TYPE) {
322 return ((IType)parent).getField(getName());
324 throw new IllegalArgumentException(Util.bind("element.illegalParent")); //$NON-NLS-1$
328 * Returns the last field document fragment in this muli-declarator statement.
330 protected DOMField getLastFieldDeclaration() {
331 DOMField field = this;
332 while (field.isVariableDeclarator() || field.hasMultipleVariableDeclarators()) {
333 if (field.fNextNode instanceof DOMField && ((DOMField)field.fNextNode).isVariableDeclarator()) {
334 field= (DOMField)field.fNextNode;
342 * @see DOMMember#getMemberDeclarationStartPosition()
344 protected int getMemberDeclarationStartPosition() {
345 return fTypeRange[0];
348 * @see IDOMNode#getNodeType()
350 public int getNodeType() {
351 return IDOMNode.FIELD;
354 * Returns a String representing this field declaration as a field
355 * declaration with one variable declarator.
357 protected char[] getSingleVariableDeclaratorContents() {
360 CharArrayBuffer buffer= new CharArrayBuffer();
361 DOMField first= getFirstFieldDeclaration();
362 if (first.isDetailed()) {
363 first.appendMemberHeaderFragment(buffer);
364 buffer.append(getType());
365 if (isVariableDeclarator()) {
368 buffer.append(fDocument, fTypeRange[1] + 1, fNameRange[0] - fTypeRange[1] - 1);
371 buffer.append(first.fDocument, first.fSourceRange[0], first.fNameRange[0] - first.fSourceRange[0]);
374 buffer.append(getName());
375 if (hasInitializer()) {
376 if (fInitializerRange[0] < 0) {
379 .append(fInitializer)
381 .append(Util.LINE_SEPARATOR);
384 .append(fDocument, fNameRange[1] + 1, fInitializerRange[0] - fNameRange[1] - 1)
385 .append(getInitializer())
387 .append(Util.LINE_SEPARATOR);
390 buffer.append(';').append(Util.LINE_SEPARATOR);
392 return buffer.getContents();
395 * @see IDOMField#getType()
397 public String getType() {
401 * Returns the souce code to be used for this
404 protected char[] getTypeContents() {
405 if (isTypeAltered()) {
406 return fType.toCharArray();
408 return CharArrayOps.subarray(fDocument, fTypeRange[0], fTypeRange[1] + 1 - fTypeRange[0]);
412 * Returns true if this field has an initializer expression,
415 protected boolean hasInitializer() {
416 return getMask(MASK_FIELD_HAS_INITIALIZER);
419 * Returns true is this field declarations has more than one
420 * variable declarator, otherwise false;
422 protected boolean hasMultipleVariableDeclarators() {
423 return fNextNode != null && (fNextNode instanceof DOMField) &&
424 ((DOMField)fNextNode).isVariableDeclarator();
427 * Inserts the given un-parented node as a sibling of this node, immediately before
428 * this node. Once inserted, the sibling is only dependent on this document fragment.
430 * <p>When a sibling is inserted before a variable declarator, it must first
433 * @see IDOMNode#insertSibling(IDOMNode)
435 public void insertSibling(IDOMNode sibling) throws IllegalArgumentException, DOMException {
436 if (isVariableDeclarator()) {
439 super.insertSibling(sibling);
442 * Returns true if this field's type has been altered
443 * from the original document contents.
445 protected boolean isTypeAltered() {
446 return getMask(MASK_FIELD_TYPE_ALTERED);
449 * Returns true if this field is declared as a secondary variable
450 * declarator for a previous field declaration.
452 protected boolean isVariableDeclarator() {
453 return getMask(MASK_FIELD_IS_VARIABLE_DECLARATOR);
458 protected DOMNode newDOMNode() {
459 return new DOMField();
462 * Normalizes this <code>DOMNode</code>'s end position.
464 void normalizeEndPosition(ILineStartFinder finder, DOMNode next) {
466 // this node's end position includes all of the characters up
467 // to the end of the enclosing node
468 DOMNode parent = (DOMNode) getParent();
469 if (parent == null || parent instanceof DOMCompilationUnit) {
470 setSourceRangeEnd(fDocument.length - 1);
473 int temp = ((DOMType)parent).getCloseBodyPosition() - 1;
474 setSourceRangeEnd(temp);
475 fInsertionPosition = Math.max(finder.getLineStart(temp + 1), getEndPosition());
478 // this node's end position is just before the start of the next node
479 // unless the next node is a field that is declared along with this one
480 int temp = next.getStartPosition() - 1;
481 fInsertionPosition = Math.max(finder.getLineStart(temp + 1), getEndPosition());
483 next.normalizeStartPosition(getEndPosition(), finder);
484 if (next instanceof DOMField) {
485 DOMField field = (DOMField) next;
486 if (field.isVariableDeclarator() && fTypeRange[0] == field.fTypeRange[0])
489 setSourceRangeEnd(next.getStartPosition() - 1);
493 * Normalizes this <code>DOMNode</code>'s start position.
495 void normalizeStartPosition(int endPosition, ILineStartFinder finder) {
496 if (isVariableDeclarator()) {
497 // start position is end of last element
498 setStartPosition(fPreviousNode.getEndPosition() + 1);
500 super.normalizeStartPosition(endPosition, finder);
504 * Offsets all the source indexes in this node by the given amount.
506 protected void offset(int offset) {
507 super.offset(offset);
508 offsetRange(fInitializerRange, offset);
509 offsetRange(fTypeRange, offset);
512 * Separates this node from its parent and siblings, maintaining any ties that
513 * this node has to the underlying document fragment.
515 * <p>When a field with multiple declarators is removed, its declaration
516 * must first be expanded.
518 * @see IDOMNode#remove()
520 public void remove() {
525 * @see IDOMMember#setComment(String)
527 public void setComment(String comment) {
529 super.setComment(comment);
532 * @see IDOMMember#setFlags(int)
534 public void setFlags(int flags) {
536 super.setFlags(flags);
539 * Sets the state of this field declaration as having
540 * an initializer expression.
542 protected void setHasInitializer(boolean hasInitializer) {
543 setMask(MASK_FIELD_HAS_INITIALIZER, hasInitializer);
546 * @see IDOMField#setInitializer(char[])
548 public void setInitializer(String initializer) {
551 setHasInitializer(initializer != null);
552 fInitializer= initializer;
555 * Sets the initializer range.
557 void setInitializerRange(int start, int end) {
558 fInitializerRange[0] = start;
559 fInitializerRange[1] = end;
562 * Sets the state of this field declaration as being a
563 * secondary variable declarator for a previous field
566 protected void setIsVariableDeclarator(boolean isVariableDeclarator) {
567 setMask(MASK_FIELD_IS_VARIABLE_DECLARATOR, isVariableDeclarator);
570 * @see IDOMField#setName(char[])
572 public void setName(String name) throws IllegalArgumentException {
574 throw new IllegalArgumentException(Util.bind("element.nullName")); //$NON-NLS-1$
577 setTypeAltered(true);
581 * @see IDOMField#setType(char[])
583 public void setType(String typeName) throws IllegalArgumentException {
584 if (typeName == null) {
585 throw new IllegalArgumentException(Util.bind("element.nullType")); //$NON-NLS-1$
590 setTypeAltered(true);
591 setNameAltered(true);
595 * Sets the state of this field declaration as having
596 * the field type altered from the original document.
598 protected void setTypeAltered(boolean typeAltered) {
599 setMask(MASK_FIELD_TYPE_ALTERED, typeAltered);
602 * @see DOMNode#shareContents(DOMNode)
604 protected void shareContents(DOMNode node) {
605 super.shareContents(node);
606 DOMField field= (DOMField)node;
607 fInitializer= field.fInitializer;
608 fInitializerRange= rangeCopy(field.fInitializerRange);
610 fTypeRange= rangeCopy(field.fTypeRange);
613 * @see IDOMNode#toString()
615 public String toString() {
616 return "FIELD: " + getName(); //$NON-NLS-1$