Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoland Grunberg2019-10-24 22:37:11 +0000
committerRoland Grunberg2019-12-03 21:31:41 +0000
commit9c0cf52f3525e62dc2c7dbd9ccbcf3edd9f8ab89 (patch)
treea41ba44b744b50ab48ba347e35fe03919eef6a58
parentdeea9fcf7ccba05989e3c83f7a23150a0aa61920 (diff)
downloadeclipse.jdt.core-R4_14_maintenance.tar.gz
eclipse.jdt.core-R4_14_maintenance.tar.xz
eclipse.jdt.core-R4_14_maintenance.zip
- Allow enablement of this feature using jdt.core.sharedIndexLocation property - Index entries not present in the common location will still cause the index manager to fall back to creating the index in the normal workspace location Change-Id: I0a2d41a3abf23c64f98c8b3060c281dc71e48509
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaIndexTests.java59
-rw-r--r--org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ClasspathEntry.java30
2 files changed, 87 insertions, 2 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaIndexTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaIndexTests.java
index f29622ff8b..ddfaa7ac6e 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaIndexTests.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaIndexTests.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2012 IBM Corporation and others.
+ * Copyright (c) 2012, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -16,6 +16,9 @@ package org.eclipse.jdt.core.tests.model;
import java.io.File;
import java.io.IOException;
import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.zip.CRC32;
import junit.framework.Test;
@@ -34,6 +37,7 @@ import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.index.*;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.core.tests.util.Util;
+import org.eclipse.jdt.internal.core.ClasspathEntry;
import org.eclipse.jdt.internal.core.JavaModelManager;
import org.eclipse.jdt.internal.core.UserLibraryClasspathContainer;
import org.osgi.service.prefs.BackingStoreException;
@@ -50,6 +54,7 @@ public class JavaIndexTests extends AbstractJavaSearchTests {
public static Test suite() {
return buildModelTestSuite(JavaIndexTests.class);
}
+
// Test that the index file is really generated.
public void testGenerateIndex() throws IOException {
String indexFilePath = getExternalResourcePath("Test.index");
@@ -866,4 +871,56 @@ public class JavaIndexTests extends AbstractJavaSearchTests {
deleteProject("ForIndex");
}
}
+
+ // Test shared index location functionality
+ public void testSharedIndexLocation() throws CoreException, IOException {
+ // Create temporary testing folder
+ String sharedIndexDir = Files.createTempDirectory("shared_index").toFile().getCanonicalPath();
+ // enable shared index
+ ClasspathEntry.setSharedIndexLocation(sharedIndexDir, getClass());
+ // path of library must be platform neutral
+ String jarFilePath = Path.fromOSString(Paths.get(sharedIndexDir, "Test.jar").toString()).toPortableString();
+ // compute index file
+ CRC32 checksumCalculator = new CRC32();
+ checksumCalculator.update(jarFilePath.getBytes());
+ String fileName = Long.toString(checksumCalculator.getValue()) + ".index";
+ String indexFilePath = Paths.get(sharedIndexDir, fileName).toString();
+ try {
+ createJar(new String[] {
+ "pkg/Test.java",
+ "package pkg;\n" +
+ "public class Test {\n" +
+ " protected Test(int i) {}\n" +
+ "}"}, jarFilePath);
+
+ JavaIndexer.generateIndexForJar(jarFilePath, indexFilePath);
+ assertTrue(new File(indexFilePath).exists());
+ long modified = new File(indexFilePath).lastModified();
+
+ IJavaProject p = createJavaProject("P");
+ Path libPath = new Path(jarFilePath);
+ IClasspathEntry entry = JavaCore.newLibraryEntry(libPath, null, null, null, null, false);
+ setClasspath(p, new IClasspathEntry[] { entry });
+
+ waitUntilIndexesReady();
+
+ // Test that search works properly
+ search("Test", TYPE, DECLARATIONS, EXACT_RULE,
+ SearchEngine.createJavaSearchScope(new IJavaElement[] { p }));
+ assertSearchResults(Paths.get(sharedIndexDir, "Test.jar").toString() + " pkg.Test");
+
+ // Test that specified index file is really used
+ java.io.File indexFile = JavaModelManager.getIndexManager().getIndex(libPath, false, false).getIndexFile();
+ assertEquals("Specified index file is not being used", indexFilePath, indexFile.toString());
+
+ // Ensure that the index file is not modified
+ assertEquals(modified, new File(indexFilePath).lastModified());
+ } finally {
+ deleteProject("P");
+ new File(indexFilePath).delete();
+ new File(jarFilePath).delete();
+ new File(sharedIndexDir).delete();
+ ClasspathEntry.setSharedIndexLocation(null, getClass());
+ }
+ }
}
diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ClasspathEntry.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ClasspathEntry.java
index dc3de3704c..374d128034 100644
--- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ClasspathEntry.java
+++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ClasspathEntry.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2018 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -31,6 +31,7 @@ import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
+import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@@ -38,6 +39,7 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
@@ -122,6 +124,9 @@ public class ClasspathEntry implements IClasspathEntry {
public static final String TAG_DISCOURAGED = "discouraged"; //$NON-NLS-1$
public static final String TAG_IGNORE_IF_BETTER = "ignoreifbetter"; //$NON-NLS-1$
+ // common index location for all workspaces
+ private static String SHARED_INDEX_LOCATION = System.getProperty("jdt.core.sharedIndexLocation"); //$NON-NLS-1$
+
/**
* Describes the kind of classpath entry - one of
* CPE_PROJECT, CPE_LIBRARY, CPE_SOURCE, CPE_VARIABLE or CPE_CONTAINER
@@ -1757,6 +1762,18 @@ public class ClasspathEntry implements IClasspathEntry {
public URL getLibraryIndexLocation() {
switch(getEntryKind()) {
case IClasspathEntry.CPE_LIBRARY :
+ if (SHARED_INDEX_LOCATION != null) {
+ try {
+ String pathString = getPath().toPortableString();
+ CRC32 checksumCalculator = new CRC32();
+ checksumCalculator.update(pathString.getBytes());
+ String fileName = Long.toString(checksumCalculator.getValue()) + ".index"; //$NON-NLS-1$
+ return new URL("file", null, Paths.get(SHARED_INDEX_LOCATION, fileName).toString()); //$NON-NLS-1$
+ } catch (MalformedURLException e1) {
+ Util.log(e1); // should not happen if protocol known (eg. 'file')
+ }
+ }
+ break;
case IClasspathEntry.CPE_VARIABLE :
break;
default :
@@ -2523,4 +2540,15 @@ public class ClasspathEntry implements IClasspathEntry {
}
return JavaModelStatus.VERIFIED_OK;
}
+
+ /*
+ * For testing shared index location in JavaIndexTests only
+ */
+ public static void setSharedIndexLocation(String value, Class<?> clazz) throws IllegalArgumentException{
+ if (clazz != null && "org.eclipse.jdt.core.tests.model.JavaIndexTests".equals(clazz.getName())) { //$NON-NLS-1$
+ SHARED_INDEX_LOCATION = value;
+ } else {
+ throw new IllegalArgumentException("Cannot set index location for specified test class"); //$NON-NLS-1$
+ }
+ }
}

Back to the top