Skip to content

Commit 1dc8186

Browse files
trancexpressiloveeclipse
authored andcommitted
Add preference for disabling indexing of restricted files
This change adds a new preference to org.eclipse.jdt.core: disableRestrictedFileIndexing If the preference is set to 'true' and a file is restricted, index requests for the file will be ignored. Example preference for product customization: org.eclipse.jdt.core/disableRestrictedFileIndexing=true Fixes: eclipse-jdt#4970
1 parent d3c4284 commit 1dc8186

File tree

3 files changed

+100
-3
lines changed

3 files changed

+100
-3
lines changed

org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/IndexManagerTests.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,17 @@
2929
import org.eclipse.core.runtime.CoreException;
3030
import org.eclipse.core.runtime.NullProgressMonitor;
3131
import org.eclipse.core.runtime.Path;
32+
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
33+
import org.eclipse.core.runtime.preferences.InstanceScope;
34+
import org.eclipse.jdt.core.IJavaElement;
3235
import org.eclipse.jdt.core.IJavaProject;
36+
import org.eclipse.jdt.core.JavaCore;
3337
import org.eclipse.jdt.core.compiler.CharOperation;
38+
import org.eclipse.jdt.core.search.IJavaSearchConstants;
39+
import org.eclipse.jdt.core.search.IJavaSearchScope;
40+
import org.eclipse.jdt.core.search.SearchEngine;
3441
import org.eclipse.jdt.core.search.SearchPattern;
42+
import org.eclipse.jdt.core.tests.model.AbstractJavaSearchTests.JavaSearchResultCollector;
3543
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
3644
import org.eclipse.jdt.internal.core.JavaModelManager;
3745
import org.eclipse.jdt.internal.core.index.EntryResult;
@@ -176,6 +184,45 @@ public void testSearchMetaIndex_ForSourceTypeDeclarations() throws CoreException
176184
assertEquals("No results found", 1, indexNames.get().size());
177185
}
178186

187+
public void testDisableIndexingForRestrictedFile() throws Exception {
188+
if (SKIP_TESTS) return;
189+
this.indexDisabledForTest = true;
190+
IEclipsePreferences node = InstanceScope.INSTANCE.getNode(JavaCore.PLUGIN_ID);
191+
String pref = JavaModelManager.DISABLE_RESTRICTED_FILE_INDEXING_PREFERENCE;
192+
node.putBoolean(pref, true);
193+
node.flush();
194+
boolean wasIndexerEnabled = JavaModelManager.getIndexManager().isEnabled();
195+
try {
196+
disableIndexer();
197+
198+
createFolder("/IndexProject/src/p");
199+
IFile file1 = createFile("/IndexProject/src/p/TestClass1.java", "package p;\n public class TestClass1 {\n" + "}");
200+
createFile("/IndexProject/src/p/TestClass2.java", "package p;\n public class TestClass2 {\n" + "}");
201+
file1.setContentRestricted(true);
202+
JavaModelManager.getIndexManager().indexAll(this.project.getProject());
203+
waitUntilIndexesReady();
204+
205+
JavaSearchResultCollector collector = new JavaSearchResultCollector();
206+
IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {this.project});
207+
search("TestClass1", IJavaSearchConstants.TYPE, IJavaSearchConstants.ALL_OCCURRENCES, scope, collector);
208+
assertSearchResults(
209+
"",
210+
collector);
211+
212+
collector = new JavaSearchResultCollector();
213+
search("TestClass2", IJavaSearchConstants.TYPE, IJavaSearchConstants.ALL_OCCURRENCES, scope, collector);
214+
assertSearchResults(
215+
"src/p/TestClass2.java p.TestClass2 [TestClass2]",
216+
collector);
217+
} finally {
218+
if (wasIndexerEnabled) {
219+
enableIndexer();
220+
}
221+
node.remove(pref);
222+
node.flush();
223+
}
224+
}
225+
179226
private void changeFile(String path, String content) {
180227
IFile file = getFile(path);
181228
if (!file.exists()) {

org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JavaModelManager.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@ public void setCache(IPath path, ZipFile zipFile) {
329329
public static final String CONTAINER_INITIALIZER_PERF = JavaCore.PLUGIN_ID + "/perf/containerinitializer" ; //$NON-NLS-1$
330330
public static final String RECONCILE_PERF = JavaCore.PLUGIN_ID + "/perf/reconcile" ; //$NON-NLS-1$
331331

332+
public static final String DISABLE_RESTRICTED_FILE_INDEXING_PREFERENCE = "disableRestrictedFileIndexing" ; //$NON-NLS-1$
333+
332334
public static boolean PERF_VARIABLE_INITIALIZER = false;
333335
public static boolean PERF_CONTAINER_INITIALIZER = false;
334336
// Non-static, which will give it a chance to retain the default when and if JavaModelManager is restarted.
@@ -349,6 +351,8 @@ public void setCache(IPath path, ZipFile zipFile) {
349351
static final int PREF_INSTANCE = 0;
350352
static final int PREF_DEFAULT = 1;
351353

354+
private static volatile boolean disableRestrictedFileIndexing;
355+
352356
static final Object[][] NO_PARTICIPANTS = new Object[0][];
353357

354358
private static DebugTrace DEBUG_TRACE;
@@ -1762,7 +1766,9 @@ public void preferenceChange(IEclipsePreferences.PreferenceChangeEvent event) {
17621766
UserLibraryManager manager = JavaModelManager.getUserLibraryManager();
17631767
manager.updateUserLibrary(libName, (String)event.getNewValue());
17641768
}
1765-
}
1769+
} else if (propertyName.equals(DISABLE_RESTRICTED_FILE_INDEXING_PREFERENCE)) {
1770+
setDisableRestrictedFileIndexing();
1771+
}
17661772
// Reset all project caches (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=233568 )
17671773
try {
17681774
IJavaProject[] projects = JavaModelManager.getJavaModelManager().getJavaModel().getJavaProjects();
@@ -3396,6 +3402,8 @@ public void removed(IEclipsePreferences.NodeChangeEvent event) {
33963402
}
33973403
};
33983404
((IEclipsePreferences) this.preferencesLookup[PREF_DEFAULT].parent()).addNodeChangeListener(this.defaultNodeListener);
3405+
3406+
setDisableRestrictedFileIndexing();
33993407
}
34003408

34013409
void touchProjectsAsync(final IProject[] projectsToTouch) throws JavaModelException {
@@ -5732,4 +5740,13 @@ public static <T, E extends Exception> T cacheZipFiles(JavaCallable<T, E> callab
57325740
getJavaModelManager().flushZipFiles(instance);
57335741
}
57345742
}
5743+
5744+
private static void setDisableRestrictedFileIndexing() {
5745+
disableRestrictedFileIndexing = Platform.getPreferencesService().getBoolean(
5746+
JavaCore.PLUGIN_ID, DISABLE_RESTRICTED_FILE_INDEXING_PREFERENCE, false, null);
5747+
}
5748+
5749+
public static boolean disableRestrictedFileIndexing() {
5750+
return disableRestrictedFileIndexing;
5751+
}
57355752
}

org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/indexing/SourceIndexer.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
import org.eclipse.core.resources.IFile;
1919
import org.eclipse.core.resources.IProject;
20+
import org.eclipse.core.resources.IWorkspaceRoot;
2021
import org.eclipse.core.resources.ResourcesPlugin;
22+
import org.eclipse.core.runtime.CoreException;
2123
import org.eclipse.core.runtime.ILog;
2224
import org.eclipse.core.runtime.IPath;
2325
import org.eclipse.core.runtime.Path;
@@ -100,6 +102,9 @@ private boolean usedDomBasedIndexing() {
100102

101103
@Override
102104
public void indexDocument() {
105+
if (disabledForFile()) {
106+
return;
107+
}
103108
if (usedDomBasedIndexing()) {
104109
indexDocumentFromDOM();
105110
return;
@@ -385,8 +390,8 @@ public void indexResolvedDocument() {
385390
}
386391

387392
private org.eclipse.jdt.core.ICompilationUnit getUnit() {
388-
if (this.document instanceof JavaSearchDocument javaSearchDoc) {
389-
IFile file = javaSearchDoc.getFile();
393+
IFile file = getJavaSearchFile();
394+
if (file != null) {
390395
try {
391396
if (JavaProject.hasJavaNature(file.getProject())) {
392397
IJavaProject javaProject = JavaCore.create(file.getProject());
@@ -405,6 +410,13 @@ private org.eclipse.jdt.core.ICompilationUnit getUnit() {
405410
return null;
406411
}
407412

413+
private IFile getJavaSearchFile() {
414+
if (this.document instanceof JavaSearchDocument javaSearchDoc) {
415+
return javaSearchDoc.getFile();
416+
}
417+
return null;
418+
}
419+
408420
/**
409421
* @return whether the operation was successful
410422
*/
@@ -454,4 +466,25 @@ public boolean preVisit2(org.eclipse.jdt.core.dom.ASTNode node) {
454466
return false;
455467
}
456468

469+
private boolean disabledForFile() {
470+
if (JavaModelManager.disableRestrictedFileIndexing()) {
471+
IFile file = getJavaSearchFile();
472+
if (file == null) {
473+
IPath path = new Path(this.document.getPath());
474+
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
475+
file = root.getFile(path);
476+
}
477+
try {
478+
return file.isContentRestricted();
479+
} catch (CoreException e) {
480+
JavaCore.getPlugin().getLog().log(e.getStatus());
481+
/*
482+
* Assume indexing is disabled for the file, since the preference for disabling is set
483+
* but we cannot determine if the file is restricted.
484+
*/
485+
return true;
486+
}
487+
}
488+
return false;
489+
}
457490
}

0 commit comments

Comments
 (0)