This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 125060
Collapse All | Expand All

(-)src/org/netbeans/modules/editor/java/ExcludeCompletion.java (+145 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of either the GNU
7
 * General Public License Version 2 only ("GPL") or the Common
8
 * Development and Distribution License("CDDL") (collectively, the
9
 * "License"). You may not use this file except in compliance with the
10
 * License. You can obtain a copy of the License at
11
 * http://www.netbeans.org/cddl-gplv2.html
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
 * specific language governing permissions and limitations under the
14
 * License.  When distributing the software, include this License Header
15
 * Notice in each file and include the License file at
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
17
 * particular file as subject to the "Classpath" exception as provided
18
 * by Sun in the GPL Version 2 section of the License file that
19
 * accompanied this code. If applicable, add the following below the
20
 * License Header, with the fields enclosed by brackets [] replaced by
21
 * your own identifying information:
22
 * "Portions Copyrighted [year] [name of copyright owner]"
23
 *
24
 * If you wish your version of this file to be governed by only the CDDL
25
 * or only the GPL Version 2, indicate your decision by adding
26
 * "[Contributor] elects to include this software in this distribution
27
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
28
 * single choice of license, a recipient has the option to distribute
29
 * your version of this file under either the CDDL, the GPL Version 2 or
30
 * to extend the choice of license to its licensees as provided above.
31
 * However, if you add GPL Version 2 code and therefore, elected the GPL
32
 * Version 2 license, then the option applies only if the new code is
33
 * made subject to such option by the copyright holder.
34
 *
35
 * Contributor(s):
36
 *
37
 * Samuel Halliday
38
 *
39
 * Portions Copyrighted 2009 Sun Microsystems, Inc.
40
 */
41
package org.netbeans.modules.editor.java;
42
43
import java.util.ArrayList;
44
import java.util.Collection;
45
import java.util.prefs.PreferenceChangeEvent;
46
import java.util.prefs.PreferenceChangeListener;
47
import java.util.prefs.Preferences;
48
import org.netbeans.api.editor.mimelookup.MimeLookup;
49
50
/**
51
 * A whitelist/blacklist of excluded classes and packages for the Java completer.
52
 * Requested in RFE #125060.
53
 *
54
 * @author Samuel Halliday
55
 */
56
public final class ExcludeCompletion implements PreferenceChangeListener {
57
58
	// TODO: move to CodeCompletionPanel
59
	public static final String JAVA_COMPLETION_WHITELIST = "javaCompletionWhitelist"; //NOI18N
60
	public static final String JAVA_COMPLETION_BLACKLIST = "javaCompletionBlacklist"; //NOI18N
61
	private final Collection<String> exclude = new ArrayList<String>();
62
	private final Collection<String> include = new ArrayList<String>();
63
	private static volatile ExcludeCompletion singleton = null;
64
65
	/**
66
	 * Cheap operation to return an instance
67
	 *
68
	 * @return
69
	 */
70
	public static ExcludeCompletion getInstance() {
71
		// lazy check without lock
72
		if (singleton == null)
73
			synchronized (ExcludeCompletion.class) {
74
				if (singleton == null) {
75
					singleton = new ExcludeCompletion();
76
					// is there not a way to just register to listen to Java Completion changes?
77
					// the preferences tree does not appear to be very deep
78
					Preferences preferences =
79
							MimeLookup.getLookup(JavaKit.JAVA_MIME_TYPE).lookup(Preferences.class);
80
					// ?? why is this commented "weak" code used in Utilities?
81
					//		WeakListeners.create(PreferenceChangeListener.class, this, preferences);
82
					//		preferences.addPreferenceChangeListener(null);
83
					preferences.addPreferenceChangeListener(singleton);
84
					// initial population
85
					preferences.put(JAVA_COMPLETION_BLACKLIST, "sun,apple");
86
					preferences.put(JAVA_COMPLETION_WHITELIST, "sun.security");
87
				}
88
			}
89
		assert singleton != null;
90
		return singleton;
91
	}
92
93
	private ExcludeCompletion() {
94
	}
95
96
	/**
97
	 * @param fqn Fully Qualified Name
98
	 * @return
99
	 */
100
	public boolean isExcluded(String fqn) {
101
		//#122334: do not propose imports from the default package
102
		if (fqn == null || fqn.length() == 0)
103
			return true;
104
105
		if (include.size() > 0)
106
			for (String entry : include) {
107
				if (entry.length() > fqn.length())
108
					continue;
109
				if (fqn.startsWith(entry))
110
					return false;
111
			}
112
113
		if (exclude.size() > 0)
114
			for (String entry : exclude) {
115
				if (entry.length() > fqn.length())
116
					continue;
117
				if (fqn.startsWith(entry))
118
					return true;
119
			}
120
121
		return false;
122
	}
123
124
	public void preferenceChange(PreferenceChangeEvent evt) {
125
		if (evt == null)
126
			return;
127
		String key = evt.getKey();
128
		if (JAVA_COMPLETION_BLACKLIST.equals(key))
129
			update(exclude, evt.getNewValue());
130
		else if (JAVA_COMPLETION_WHITELIST.equals(key))
131
			update(include, evt.getNewValue());
132
	}
133
134
	private void update(Collection<String> existing, String updated) {
135
		// ??: how thread safe does this class need to be with regards to preferences?
136
		existing.clear();
137
		if (updated == null || updated.length() == 0)
138
			return;
139
		String[] entries = updated.split(","); //NOI18N
140
		for (String entry : entries) {
141
			if (entry != null && entry.length() > 0)
142
				existing.add(entry);
143
		}
144
	}
145
}
(-)src/org/netbeans/modules/editor/java/JavaCompletionProvider.java (-2 / +9 lines)
Lines 2728-2737 Link Here
2728
        private void addPackages(Env env, String fqnPrefix, boolean inPkgStmt) {
2728
        private void addPackages(Env env, String fqnPrefix, boolean inPkgStmt) {
2729
            if (fqnPrefix == null)
2729
            if (fqnPrefix == null)
2730
                fqnPrefix = EMPTY;
2730
                fqnPrefix = EMPTY;
2731
            for (String pkgName : env.getController().getClasspathInfo().getClassIndex().getPackageNames(fqnPrefix, true,EnumSet.allOf(ClassIndex.SearchScope.class)))
2731
			ExcludeCompletion excluder = ExcludeCompletion.getInstance();
2732
                if (pkgName.length() > 0)
2732
            for (String pkgName : env.getController().getClasspathInfo().getClassIndex().getPackageNames(fqnPrefix, true,EnumSet.allOf(ClassIndex.SearchScope.class))){
2733
				if (excluder.isExcluded(pkgName))
2734
					continue;
2733
                    results.add(JavaCompletionItem.createPackageItem(pkgName, anchorOffset, inPkgStmt));
2735
                    results.add(JavaCompletionItem.createPackageItem(pkgName, anchorOffset, inPkgStmt));
2734
        }
2736
        }
2737
        }
2735
        
2738
        
2736
        private void addTypes(Env env, EnumSet<ElementKind> kinds, DeclaredType baseType, Set<? extends Element> toExclude, boolean insideNew) throws IOException {
2739
        private void addTypes(Env env, EnumSet<ElementKind> kinds, DeclaredType baseType, Set<? extends Element> toExclude, boolean insideNew) throws IOException {
2737
            if (queryType == COMPLETION_ALL_QUERY_TYPE) {
2740
            if (queryType == COMPLETION_ALL_QUERY_TYPE) {
Lines 2807-2813 Link Here
2807
            ClassIndex.NameKind kind = env.isCamelCasePrefix() ?
2810
            ClassIndex.NameKind kind = env.isCamelCasePrefix() ?
2808
                Utilities.isCaseSensitive() ? ClassIndex.NameKind.CAMEL_CASE : ClassIndex.NameKind.CAMEL_CASE_INSENSITIVE :
2811
                Utilities.isCaseSensitive() ? ClassIndex.NameKind.CAMEL_CASE : ClassIndex.NameKind.CAMEL_CASE_INSENSITIVE :
2809
                Utilities.isCaseSensitive() ? ClassIndex.NameKind.PREFIX : ClassIndex.NameKind.CASE_INSENSITIVE_PREFIX;
2812
                Utilities.isCaseSensitive() ? ClassIndex.NameKind.PREFIX : ClassIndex.NameKind.CASE_INSENSITIVE_PREFIX;
2813
			ExcludeCompletion excluder = ExcludeCompletion.getInstance();
2810
            for(ElementHandle<TypeElement> name : controller.getClasspathInfo().getClassIndex().getDeclaredTypes(prefix != null ? prefix : EMPTY, kind, EnumSet.allOf(ClassIndex.SearchScope.class))) {
2814
            for(ElementHandle<TypeElement> name : controller.getClasspathInfo().getClassIndex().getDeclaredTypes(prefix != null ? prefix : EMPTY, kind, EnumSet.allOf(ClassIndex.SearchScope.class))) {
2815
				String fqn = name.getQualifiedName();
2816
				if (excluder.isExcluded(fqn))
2817
					continue;
2811
                LazyTypeCompletionItem item = LazyTypeCompletionItem.create(name, kinds, anchorOffset, controller.getSnapshot().getSource(), insideNew);
2818
                LazyTypeCompletionItem item = LazyTypeCompletionItem.create(name, kinds, anchorOffset, controller.getSnapshot().getSource(), insideNew);
2812
                if (item.isAnnonInner())
2819
                if (item.isAnnonInner())
2813
                    continue;
2820
                    continue;
(-)src/org/netbeans/modules/java/editor/imports/ComputeImports.java (-3 / +5 lines)
Lines 72-77 Link Here
72
import org.netbeans.api.java.source.ClassIndex.NameKind;
72
import org.netbeans.api.java.source.ClassIndex.NameKind;
73
import org.netbeans.api.java.source.ElementHandle;
73
import org.netbeans.api.java.source.ElementHandle;
74
import org.netbeans.api.java.source.support.CancellableTreePathScanner;
74
import org.netbeans.api.java.source.support.CancellableTreePathScanner;
75
import org.netbeans.modules.editor.java.ExcludeCompletion;
75
import org.netbeans.modules.java.editor.javadoc.JavadocImports;
76
import org.netbeans.modules.java.editor.javadoc.JavadocImports;
76
import org.openide.util.Union2;
77
import org.openide.util.Union2;
77
78
Lines 127-132 Link Here
127
        
128
        
128
        unresolvedNames.addAll(JavadocImports.computeUnresolvedImports(info));
129
        unresolvedNames.addAll(JavadocImports.computeUnresolvedImports(info));
129
        
130
        
131
		ExcludeCompletion excluder = ExcludeCompletion.getInstance();
130
        for (String unresolved : unresolvedNames) {
132
        for (String unresolved : unresolvedNames) {
131
            if (isCancelled())
133
            if (isCancelled())
132
                return null;
134
                return null;
Lines 145-155 Link Here
145
                    continue;
147
                    continue;
146
                }
148
                }
147
                
149
                
148
                //#122334: do not propose imports from the default package:
150
				String fqn = info.getElements().getPackageOf(te).getQualifiedName().toString();
149
                if (info.getElements().getPackageOf(te).getQualifiedName().length() != 0) {
151
				if (excluder.isExcluded(fqn))
152
					continue;
150
                    classes.add(te);
153
                    classes.add(te);
151
                }
154
                }
152
            }
153
            Collections.sort(classes, new Comparator<TypeElement>() {
155
            Collections.sort(classes, new Comparator<TypeElement>() {
154
                public int compare(TypeElement te1, TypeElement te2) {
156
                public int compare(TypeElement te1, TypeElement te2) {
155
                    return (te1 == te2) ? 0 : te1.getQualifiedName().toString().compareTo(te2.getQualifiedName().toString());
157
                    return (te1 == te2) ? 0 : te1.getQualifiedName().toString().compareTo(te2.getQualifiedName().toString());
(-)src/org/netbeans/modules/java/editor/imports/JavaFixAllImports.java (-1 / +2 lines)
Lines 321-327 Link Here
321
321
322
        private ImportVisitor (CompilationInfo info) {
322
        private ImportVisitor (CompilationInfo info) {
323
            this.info = info;
323
            this.info = info;
324
            currentPackage = info.getCompilationUnit().getPackageName().toString();
324
			ExpressionTree pkg = info.getCompilationUnit().getPackageName();
325
            currentPackage = pkg != null ? pkg.toString() : "";
325
            imports = new ArrayList<TreePathHandle>();
326
            imports = new ArrayList<TreePathHandle>();
326
        }
327
        }
327
328
(-)src/org/netbeans/modules/java/editor/javadoc/JavadocCompletionQuery.java (-2 / +10 lines)
Lines 94-99 Link Here
94
import org.netbeans.api.java.source.TreeUtilities;
94
import org.netbeans.api.java.source.TreeUtilities;
95
import org.netbeans.api.lexer.Token;
95
import org.netbeans.api.lexer.Token;
96
import org.netbeans.api.lexer.TokenSequence;
96
import org.netbeans.api.lexer.TokenSequence;
97
import org.netbeans.modules.editor.java.ExcludeCompletion;
97
import org.netbeans.modules.editor.java.JavaCompletionItem;
98
import org.netbeans.modules.editor.java.JavaCompletionItem;
98
import org.netbeans.modules.editor.java.LazyTypeCompletionItem;
99
import org.netbeans.modules.editor.java.LazyTypeCompletionItem;
99
import org.netbeans.modules.editor.java.Utilities;
100
import org.netbeans.modules.editor.java.Utilities;
Lines 602-611 Link Here
602
            }
603
            }
603
        }
604
        }
604
        
605
        
605
        for (String pkgName : jdctx.javac.getClasspathInfo().getClassIndex().getPackageNames(pkgPrefix, true, EnumSet.allOf(ClassIndex.SearchScope.class)))
606
		ExcludeCompletion excluder = ExcludeCompletion.getInstance();
606
            if (pkgName.length() > 0)
607
        for (String pkgName : jdctx.javac.getClasspathInfo().getClassIndex().getPackageNames(pkgPrefix, true, EnumSet.allOf(ClassIndex.SearchScope.class))){
608
			if (excluder.isExcluded(pkgName))
609
				continue;
607
                items.add(JavaCompletionItem.createPackageItem(pkgName, substitutionOffset, false));
610
                items.add(JavaCompletionItem.createPackageItem(pkgName, substitutionOffset, false));
608
    }
611
    }
612
    }
609
    
613
    
610
    private void completeThrowsOrPkg(String fqn, String prefix, int substitutionOffset, JavadocContext jdctx) {
614
    private void completeThrowsOrPkg(String fqn, String prefix, int substitutionOffset, JavadocContext jdctx) {
611
        final Elements elements = jdctx.javac.getElements();
615
        final Elements elements = jdctx.javac.getElements();
Lines 1006-1012 Link Here
1006
//        ClassIndex.NameKind kind = env.isCamelCasePrefix() ?
1010
//        ClassIndex.NameKind kind = env.isCamelCasePrefix() ?
1007
//            Utilities.isCaseSensitive() ? ClassIndex.NameKind.CAMEL_CASE : ClassIndex.NameKind.CAMEL_CASE_INSENSITIVE :
1011
//            Utilities.isCaseSensitive() ? ClassIndex.NameKind.CAMEL_CASE : ClassIndex.NameKind.CAMEL_CASE_INSENSITIVE :
1008
//            Utilities.isCaseSensitive() ? ClassIndex.NameKind.PREFIX : ClassIndex.NameKind.CASE_INSENSITIVE_PREFIX;
1012
//            Utilities.isCaseSensitive() ? ClassIndex.NameKind.PREFIX : ClassIndex.NameKind.CASE_INSENSITIVE_PREFIX;
1013
		ExcludeCompletion excluder = ExcludeCompletion.getInstance();
1009
        for(ElementHandle<TypeElement> name : controller.getClasspathInfo().getClassIndex().getDeclaredTypes(prefix, kind, EnumSet.allOf(ClassIndex.SearchScope.class))) {
1014
        for(ElementHandle<TypeElement> name : controller.getClasspathInfo().getClassIndex().getDeclaredTypes(prefix, kind, EnumSet.allOf(ClassIndex.SearchScope.class))) {
1015
			String fqn = name.getQualifiedName();
1016
			if (excluder.isExcluded(fqn))
1017
				continue;
1010
            LazyTypeCompletionItem item = LazyTypeCompletionItem.create(name, kinds, substitutionOffset, controller.getSnapshot().getSource(), false);
1018
            LazyTypeCompletionItem item = LazyTypeCompletionItem.create(name, kinds, substitutionOffset, controller.getSnapshot().getSource(), false);
1011
            // XXX item.isAnnonInner() is package private :-(
1019
            // XXX item.isAnnonInner() is package private :-(
1012
//            if (item.isAnnonInner())
1020
//            if (item.isAnnonInner())

Return to bug 125060