Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 257993c9bde9665e9b1eb5451500a7e4158b7eda (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.internal.core.jdom;

import java.util.Enumeration;

import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.core.jdom.*;
import org.eclipse.jdt.internal.core.util.Messages;
import org.eclipse.jdt.internal.core.util.CharArrayBuffer;
/**
 * DOMField provides an implementation of IDOMField.
 *
 * @see IDOMField
 * @see DOMNode
 * @deprecated The JDOM was made obsolete by the addition in 2.0 of the more
 * powerful, fine-grained DOM/AST API found in the 
 * org.eclipse.jdt.core.dom package.
 */
class DOMField extends DOMMember implements IDOMField {
	
	/**
	 * Contains the type of the field when the type
	 * has been altered from the contents in the
	 * document, otherwise <code>null</code>.
	 */
	protected String fType;

	/**
	 * The original inclusive source range of the
	 * field's type in the document.
	 */
	protected int[] fTypeRange;
		
	/**
	 * The contents of the initializer when the
	 * initializer has been altered from the
	 * original state in the document, otherwise
	 * <code>null</code>.
	 */
	protected String fInitializer;

	/**
	 * The original inclusive source range of the
	 * initializer in the document.
	 */
	protected int[] fInitializerRange;

/**
 * Constructs an empty field node.
 */
DOMField() {
	// Constructs an empty field node
}
/**
 * Creates a new detailed FIELD document fragment on the given range of the document.
 *
 * @param document - the document containing this node's original contents
 * @param sourceRange - a two element array of integers describing the
 *		entire inclusive source range of this node within its document.
 * 		Contents start on and include the character at the first position.
 *		Contents end on and include the character at the last position.
 *		An array of -1's indicates this node's contents do not exist
 *		in the document.
 * @param name - the identifier portion of the name of this field,
 * 		corresponding to VariableDeclaratorId (JLS 8.3).
 * @param nameRange - a two element array of integers describing the
 *		entire inclusive source range of this node's name within its document,
 *		including any array qualifiers that might follow the name.
 * @param commentRange - a two element array describing the comments that precede
 *		the member declaration. The first matches the start of this node's
 *		sourceRange, and the second is the new-line or first non-whitespace
 *		character following the last comment. If no comments are present,
 *		this array contains two -1's.
 * @param flags - an integer representing the modifiers for this member. The
 *		integer can be analyzed with org.eclipse.jdt.core.Flags
 * @param modifierRange - a two element array describing the location of
 *		modifiers for this member within its source range. The first integer
 *		is the first character of the first modifier for this member, and
 *		the second integer is the last whitespace character preceeding the
 *		next part of this member declaration. If there are no modifiers present
 *		in this node's source code (that is, package default visibility), this array
 *		contains two -1's.
 * @param typeRange - a two element array describing the location of the
 *		typeName in the document - the positions of the first and last characters
 *		of the typeName.
 * @param type - the type of the field, in normalized form, as defined in
 *      Type in Field Declaration (JLS 8.3)
 * @param hasInitializer - true if this field declaration includes an initializer,
 *		otherwise false
 * @param initRange - a two element array describing the location of the initializer
 *		in the declaration. The first integer is the position of the character
 *		following the equals sign, and the position of the last character is the last
 *		in the initializer. If this field has no initializer, this array contains
 *		two -1's.
 * @param isVariableDeclarator - true if the field is a seconday variable declarator
 * 		for a previous field declaration.
 */
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) {
	super(document, sourceRange, name, nameRange, commentRange, flags, modifierRange);

	fType= type;
	fTypeRange= typeRange;
	setHasInitializer(hasInitializer);
	fInitializerRange= initRange;
	setIsVariableDeclarator(isVariableDeclarator);
	setMask(MASK_DETAILED_SOURCE_INDEXES, true);

}
/**
 * Creates a new simple FIELD document fragment on the given range of the document.
 *
 * @param document - the document containing this node's original contents
 * @param sourceRange - a two element array of integers describing the
 *		entire inclusive source range of this node within its document.
 * 		Contents start on and include the character at the first position.
 *		Contents end on and include the character at the last position.
 *		An array of -1's indicates this node's contents do not exist
 *		in the document.
 * @param name - the identifier portion of the name of this field,
 * 		corresponding to VariableDeclaratorId (JLS 8.3).
 * @param nameRange - a two element array of integers describing the
 *		entire inclusive source range of this node's name within its document,
 *		including any array qualifiers that might follow the name.
 * @param flags - an integer representing the modifiers for this member. The
 *		integer can be analyzed with org.eclipse.jdt.core.Flags
 * @param type - the type of the field, in normalized form, as defined in
 *      Type in Field Declaration (JLS 8.3)
 * @param isVariableDeclarator - true if the field is a seconday variable declarator
 * 		for a previous field declaration.
 */
DOMField(char[] document, int[] sourceRange, String name, int[] nameRange, int flags, String type, boolean isVariableDeclarator) {
	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);
	setMask(MASK_DETAILED_SOURCE_INDEXES, false);
}
/**
 * Appends this member's body contents to the given CharArrayBuffer.
 * Body contents include the member body and any trailing whitespace.
 *
 * <p>A field does not have a body.
 *
 * @see DOMMember#appendMemberBodyContents(CharArrayBuffer)
 */
protected void appendMemberBodyContents(CharArrayBuffer buffer) {
	// nothing to do
}
/**
 * @see DOMMember#appendMemberDeclarationContents(CharArrayBuffer)
 */
protected void appendMemberDeclarationContents(CharArrayBuffer buffer) {

	if (isVariableDeclarator()) {
		buffer.append(fDocument, fSourceRange[0], fNameRange[0] - fSourceRange[0]);
	} else {
		buffer
			.append(getTypeContents())
			.append(fDocument, fTypeRange[1] + 1, fNameRange[0] - fTypeRange[1] - 1);
	}
	
	buffer.append(getNameContents());
	if (hasInitializer()) {
		if (fInitializerRange[0] < 0) {
			buffer
				.append('=')
				.append(fInitializer)
				.append(fDocument, fNameRange[1] + 1, fSourceRange[1] - fNameRange[1]);
		} else {
			buffer
				.append(fDocument, fNameRange[1] + 1, fInitializerRange[0] - fNameRange[1] - 1)
				.append(getInitializer())
				.append(fDocument, fInitializerRange[1] + 1, fSourceRange[1] - fInitializerRange[1]);
		}
	} else {
		if (fInitializerRange[0] < 0) {
			buffer.append(fDocument, fNameRange[1] + 1, fSourceRange[1] - fNameRange[1]);
		} else {
			buffer.append(fDocument, fInitializerRange[1] + 1, fSourceRange[1] - fInitializerRange[1]);
		}
	}

}
/**
 * Appends this member's header contents to the given CharArrayBuffer.
 * Header contents include any preceding comments and modifiers.
 *
 * <p>If this field is a secondary variable declarator, there is no header.
 *
 * @see DOMMember#appendMemberHeaderFragment(CharArrayBuffer)
 */
protected void appendMemberHeaderFragment(CharArrayBuffer buffer) {

	if (isVariableDeclarator()) {
		return;
	} else {
		super.appendMemberHeaderFragment(buffer);
	}

}
/**
 * @see DOMMember#appendSimpleContents(CharArrayBuffer)
 */
protected void appendSimpleContents(CharArrayBuffer buffer) {
	// append eveything before my name
	buffer.append(fDocument, fSourceRange[0], fNameRange[0] - fSourceRange[0]);
	// append my name
	buffer.append(fName);
	// append everything after my name
	buffer.append(fDocument, fNameRange[1] + 1, fSourceRange[1] - fNameRange[1]);
}
/**
 * Generates detailed source indexes for this node if possible.
 *
 * @exception DOMException if unable to generate detailed source indexes
 * 	for this node
 */
protected void becomeDetailed() throws DOMException {
	if (!isDetailed()) {
		if (isVariableDeclarator() || hasMultipleVariableDeclarators()) {
			DOMNode first = getFirstFieldDeclaration();
			DOMNode last = getLastFieldDeclaration();
			DOMNode node= first;
			String source= first.getContents();
			while (node != last) {
				node= node.fNextNode;
				source+=node.getContents();
			}
			DOMBuilder builder = new DOMBuilder();
			IDOMField[] details= builder.createFields(source.toCharArray());
			if (details.length == 0) {
				throw new DOMException(Messages.dom_cannotDetail); 
			} else {
				node= this;
				for (int i= 0; i < details.length; i++) {
					node.shareContents((DOMNode)details[i]);
					node= node.fNextNode;
				}
			}
		} else {
			super.becomeDetailed();
		}

	}
}
/**
 * @see IDOMNode#clone()
 */
public Object clone() {
	if (isVariableDeclarator() || hasMultipleVariableDeclarators()) {
		return getFactory().createField(new String(getSingleVariableDeclaratorContents()));
	} else {
		return super.clone();
	}
}
/**
 * Expands all variable declarators in this field declaration into
 * stand-alone field declarations. 
 */
protected void expand() {
	if (isVariableDeclarator() || hasMultipleVariableDeclarators()) {
		Enumeration siblings= new SiblingEnumeration(getFirstFieldDeclaration());
		DOMField field= (DOMField)siblings.nextElement();
		DOMNode next= field.fNextNode;
		while (siblings.hasMoreElements() && (next instanceof DOMField) && (((DOMField)next).isVariableDeclarator())) {
			field.localizeContents();
			if (field.fParent != null) {
				field.fParent.fragment();
			}
			field= (DOMField)siblings.nextElement();
			next= field.fNextNode;
		}
		field.localizeContents();
	}
}
/**
 * @see DOMNode#getDetailedNode()
 */
protected DOMNode getDetailedNode() {
	if (isVariableDeclarator() || hasMultipleVariableDeclarators()) {
		return (DOMNode)getFactory().createField(new String(getSingleVariableDeclaratorContents()));
	} else {
		return (DOMNode)getFactory().createField(getContents());
	}
}
/**
 * Returns the first field document fragment that defines
 * the type for this variable declarator.
 */
protected DOMField getFirstFieldDeclaration() {
	if (isVariableDeclarator()) {
		return ((DOMField)fPreviousNode).getFirstFieldDeclaration();
	} else {
		return this;
	}
}
/**
 * @see IDOMField#getInitializer()
 */
public String getInitializer() {
	becomeDetailed();
	if (hasInitializer()) {
		if (fInitializer != null) {
			return fInitializer;
		} else {
			return new String(fDocument, fInitializerRange[0], fInitializerRange[1] + 1 - fInitializerRange[0]);
		}
	} else {
		return null;
	}
}
/**
 * @see IDOMNode#getJavaElement
 */
public IJavaElement getJavaElement(IJavaElement parent) throws IllegalArgumentException {
	if (parent.getElementType() == IJavaElement.TYPE) {
		return ((IType)parent).getField(getName());
	} else {
		throw new IllegalArgumentException(Messages.element_illegalParent); 
	}
}
/**
 * Returns the last field document fragment in this muli-declarator statement.
 */
protected DOMField getLastFieldDeclaration() {
	DOMField field = this;
	while (field.isVariableDeclarator() || field.hasMultipleVariableDeclarators()) {
		if (field.fNextNode instanceof DOMField && ((DOMField)field.fNextNode).isVariableDeclarator()) {
			field= (DOMField)field.fNextNode;
		} else {
			break;
		}
	}
	return field;
}
/**
 * @see DOMMember#getMemberDeclarationStartPosition()
 */
protected int getMemberDeclarationStartPosition() {
	return fTypeRange[0];
}
/**
 * @see IDOMNode#getNodeType()
 */
public int getNodeType() {
	return IDOMNode.FIELD;
}
/**
 * Returns a String representing this field declaration as a field
 * declaration with one variable declarator.
 */
protected char[] getSingleVariableDeclaratorContents() {


	CharArrayBuffer buffer= new CharArrayBuffer();
	DOMField first= getFirstFieldDeclaration();
	if (first.isDetailed()) {
		first.appendMemberHeaderFragment(buffer);
		buffer.append(getType());
		if (isVariableDeclarator()) {
			buffer.append(' ');
		} else {
			buffer.append(fDocument, fTypeRange[1] + 1, fNameRange[0] - fTypeRange[1] - 1);
		}
	} else {
		buffer.append(first.fDocument, first.fSourceRange[0], first.fNameRange[0] - first.fSourceRange[0]);
	}
	
	buffer.append(getName());
	if (hasInitializer()) {
		if (fInitializerRange[0] < 0) {
			buffer
				.append('=')
				.append(fInitializer)
				.append(';')
				.append(org.eclipse.jdt.internal.compiler.util.Util.LINE_SEPARATOR);
		} else {
			buffer
				.append(fDocument, fNameRange[1] + 1, fInitializerRange[0] - fNameRange[1] - 1)
				.append(getInitializer())
				.append(';')
				.append(org.eclipse.jdt.internal.compiler.util.Util.LINE_SEPARATOR);
		}
	} else {
		buffer.append(';').append(org.eclipse.jdt.internal.compiler.util.Util.LINE_SEPARATOR);
	}
	return buffer.getContents();
}
/**
 * @see IDOMField#getType()
 */
public String getType() {
	return fType;
}
/**
 * Returns the souce code to be used for this
 * field's type.
 */
protected char[] getTypeContents() {
	if (isTypeAltered()) {
		return fType.toCharArray();
	} else {
		return CharOperation.subarray(fDocument, fTypeRange[0], fTypeRange[1] + 1);
	}
}
/**
 * Returns true if this field has an initializer expression,
 * otherwise false.
 */
protected boolean hasInitializer() {
	return getMask(MASK_FIELD_HAS_INITIALIZER);
}
/**
 * Returns true is this field declarations has more than one
 * variable declarator, otherwise false;
 */
protected boolean hasMultipleVariableDeclarators() {
	return fNextNode != null && (fNextNode instanceof DOMField) &&
		((DOMField)fNextNode).isVariableDeclarator();
}
/**
 * Inserts the given un-parented node as a sibling of this node, immediately before
 * this node. Once inserted, the sibling is only dependent on this document fragment.
 *
 * <p>When a sibling is inserted before a variable declarator, it must first
 * be expanded.
 *
 * @see IDOMNode#insertSibling(IDOMNode)
 */
public void insertSibling(IDOMNode sibling) throws IllegalArgumentException, DOMException {
	if (isVariableDeclarator()) {
		expand();
	} 
	super.insertSibling(sibling);
}
/**
 * Returns true if this field's type has been altered
 * from the original document contents.
 */
protected boolean isTypeAltered() {
	return getMask(MASK_FIELD_TYPE_ALTERED);
}
/**
 * Returns true if this field is declared as a secondary variable
 * declarator for a previous field declaration.
 */
protected boolean isVariableDeclarator() {
	return getMask(MASK_FIELD_IS_VARIABLE_DECLARATOR);
}
/**
 * @see DOMNode
 */
protected DOMNode newDOMNode() {
	return new DOMField();
}
/**
 * Normalizes this <code>DOMNode</code>'s end position.
 */
void normalizeEndPosition(ILineStartFinder finder, DOMNode next) {
	if (next == null) {
		// this node's end position includes all of the characters up
		// to the end of the enclosing node
		DOMNode parent = (DOMNode) getParent();
		if (parent == null || parent instanceof DOMCompilationUnit) {
			setSourceRangeEnd(fDocument.length - 1);
		} else {
			// parent is a type
			int temp = ((DOMType)parent).getCloseBodyPosition() - 1;
			setSourceRangeEnd(temp);
			fInsertionPosition = Math.max(finder.getLineStart(temp + 1), getEndPosition());
		}
	} else {
		// this node's end position is just before the start of the next node
		// unless the next node is a field that is declared along with this one
		int temp = next.getStartPosition() - 1;
		fInsertionPosition = Math.max(finder.getLineStart(temp + 1), getEndPosition());
		
		next.normalizeStartPosition(getEndPosition(), finder);
		if (next instanceof DOMField) {
			DOMField field = (DOMField) next;
			if (field.isVariableDeclarator() && fTypeRange[0] == field.fTypeRange[0])
				return;
		}
		setSourceRangeEnd(next.getStartPosition() - 1);
	}
}
/**
 * Normalizes this <code>DOMNode</code>'s start position.
 */
void normalizeStartPosition(int endPosition, ILineStartFinder finder) {
	if (isVariableDeclarator()) {
		// start position is end of last element
		setStartPosition(fPreviousNode.getEndPosition() + 1);
	} else {
		super.normalizeStartPosition(endPosition, finder);
	}
}
/**
 * Offsets all the source indexes in this node by the given amount.
 */
protected void offset(int offset) {
	super.offset(offset);
	offsetRange(fInitializerRange, offset);
	offsetRange(fTypeRange, offset);
}
/**
 * Separates this node from its parent and siblings, maintaining any ties that
 * this node has to the underlying document fragment.
 *
 * <p>When a field with multiple declarators is removed, its declaration
 * must first be expanded.
 *
 * @see IDOMNode#remove()
 */
public void remove() {
	expand();
	super.remove();
}
/**
 * @see IDOMMember#setComment(String)
 */
public void setComment(String comment) {
	expand();
	super.setComment(comment);
}
/**
 * @see IDOMMember#setFlags(int)
 */
public void setFlags(int flags) {
	expand();
	super.setFlags(flags);
}
/**
 * Sets the state of this field declaration as having
 * an initializer expression.
 */
protected void setHasInitializer(boolean hasInitializer) {
	setMask(MASK_FIELD_HAS_INITIALIZER, hasInitializer);
}
/**
 * @see IDOMField#setInitializer(String)
 */
public void setInitializer(String initializer) {
	becomeDetailed();
	fragment();
	setHasInitializer(initializer != null);
	fInitializer= initializer;
}
/**
 * Sets the initializer range.
 */
void setInitializerRange(int start, int end) {
	fInitializerRange[0] = start;
	fInitializerRange[1] = end;
}
/**
 * Sets the state of this field declaration as being a
 * secondary variable declarator for a previous field
 * declaration.
 */
protected void setIsVariableDeclarator(boolean isVariableDeclarator) {
	setMask(MASK_FIELD_IS_VARIABLE_DECLARATOR, isVariableDeclarator);
}
/**
 * @see IDOMField#setName(String)
 */
public void setName(String name) throws IllegalArgumentException {
	if (name == null) {
		throw new IllegalArgumentException(Messages.element_nullName); 
	} else {
		super.setName(name);
		setTypeAltered(true);
	}
}
/**
 * @see IDOMField#setType(String)
 */
public void setType(String typeName) throws IllegalArgumentException {
	if (typeName == null) {
		throw new IllegalArgumentException(Messages.element_nullType); 
	}
	becomeDetailed();
	expand();
	fragment();
	setTypeAltered(true);
	setNameAltered(true);
	fType= typeName;
}
/**
 * Sets the state of this field declaration as having
 * the field type altered from the original document.
 */
protected void setTypeAltered(boolean typeAltered) {
	setMask(MASK_FIELD_TYPE_ALTERED, typeAltered);
}
/**
 * @see DOMNode#shareContents(DOMNode)
 */
protected void shareContents(DOMNode node) {
	super.shareContents(node);
	DOMField field= (DOMField)node;
	fInitializer= field.fInitializer;
	fInitializerRange= rangeCopy(field.fInitializerRange);
	fType= field.fType;
	fTypeRange= rangeCopy(field.fTypeRange);
}
/**
 * @see IDOMNode#toString()
 */
public String toString() {
	return "FIELD: " + getName(); //$NON-NLS-1$
}
}

Back to the top