Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bc6c2e28fe1a2235a8ba5739c6076b6656d87be8 (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
/*******************************************************************************
 * Copyright (c) 2004 QNX Software Systems and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     QNX Software Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.core.browser.typehierarchy;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.cdt.core.ICLogConstants;
import org.eclipse.cdt.core.browser.AllTypesCache;
import org.eclipse.cdt.core.browser.ITypeInfo;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ElementChangedEvent;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICElementDelta;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.IElementChangedListener;
import org.eclipse.cdt.core.parser.ast.ASTAccessVisibility;
import org.eclipse.cdt.core.search.ICSearchScope;
import org.eclipse.cdt.internal.core.model.Util;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.Platform;

public class TypeHierarchy implements ITypeHierarchy, IElementChangedListener {

	public static boolean DEBUG = false;
    
	private static final class TypeEntry {
		ITypeInfo type;
		ASTAccessVisibility access;
		TypeEntry(ITypeInfo type, ASTAccessVisibility access) {
			this.type = type;
			this.access = access;
		}
	}
	private static final int INITIAL_SUPER_TYPES = 1;
	private static final int INITIAL_SUB_TYPES = 1;
	private static final ITypeInfo[] NO_TYPES = new ITypeInfo[0];
	private ArrayList fRootTypes = new ArrayList();
	private Map fTypeToSuperTypes = new HashMap();
	private Map fTypeToSubTypes = new HashMap();

	private ITypeInfo fFocusType;
	
	/**
	 * The progress monitor to report work completed too.
	 */
	protected IProgressMonitor fProgressMonitor = null;
	/**
	 * Change listeners - null if no one is listening.
	 */
	protected ArrayList fChangeListeners = null;
	
	/*
	 * A map from Openables to ArrayLists of ITypes
	 */
	public Map files = null;
	
	/**
	 * Whether this hierarchy should contains subtypes.
	 */
	protected boolean fComputeSubtypes;

	/**
	 * The scope this hierarchy should restrain itsef in.
	 */
	ICSearchScope fScope;
	
	/*
	 * Whether this hierarchy needs refresh
	 */
	public boolean fNeedsRefresh = true;
//	/*
//	 * Collects changes to types
//	 */
//	protected ChangeCollector fChangeCollector;
	
	
	
	/**
	 * Creates a TypeHierarchy on the given type.
	 */
	public TypeHierarchy(ITypeInfo type) {
		fFocusType = type;
	}
	
	/**
	 * Adds the type to the collection of root classes
	 * if the classes is not already present in the collection.
	 */
	public void addRootType(ITypeInfo type) {
		if (!fRootTypes.contains(type)) {
			fRootTypes.add(type);
		}
	}
	
	/**
	 * Adds the given supertype to the type.
	 */
	public void addSuperType(ITypeInfo type, ITypeInfo superType, ASTAccessVisibility access) {
		Collection superEntries = (Collection) fTypeToSuperTypes.get(type);
		if (superEntries == null) {
		    superEntries = new ArrayList(INITIAL_SUPER_TYPES);
			fTypeToSuperTypes.put(type, superEntries);
		}
		Collection subTypes = (Collection) fTypeToSubTypes.get(superType);
		if (subTypes == null) {
			subTypes = new ArrayList(INITIAL_SUB_TYPES);
			fTypeToSubTypes.put(superType, subTypes);
		}
		if (!subTypes.contains(type)) {
			subTypes.add(type);
		}
		for (Iterator i = superEntries.iterator(); i.hasNext(); ) {
			TypeEntry entry = (TypeEntry)i.next();
			if (entry.type.equals(superType)) {
			    // update the access
			    entry.access = access;
				return;	// don't add if already exists
			}
		}
		TypeEntry typeEntry = new TypeEntry(superType, access);
		superEntries.add(typeEntry);
	}
	
	/**
	 * Adds the given subtype to the type.
	 */
	protected void addSubType(ITypeInfo type, ITypeInfo subType) {
		Collection subTypes = (Collection) fTypeToSubTypes.get(type);
		if (subTypes == null) {
			subTypes = new ArrayList(INITIAL_SUB_TYPES);
			fTypeToSubTypes.put(type, subTypes);
		}
		if (!subTypes.contains(subType)) {
			subTypes.add(subType);
		}

		Collection superEntries = (Collection) fTypeToSuperTypes.get(subType);
		if (superEntries == null) {
		    superEntries = new ArrayList(INITIAL_SUPER_TYPES);
			fTypeToSuperTypes.put(subType, superEntries);
		}
		for (Iterator i = superEntries.iterator(); i.hasNext(); ) {
			TypeEntry entry = (TypeEntry)i.next();
			if (entry.type.equals(type))
				return;	// don't add if already exists
		}
		// default to private access
		TypeEntry typeEntry = new TypeEntry(type, ASTAccessVisibility.PRIVATE);
		superEntries.add(typeEntry);
	}

	/**
	 * Returns true if type already has the given supertype.
	 */
	public boolean hasSuperType(ITypeInfo type, ITypeInfo superType) {
		Collection entries = (Collection) fTypeToSuperTypes.get(type);
		if (entries != null) {
			for (Iterator i = entries.iterator(); i.hasNext(); ) {
				TypeEntry entry = (TypeEntry)i.next();
				if (entry.type.equals(superType))
					return true;
			}
		}
		return false;
	}

	/**
	 * Returns an array of supertypes for the given type - will never return null.
	 */
	public ITypeInfo[] getSuperTypes(ITypeInfo type) {
		Collection entries = (Collection) fTypeToSuperTypes.get(type);
		if (entries != null) {
			ArrayList superTypes = new ArrayList(INITIAL_SUPER_TYPES);
			for (Iterator i = entries.iterator(); i.hasNext(); ) {
				TypeEntry entry = (TypeEntry)i.next();
				superTypes.add(entry.type);
			}
			return (ITypeInfo[])superTypes.toArray(new ITypeInfo[superTypes.size()]);
		}
		return NO_TYPES;
	}
	
	/**
	 * Returns an array of subtypes for the given type - will never return null.
	 */
	public ITypeInfo[] getSubTypes(ITypeInfo type) {
		Collection subTypes = (Collection) fTypeToSubTypes.get(type);
		if (subTypes != null) {
			return (ITypeInfo[])subTypes.toArray(new ITypeInfo[subTypes.size()]);
		}
		return NO_TYPES;
	}

    /* (non-Javadoc)
     * @see org.eclipse.cdt.core.browser.typehierarchy.ITypeHierarchy#addTypeHierarchyChangedListener(org.eclipse.cdt.core.browser.typehierarchy.ITypeHierarchyChangedListener)
     */
    public void addTypeHierarchyChangedListener(ITypeHierarchyChangedListener listener) {
    	ArrayList listeners = fChangeListeners;
    	if (listeners == null) {
    		fChangeListeners = listeners = new ArrayList();
    	}
    	
    	// register with JavaCore to get Java element delta on first listener added
    	if (listeners.size() == 0) {
    		CoreModel.getDefault().addElementChangedListener(this);
    	}
    	
    	// add listener only if it is not already present
    	if (listeners.indexOf(listener) == -1) {
    		listeners.add(listener);
    	}
    }


    /**
     * @see ITypeHierarchy
     */
    public synchronized void removeTypeHierarchyChangedListener(ITypeHierarchyChangedListener listener) {
    	ArrayList listeners = fChangeListeners;
    	if (listeners == null) {
    		return;
    	}
    	listeners.remove(listener);

    	// deregister from JavaCore on last listener removed
    	if (listeners.isEmpty()) {
    		CoreModel.getDefault().removeElementChangedListener(this);
    	}
    }
    
    /**
     * Determines if the change effects this hierarchy, and fires
     * change notification if required.
     */
    public void elementChanged(ElementChangedEvent event) {
    	// type hierarchy change has already been fired
    	if (fNeedsRefresh) return;
    	
    	if (isAffected(event.getDelta())) {
    		fNeedsRefresh = true;
    		fireChange();
    	}
    }
    
    /**
     * Returns true if the given delta could change this type hierarchy
     */
    public synchronized boolean isAffected(ICElementDelta delta) {
//    	ICElement element= delta.getElement();
//    	switch (element.getElementType()) {
//    		case ICElement.C_MODEL:
//    			return isAffectedByCModel(delta, element);
//    		case ICElement.C_PROJECT:
//    			return isAffectedByCProject(delta, element);
//    		case ICElement.C_UNIT:
//    			return isAffectedByOpenable(delta, element);
//    	}
//    	return false;
    	return true;
    }
    
    /**
     * Notifies listeners that this hierarchy has changed and needs
     * refreshing. Note that listeners can be removed as we iterate
     * through the list.
     */
    public void fireChange() {
    	ArrayList listeners = fChangeListeners;
    	if (listeners == null) {
    		return;
    	}
    	if (DEBUG) {
    		System.out.println("FIRING hierarchy change ["+Thread.currentThread()+"]"); //$NON-NLS-1$ //$NON-NLS-2$
    		if (fFocusType != null) {
    			System.out.println("    for hierarchy focused on " + fFocusType.toString()); //$NON-NLS-1$
    		}
    	}
    	// clone so that a listener cannot have a side-effect on this list when being notified
    	listeners = (ArrayList)listeners.clone();
    	for (int i= 0; i < listeners.size(); i++) {
    		final ITypeHierarchyChangedListener listener= (ITypeHierarchyChangedListener)listeners.get(i);
    		Platform.run(new ISafeRunnable() {
    			public void handleException(Throwable exception) {
    				Util.log(exception, "Exception occurred in listener of Type hierarchy change notification", ICLogConstants.CDT); //$NON-NLS-1$
    			}
    			public void run() throws Exception {
    				listener.typeHierarchyChanged(TypeHierarchy.this);
    			}
    		});
    	}
    }

    /* (non-Javadoc)
     * @see org.eclipse.cdt.core.browser.typehierarchy.ITypeHierarchy#contains(org.eclipse.cdt.core.model.ICElement)
     */
    public boolean contains(ICElement type) {
    	// classes
	    ITypeInfo info = AllTypesCache.getTypeForElement(type, true, true, null);
        
	    if (info == null)
	        return false;
        
    	if (fTypeToSuperTypes.get(info) != null) {
    		return true;
    	}

    	// root classes
    	if (fRootTypes.contains(type)) return true;

        return false;
    }
    
    /**
     * @see ITypeHierarchy
     */
    public boolean exists() {
    	if (!fNeedsRefresh) return true;
    	
    	return (fFocusType == null || fFocusType.exists()) && cProject().exists();
    }
    
    /**
     * Returns the C project this hierarchy was created in.
     */
    public ICProject cProject() {
        IProject project = fFocusType.getCache().getProject();
        return findCProject(project);
    }
   	private ICProject findCProject(IProject project) {
		try {
			ICProject[] cProjects = CoreModel.getDefault().getCModel().getCProjects();
			if (cProjects != null) {
				for (int i = 0; i < cProjects.length; ++i) {
					ICProject cProject = cProjects[i];
					if (project.equals(cProjects[i].getProject()))
						return cProject;
				}
			}
		} catch (CModelException e) {
		}
		return null;
	}

    /* (non-Javadoc)
     * @see org.eclipse.cdt.core.browser.typehierarchy.ITypeHierarchy#getAllClasses()
     */
    public ICElement[] getAllClasses() {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see org.eclipse.cdt.core.browser.typehierarchy.ITypeHierarchy#getRootClasses()
     */
    public ICElement[] getRootClasses() {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see org.eclipse.cdt.core.browser.typehierarchy.ITypeHierarchy#getSubtypes(org.eclipse.cdt.core.model.ICElement)
     */
    public ICElement[] getSubtypes(ICElement type) {
	    List list = new ArrayList();
	    ITypeInfo info = AllTypesCache.getTypeForElement(type, true, true, null);
		Collection entries = (Collection) fTypeToSubTypes.get(info);
		if (entries != null) {
			for (Iterator i = entries.iterator(); i.hasNext(); ) {
				ITypeInfo subType = (ITypeInfo)i.next();
				ICElement elem = AllTypesCache.getElementForType(subType, true, true, null);
				if (elem != null) {
				    list.add(elem);
				}
			}
		}
		return (ICElement[])list.toArray(new ICElement[list.size()]);
    }

    /* (non-Javadoc)
     * @see org.eclipse.cdt.core.browser.typehierarchy.ITypeHierarchy#getAllSuperclasses(org.eclipse.cdt.core.model.ICElement)
     */
    public ICElement[] getAllSubtypes(ICElement type) {
	    List list = new ArrayList();
	    ITypeInfo info = AllTypesCache.getTypeForElement(type, true, true, null);
	    addSubs(info, list);
	    //convert list to ICElements
	    ICElement[] elems = new ICElement[list.size()];
	    int count = 0;
	    for (Iterator i = list.iterator(); i.hasNext(); ) {
	        ITypeInfo subType = (ITypeInfo) i.next();
	        elems[count++] = AllTypesCache.getElementForType(subType, true, true, null);
	    }
	    return elems;
    }

    private void addSubs(ITypeInfo type, List list) {
		Collection entries = (Collection) fTypeToSubTypes.get(type);
		if (entries != null) {
			for (Iterator i = entries.iterator(); i.hasNext(); ) {
				ITypeInfo subType = (ITypeInfo)i.next();
				if (!list.contains(subType)) {
				    list.add(subType);
				}
			    addSubs(subType, list);
			}
		}
    }

    /* (non-Javadoc)
     * @see org.eclipse.cdt.core.browser.typehierarchy.ITypeHierarchy#getSupertypes(org.eclipse.cdt.core.model.ICElement)
     */
    public ICElement[] getSupertypes(ICElement type) {
	    List list = new ArrayList();
	    ITypeInfo info = AllTypesCache.getTypeForElement(type, true, true, null);
		Collection entries = (Collection) fTypeToSuperTypes.get(info);
		if (entries != null) {
			for (Iterator i = entries.iterator(); i.hasNext(); ) {
				TypeEntry entry = (TypeEntry)i.next();
				ITypeInfo superType = entry.type;
				ICElement elem = AllTypesCache.getElementForType(superType, true, true, null);
				if (elem != null) {
				    list.add(elem);
				}
			}
		}
		return (ICElement[])list.toArray(new ICElement[list.size()]);
    }

    /* (non-Javadoc)
     * @see org.eclipse.cdt.core.browser.typehierarchy.ITypeHierarchy#getAllSuperclasses(org.eclipse.cdt.core.model.ICElement)
     */
    public ICElement[] getAllSupertypes(ICElement type) {
	    List list = new ArrayList();
	    ITypeInfo info = AllTypesCache.getTypeForElement(type, true, true, null);
	    addSupers(info, list);
	    //convert list to ICElements
	    ICElement[] elems = new ICElement[list.size()];
	    int count = 0;
	    for (Iterator i = list.iterator(); i.hasNext(); ) {
	        ITypeInfo superType = (ITypeInfo) i.next();
	        elems[count++] = AllTypesCache.getElementForType(superType, true, true, null);
	    }
	    return elems;
    }

    private void addSupers(ITypeInfo type, List list) {
		Collection entries = (Collection) fTypeToSuperTypes.get(type);
		if (entries != null) {
			for (Iterator i = entries.iterator(); i.hasNext(); ) {
				TypeEntry entry = (TypeEntry)i.next();
				ITypeInfo superType = entry.type;
				if (!list.contains(superType)) {
				    list.add(superType);
				}
			    addSupers(superType, list);
			}
		}
    }

    /* (non-Javadoc)
     * @see org.eclipse.cdt.core.browser.typehierarchy.ITypeHierarchy#getType()
     */
    public ICElement getType() {
        if (fFocusType != null)
            return AllTypesCache.getElementForType(fFocusType, true, true, null);
        return null;
    }

    /**
     * @see ITypeHierarchy
     * TODO (jerome) should use a PerThreadObject to build the hierarchy instead of synchronizing
     * (see also isAffected(IJavaElementDelta))
     */
    public synchronized void refresh(IProgressMonitor monitor) throws CModelException {
    	try {
    		fProgressMonitor = monitor;
    		if (monitor != null) {
    			if (fFocusType != null) {
    				monitor.beginTask(TypeHierarchyMessages.getFormattedString("hierarchy.creatingOnType", fFocusType.getQualifiedTypeName().getFullyQualifiedName()), 100); //$NON-NLS-1$
    			} else {
    				monitor.beginTask(TypeHierarchyMessages.getString("hierarchy.creating"), 100); //$NON-NLS-1$
    			}
    		}
    		long start = -1;
    		if (DEBUG) {
    			start = System.currentTimeMillis();
    			if (fComputeSubtypes) {
    				System.out.println("CREATING TYPE HIERARCHY [" + Thread.currentThread() + "]"); //$NON-NLS-1$ //$NON-NLS-2$
    			} else {
    				System.out.println("CREATING SUPER TYPE HIERARCHY [" + Thread.currentThread() + "]"); //$NON-NLS-1$ //$NON-NLS-2$
    			}
    			if (fFocusType != null) {
    				System.out.println("  on type " + fFocusType.toString()); //$NON-NLS-1$
    			}
    		}

    		compute();
//    		initializeRegions();
    		fNeedsRefresh = false;
//    		fChangeCollector = null;

    		if (DEBUG) {
    			if (fComputeSubtypes) {
    				System.out.println("CREATED TYPE HIERARCHY in " + (System.currentTimeMillis() - start) + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
    			} else {
    				System.out.println("CREATED SUPER TYPE HIERARCHY in " + (System.currentTimeMillis() - start) + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
    			}
    			System.out.println(this.toString());
    		}
    	} finally {
    		if (monitor != null) {
    			monitor.done();
    		}
    		fProgressMonitor = null;
    	}
    }

    /**
     * Compute this type hierarchy.
     */
    protected void compute() {
    	if (fFocusType != null) {
//    		HierarchyBuilder builder = 
//    			new IndexBasedHierarchyBuilder(
//    				this, 
//    				this.scope);
//    		builder.build(this.computeSubtypes);
    	    
//			initialize(1);
//			buildSupertypes();
    	    
    	} // else a RegionBasedTypeHierarchy should be used
    }
    
    /**
     * Initializes this hierarchy's internal tables with the given size.
     */
 /*   protected void initialize(int size) {
    	if (size < 10) {
    		size = 10;
    	}
    	int smallSize = (size / 2);
    	this.classToSuperclass = new HashMap(size);
    	this.interfaces = new ArrayList(smallSize);
    	this.missingTypes = new ArrayList(smallSize);
    	this.rootClasses = new TypeVector();
    	this.typeToSubtypes = new HashMap(smallSize);
    	this.typeToSuperInterfaces = new HashMap(smallSize);
    	this.typeFlags = new HashMap(smallSize);
    	
    	this.projectRegion = new Region();
    	this.packageRegion = new Region();
    	this.files = new HashMap(5);
    }
*/
    
    /* (non-Javadoc)
     * @see org.eclipse.cdt.core.browser.typehierarchy.ITypeHierarchy#store(java.io.OutputStream, org.eclipse.core.runtime.IProgressMonitor)
     */
    public void store(OutputStream outputStream, IProgressMonitor monitor) throws CModelException {
        // TODO Auto-generated method stub
        
    }

}

Back to the top