Skip to main content
summaryrefslogtreecommitdiffstats
blob: bc883d4625a7cfb95aa3c325a315ce0294d185fb (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
package org.eclipse.jdt.internal.core;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;

import org.eclipse.jdt.core.*;
import org.eclipse.jdt.core.jdom.*;

import java.util.Vector;

/**
 * Root of Java element handle hierarchy.
 *
 * @see IJavaElement
 */
public abstract class JavaElement extends PlatformObject implements IJavaElement {

	public static final char JEM_JAVAPROJECT= '=';
	public static final char JEM_PACKAGEFRAGMENTROOT= Path.SEPARATOR;
	public static final char JEM_PACKAGEFRAGMENT= '<';
	public static final char JEM_FIELD= '^';
	public static final char JEM_METHOD= '~';
	public static final char JEM_INITIALIZER= '|';
	public static final char JEM_COMPILATIONUNIT= '{';
	public static final char JEM_CLASSFILE= '(';
	public static final char JEM_TYPE= '[';
	public static final char JEM_PACKAGEDECLARATION= '%';
	public static final char JEM_IMPORTDECLARATION= '#';

	/**
	 * A count to uniquely identify this element in the case
	 * that a duplicate named element exists. For example, if
	 * there are two fields in a compilation unit with the
	 * same name, the occurrence count is used to distinguish
	 * them.  The occurrence count starts at 1 (i.e. the first 
	 * occurrence is occurrence 1, not occurrence 0).
	 */
	protected int fOccurrenceCount = 1;


	/**
	 * This element's type - one of the constants defined
	 * in IJavaLanguageElementTypes.
	 */
	protected int fLEType = 0;

	/**
	 * This element's parent, or <code>null</code> if this
	 * element does not have a parent.
	 */
	protected IJavaElement fParent;

	/**
	 * This element's name, or an empty <code>String</code> if this
	 * element does not have a name.
	 */
	protected String fName;

	/**
	 * Direct access to the Java Model Manager
	 */
	protected static JavaModelManager fgJavaModelManager = JavaModelManager.getJavaModelManager();
	
/**
 * Constructs a handle for a java element of the specified type, with
 * the given parent element and name.
 *
 * @param type - one of the constants defined in IJavaLanguageElement
 *
 * @exception IllegalArgumentException if the type is not one of the valid
 *		Java element type constants
 *
 */
protected JavaElement(int type, IJavaElement parent, String name) throws IllegalArgumentException {
	if (type < JAVA_MODEL || type > IMPORT_DECLARATION) {
		throw new IllegalArgumentException(Util.bind("element.invalidType")); //$NON-NLS-1$
	}
	fLEType= type;
	fParent= parent;
	fName= name;
}
/**
 * @see IOpenable
 */
public void close() throws JavaModelException {
	Object info = fgJavaModelManager.peekAtInfo(this);
	if (info != null) {
		if (this instanceof IParent) {
			IJavaElement[] children = ((JavaElementInfo) info).getChildren();
			for (int i = 0, size = children.length; i < size; ++i) {
				JavaElement child = (JavaElement) children[i];
				child.close();
			}
		}
		closing(info);
		fgJavaModelManager.removeInfo(this);
	}
}
/**
 * This element is being closed.  Do any necessary cleanup.
 */
protected void closing(Object info) throws JavaModelException {
}
/**
 * Returns true if this handle represents the same Java element
 * as the given handle. By default, two handles represent the same
 * element if they are identical or if they represent the same type
 * of element, have equal names, parents, and occurrence counts.
 *
 * <p>If a subclass has other requirements for equality, this method
 * must be overridden.
 *
 * @see Object#equals
 */
public boolean equals(Object o) {
	if (this == o)
		return true;
	if (o instanceof JavaElement) {
		JavaElement other = (JavaElement) o;
		if (fLEType != other.fLEType)
			return false;
		return fName.equals(other.fName) &&
				fParent.equals(other.fParent) &&
				fOccurrenceCount == other.fOccurrenceCount;
	}
	return false;
}
/**
 * Returns true if this <code>JavaElement</code> is equivalent to the given
 * <code>IDOMNode</code>.
 */
protected boolean equalsDOMNode(IDOMNode node) throws JavaModelException {
	return false;
}
/**
 * @see IJavaElement
 */
public boolean exists() {
	try {
		getRawInfo();
		return true;
	} catch (JavaModelException e) {
		return false;
	}
}
/**
 * Returns the <code>IDOMNode</code> that corresponds to this <code>JavaElement</code>
 * or <code>null</code> if there is no corresponding node.
 */
public IDOMNode findNode(IDOMCompilationUnit dom) {
	int type = getElementType();
	if (type == IJavaElement.COMPILATION_UNIT || 
		type == IJavaElement.FIELD || 
		type == IJavaElement.IMPORT_DECLARATION || 
		type == IJavaElement.INITIALIZER || 
		type == IJavaElement.METHOD || 
		type == IJavaElement.PACKAGE_DECLARATION || 
		type == IJavaElement.TYPE) {
		Vector path = new Vector();
		IJavaElement element = this;
		while (element != null && element.getElementType() != IJavaElement.COMPILATION_UNIT) {
			if (element.getElementType() != IJavaElement.IMPORT_CONTAINER) {
				// the DOM does not have import containers, so skip them
				path.insertElementAt(element, 0);
			}
			element = element.getParent();
		}
		if (path.size() == 0) {
			try {
				if (equalsDOMNode(dom)) {
					return dom;
				} else {
					return null;
				}
			} catch(JavaModelException e) {
				return null;
			}
		}
		return ((JavaElement) path.elementAt(0)).followPath(path, 0, dom.getFirstChild());
	} else {
		return null;
	}
}
/**
 */
protected IDOMNode followPath(Vector path, int position, IDOMNode node) {

	try {
		if (equalsDOMNode(node)) {
			if (position == (path.size() - 1)) {
				return node;
			} else {
				if (node.getFirstChild() != null) {
					position++;
					return ((JavaElement)path.elementAt(position)).followPath(path, position, node.getFirstChild());
				} else {
					return null;
				}
			}
		} else if (node.getNextNode() != null) {
			return followPath(path, position, node.getNextNode());
		} else {
			return null;
		}
	} catch (JavaModelException e) {
		return null;
	}

}
/**
 * @see IParent 
 */
public IJavaElement[] getChildren() throws JavaModelException {
	return getElementInfo().getChildren();
}
/**
 * Returns a collection of (immediate) children of this node of the
 * specified type.
 *
 * @param type - one of constants defined by IJavaLanguageElementTypes
 */
public Vector getChildrenOfType(int type) throws JavaModelException {
	IJavaElement[] children = getChildren();
	int size = children.length;
	Vector v = new Vector(size);
	for (int i = 0; i < size; ++i) {
		JavaElement elt = (JavaElement)children[i];
		if (elt.getElementType() == type) {
			v.addElement(elt);
		}
	}
	return v;
}
/**
 * @see IMember
 */
public IClassFile getClassFile() {
	return null;
}
/**
 * @see IMember
 */
public ICompilationUnit getCompilationUnit() {
	return null;
}
/**
 * Returns the info for this handle.  
 * If this element is not already open, it and all of its parents are opened.
 * Does not return null.
 *
 * @exception JavaModelException if the element is not present or not accessible
 */
public JavaElementInfo getElementInfo() throws JavaModelException {
	synchronized(fgJavaModelManager){
		Object info = fgJavaModelManager.getInfo(this);
		if (info == null) {
			openHierarchy();
			info= fgJavaModelManager.getInfo(this);
			if (info == null) {
				throw newNotPresentException();
			}
		}
		return (JavaElementInfo)info;
	}
}
/**
 * @see IAdaptable
 */
public String getElementName() {
	return fName;
}
/**
 * @see IJavaElement
 */
public int getElementType() {
	return fLEType;
}
/**
 * @see IJavaElement
 */
public String getHandleIdentifier() {
	return getHandleMemento();
}
/**
 * @see JavaElement#getHandleMemento()
 */
public String getHandleMemento(){
	StringBuffer buff= new StringBuffer(((JavaElement)getParent()).getHandleMemento());
	buff.append(getHandleMementoDelimiter());
	buff.append(getElementName());
	return buff.toString();
}
/**
 * Returns the <code>char</code> that marks the start of this handles
 * contribution to a memento.
 */
protected abstract char getHandleMementoDelimiter();
/**
 * @see IJavaElement
 */
public IJavaModel getJavaModel() {
	return getParent().getJavaModel();
}
/**
 * Returns the JavaModelManager
 */
public JavaModelManager getJavaModelManager() {
	return fgJavaModelManager;
}
/**
 * @see IJavaElement
 */
public IJavaProject getJavaProject() {
	return getParent().getJavaProject();
}
/**
 * Returns the occurrence count of the handle.
 */
protected int getOccurrenceCount() {
	return fOccurrenceCount;
}
/**
 * Return the first instance of IOpenable in the parent
 * hierarchy of this element.
 *
 * <p>Subclasses that are not IOpenable's must override this method.
 */
public IOpenable getOpenableParent() {
	
	return (IOpenable)fParent;
}
/**
 * @see IJavaElement
 */
public IJavaElement getParent() {
	return fParent;
}
/**
 * Returns the info for this handle.  
 * If this element is not already open, it and all of its parents are opened.
 * Does not return null.
 *
 * @exception JavaModelException if the element is not present or not accessible
 */
public Object getRawInfo() throws JavaModelException {
	Object info = fgJavaModelManager.getInfo(this);
	if (info == null) {
		openHierarchy();
		info= fgJavaModelManager.getInfo(this);
		if (info == null) {
			throw newNotPresentException();
		}
	}
	return info;
}
/**
 * Returns the element that is located at the given source position
 * in this element.  This is a helper method for <code>ICompilationUnit#getElementAt</code>,
 * and only works on compilation units and types. The position given is
 * known to be within this element's source range already, and if no finer
 * grained element is found at the position, this element is returned.
 */
protected IJavaElement getSourceElementAt(int position) throws JavaModelException {
	if (this instanceof ISourceReference) {
		IJavaElement[] children = getChildren();
		int i;
		for (i = 0; i < children.length; i++) {
			IJavaElement aChild = children[i];
			if (aChild instanceof SourceRefElement) {
				SourceRefElement child = (SourceRefElement) children[i];
				ISourceRange range = child.getSourceRange();
				if (position < range.getOffset() + range.getLength() && position >= range.getOffset()) {
					if (child.getElementType() == TYPE) {
						return child.getSourceElementAt(position);
					} else {
						return child;
					}
				}
			}
		}
	} else {
		// should not happen
		Assert.isTrue(false);
	}
	return this;
}
/**
 * Returns the SourceMapper facility for this element, or
 * <code>null</code> if this element does not have a
 * SourceMapper.
 */
public SourceMapper getSourceMapper() {
	return ((JavaElement)getParent()).getSourceMapper();
}
public abstract IResource getUnderlyingResource() throws JavaModelException;
/**
 * Returns the workspace associated with this object.
 */
public IWorkspace getWorkspace() {
	return getJavaModel().getWorkspace();
}
/**
 * Returns the hash code for this Java element. By default,
 * the hash code for an element is a combination of its name
 * and parent's hash code. Elements with other requirements must
 * override this method.
 */
public int hashCode() {
	return Util.combineHashCodes(fName.hashCode(), fParent.hashCode());
}
/**
 * Returns true if this element is an ancestor of the given element,
 * otherwise false.
 */
protected boolean isAncestorOf(IJavaElement e) {
	IJavaElement parent= e.getParent();
	while (parent != null && !parent.equals(this)) {
		parent= parent.getParent();
	}
	return parent != null;
}
/**
 * @see IJavaElement
 */
public boolean isReadOnly() {
	return false;
}
/**
 * @see IJavaElement
 */
public boolean isStructureKnown() throws JavaModelException {
	return getElementInfo().isStructureKnown();
}
/**
 * Creates and returns and not present exception for this element.
 */
protected JavaModelException newNotPresentException() {
	return new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, this));
}
/**
 * Default is to not do any source indices updates.
 */
public void offsetSourceEndAndChildren(int amount, IJavaElement child) {
	
}
/**
 * Default behaviour is not to change the source range
 * for the Java element
 */
public void offsetSourceRange(int amount) {
}
/**
 * Opens this element and all parents that are not already open.
 *
 * @exception JavaModelException this element is not present or accessible
 */
protected void openHierarchy() throws JavaModelException {
	if (this instanceof IOpenable) {
		((Openable) this).openWhenClosed(null);
	} else {
		Openable openableParent = (Openable)getOpenableParent();
		if (openableParent != null) {
			JavaElementInfo openableParentInfo = (JavaElementInfo) fgJavaModelManager.getInfo((IJavaElement) openableParent);
			if (openableParentInfo == null) {
				openableParent.openWhenClosed(null);
			} else {
				throw newNotPresentException();
			}
		}
	}
}
/**
 * This element has just been opened.  Do any necessary setup.
 */
protected void opening(Object info) {
}
/**
 */
public String readableName() {

	return this.getElementName();
}
/**
 * Removes all cached info from the Java Model, including all children,
 * but does not close this element.
 */
protected void removeInfo() {
	Object info = fgJavaModelManager.peekAtInfo(this);
	if (info != null) {
		if (this instanceof IParent) {
			IJavaElement[] children = ((JavaElementInfo)info).getChildren();
			for (int i = 0, size = children.length; i < size; ++i) {
				JavaElement child = (JavaElement) children[i];
				child.removeInfo();
			}
		}
		fgJavaModelManager.removeInfo(this);
	}
}
/**
 * Runs a Java Model Operation
 */
protected void runOperation(JavaModelOperation operation, IProgressMonitor monitor) throws JavaModelException {
	fgJavaModelManager.runOperation(operation, monitor);
}
/**
 * Sets the occurrence count of the handle.
 */
protected void setOccurrenceCount(int count) {
	fOccurrenceCount = count;
}
protected String tabString(int tab) {
	StringBuffer buffer = new StringBuffer();
	for (int i = tab; i > 0; i--)
		buffer.append("  "); //$NON-NLS-1$
	return buffer.toString();
}
/**
 * @private Debugging purposes
 */
protected String toDebugString() {
	StringBuffer buffer = new StringBuffer();
	Object info = fgJavaModelManager.getInfo(this);
	this.toStringInfo(0, buffer, info);
	return buffer.toString();
}
/**
 * @private Debugging purposes
 */
public String toString() {
	StringBuffer buffer = new StringBuffer();
	toString(0, buffer);
	return buffer.toString();
}
/**
 * @private Debugging purposes
 */
protected void toString(int tab, StringBuffer buffer) {
	buffer.append(this.tabString(tab));
	Object info = fgJavaModelManager.getInfo(this);
	this.toStringInfo(tab, buffer, info);
	if (tab == 0) {
		this.toStringAncestors(buffer);
	}
	this.toStringChildren(tab, buffer, info);
}
/**
 * @private Debugging purposes
 */
protected void toStringAncestors(StringBuffer buffer) {
	JavaElement parent = (JavaElement)this.getParent();
	if (parent != null) {
		buffer.append(" [in "); //$NON-NLS-1$
		Object parentInfo = fgJavaModelManager.getInfo(parent);
		parent.toStringInfo(0, buffer, parentInfo);
		parent.toStringAncestors(buffer);
		buffer.append("]"); //$NON-NLS-1$
	}
}
/**
 * @private Debugging purposes
 */
protected void toStringChildren(int tab, StringBuffer buffer, Object info) {
	if (info == null || !(info instanceof JavaElementInfo)) return;
	IJavaElement[] children = ((JavaElementInfo)info).getChildren();
	for (int i = 0; i < children.length; i++) {
		buffer.append("\n"); //$NON-NLS-1$
		((JavaElement)children[i]).toString(tab + 1, buffer);
	}
}
/**
 * @private Debugging purposes
 */
protected void toStringInfo(int tab, StringBuffer buffer, Object info) {
	buffer.append(getElementName());
	if (info == null) {
		buffer.append(" (not open)"); //$NON-NLS-1$
	}
}
/**
 * Updates the source end position for this element.
 * Default behaviour is to do nothing.
 */
public void triggerSourceEndOffset(int amount, int nameStart, int nameEnd) {
}
/**
 * Updates the source positions for this element.
 * Default behaviour is to do nothing.
 */
public void triggerSourceRangeOffset(int amount, int nameStart, int nameEnd) {

}
}

Back to the top