1
/*******************************************************************************
2
* Copyright (c) 2007, 2010 Wind River Systems, Inc. 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
9
* Markus Schorn - initial API and implementation
10
* Sergey Prigogin (Google)
11
* Jens Elmenthaler - http://bugs.eclipse.org/173458 (camel case completion)
12
*******************************************************************************/
13
package org.eclipse.cdt.internal.core.pdom;
15
import java.util.HashMap;
16
import java.util.HashSet;
19
import java.util.regex.Pattern;
21
import org.eclipse.cdt.core.dom.ast.IASTName;
22
import org.eclipse.cdt.core.dom.ast.IBinding;
23
import org.eclipse.cdt.core.index.IIndexFileLocation;
24
import org.eclipse.cdt.core.index.IIndexLinkage;
25
import org.eclipse.cdt.core.index.IIndexMacro;
26
import org.eclipse.cdt.core.index.IndexFilter;
27
import org.eclipse.cdt.internal.core.index.IIndexFragmentBinding;
28
import org.eclipse.cdt.internal.core.index.IIndexFragmentFile;
29
import org.eclipse.cdt.internal.core.index.IIndexFragmentFileSet;
30
import org.eclipse.cdt.internal.core.index.IIndexFragmentInclude;
31
import org.eclipse.cdt.internal.core.index.IIndexFragmentName;
32
import org.eclipse.cdt.internal.core.index.IIndexScope;
33
import org.eclipse.cdt.internal.core.pdom.PDOM.ChangeEvent;
34
import org.eclipse.cdt.internal.core.pdom.PDOM.DebugLockInfo;
35
import org.eclipse.cdt.internal.core.pdom.PDOM.IListener;
36
import org.eclipse.cdt.internal.core.pdom.dom.PDOMLinkage;
37
import org.eclipse.core.runtime.CoreException;
38
import org.eclipse.core.runtime.IProgressMonitor;
41
* The PDOMProxy is returned by the PDOMManager before the indexer kicks in. Also and more
42
* importantly it is returned when the indexer has been shut down (clients may not be aware
43
* of this yet). Doing that prevents the creation of empty pdoms for deleted projects.
45
public class PDOMProxy implements IPDOM {
46
private PDOM fDelegate;
47
private int fReadLockCount;
48
private Set<IListener> fListeners= new HashSet<IListener>();
49
private Map<Thread, DebugLockInfo> fLockDebugging;
52
if (PDOM.sDEBUG_LOCKS) {
53
fLockDebugging= new HashMap<Thread, DebugLockInfo>();
56
public synchronized void acquireReadLock() throws InterruptedException {
57
if (fDelegate != null) {
58
fDelegate.acquireReadLock();
61
if (PDOM.sDEBUG_LOCKS) {
62
PDOM.incReadLock(fLockDebugging);
67
public IIndexMacro[] findMacros(char[] name, boolean isPrefix, boolean caseSensitive,
68
IndexFilter filter, IProgressMonitor monitor) throws CoreException {
69
if (fDelegate != null)
70
return fDelegate.findMacros(name, isPrefix, caseSensitive, filter, monitor);
71
return IIndexMacro.EMPTY_INDEX_MACRO_ARRAY;
74
public synchronized IIndexFragmentBinding adaptBinding(IBinding binding) throws CoreException {
75
if (fDelegate != null)
76
return fDelegate.adaptBinding(binding);
80
public synchronized IIndexFragmentBinding findBinding(IASTName astName) throws CoreException {
81
if (fDelegate != null)
82
return fDelegate.findBinding(astName);
86
public synchronized IIndexFragmentBinding[] findBindings(char[][] names, IndexFilter filter,
87
IProgressMonitor monitor) throws CoreException {
88
if (fDelegate != null)
89
return fDelegate.findBindings(names, filter, monitor);
91
return IIndexFragmentBinding.EMPTY_INDEX_BINDING_ARRAY;
94
public synchronized IIndexFragmentBinding[] findBindings(Pattern[] patterns, boolean isFullyQualified,
95
IndexFilter filter, IProgressMonitor monitor) throws CoreException {
96
if (fDelegate != null)
97
return fDelegate.findBindings(patterns, isFullyQualified, filter, monitor);
99
return IIndexFragmentBinding.EMPTY_INDEX_BINDING_ARRAY;
102
public synchronized IIndexFragmentBinding[] findBindings(char[] name, boolean filescope,
103
IndexFilter filter, IProgressMonitor monitor) throws CoreException {
104
if (fDelegate != null)
105
return fDelegate.findBindings(name, filescope, filter, monitor);
107
return IIndexFragmentBinding.EMPTY_INDEX_BINDING_ARRAY;
110
public synchronized IIndexFragmentBinding[] findBindingsForPrefix(char[] prefix, boolean filescope,
111
IndexFilter filter, IProgressMonitor monitor) throws CoreException {
112
if (fDelegate != null)
113
return fDelegate.findBindingsForPrefix(prefix, filescope, filter, monitor);
115
return IIndexFragmentBinding.EMPTY_INDEX_BINDING_ARRAY;
118
public synchronized IIndexFragmentBinding[] findBindingsForContentAssist(char[] prefix, boolean filescope,
119
IndexFilter filter, IProgressMonitor monitor) throws CoreException {
120
if (fDelegate != null)
121
return fDelegate.findBindingsForContentAssist(prefix, filescope, filter, monitor);
123
return IIndexFragmentBinding.EMPTY_INDEX_BINDING_ARRAY;
126
public synchronized IIndexFragmentInclude[] findIncludedBy(IIndexFragmentFile file) throws CoreException {
127
if (fDelegate != null)
128
return fDelegate.findIncludedBy(file);
130
return new IIndexFragmentInclude[0];
133
public synchronized IIndexFragmentName[] findNames(IBinding binding, int flags)
134
throws CoreException {
135
if (fDelegate != null)
136
return fDelegate.findNames(binding, flags);
138
return IIndexFragmentName.EMPTY_NAME_ARRAY;
141
public synchronized long getCacheHits() {
142
if (fDelegate != null)
143
return fDelegate.getCacheHits();
148
public synchronized long getCacheMisses() {
149
if (fDelegate != null)
150
return fDelegate.getCacheMisses();
155
public synchronized IIndexFragmentFile getFile(int linkageID, IIndexFileLocation location) throws CoreException {
156
if (fDelegate != null)
157
return fDelegate.getFile(linkageID, location);
162
public synchronized IIndexFragmentFile[] getFiles(IIndexFileLocation location) throws CoreException {
163
if (fDelegate != null)
164
return fDelegate.getFiles(location);
166
return IIndexFragmentFile.EMPTY_ARRAY;
169
public synchronized long getLastWriteAccess() {
170
if (fDelegate != null)
171
return fDelegate.getLastWriteAccess();
176
public synchronized IIndexLinkage[] getLinkages() {
177
if (fDelegate != null)
178
return fDelegate.getLinkages();
180
return new IIndexLinkage[0];
183
public synchronized String getProperty(String propertyName) throws CoreException {
184
if (fDelegate != null)
185
return fDelegate.getProperty(propertyName);
190
public synchronized void releaseReadLock() {
191
// read-locks not forwarded to delegate need to be released here
192
if (fReadLockCount > 0) {
194
if (PDOM.sDEBUG_LOCKS)
195
PDOM.decReadLock(fLockDebugging);
196
} else if (fDelegate != null) {
197
fDelegate.releaseReadLock();
201
public boolean hasWaitingReaders() {
202
return fDelegate != null && fDelegate.hasWaitingReaders();
205
public synchronized void resetCacheCounters() {
206
if (fDelegate != null)
207
fDelegate.resetCacheCounters();
210
@SuppressWarnings({ "rawtypes", "unchecked" })
211
public synchronized Object getAdapter(Class adapter) {
212
if (adapter.isAssignableFrom(PDOMProxy.class)) {
218
public synchronized void addListener(IListener listener) {
219
if (fDelegate != null) {
220
fDelegate.addListener(listener);
222
fListeners.add(listener);
226
public synchronized PDOMLinkage[] getLinkageImpls() {
227
if (fDelegate != null)
228
return fDelegate.getLinkageImpls();
230
return new PDOMLinkage[0];
233
public synchronized void removeListener(IListener listener) {
234
if (fDelegate != null) {
235
fDelegate.removeListener(listener);
237
fListeners.remove(listener);
241
public synchronized void setDelegate(WritablePDOM pdom) {
244
while (fReadLockCount > 0) {
245
pdom.acquireReadLock();
248
if (PDOM.sDEBUG_LOCKS) {
249
pdom.adjustThreadForReadLock(fLockDebugging);
251
} catch (InterruptedException e) {
252
Thread.currentThread().interrupt();
254
for (IListener listener : fListeners) {
255
pdom.addListener(listener);
257
ChangeEvent event= new ChangeEvent();
259
for (IListener listener : fListeners) {
260
listener.handleChange(fDelegate, event);
264
public IIndexFragmentFileSet createFileSet() {
265
return new PDOMFileSet();
268
public synchronized IIndexFragmentFile[] getAllFiles() throws CoreException {
269
if (fDelegate != null)
270
return fDelegate.getAllFiles();
271
return IIndexFragmentFile.EMPTY_ARRAY;
275
* @see org.eclipse.cdt.internal.core.index.IIndexFragment#findMacroContainers(java.util.regex.Pattern, org.eclipse.cdt.core.index.IndexFilter, org.eclipse.core.runtime.IProgressMonitor)
277
public synchronized IIndexFragmentBinding[] findMacroContainers(Pattern pattern, IndexFilter filter,
278
IProgressMonitor monitor) throws CoreException {
279
if (fDelegate != null) {
280
return fDelegate.findMacroContainers(pattern, filter, monitor);
282
return IIndexFragmentBinding.EMPTY_INDEX_BINDING_ARRAY;
285
public Object getCachedResult(Object key) {
289
public Object putCachedResult(Object key, Object value, boolean replace) {
292
public void clearResultCache() {
293
if (fDelegate != null)
294
fDelegate.clearResultCache();
297
* @see org.eclipse.cdt.internal.core.index.IIndexFragment#getInlineNamespaces()
299
public IIndexScope[] getInlineNamespaces() throws CoreException {
300
if (fDelegate != null)
301
return fDelegate.getInlineNamespaces();
303
return IIndexScope.EMPTY_INDEX_SCOPE_ARRAY;