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

(-)a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaIndexTests.java (+155 lines)
Lines 19-24 Link Here
19
import org.eclipse.core.resources.IProject;
19
import org.eclipse.core.resources.IProject;
20
import org.eclipse.core.runtime.CoreException;
20
import org.eclipse.core.runtime.CoreException;
21
import org.eclipse.core.runtime.IPath;
21
import org.eclipse.core.runtime.IPath;
22
import org.eclipse.core.runtime.IStatus;
22
import org.eclipse.core.runtime.Path;
23
import org.eclipse.core.runtime.Path;
23
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
24
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
24
import org.eclipse.core.runtime.preferences.InstanceScope;
25
import org.eclipse.core.runtime.preferences.InstanceScope;
Lines 26-32 Link Here
26
import org.eclipse.jdt.core.IClasspathAttribute;
27
import org.eclipse.jdt.core.IClasspathAttribute;
27
import org.eclipse.jdt.core.IClasspathEntry;
28
import org.eclipse.jdt.core.IClasspathEntry;
28
import org.eclipse.jdt.core.IJavaElement;
29
import org.eclipse.jdt.core.IJavaElement;
30
import org.eclipse.jdt.core.IJavaModelStatus;
31
import org.eclipse.jdt.core.IJavaModelStatusConstants;
29
import org.eclipse.jdt.core.IJavaProject;
32
import org.eclipse.jdt.core.IJavaProject;
33
import org.eclipse.jdt.core.JavaConventions;
30
import org.eclipse.jdt.core.JavaCore;
34
import org.eclipse.jdt.core.JavaCore;
31
import org.eclipse.jdt.core.index.*;
35
import org.eclipse.jdt.core.index.*;
32
import org.eclipse.jdt.core.search.SearchEngine;
36
import org.eclipse.jdt.core.search.SearchEngine;
Lines 863-866 Link Here
863
			deleteProject("ForIndex");
867
			deleteProject("ForIndex");
864
		}
868
		}
865
	}
869
	}
870
	
871
	// test that the JavaConventions.validateIndexFile reports OK for a valid file
872
	public void testValidateIndex() throws IOException {
873
		String indexFilePath = getExternalResourcePath("Test.index");
874
		String jarFilePath = getExternalResourcePath("Test.jar");
875
		try {
876
			createJar(new String[] {
877
					"pkg/Test.java",
878
					"package pkg;\n" +
879
					"public class Test {\n" +
880
					"  protected Test(int i) {}\n" +
881
					"}"}, jarFilePath);
882
			
883
			JavaIndexer.generateIndexForJar(jarFilePath, indexFilePath);
884
			IJavaModelStatus status = JavaConventions.validateIndexFile(new Path(jarFilePath), new File(indexFilePath).toURL());
885
			assertEquals(status.getCode(), IStatus.OK);
886
		} finally {
887
			new File(indexFilePath).delete();
888
			new File(jarFilePath).delete();
889
		}
890
	}
891
	
892
	// test that the JavaConventions.validateIndexFile reports error for a non-existent file
893
	public void testValidateNonExistentIndex() throws IOException {
894
		String indexFilePath = getExternalResourcePath("Test.index");
895
		String jarFilePath = getExternalResourcePath("Test.jar");
896
		new File(indexFilePath).delete();
897
		IJavaModelStatus status = JavaConventions.validateIndexFile(new Path(jarFilePath), new File(indexFilePath).toURL());
898
		assertEquals("Should have been an error", status.getCode(), IJavaModelStatusConstants.INDEX_FILE_NOT_PRESENT);
899
	}
900
	
901
	// test that the JavaConventions.validateIndexFile reports error for an improper file
902
	public void testValidateImproperIndex() throws IOException {
903
		String indexFilePath = getExternalResourcePath("Test.index");
904
		String jarFilePath = getExternalResourcePath("Test.jar");
905
		try {
906
			createJar(new String[] {
907
					"pkg/Test.java",
908
					"package pkg;\n" +
909
					"public class Test {\n" +
910
					"  protected Test(int i) {}\n" +
911
					"}"}, jarFilePath);
912
		IJavaModelStatus status = JavaConventions.validateIndexFile(new Path(jarFilePath), new File(jarFilePath).toURL());
913
		assertEquals("Should have been an error", status.getCode(), IJavaModelStatusConstants.INDEX_FILE_NOT_ABLE_TO_READ);
914
		System.out.println(status.getMessage());
915
		} finally {
916
			new File(indexFilePath).delete();
917
			new File(jarFilePath).delete();
918
		}
919
		
920
	}
921
	
922
	// test that the JavaConventions.validateIndexFile works for a index in jar
923
	public void testValidateJarIndex() throws IOException {
924
		String indexFilePath = getExternalResourcePath("Test.index");
925
		String jarFilePath = getExternalResourcePath("Test.jar");
926
		String indexZipPath =  getExternalResourcePath("TestIndex.zip");
927
		try {
928
			createJar(new String[] {
929
					"pkg/Test.java",
930
					"package pkg;\n" +
931
					"public class Test {\n" +
932
					"  protected Test(int i) {}\n" +
933
					"}"}, jarFilePath);
934
		
935
			JavaIndexer.generateIndexForJar(jarFilePath, indexFilePath);
936
			Util.zipFiles(new File[]{new File(indexFilePath)}, indexZipPath);
937
			String url = "jar:file:"+indexZipPath+"!/Test.index";
938
			IJavaModelStatus status = JavaConventions.validateIndexFile(new Path(jarFilePath), new URL(url));
939
			assertEquals(status.getCode(), IStatus.OK);
940
		} finally {
941
			new File(indexFilePath).delete();
942
			new File(jarFilePath).delete();
943
			new File(indexZipPath).delete();
944
		}
945
	}
946
	
947
	// test that the JavaConventions.validateIndexFile works for a jarurl as an index
948
	public void testValidateJarWOFileIndex() throws IOException {
949
		String indexFilePath = getExternalResourcePath("Test.index");
950
		String jarFilePath = getExternalResourcePath("Test.jar");
951
		String indexZipPath =  getExternalResourcePath("TestIndex.zip");
952
		try {
953
			createJar(new String[] {
954
					"pkg/Test.java",
955
					"package pkg;\n" +
956
					"public class Test {\n" +
957
					"  protected Test(int i) {}\n" +
958
					"}"}, jarFilePath);
959
		
960
			JavaIndexer.generateIndexForJar(jarFilePath, indexFilePath);
961
			Util.zipFiles(new File[]{new File(indexFilePath)}, indexZipPath);
962
			String url = "jar:file:"+indexZipPath+"!/";
963
			IJavaModelStatus status = JavaConventions.validateIndexFile(new Path(jarFilePath), new URL(url));
964
			assertEquals(status.getCode(), IJavaModelStatusConstants.INDEX_FILE_NOT_ABLE_TO_READ);
965
		} finally {
966
			new File(indexZipPath).delete();
967
			new File(indexFilePath).delete();
968
			new File(jarFilePath).delete();		
969
		}
970
	}
971
972
	// test that the JavaConventions.validateIndexFile works for non-existing index file in jar
973
	public void testValidateNonExistentJarIndex() throws IOException {
974
		String indexFilePath = getExternalResourcePath("Test.index");
975
		String jarFilePath = getExternalResourcePath("Test.jar");
976
		String indexZipPath =  getExternalResourcePath("TestIndexne.zip");
977
		try {
978
			createJar(new String[] {
979
					"pkg/Test.java",
980
					"package pkg;\n" +
981
					"public class Test {\n" +
982
					"  protected Test(int i) {}\n" +
983
					"}"}, jarFilePath);
984
		
985
			JavaIndexer.generateIndexForJar(jarFilePath, indexFilePath);
986
			Util.zipFiles(new File[]{new File(indexFilePath)}, indexZipPath);
987
			String url = "jar:file:"+indexZipPath+"!/Test1.index";
988
			IJavaModelStatus status = JavaConventions.validateIndexFile(new Path(jarFilePath), new URL(url));
989
			assertEquals(status.getCode(), IJavaModelStatusConstants.INDEX_FILE_NOT_PRESENT);
990
		} finally {
991
			new File(indexFilePath).delete();
992
			new File(jarFilePath).delete();
993
			new File(indexZipPath).delete();
994
		}
995
	}
996
997
	// test that the JavaConventions.validateIndexFile works for non-existing index file in jar
998
	public void testValidateImproperJarIndex() throws IOException {
999
		String indexFilePath = getExternalResourcePath("Test.index");
1000
		String jarFilePath = getExternalResourcePath("Test.jar");
1001
		String indexZipPath =  getExternalResourcePath("TestIndex.zip");
1002
		try {
1003
			createJar(new String[] {
1004
					"pkg/Test.java",
1005
					"package pkg;\n" +
1006
					"public class Test {\n" +
1007
					"  protected Test(int i) {}\n" +
1008
					"}"}, jarFilePath);
1009
		
1010
			JavaIndexer.generateIndexForJar(jarFilePath, indexFilePath);
1011
			Util.zipFiles(new File[]{new File(jarFilePath)}, indexZipPath);
1012
			String url = "jar:file:"+indexZipPath+"!/Test.jar";
1013
			IJavaModelStatus status = JavaConventions.validateIndexFile(new Path(jarFilePath), new URL(url));
1014
			assertEquals(status.getCode(), IJavaModelStatusConstants.INDEX_FILE_NOT_ABLE_TO_READ);
1015
		} finally {
1016
			new File(indexFilePath).delete();
1017
			new File(jarFilePath).delete();
1018
			new File(indexZipPath).delete();
1019
		}
1020
	}
866
}
1021
}
(-)a/org.eclipse.jdt.core/model/org/eclipse/jdt/core/IJavaModelStatusConstants.java (-1 / +13 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
2
 * Copyright (c) 2000, 2012 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 352-355 Link Here
352
	 * @since 3.6.4
352
	 * @since 3.6.4
353
	 */
353
	 */
354
	public static final int OUTPUT_LOCATION_OVERLAPPING_ANOTHER_SOURCE = 1013;
354
	public static final int OUTPUT_LOCATION_OVERLAPPING_ANOTHER_SOURCE = 1013;
355
	
356
	/**
357
	 * <p>Status constant indicating that the specified index file is not present. </p>
358
	 * @since 3.8
359
	 */
360
	public static final int INDEX_FILE_NOT_PRESENT = 1014;
361
	
362
	/**
363
	 * <p>Status constant indicating that there is an error reading the index file. </p>
364
	 * @since 3.8
365
	 */
366
	public static final int INDEX_FILE_NOT_ABLE_TO_READ = 1015;
355
}
367
}
(-)a/org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaConventions.java (-1 / +15 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
2
 * Copyright (c) 2000, 2012 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 10-15 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.jdt.core;
11
package org.eclipse.jdt.core;
12
12
13
import java.net.URL;
13
import java.util.StringTokenizer;
14
import java.util.StringTokenizer;
14
15
15
import org.eclipse.core.resources.IResource;
16
import org.eclipse.core.resources.IResource;
Lines 628-633 Link Here
628
	}
629
	}
629
630
630
	/**
631
	/**
632
	 * Validates the given index file for existence and validity. Returns a Java model status describing the problem related to 
633
	 * the index file if any, a status object with code <code>IStatus.OK</code> if the index file is good.
634
	 * 
635
	 * @param jarLocation the location of the jar
636
	 * @param indexURL the URL of the index file, this should not be null
637
	 * @return a java model status describing the problem related to this classpath entry if any, a status object with code <code>IStatus.OK</code> if the entry is fine
638
	 * @since 3.8
639
	 */
640
	public static IJavaModelStatus validateIndexFile(IPath jarLocation, URL indexURL) {
641
		return ClasspathEntry.validateIndexFile(jarLocation, indexURL);
642
	}
643
644
	/**
631
	 * Validate the given type variable name.
645
	 * Validate the given type variable name.
632
	 * <p>
646
	 * <p>
633
	 * Syntax of a type variable name corresponds to a Java identifier (JLS3 4.3).
647
	 * Syntax of a type variable name corresponds to a Java identifier (JLS3 4.3).
(-)a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ClasspathEntry.java (-1 / +27 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
2
 * Copyright (c) 2000, 2012 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 55-60 Link Here
55
import org.eclipse.jdt.internal.compiler.env.AccessRuleSet;
55
import org.eclipse.jdt.internal.compiler.env.AccessRuleSet;
56
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
56
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
57
import org.eclipse.jdt.internal.compiler.util.ManifestAnalyzer;
57
import org.eclipse.jdt.internal.compiler.util.ManifestAnalyzer;
58
import org.eclipse.jdt.internal.core.index.Index;
59
import org.eclipse.jdt.internal.core.index.IndexLocation;
58
import org.eclipse.jdt.internal.core.util.Messages;
60
import org.eclipse.jdt.internal.core.util.Messages;
59
import org.eclipse.jdt.internal.core.util.Util;
61
import org.eclipse.jdt.internal.core.util.Util;
60
import org.w3c.dom.DOMException;
62
import org.w3c.dom.DOMException;
Lines 2084-2089 Link Here
2084
		return JavaModelStatus.VERIFIED_OK;
2086
		return JavaModelStatus.VERIFIED_OK;
2085
	}
2087
	}
2086
2088
2089
	/**
2090
	 * Validates the given index file for existence and validity. Returns a Java model status describing the problem related to 
2091
	 * the index file if any, a status object with code <code>IStatus.OK</code> if the index file is good.
2092
	 * 
2093
	 * @param jarLocation the location of the jar
2094
	 * @param indexURL the URL of the index file
2095
	 * @return a java model status describing the problem related to this classpath entry if any, a status object with code <code>IStatus.OK</code> if the entry is fine
2096
	 * @since 3.8
2097
	 */
2098
	public static IJavaModelStatus validateIndexFile(IPath jarLocation, URL indexURL) {
2099
		IndexLocation indexLocation = IndexLocation.createIndexLocation(indexURL);
2100
		if (indexLocation == null || !indexLocation.exists()) {
2101
			return new JavaModelStatus(IJavaModelStatusConstants.INDEX_FILE_NOT_PRESENT, Messages.bind(Messages.classpath_indexfile_notpresent, indexURL, jarLocation));
2102
		}
2103
		try {		
2104
			new Index(indexLocation, jarLocation.toString(), true);
2105
			return JavaModelStatus.VERIFIED_OK;
2106
		} catch (IOException e) {
2107
			return new JavaModelStatus(IJavaModelStatusConstants.INDEX_FILE_NOT_ABLE_TO_READ, Messages.bind(Messages.classpath_indexfile_errorreading, indexURL, jarLocation));
2108
		} finally {
2109
			indexLocation.close();
2110
		}
2111
	}
2112
2087
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=232816, Now we have the facility to include a container
2113
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=232816, Now we have the facility to include a container
2088
	// name in diagnostics. If the parameter ``container'' is not null, it is used to point to the library
2114
	// name in diagnostics. If the parameter ``container'' is not null, it is used to point to the library
2089
	// more fully.
2115
	// more fully.
(-)a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/Messages.java (-1 / +3 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
2
 * Copyright (c) 2000, 2012 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 181-186 Link Here
181
	public static String classpath_incompatibleLibraryJDKLevelInContainer;
181
	public static String classpath_incompatibleLibraryJDKLevelInContainer;
182
	public static String classpath_duplicateEntryExtraAttribute;
182
	public static String classpath_duplicateEntryExtraAttribute;
183
	public static String classpath_deprecated_variable;
183
	public static String classpath_deprecated_variable;
184
	public static String classpath_indexfile_notpresent;
185
	public static String classpath_indexfile_errorreading;
184
	public static String file_notFound;
186
	public static String file_notFound;
185
	public static String file_badFormat;
187
	public static String file_badFormat;
186
	public static String path_nullPath;
188
	public static String path_nullPath;
(-)a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/messages.properties (-1 / +3 lines)
Lines 1-5 Link Here
1
###############################################################################
1
###############################################################################
2
# Copyright (c) 2000, 2011 IBM Corporation and others.
2
# Copyright (c) 2000, 2012 IBM Corporation and others.
3
# All rights reserved. This program and the accompanying materials
3
# All rights reserved. This program and the accompanying materials
4
# are made available under the terms of the Eclipse Public License v1.0
4
# are made available under the terms of the Eclipse Public License v1.0
5
# which accompanies this distribution, and is available at
5
# which accompanies this distribution, and is available at
Lines 177-182 Link Here
177
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
177
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
178
classpath_duplicateEntryExtraAttribute = Duplicate extra attribute: ''{0}'' in classpath entry ''{1}'' for project ''{2}''
178
classpath_duplicateEntryExtraAttribute = Duplicate extra attribute: ''{0}'' in classpath entry ''{1}'' for project ''{2}''
179
classpath_deprecated_variable = Classpath variable ''{0}'' in project ''{1}'' is deprecated: {2}
179
classpath_deprecated_variable = Classpath variable ''{0}'' in project ''{1}'' is deprecated: {2}
180
classpath_indexfile_notpresent = Index file ''{0}'' specified for ''{1}'' is not present
181
classpath_indexfile_errorreading = Error reading the index file ''{0}'' specified for ''{1}''
180
182
181
### miscellaneous
183
### miscellaneous
182
buffer_closed=Buffer is closed
184
buffer_closed=Buffer is closed

Return to bug 368523