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

(-)org/netbeans/modules/editor/java/ExcludeCompletion.java (+142 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.concurrent.locks.ReadWriteLock;
46
import java.util.concurrent.locks.ReentrantReadWriteLock;
47
48
/**
49
 * A whitelist/blacklist of excluded classes and packages for the Java completer.
50
 * Requested in RFE #125060.
51
 *
52
 * @author Samuel Halliday
53
 */
54
final class ExcludeCompletion {
55
56
    private final Collection<String> exclude = new ArrayList<String>();
57
    private final Collection<String> include = new ArrayList<String>();
58
    private final ReadWriteLock lock = new ReentrantReadWriteLock();
59
60
    /** */
61
    public ExcludeCompletion() {
62
    }
63
64
    /**
65
     * @param fqn Fully Qualified Name
66
     * @return
67
     */
68
    public boolean isExcluded(final CharSequence fqn) {
69
        //#122334: do not propose imports from the default package
70
        if (fqn == null || fqn.length() == 0) {
71
            return true;
72
        }
73
74
        String s = fqn.toString();
75
        lock.readLock().lock();
76
        try {
77
            if (!include.isEmpty()) {
78
                for (String entry : include) {
79
                    if (entry.length() > fqn.length()) {
80
                        if (entry.startsWith(s)) {
81
                            return false;
82
                        }
83
                    } else if (s.startsWith(entry)) {
84
                        return false;
85
                    }
86
                }
87
            }
88
89
            if (!exclude.isEmpty()) {
90
                for (String entry : exclude) {
91
                    if (entry.endsWith(".") && fqn.equals(entry.substring(0, entry.length() - 1))) { // NOI18N
92
                        // exclude packages names (no trailing fullstop)
93
                        return true;
94
                    }
95
                    if (entry.length() > fqn.length()) {
96
                        // fqn not long enough to filter yet
97
                        continue;
98
                    }
99
                    if (s.startsWith(entry)) {
100
                        return true;
101
                    }
102
                }
103
            }
104
105
            return false;
106
        } finally {
107
            lock.readLock().unlock();
108
        }
109
    }
110
111
    /**
112
     * @param blacklist comma separated list of fqns
113
     */
114
    public void updateBlacklist(String blacklist) {
115
        update(exclude, blacklist);
116
    }
117
118
    /**
119
     * @param whitelist comma separated list of fqns
120
     */
121
    public void updateWhitelist(String whitelist) {
122
        update(include, whitelist);
123
    }
124
125
    private void update(Collection<String> existing, String updated) {
126
        lock.writeLock().lock();
127
        try {
128
            existing.clear();
129
            if (updated == null || updated.length() == 0) {
130
                return;
131
            }
132
            String[] entries = updated.split(","); //NOI18N
133
            for (String entry : entries) {
134
                if (entry != null && entry.length() != 0) {
135
                    existing.add(entry);
136
                }
137
            }
138
        } finally {
139
            lock.writeLock().unlock();
140
        }
141
    }
142
}
(-)org/netbeans/modules/editor/java/JavaCompletionProvider.java (-5 / +9 lines)
Lines 2422-2431 Link Here
2422
                                    (Utilities.isShowDeprecatedMembers() || !elements.isDeprecated(e)) &&
2422
                                    (Utilities.isShowDeprecatedMembers() || !elements.isDeprecated(e)) &&
2423
                                    tu.isAccessible(scope, e, t);
2423
                                    tu.isAccessible(scope, e, t);
2424
                        case METHOD:
2424
                        case METHOD:
2425
                            return startsWith(env, e.getSimpleName().toString(), prefix) &&
2425
                            String sn = e.getSimpleName().toString();
2426
                            return startsWith(env, sn, prefix) &&
2426
                                    (Utilities.isShowDeprecatedMembers() || !elements.isDeprecated(e)) &&
2427
                                    (Utilities.isShowDeprecatedMembers() || !elements.isDeprecated(e)) &&
2427
                                    (!isStatic || e.getModifiers().contains(STATIC)) &&
2428
                                    (!isStatic || e.getModifiers().contains(STATIC)) &&
2428
                                    tu.isAccessible(scope, e, t);
2429
                                    tu.isAccessible(scope, e, t) &&
2430
                                    !Utilities.isExcluded(Utilities.getElementName(e.getEnclosingElement(), true) + "." + sn); //NOI18N
2429
                    }
2431
                    }
2430
                    return false;
2432
                    return false;
2431
                }
2433
                }
Lines 2606-2616 Link Here
2606
                                    isOfKindAndType(asMemberOf(e, t, types), e, kinds, baseType, scope, trees, types) &&
2608
                                    isOfKindAndType(asMemberOf(e, t, types), e, kinds, baseType, scope, trees, types) &&
2607
                                    tu.isAccessible(scope, e, t);
2609
                                    tu.isAccessible(scope, e, t);
2608
                        case METHOD:
2610
                        case METHOD:
2609
                            return startsWith(env, e.getSimpleName().toString(), prefix) &&
2611
                            String sn = e.getSimpleName().toString();
2612
                            return startsWith(env, sn, prefix) &&
2610
                                    (Utilities.isShowDeprecatedMembers() || !elements.isDeprecated(e)) &&
2613
                                    (Utilities.isShowDeprecatedMembers() || !elements.isDeprecated(e)) &&
2611
                                    isOfKindAndType(((ExecutableType)asMemberOf(e, t, types)).getReturnType(), e, kinds, baseType, scope, trees, types) &&
2614
                                    isOfKindAndType(((ExecutableType)asMemberOf(e, t, types)).getReturnType(), e, kinds, baseType, scope, trees, types) &&
2612
                                    (isSuperCall && e.getModifiers().contains(PROTECTED) || tu.isAccessible(scope, e, isSuperCall && enclType != null ? enclType : t)) &&
2615
                                    (isSuperCall && e.getModifiers().contains(PROTECTED) || tu.isAccessible(scope, e, isSuperCall && enclType != null ? enclType : t)) &&
2613
                                    (!isStatic || e.getModifiers().contains(STATIC));
2616
                                    (!isStatic || e.getModifiers().contains(STATIC)) &&
2617
                                    !Utilities.isExcluded(Utilities.getElementName(e.getEnclosingElement(), true) + "." + sn); //NOI18N
2614
                        case CLASS:
2618
                        case CLASS:
2615
                        case ENUM:
2619
                        case ENUM:
2616
                        case INTERFACE:
2620
                        case INTERFACE:
Lines 2730-2736 Link Here
2730
            if (fqnPrefix == null)
2734
            if (fqnPrefix == null)
2731
                fqnPrefix = EMPTY;
2735
                fqnPrefix = EMPTY;
2732
            for (String pkgName : env.getController().getClasspathInfo().getClassIndex().getPackageNames(fqnPrefix, true,EnumSet.allOf(ClassIndex.SearchScope.class)))
2736
            for (String pkgName : env.getController().getClasspathInfo().getClassIndex().getPackageNames(fqnPrefix, true,EnumSet.allOf(ClassIndex.SearchScope.class)))
2733
                if (pkgName.length() > 0)
2737
                if (!Utilities.isExcluded(pkgName))
2734
                    results.add(JavaCompletionItem.createPackageItem(pkgName, anchorOffset, inPkgStmt));
2738
                    results.add(JavaCompletionItem.createPackageItem(pkgName, anchorOffset, inPkgStmt));
2735
        }
2739
        }
2736
        
2740
        
(-)org/netbeans/modules/editor/java/LazyTypeCompletionItem.java (-1 / +1 lines)
Lines 117-123 Link Here
117
                            TypeElement e = handle.resolve(controller);
117
                            TypeElement e = handle.resolve(controller);
118
                            Elements elements = controller.getElements();
118
                            Elements elements = controller.getElements();
119
                            if (e != null && (Utilities.isShowDeprecatedMembers() || !elements.isDeprecated(e)) && controller.getTrees().isAccessible(scope, e)) {
119
                            if (e != null && (Utilities.isShowDeprecatedMembers() || !elements.isDeprecated(e)) && controller.getTrees().isAccessible(scope, e)) {
120
                                if (isOfKind(e, kinds) && (!isInDefaultPackage(e) || isInDefaultPackage(scope.getEnclosingClass())))
120
                                if (isOfKind(e, kinds) && (!isInDefaultPackage(e) || isInDefaultPackage(scope.getEnclosingClass())) && !Utilities.isExcluded(e.getQualifiedName()))
121
                                    delegate = JavaCompletionItem.createTypeItem(e, (DeclaredType)e.asType(), substitutionOffset, true, controller.getElements().isDeprecated(e), insideNew, false);
121
                                    delegate = JavaCompletionItem.createTypeItem(e, (DeclaredType)e.asType(), substitutionOffset, true, controller.getElements().isDeprecated(e), insideNew, false);
122
                            }
122
                            }
123
                        }
123
                        }
(-)org/netbeans/modules/editor/java/Utilities.java (-5 / +24 lines)
Lines 113-137 Link Here
113
                showDeprecatedMembers = preferences.getBoolean(SimpleValueNames.SHOW_DEPRECATED_MEMBERS, true);
113
                showDeprecatedMembers = preferences.getBoolean(SimpleValueNames.SHOW_DEPRECATED_MEMBERS, true);
114
            }
114
            }
115
            if (settingName == null || CodeCompletionPanel.GUESS_METHOD_ARGUMENTS.equals(settingName)) {
115
            if (settingName == null || CodeCompletionPanel.GUESS_METHOD_ARGUMENTS.equals(settingName)) {
116
                guessMethodArguments = preferences.getBoolean(CodeCompletionPanel.GUESS_METHOD_ARGUMENTS, true);
116
                guessMethodArguments = preferences.getBoolean(CodeCompletionPanel.GUESS_METHOD_ARGUMENTS, CodeCompletionPanel.GUESS_METHOD_ARGUMENTS_DEFAULT);
117
            }
117
            }
118
            if (settingName == null || CodeCompletionPanel.JAVA_AUTO_POPUP_ON_IDENTIFIER_PART.equals(settingName)) {
118
            if (settingName == null || CodeCompletionPanel.JAVA_AUTO_POPUP_ON_IDENTIFIER_PART.equals(settingName)) {
119
                autoPopupOnJavaIdentifierPart = preferences.getBoolean(CodeCompletionPanel.JAVA_AUTO_POPUP_ON_IDENTIFIER_PART, false);
119
                autoPopupOnJavaIdentifierPart = preferences.getBoolean(CodeCompletionPanel.JAVA_AUTO_POPUP_ON_IDENTIFIER_PART, CodeCompletionPanel.JAVA_AUTO_POPUP_ON_IDENTIFIER_PART_DEFAULT);
120
            }
120
            }
121
            if (settingName == null || CodeCompletionPanel.JAVA_AUTO_COMPLETION_TRIGGERS.equals(settingName)) {
121
            if (settingName == null || CodeCompletionPanel.JAVA_AUTO_COMPLETION_TRIGGERS.equals(settingName)) {
122
                javaCompletionAutoPopupTriggers = preferences.get(CodeCompletionPanel.JAVA_AUTO_COMPLETION_TRIGGERS, "."); //NOI18N
122
                javaCompletionAutoPopupTriggers = preferences.get(CodeCompletionPanel.JAVA_AUTO_COMPLETION_TRIGGERS, CodeCompletionPanel.JAVA_AUTO_COMPLETION_TRIGGERS_DEFAULT);
123
            }
123
            }
124
            if (settingName == null || CodeCompletionPanel.JAVA_COMPLETION_SELECTORS.equals(settingName)) {
124
            if (settingName == null || CodeCompletionPanel.JAVA_COMPLETION_SELECTORS.equals(settingName)) {
125
                javaCompletionSelectors = preferences.get(CodeCompletionPanel.JAVA_COMPLETION_SELECTORS, ".,;:([+-="); //NOI18N
125
                javaCompletionSelectors = preferences.get(CodeCompletionPanel.JAVA_COMPLETION_SELECTORS, CodeCompletionPanel.JAVA_COMPLETION_SELECTORS_DEFAULT);
126
            }
126
            }
127
            if (settingName == null || CodeCompletionPanel.JAVADOC_AUTO_COMPLETION_TRIGGERS.equals(settingName)) {
127
            if (settingName == null || CodeCompletionPanel.JAVADOC_AUTO_COMPLETION_TRIGGERS.equals(settingName)) {
128
                javadocCompletionAutoPopupTriggers = preferences.get(CodeCompletionPanel.JAVADOC_AUTO_COMPLETION_TRIGGERS, ".#@"); //NOI18N
128
                javadocCompletionAutoPopupTriggers = preferences.get(CodeCompletionPanel.JAVADOC_AUTO_COMPLETION_TRIGGERS, CodeCompletionPanel.JAVADOC_AUTO_COMPLETION_TRIGGERS_DEFAULT);
129
            }
129
            }
130
            if (settingName == null || CodeCompletionPanel.JAVA_COMPLETION_BLACKLIST.equals(settingName)) {
131
                String blacklist = preferences.get(CodeCompletionPanel.JAVA_COMPLETION_BLACKLIST, CodeCompletionPanel.JAVA_COMPLETION_BLACKLIST_DEFAULT);
132
                excluder.updateBlacklist(blacklist);
130
        }
133
        }
134
            if (settingName == null || CodeCompletionPanel.JAVA_COMPLETION_WHITELIST.equals(settingName)) {
135
                String whitelist = preferences.get(CodeCompletionPanel.JAVA_COMPLETION_WHITELIST, CodeCompletionPanel.JAVA_COMPLETION_WHITELIST_DEFAULT);
136
                excluder.updateWhitelist(whitelist);
137
            }
138
        }
131
    };
139
    };
132
    
140
    
133
    private static String cachedPrefix = null;
141
    private static String cachedPrefix = null;
134
    private static Pattern cachedPattern = null;
142
    private static Pattern cachedPattern = null;
143
    private static volatile ExcludeCompletion excluder = null;
135
    
144
    
136
    public static boolean startsWith(String theString, String prefix) {
145
    public static boolean startsWith(String theString, String prefix) {
137
        if (theString == null || theString.length() == 0 || ERROR.equals(theString))
146
        if (theString == null || theString.length() == 0 || ERROR.equals(theString))
Lines 215-223 Link Here
215
        return javadocCompletionAutoPopupTriggers;
224
        return javadocCompletionAutoPopupTriggers;
216
    }
225
    }
217
226
227
    /**
228
     * @param fqn Fully Qualified Name
229
     * @return
230
     */
231
    public static boolean isExcluded(CharSequence fqn){
232
        lazyInit();
233
        return excluder.isExcluded(fqn);
234
    }
235
218
    private static void lazyInit() {
236
    private static void lazyInit() {
219
        if (!inited) {
237
        if (!inited) {
220
            inited = true;
238
            inited = true;
239
            excluder = new ExcludeCompletion();
221
            preferences = MimeLookup.getLookup(JavaKit.JAVA_MIME_TYPE).lookup(Preferences.class);
240
            preferences = MimeLookup.getLookup(JavaKit.JAVA_MIME_TYPE).lookup(Preferences.class);
222
            preferences.addPreferenceChangeListener(WeakListeners.create(PreferenceChangeListener.class, preferencesTracker, preferences));
241
            preferences.addPreferenceChangeListener(WeakListeners.create(PreferenceChangeListener.class, preferencesTracker, preferences));
223
            preferencesTracker.preferenceChange(null);
242
            preferencesTracker.preferenceChange(null);
(-)org/netbeans/modules/java/editor/imports/ComputeImports.java (-2 / +3 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.Utilities;
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 145-152 Link Here
145
                    continue;
146
                    continue;
146
                }
147
                }
147
                
148
                
148
                //#122334: do not propose imports from the default package:
149
				CharSequence fqn = info.getElements().getPackageOf(te).getQualifiedName();
149
                if (info.getElements().getPackageOf(te).getQualifiedName().length() != 0) {
150
                if (!Utilities.isExcluded(fqn)){
150
                    classes.add(te);
151
                    classes.add(te);
151
                }
152
                }
152
            }
153
            }
(-)org/netbeans/modules/java/editor/javadoc/JavadocCompletionQuery.java (-1 / +1 lines)
Lines 603-609 Link Here
603
        }
603
        }
604
        
604
        
605
        for (String pkgName : jdctx.javac.getClasspathInfo().getClassIndex().getPackageNames(pkgPrefix, true, EnumSet.allOf(ClassIndex.SearchScope.class)))
605
        for (String pkgName : jdctx.javac.getClasspathInfo().getClassIndex().getPackageNames(pkgPrefix, true, EnumSet.allOf(ClassIndex.SearchScope.class)))
606
            if (pkgName.length() > 0)
606
            if (!Utilities.isExcluded(pkgName))
607
                items.add(JavaCompletionItem.createPackageItem(pkgName, substitutionOffset, false));
607
                items.add(JavaCompletionItem.createPackageItem(pkgName, substitutionOffset, false));
608
    }
608
    }
609
    
609
    

Return to bug 125060