View | Details | Raw Unified | Return to bug 293861 | Differences between
and this patch

Collapse All | Expand All

(-)search/org/eclipse/jdt/internal/core/search/indexing/AddJarFileToIndex.java (-3 / +37 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2008 IBM Corporation and others.
2
 * Copyright (c) 2000, 2010 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 22-31 Link Here
22
import org.eclipse.core.runtime.IPath;
22
import org.eclipse.core.runtime.IPath;
23
import org.eclipse.core.runtime.IProgressMonitor;
23
import org.eclipse.core.runtime.IProgressMonitor;
24
import org.eclipse.core.runtime.Path;
24
import org.eclipse.core.runtime.Path;
25
import org.eclipse.jdt.core.compiler.InvalidInputException;
25
import org.eclipse.jdt.core.search.IJavaSearchScope;
26
import org.eclipse.jdt.core.search.IJavaSearchScope;
26
import org.eclipse.jdt.core.search.SearchEngine;
27
import org.eclipse.jdt.core.search.SearchEngine;
27
import org.eclipse.jdt.core.search.SearchParticipant;
28
import org.eclipse.jdt.core.search.SearchParticipant;
29
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
30
import org.eclipse.jdt.internal.compiler.parser.Scanner;
31
import org.eclipse.jdt.internal.compiler.parser.TerminalTokens;
28
import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable;
32
import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable;
33
import org.eclipse.jdt.internal.compiler.util.SuffixConstants;
29
import org.eclipse.jdt.internal.compiler.util.Util;
34
import org.eclipse.jdt.internal.compiler.util.Util;
30
import org.eclipse.jdt.internal.core.JavaModelManager;
35
import org.eclipse.jdt.internal.core.JavaModelManager;
31
import org.eclipse.jdt.internal.core.index.Index;
36
import org.eclipse.jdt.internal.core.index.Index;
Lines 36-41 Link Here
36
41
37
	private static final char JAR_SEPARATOR = IJavaSearchScope.JAR_FILE_ENTRY_SEPARATOR.charAt(0);
42
	private static final char JAR_SEPARATOR = IJavaSearchScope.JAR_FILE_ENTRY_SEPARATOR.charAt(0);
38
	IFile resource;
43
	IFile resource;
44
	Scanner scanner;
39
45
40
	public AddJarFileToIndex(IFile resource, IndexManager manager) {
46
	public AddJarFileToIndex(IFile resource, IndexManager manager) {
41
		super(resource.getFullPath(), manager);
47
		super(resource.getFullPath(), manager);
Lines 153-159 Link Here
153
						// iterate each entry to index it
159
						// iterate each entry to index it
154
						ZipEntry ze = (ZipEntry) e.nextElement();
160
						ZipEntry ze = (ZipEntry) e.nextElement();
155
						String zipEntryName = ze.getName();
161
						String zipEntryName = ze.getName();
156
						if (Util.isClassFileName(zipEntryName))
162
						if (Util.isClassFileName(zipEntryName) && isValidPackageNameForClass(zipEntryName))
163
								// the class file may not be there if the package name is not valid
157
							indexedFileNames.put(zipEntryName, EXISTS);
164
							indexedFileNames.put(zipEntryName, EXISTS);
158
					}
165
					}
159
					boolean needToReindex = indexedFileNames.elementSize != max; // a new file was added
166
					boolean needToReindex = indexedFileNames.elementSize != max; // a new file was added
Lines 195-201 Link Here
195
202
196
					// iterate each entry to index it
203
					// iterate each entry to index it
197
					ZipEntry ze = (ZipEntry) e.nextElement();
204
					ZipEntry ze = (ZipEntry) e.nextElement();
198
					if (Util.isClassFileName(ze.getName())) {
205
					String zipEntryName = ze.getName();
206
					if (Util.isClassFileName(zipEntryName) && 
207
							isValidPackageNameForClass(zipEntryName)) {
208
						// index only classes coming from valid packages - https://bugs.eclipse.org/bugs/show_bug.cgi?id=293861
199
						final byte[] classFileBytes = org.eclipse.jdt.internal.compiler.util.Util.getZipEntryByteContent(ze, zip);
209
						final byte[] classFileBytes = org.eclipse.jdt.internal.compiler.util.Util.getZipEntryByteContent(ze, zip);
200
						JavaSearchDocument entryDocument = new JavaSearchDocument(ze, zipFilePath, classFileBytes, participant);
210
						JavaSearchDocument entryDocument = new JavaSearchDocument(ze, zipFilePath, classFileBytes, participant);
201
						this.manager.indexDocument(entryDocument, participant, index, this.containerPath);
211
						this.manager.indexDocument(entryDocument, participant, index, this.containerPath);
Lines 229-234 Link Here
229
			return super.getJobFamily();
239
			return super.getJobFamily();
230
		return this.containerPath.toOSString(); // external jar
240
		return this.containerPath.toOSString(); // external jar
231
	}
241
	}
242
	private  boolean isValidPackageNameForClass(String className) {
243
		char[] classNameArray = className.toCharArray();
244
		if (this.scanner == null)
245
			this.scanner = new Scanner(false /* comment */, true /* whitespace */, false /* nls */,
246
					ClassFileConstants.JDK1_3/* sourceLevel */, null/* taskTag */, null/* taskPriorities */, true /* taskCaseSensitive */);
247
		this.scanner.setSource(classNameArray); 
248
		this.scanner.eofPosition = classNameArray.length - SuffixConstants.SUFFIX_CLASS.length;
249
		try {
250
			if (this.scanner.scanIdentifier() == TerminalTokens.TokenNameIdentifier) {
251
				while (this.scanner.eofPosition > this.scanner.currentPosition) {
252
					if (this.scanner.getNextChar() != '/' || this.scanner.eofPosition <= this.scanner.currentPosition) {
253
						return false;
254
					}
255
					if (this.scanner.scanIdentifier() != TerminalTokens.TokenNameIdentifier) {
256
						return false;
257
					}
258
				}
259
				return true;
260
			}
261
		} catch (InvalidInputException e) {
262
			// invalid class name
263
		}
264
		return false;
265
	}
232
	protected Integer updatedIndexState() {
266
	protected Integer updatedIndexState() {
233
		return IndexManager.REBUILDING_STATE;
267
		return IndexManager.REBUILDING_STATE;
234
	}
268
	}
(-)src/org/eclipse/jdt/core/tests/model/JavaSearchBugsTests.java (-1 / +46 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
2
 * Copyright (c) 2000, 2010 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 10757-10760 Link Here
10757
	search("f266837", FIELD, DECLARATIONS, requestor);
10757
	search("f266837", FIELD, DECLARATIONS, requestor);
10758
}
10758
}
10759
10759
10760
/**
10761
 * @bug 293861: Problem with refactoring when existing jar with invalid package names
10762
 * @test Ensure that the search doesn't return classes with invalid package names
10763
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=293861"
10764
 */
10765
public void testBug293861a() throws CoreException {
10766
	try 
10767
	{
10768
		IJavaProject project = createJavaProject("P");
10769
		addClasspathEntry(project, JavaCore.newLibraryEntry(new Path("/JavaSearchBugs/lib/b293861.jar"), null, null));
10770
		int mask = IJavaSearchScope.APPLICATION_LIBRARIES | IJavaSearchScope.SOURCES | IJavaSearchScope.REFERENCED_PROJECTS;
10771
		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { project }, mask);
10772
				
10773
		search("b293861TestFunc", IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS, scope);
10774
		assertSearchResults("No search results expected", "", this.resultCollector);
10775
	} finally {
10776
		deleteProject("P");
10777
	}
10778
}
10779
10780
/*
10781
 * SearchEngine#searchAllTypeNames should also not return classes with invalid package names
10782
 */
10783
public void testBug293861b() throws CoreException {
10784
	try
10785
	{
10786
		IJavaProject project = createJavaProject("P");
10787
		addClasspathEntry(project, JavaCore.newLibraryEntry(new Path("/JavaSearchBugs/lib/b293861.jar"), null, null));
10788
		int mask = IJavaSearchScope.APPLICATION_LIBRARIES | IJavaSearchScope.SOURCES | IJavaSearchScope.REFERENCED_PROJECTS;
10789
		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { project }, mask);
10790
		
10791
		TypeNameMatchCollector collector = new TypeNameMatchCollector();
10792
		new SearchEngine().searchAllTypeNames(
10793
				null,
10794
				new char[][] {"b293861Test".toCharArray()},
10795
				scope,
10796
				collector,
10797
				IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH,
10798
				null);
10799
		assertSearchResults("No search results expected", "", collector);		
10800
	} finally {
10801
		deleteProject("P");
10802
	}
10803
}
10804
10760
}
10805
}

Return to bug 293861