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 / +38 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2008 IBM Corporation and others.
2
 * Copyright (c) 2000, 2009 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-30 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.CharOperation;
26
import org.eclipse.jdt.core.compiler.InvalidInputException;
25
import org.eclipse.jdt.core.search.IJavaSearchScope;
27
import org.eclipse.jdt.core.search.IJavaSearchScope;
26
import org.eclipse.jdt.core.search.SearchEngine;
28
import org.eclipse.jdt.core.search.SearchEngine;
27
import org.eclipse.jdt.core.search.SearchParticipant;
29
import org.eclipse.jdt.core.search.SearchParticipant;
30
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
31
import org.eclipse.jdt.internal.compiler.parser.Scanner;
32
import org.eclipse.jdt.internal.compiler.parser.TerminalTokens;
28
import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable;
33
import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable;
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;
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) && (indexedFileNames.get(zipEntryName) != null)
163
								|| isValidPackageNameForClass(zipEntryName))
164
								// the class file may not be there if the package name is not valid
157
							indexedFileNames.put(zipEntryName, EXISTS);
165
							indexedFileNames.put(zipEntryName, EXISTS);
158
					}
166
					}
159
					boolean needToReindex = indexedFileNames.elementSize != max; // a new file was added
167
					boolean needToReindex = indexedFileNames.elementSize != max; // a new file was added
Lines 195-201 Link Here
195
203
196
					// iterate each entry to index it
204
					// iterate each entry to index it
197
					ZipEntry ze = (ZipEntry) e.nextElement();
205
					ZipEntry ze = (ZipEntry) e.nextElement();
198
					if (Util.isClassFileName(ze.getName())) {
206
					String zipEntryName = ze.getName();
207
					if (Util.isClassFileName(zipEntryName) && 
208
							isValidPackageNameForClass(zipEntryName)) {
209
						// 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);
210
						final byte[] classFileBytes = org.eclipse.jdt.internal.compiler.util.Util.getZipEntryByteContent(ze, zip);
200
						JavaSearchDocument entryDocument = new JavaSearchDocument(ze, zipFilePath, classFileBytes, participant);
211
						JavaSearchDocument entryDocument = new JavaSearchDocument(ze, zipFilePath, classFileBytes, participant);
201
						this.manager.indexDocument(entryDocument, participant, index, this.containerPath);
212
						this.manager.indexDocument(entryDocument, participant, index, this.containerPath);
Lines 235-238 Link Here
235
	public String toString() {
246
	public String toString() {
236
		return "indexing " + this.containerPath.toString(); //$NON-NLS-1$
247
		return "indexing " + this.containerPath.toString(); //$NON-NLS-1$
237
	}
248
	}
249
		
250
	private  boolean isValidPackageNameForClass(String className) {
251
		char[] classNameArray = className.toCharArray();
252
		int start = 0;
253
		int end;
254
		if (this.scanner == null)
255
			this.scanner = new Scanner(false /* comment */, true /* whitespace */, false /* nls */,
256
					ClassFileConstants.JDK1_5/* sourceLevel */, null/* taskTag */, null/* taskPriorities */, true /* taskCaseSensitive */);
257
		this.scanner.setSource(classNameArray);
258
		// all the folders should be valid java names and the class name need not be validated
259
		while ((end = CharOperation.indexOf('/', classNameArray, start)) != -1) {
260
			this.scanner.startPosition = this.scanner.currentPosition = start;
261
			this.scanner.eofPosition = end;
262
			start = end +1;
263
			try {
264
				if (this.scanner.scanIdentifier() != TerminalTokens.TokenNameIdentifier ||
265
					this.scanner.currentPosition != this.scanner.eofPosition) 
266
					return false;
267
			} catch (InvalidInputException e) {
268
				return false;
269
			}
270
		}
271
		return true;
272
	}
238
}
273
}
(-)src/org/eclipse/jdt/core/tests/model/JavaSearchBugsTests.java (+45 lines)
Lines 11070-11073 Link Here
11070
		removeClasspathEntry(JAVA_PROJECT, new Path(libPath));
11070
		removeClasspathEntry(JAVA_PROJECT, new Path(libPath));
11071
	}
11071
	}
11072
}
11072
}
11073
/**
11074
 * @bug 293861: Problem with refactoring when existing jar with invalid package names
11075
 * @test Ensure that the search doesn't return classes with invalid package names
11076
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=293861"
11077
 */
11078
public void testBug293861a() throws CoreException {
11079
	try 
11080
	{
11081
		IJavaProject project = createJavaProject("P");
11082
		addClasspathEntry(project, JavaCore.newLibraryEntry(new Path("/JavaSearchBugs/lib/b293861.jar"), null, null));
11083
		int mask = IJavaSearchScope.APPLICATION_LIBRARIES | IJavaSearchScope.SOURCES | IJavaSearchScope.REFERENCED_PROJECTS;
11084
		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { project }, mask);
11085
				
11086
		search("b293861TestFunc", IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS, scope);
11087
		assertSearchResults("No search results expected", "", this.resultCollector);
11088
	} finally {
11089
		deleteProject("P");
11090
	}
11091
}
11092
11093
/*
11094
 * SearchEngine#searchAllTypeNames should also not return classes with invalid package names
11095
 */
11096
public void testBug293861b() throws CoreException {
11097
	try
11098
	{
11099
		IJavaProject project = createJavaProject("P");
11100
		addClasspathEntry(project, JavaCore.newLibraryEntry(new Path("/JavaSearchBugs/lib/b293861.jar"), null, null));
11101
		int mask = IJavaSearchScope.APPLICATION_LIBRARIES | IJavaSearchScope.SOURCES | IJavaSearchScope.REFERENCED_PROJECTS;
11102
		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { project }, mask);
11103
		
11104
		TypeNameMatchCollector collector = new TypeNameMatchCollector();
11105
		new SearchEngine().searchAllTypeNames(
11106
				null,
11107
				new char[][] {"b293861Test".toCharArray()},
11108
				scope,
11109
				collector,
11110
				IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH,
11111
				null);
11112
		assertSearchResults("No search results expected", "", collector);		
11113
	} finally {
11114
		deleteProject("P");
11115
	}
11116
}
11117
11073
}
11118
}

Return to bug 293861