001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.java.source.usages;
043:
044: import java.io.IOException;
045: import java.lang.ref.WeakReference;
046: import java.util.ArrayList;
047: import java.util.Collections;
048: import java.util.Iterator;
049: import java.util.List;
050: import java.util.Set;
051: import org.netbeans.api.java.source.ClassIndex;
052: import org.netbeans.api.java.source.JavaSource;
053: import org.openide.filesystems.FileObject;
054: import org.openide.util.Utilities;
055:
056: /** Should probably final class with private constructor.
057: *
058: * @author Petr Hrebejk, Tomas Zezula
059: */
060: public abstract class ClassIndexImpl {
061:
062: public final List<WeakReference<ClassIndexImplListener>> listeners = Collections
063: .synchronizedList(new ArrayList<WeakReference<ClassIndexImplListener>>());
064:
065: public static enum UsageType {
066:
067: SUPER_CLASS(0), SUPER_INTERFACE(1), FIELD_REFERENCE(2), METHOD_REFERENCE(
068: 3), TYPE_REFERENCE(4);
069:
070: private int offset;
071:
072: UsageType(final int offset) {
073: this .offset = offset;
074: }
075:
076: int getOffset() {
077: return this .offset;
078: }
079: }
080:
081: public static ClassIndexFactory FACTORY;
082:
083: public abstract <T> void search(final String binaryName,
084: final Set<UsageType> usageType,
085: final ResultConvertor<T> convertor,
086: final Set<? super T> result) throws InterruptedException;
087:
088: public abstract <T> void getDeclaredTypes(String name,
089: ClassIndex.NameKind kind,
090: final ResultConvertor<T> convertor,
091: final Set<? super T> result) throws InterruptedException;
092:
093: public abstract void getPackageNames(String prefix,
094: boolean directOnly, Set<String> result)
095: throws InterruptedException;
096:
097: public abstract FileObject[] getSourceRoots();
098:
099: public abstract BinaryAnalyser getBinaryAnalyser();
100:
101: public abstract SourceAnalyser getSourceAnalyser();
102:
103: public abstract String getSourceName(String binaryName);
104:
105: public abstract void setDirty(JavaSource js);
106:
107: public abstract boolean isSource();
108:
109: protected abstract void close() throws IOException;
110:
111: public void addClassIndexImplListener(
112: final ClassIndexImplListener listener) {
113: assert listener != null;
114: this .listeners.add(new Ref(listener));
115: }
116:
117: public void removeClassIndexImplListener(
118: final ClassIndexImplListener listener) {
119: assert listener != null;
120: synchronized (this .listeners) {
121: for (Iterator<WeakReference<ClassIndexImplListener>> it = this .listeners
122: .iterator(); it.hasNext();) {
123: WeakReference<ClassIndexImplListener> lr = it.next();
124: ClassIndexImplListener l = lr.get();
125: if (listener == l) {
126: it.remove();
127: }
128: }
129: }
130: }
131:
132: public void typesEvent(final ClassIndexImplEvent added,
133: final ClassIndexImplEvent removed,
134: final ClassIndexImplEvent changed) {
135: WeakReference<ClassIndexImplListener>[] _listeners;
136: synchronized (this .listeners) {
137: _listeners = this .listeners
138: .toArray(new WeakReference[this .listeners.size()]);
139: }
140: for (WeakReference<ClassIndexImplListener> lr : _listeners) {
141: ClassIndexImplListener l = lr.get();
142: if (l != null) {
143: if (added != null) {
144: l.typesAdded(added);
145: }
146: if (removed != null) {
147: l.typesRemoved(removed);
148: }
149: if (changed != null) {
150: l.typesChanged(changed);
151: }
152: }
153: }
154: }
155:
156: private class Ref extends WeakReference<ClassIndexImplListener>
157: implements Runnable {
158: public Ref(ClassIndexImplListener listener) {
159: super (listener, Utilities.activeReferenceQueue());
160: }
161:
162: public void run() {
163: listeners.remove(this);
164: }
165: }
166: }
|