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 1893d3f..e0958e6 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 @@ -19,6 +19,7 @@ import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.preferences.IEclipsePreferences; import org.eclipse.core.runtime.preferences.InstanceScope; @@ -26,7 +27,10 @@ import org.eclipse.jdt.core.IClasspathAttribute; import org.eclipse.jdt.core.IClasspathEntry; import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.IJavaModelStatus; +import org.eclipse.jdt.core.IJavaModelStatusConstants; import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.JavaConventions; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.index.*; import org.eclipse.jdt.core.search.SearchEngine; @@ -863,4 +867,155 @@ deleteProject("ForIndex"); } } + + // test that the JavaConventions.validateIndexFile reports OK for a valid file + public void testValidateIndex() throws IOException { + String indexFilePath = getExternalResourcePath("Test.index"); + String jarFilePath = getExternalResourcePath("Test.jar"); + try { + createJar(new String[] { + "pkg/Test.java", + "package pkg;\n" + + "public class Test {\n" + + " protected Test(int i) {}\n" + + "}"}, jarFilePath); + + JavaIndexer.generateIndexForJar(jarFilePath, indexFilePath); + IJavaModelStatus status = JavaConventions.validateIndexFile(new Path(jarFilePath), new File(indexFilePath).toURL()); + assertEquals(status.getCode(), IStatus.OK); + } finally { + new File(indexFilePath).delete(); + new File(jarFilePath).delete(); + } + } + + // test that the JavaConventions.validateIndexFile reports error for a non-existent file + public void testValidateNonExistentIndex() throws IOException { + String indexFilePath = getExternalResourcePath("Test.index"); + String jarFilePath = getExternalResourcePath("Test.jar"); + new File(indexFilePath).delete(); + IJavaModelStatus status = JavaConventions.validateIndexFile(new Path(jarFilePath), new File(indexFilePath).toURL()); + assertEquals("Should have been an error", status.getCode(), IJavaModelStatusConstants.INDEX_FILE_NOT_PRESENT); + } + + // test that the JavaConventions.validateIndexFile reports error for an improper file + public void testValidateImproperIndex() throws IOException { + String indexFilePath = getExternalResourcePath("Test.index"); + String jarFilePath = getExternalResourcePath("Test.jar"); + try { + createJar(new String[] { + "pkg/Test.java", + "package pkg;\n" + + "public class Test {\n" + + " protected Test(int i) {}\n" + + "}"}, jarFilePath); + IJavaModelStatus status = JavaConventions.validateIndexFile(new Path(jarFilePath), new File(jarFilePath).toURL()); + assertEquals("Should have been an error", status.getCode(), IJavaModelStatusConstants.INDEX_FILE_NOT_ABLE_TO_READ); + System.out.println(status.getMessage()); + } finally { + new File(indexFilePath).delete(); + new File(jarFilePath).delete(); + } + + } + + // test that the JavaConventions.validateIndexFile works for a index in jar + public void testValidateJarIndex() throws IOException { + String indexFilePath = getExternalResourcePath("Test.index"); + String jarFilePath = getExternalResourcePath("Test.jar"); + String indexZipPath = getExternalResourcePath("TestIndex.zip"); + try { + createJar(new String[] { + "pkg/Test.java", + "package pkg;\n" + + "public class Test {\n" + + " protected Test(int i) {}\n" + + "}"}, jarFilePath); + + JavaIndexer.generateIndexForJar(jarFilePath, indexFilePath); + Util.zipFiles(new File[]{new File(indexFilePath)}, indexZipPath); + String url = "jar:file:"+indexZipPath+"!/Test.index"; + IJavaModelStatus status = JavaConventions.validateIndexFile(new Path(jarFilePath), new URL(url)); + assertEquals(status.getCode(), IStatus.OK); + } finally { + new File(indexFilePath).delete(); + new File(jarFilePath).delete(); + new File(indexZipPath).delete(); + } + } + + // test that the JavaConventions.validateIndexFile works for a jarurl as an index + public void testValidateJarWOFileIndex() throws IOException { + String indexFilePath = getExternalResourcePath("Test.index"); + String jarFilePath = getExternalResourcePath("Test.jar"); + String indexZipPath = getExternalResourcePath("TestIndex.zip"); + try { + createJar(new String[] { + "pkg/Test.java", + "package pkg;\n" + + "public class Test {\n" + + " protected Test(int i) {}\n" + + "}"}, jarFilePath); + + JavaIndexer.generateIndexForJar(jarFilePath, indexFilePath); + Util.zipFiles(new File[]{new File(indexFilePath)}, indexZipPath); + String url = "jar:file:"+indexZipPath+"!/"; + IJavaModelStatus status = JavaConventions.validateIndexFile(new Path(jarFilePath), new URL(url)); + assertEquals(status.getCode(), IJavaModelStatusConstants.INDEX_FILE_NOT_ABLE_TO_READ); + } finally { + new File(indexZipPath).delete(); + new File(indexFilePath).delete(); + new File(jarFilePath).delete(); + } + } + + // test that the JavaConventions.validateIndexFile works for non-existing index file in jar + public void testValidateNonExistentJarIndex() throws IOException { + String indexFilePath = getExternalResourcePath("Test.index"); + String jarFilePath = getExternalResourcePath("Test.jar"); + String indexZipPath = getExternalResourcePath("TestIndexne.zip"); + try { + createJar(new String[] { + "pkg/Test.java", + "package pkg;\n" + + "public class Test {\n" + + " protected Test(int i) {}\n" + + "}"}, jarFilePath); + + JavaIndexer.generateIndexForJar(jarFilePath, indexFilePath); + Util.zipFiles(new File[]{new File(indexFilePath)}, indexZipPath); + String url = "jar:file:"+indexZipPath+"!/Test1.index"; + IJavaModelStatus status = JavaConventions.validateIndexFile(new Path(jarFilePath), new URL(url)); + assertEquals(status.getCode(), IJavaModelStatusConstants.INDEX_FILE_NOT_PRESENT); + } finally { + new File(indexFilePath).delete(); + new File(jarFilePath).delete(); + new File(indexZipPath).delete(); + } + } + + // test that the JavaConventions.validateIndexFile works for non-existing index file in jar + public void testValidateImproperJarIndex() throws IOException { + String indexFilePath = getExternalResourcePath("Test.index"); + String jarFilePath = getExternalResourcePath("Test.jar"); + String indexZipPath = getExternalResourcePath("TestIndex.zip"); + try { + createJar(new String[] { + "pkg/Test.java", + "package pkg;\n" + + "public class Test {\n" + + " protected Test(int i) {}\n" + + "}"}, jarFilePath); + + JavaIndexer.generateIndexForJar(jarFilePath, indexFilePath); + Util.zipFiles(new File[]{new File(jarFilePath)}, indexZipPath); + String url = "jar:file:"+indexZipPath+"!/Test.jar"; + IJavaModelStatus status = JavaConventions.validateIndexFile(new Path(jarFilePath), new URL(url)); + assertEquals(status.getCode(), IJavaModelStatusConstants.INDEX_FILE_NOT_ABLE_TO_READ); + } finally { + new File(indexFilePath).delete(); + new File(jarFilePath).delete(); + new File(indexZipPath).delete(); + } + } } diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/core/IJavaModelStatusConstants.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/core/IJavaModelStatusConstants.java index a88db5e..aab0c65 100644 --- a/org.eclipse.jdt.core/model/org/eclipse/jdt/core/IJavaModelStatusConstants.java +++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/core/IJavaModelStatusConstants.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2011 IBM Corporation and others. + * Copyright (c) 2000, 2012 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -352,4 +352,16 @@ * @since 3.6.4 */ public static final int OUTPUT_LOCATION_OVERLAPPING_ANOTHER_SOURCE = 1013; + + /** + *

Status constant indicating that the specified index file is not present.

+ * @since 3.8 + */ + public static final int INDEX_FILE_NOT_PRESENT = 1014; + + /** + *

Status constant indicating that there is an error reading the index file.

+ * @since 3.8 + */ + public static final int INDEX_FILE_NOT_ABLE_TO_READ = 1015; } diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaConventions.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaConventions.java index 8ae6fdc..267f5c3 100644 --- a/org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaConventions.java +++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaConventions.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2011 IBM Corporation and others. + * Copyright (c) 2000, 2012 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -10,6 +10,7 @@ *******************************************************************************/ package org.eclipse.jdt.core; +import java.net.URL; import java.util.StringTokenizer; import org.eclipse.core.resources.IResource; @@ -628,6 +629,19 @@ } /** + * Validates the given index file for existence and validity. Returns a Java model status describing the problem related to + * the index file if any, a status object with code IStatus.OK if the index file is good. + * + * @param jarLocation the location of the jar + * @param indexURL the URL of the index file, this should not be null + * @return a java model status describing the problem related to this classpath entry if any, a status object with code IStatus.OK if the entry is fine + * @since 3.8 + */ + public static IJavaModelStatus validateIndexFile(IPath jarLocation, URL indexURL) { + return ClasspathEntry.validateIndexFile(jarLocation, indexURL); + } + + /** * Validate the given type variable name. *

* Syntax of a type variable name corresponds to a Java identifier (JLS3 4.3). 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 810a6b2..0291bfc 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, 2011 IBM Corporation and others. + * Copyright (c) 2000, 2012 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -55,6 +55,8 @@ import org.eclipse.jdt.internal.compiler.env.AccessRuleSet; import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; import org.eclipse.jdt.internal.compiler.util.ManifestAnalyzer; +import org.eclipse.jdt.internal.core.index.Index; +import org.eclipse.jdt.internal.core.index.IndexLocation; import org.eclipse.jdt.internal.core.util.Messages; import org.eclipse.jdt.internal.core.util.Util; import org.w3c.dom.DOMException; @@ -2084,6 +2086,30 @@ return JavaModelStatus.VERIFIED_OK; } + /** + * Validates the given index file for existence and validity. Returns a Java model status describing the problem related to + * the index file if any, a status object with code IStatus.OK if the index file is good. + * + * @param jarLocation the location of the jar + * @param indexURL the URL of the index file + * @return a java model status describing the problem related to this classpath entry if any, a status object with code IStatus.OK if the entry is fine + * @since 3.8 + */ + public static IJavaModelStatus validateIndexFile(IPath jarLocation, URL indexURL) { + IndexLocation indexLocation = IndexLocation.createIndexLocation(indexURL); + if (indexLocation == null || !indexLocation.exists()) { + return new JavaModelStatus(IJavaModelStatusConstants.INDEX_FILE_NOT_PRESENT, Messages.bind(Messages.classpath_indexfile_notpresent, indexURL, jarLocation)); + } + try { + new Index(indexLocation, jarLocation.toString(), true); + return JavaModelStatus.VERIFIED_OK; + } catch (IOException e) { + return new JavaModelStatus(IJavaModelStatusConstants.INDEX_FILE_NOT_ABLE_TO_READ, Messages.bind(Messages.classpath_indexfile_errorreading, indexURL, jarLocation)); + } finally { + indexLocation.close(); + } + } + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=232816, Now we have the facility to include a container // name in diagnostics. If the parameter ``container'' is not null, it is used to point to the library // more fully. diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/Messages.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/Messages.java index ff1cf83..0e5b509 100644 --- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/Messages.java +++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/Messages.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2011 IBM Corporation and others. + * Copyright (c) 2000, 2012 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -181,6 +181,8 @@ public static String classpath_incompatibleLibraryJDKLevelInContainer; public static String classpath_duplicateEntryExtraAttribute; public static String classpath_deprecated_variable; + public static String classpath_indexfile_notpresent; + public static String classpath_indexfile_errorreading; public static String file_notFound; public static String file_badFormat; public static String path_nullPath; diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/messages.properties b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/messages.properties index 5af88db..7e0a614 100644 --- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/messages.properties +++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/messages.properties @@ -1,5 +1,5 @@ ############################################################################### -# Copyright (c) 2000, 2011 IBM Corporation and others. +# Copyright (c) 2000, 2012 IBM Corporation and others. # All rights reserved. This program and the accompanying materials # are made available under the terms of the Eclipse Public License v1.0 # which accompanies this distribution, and is available at @@ -177,6 +177,8 @@ classpath_incompatibleLibraryJDKLevelInContainer = Incompatible .class files version in required binaries. Project ''{0}'' is targeting a {1} runtime, but is compiled against ''{2}'' (from the {3}) which requires a {4} runtime classpath_duplicateEntryExtraAttribute = Duplicate extra attribute: ''{0}'' in classpath entry ''{1}'' for project ''{2}'' classpath_deprecated_variable = Classpath variable ''{0}'' in project ''{1}'' is deprecated: {2} +classpath_indexfile_notpresent = Index file ''{0}'' specified for ''{1}'' is not present +classpath_indexfile_errorreading = Error reading the index file ''{0}'' specified for ''{1}'' ### miscellaneous buffer_closed=Buffer is closed