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

Collapse All | Expand All

(-)src/org/eclipse/jdt/core/tests/model/CompletionTests2.java (+210 lines)
Lines 33-38 Link Here
33
import org.eclipse.jdt.core.ICompilationUnit;
33
import org.eclipse.jdt.core.ICompilationUnit;
34
import org.eclipse.jdt.core.IJavaProject;
34
import org.eclipse.jdt.core.IJavaProject;
35
import org.eclipse.jdt.core.JavaCore;
35
import org.eclipse.jdt.core.JavaCore;
36
import org.eclipse.jdt.core.tests.util.Util;
36
import org.eclipse.jdt.internal.codeassist.RelevanceConstants;
37
import org.eclipse.jdt.internal.codeassist.RelevanceConstants;
37
import org.eclipse.jdt.internal.core.JavaModelManager;
38
import org.eclipse.jdt.internal.core.JavaModelManager;
38
39
Lines 2338-2341 Link Here
2338
		deleteResource(new File(jarName));
2339
		deleteResource(new File(jarName));
2339
	}
2340
	}
2340
}
2341
}
2342
public void testBug237469a() throws Exception {
2343
	String externalJar1 = Util.getOutputDirectory() + File.separator + "bug237469a.jar"; //$NON-NLS-1$
2344
	String externalJar2 = Util.getOutputDirectory() + File.separator + "bug237469b.jar"; //$NON-NLS-1$
2345
	
2346
	try {
2347
		// create variable
2348
//		JavaCore.setClasspathVariables(
2349
//			new String[] {"JCL_LIB", "JCL_SRC", "JCL_SRCROOT"},
2350
//			new IPath[] {getExternalJCLPath(), getExternalJCLSourcePath(), getExternalJCLRootSourcePath()},
2351
//			null);
2352
		
2353
		
2354
		// create external jar 1
2355
		Util.createJar(
2356
				new String[] {
2357
					"test/IProject.java", //$NON-NLS-1$
2358
					"package test;\n" + //$NON-NLS-1$
2359
					"public class IProject {\n" + //$NON-NLS-1$
2360
					"}" //$NON-NLS-1$
2361
				},
2362
				new HashMap(),
2363
				externalJar1);
2364
		
2365
		// create external jar 2
2366
		Util.createJar(
2367
				new String[] {
2368
					"test/IJavaProject.java", //$NON-NLS-1$
2369
					"package test;\n" + //$NON-NLS-1$
2370
					"import test.IProject;\n" + //$NON-NLS-1$
2371
					"public class IJavaProject {\n" + //$NON-NLS-1$
2372
					"	IProject project = null;\n" + //$NON-NLS-1$
2373
					"}" //$NON-NLS-1$
2374
				},
2375
				null,
2376
				new HashMap(),
2377
				new String[]{externalJar1},
2378
				externalJar2);
2379
2380
		// create P1
2381
		this.createJavaProject(
2382
			"PS1",
2383
			new String[]{"src"},
2384
			new String[]{"JCL_LIB", externalJar1, externalJar2},
2385
			 "bin");
2386
2387
		this.createFolder("/PS1/src/test");
2388
		this.createFile(
2389
				"/PS1/src/test/Y.java",
2390
				"package test;\n"+
2391
				"import test.IProject;\n"+
2392
				"import test.IJavaProject;\n"+
2393
				"public class Y {\n"+
2394
				"  IProject project;\n"+
2395
				"  IJavaProject javaProject;\n"+
2396
				"}");
2397
2398
		// create P2
2399
		this.createJavaProject(
2400
			"PS2",
2401
			new String[]{"src"},
2402
			new String[]{"JCL_LIB", externalJar2},
2403
			new String[]{"/PS1"},
2404
			"bin");
2405
2406
		this.createFolder("/PS2/src/test");
2407
		this.createFile(
2408
				"/PS2/src/test/X.java",
2409
				"package test;\n"+
2410
				"public class X extends test.Y {\n"+
2411
				"  private Object initializer;\n"+
2412
				"  public void foo() {\n"+
2413
				"    initializer\n"+
2414
				"  }\n"+
2415
				"}");
2416
2417
		waitUntilIndexesReady();
2418
2419
		// do completion
2420
		CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2();
2421
		ICompilationUnit cu= getCompilationUnit("PS2", "src", "test", "X.java");
2422
2423
		String str = cu.getSource();
2424
		String completeBehind = "initializer";
2425
		int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length();
2426
		cu.codeComplete(cursorLocation, requestor);
2427
2428
		assertResults(
2429
			"initializer[FIELD_REF]{initializer, Ltest.X;, Ljava.lang.Object;, initializer, "+(R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_EXACT_NAME + R_UNQUALIFIED + R_NON_RESTRICTED) + "}",
2430
			requestor.getResults());
2431
	} finally {
2432
		this.deleteProject("PS1");
2433
		this.deleteProject("PS2");
2434
		this.deleteFile(externalJar1);
2435
		this.deleteFile(externalJar2);
2436
	}
2437
}
2438
public void testBug237469b() throws Exception {
2439
	String externalJar1 = Util.getOutputDirectory() + File.separator + "bug237469a.jar"; //$NON-NLS-1$
2440
	String externalJar2 = Util.getOutputDirectory() + File.separator + "bug237469b.jar"; //$NON-NLS-1$
2441
	
2442
	try {
2443
		// create variable
2444
//		JavaCore.setClasspathVariables(
2445
//			new String[] {"JCL_LIB", "JCL_SRC", "JCL_SRCROOT"},
2446
//			new IPath[] {getExternalJCLPath(), getExternalJCLSourcePath(), getExternalJCLRootSourcePath()},
2447
//			null);
2448
		
2449
		
2450
		// create external jar 1
2451
		Util.createJar(
2452
				new String[] {
2453
					"test/IProject.java", //$NON-NLS-1$
2454
					"package test;\n" + //$NON-NLS-1$
2455
					"public class IProject {\n" + //$NON-NLS-1$
2456
					"}" //$NON-NLS-1$
2457
				},
2458
				new HashMap(),
2459
				externalJar1);
2460
		
2461
		// create external jar 2
2462
		Util.createJar(
2463
				new String[] {
2464
					"test/IJavaProject.java", //$NON-NLS-1$
2465
					"package test;\n" + //$NON-NLS-1$
2466
					"import test.IProject;\n" + //$NON-NLS-1$
2467
					"public class IJavaProject {\n" + //$NON-NLS-1$
2468
					"	IProject project = null;\n" + //$NON-NLS-1$
2469
					"}" //$NON-NLS-1$
2470
				},
2471
				null,
2472
				new HashMap(),
2473
				new String[]{externalJar1},
2474
				externalJar2);
2475
2476
		// create P1
2477
		this.createJavaProject(
2478
			"PS1",
2479
			new String[]{"src"},
2480
			new String[]{"JCL_LIB", externalJar1, externalJar2},
2481
			 "bin");
2482
2483
		this.createFolder("/PS1/src/test");
2484
		this.createFile(
2485
				"/PS1/src/test/Y.java",
2486
				"package test;\n"+
2487
				"import test.IProject;\n"+
2488
				"import test.IJavaProject;\n"+
2489
				"public class Y {\n"+
2490
				"  IProject project;\n"+
2491
				"  IJavaProject javaProject;\n"+
2492
				"}");
2493
2494
		// create P2
2495
		this.createJavaProject(
2496
			"PS2",
2497
			new String[]{"src"},
2498
			new String[]{"JCL_LIB", externalJar2},
2499
			new String[]{"/PS1"},
2500
			"bin");
2501
2502
		this.createFolder("/PS2/src/test");
2503
		this.createFile(
2504
				"/PS2/src/test/X.java",
2505
				"package test;\n"+
2506
				"public class X extends test.Y {\n"+
2507
				"  private X initializer;\n"+
2508
				"  public void foo() {\n"+
2509
				"    Object o; o.equals\n"+
2510
				"  }\n"+
2511
				"}");
2512
2513
		waitUntilIndexesReady();
2514
2515
		// do completion
2516
		CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(false, false, false, false);
2517
		requestor.setRequireExtendedContext(true);
2518
		requestor.setComputeEnclosingElement(false);
2519
		requestor.setComputeVisibleElements(true);
2520
		requestor.setAssignableType("Ltest/X;");
2521
		
2522
		ICompilationUnit cu= getCompilationUnit("PS2", "src", "test", "X.java");
2523
2524
		String str = cu.getSource();
2525
		String completeBehind = "equals";
2526
		
2527
		int tokenStart = str.lastIndexOf(completeBehind);
2528
		int tokenEnd = tokenStart + completeBehind.length() - 1;
2529
		int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length();
2530
		cu.codeComplete(cursorLocation, requestor);
2531
		
2532
		assertResults(
2533
				"completion offset="+(cursorLocation)+"\n" +
2534
				"completion range=["+(tokenStart)+", "+(tokenEnd)+"]\n" +
2535
				"completion token=\"equals\"\n" +
2536
				"completion token kind=TOKEN_KIND_NAME\n" +
2537
				"expectedTypesSignatures=null\n" +
2538
				"expectedTypesKeys=null\n"+
2539
				"completion token location=UNKNOWN\n"+
2540
				"visibleElements={\n" +
2541
				"	initializer {key=Ltest/X;.initializer)Ltest/X;} [in X [in X.java [in test [in src [in PS2]]]]],\n" +
2542
				"}",
2543
				requestor.getContext());
2544
	} finally {
2545
		this.deleteProject("PS1");
2546
		this.deleteProject("PS2");
2547
		this.deleteFile(externalJar1);
2548
		this.deleteFile(externalJar2);
2549
	}
2550
}
2341
}
2551
}
(-)codeassist/org/eclipse/jdt/internal/codeassist/InternalExtendedCompletionContext.java (-36 / +42 lines)
Lines 158-203 Link Here
158
	}
158
	}
159
	
159
	
160
	private void computeVisibleElementBindings() {
160
	private void computeVisibleElementBindings() {
161
		this.hasComputedVisibleElementBindings = true;
161
		CompilationUnitDeclaration previousUnitBeingCompleted = this.lookupEnvironment.unitBeingCompleted;
162
		
162
		this.lookupEnvironment.unitBeingCompleted = this.compilationUnitDeclaration;
163
		Scope scope = this.assistScope;
163
		try {
164
		ASTNode astNode = this.assistNode;
164
			this.hasComputedVisibleElementBindings = true;
165
		boolean notInJavadoc = this.completionContext.javadoc == 0;
165
			
166
		
166
			Scope scope = this.assistScope;
167
		this.visibleLocalVariables = new ObjectVector();
167
			ASTNode astNode = this.assistNode;
168
		this.visibleFields = new ObjectVector();
168
			boolean notInJavadoc = this.completionContext.javadoc == 0;
169
		this.visibleMethods = new ObjectVector();
169
			
170
		this.bindingsToNodes = new HashMap();
170
			this.visibleLocalVariables = new ObjectVector();
171
		
171
			this.visibleFields = new ObjectVector();
172
		ReferenceContext referenceContext = scope.referenceContext();
172
			this.visibleMethods = new ObjectVector();
173
		if (referenceContext instanceof AbstractMethodDeclaration) {
173
			this.bindingsToNodes = new HashMap();
174
			// completion is inside a method body
174
			
175
			searchVisibleVariablesAndMethods(scope, visibleLocalVariables, visibleFields, visibleMethods, notInJavadoc);
175
			ReferenceContext referenceContext = scope.referenceContext();
176
		} else if (referenceContext instanceof TypeDeclaration) {
176
			if (referenceContext instanceof AbstractMethodDeclaration) {
177
			TypeDeclaration typeDeclaration = (TypeDeclaration) referenceContext;
177
				// completion is inside a method body
178
			FieldDeclaration[] fields = typeDeclaration.fields;
178
				searchVisibleVariablesAndMethods(scope, visibleLocalVariables, visibleFields, visibleMethods, notInJavadoc);
179
			if (fields != null) {
179
			} else if (referenceContext instanceof TypeDeclaration) {
180
				done : for (int i = 0; i < fields.length; i++) {
180
				TypeDeclaration typeDeclaration = (TypeDeclaration) referenceContext;
181
					if (fields[i] instanceof Initializer) {
181
				FieldDeclaration[] fields = typeDeclaration.fields;
182
						Initializer initializer = (Initializer) fields[i];
182
				if (fields != null) {
183
						if (initializer.block.sourceStart <= astNode.sourceStart &&
183
					done : for (int i = 0; i < fields.length; i++) {
184
								astNode.sourceStart < initializer.bodyEnd) {
184
						if (fields[i] instanceof Initializer) {
185
							// completion is inside an initializer
185
							Initializer initializer = (Initializer) fields[i];
186
							searchVisibleVariablesAndMethods(scope, visibleLocalVariables, visibleFields, visibleMethods, notInJavadoc);
186
							if (initializer.block.sourceStart <= astNode.sourceStart &&
187
							break done;
187
									astNode.sourceStart < initializer.bodyEnd) {
188
								// completion is inside an initializer
189
								searchVisibleVariablesAndMethods(scope, visibleLocalVariables, visibleFields, visibleMethods, notInJavadoc);
190
								break done;
191
							}
192
						} else {
193
							FieldDeclaration fieldDeclaration = fields[i];
194
							if (fieldDeclaration.initialization != null && 
195
									fieldDeclaration.initialization.sourceStart <= astNode.sourceStart &&
196
									astNode.sourceEnd <= fieldDeclaration.initialization.sourceEnd) {
197
								// completion is inside a field initializer
198
								searchVisibleVariablesAndMethods(scope, visibleLocalVariables, visibleFields, visibleMethods, notInJavadoc);
199
								break done;
200
							}  
188
						}
201
						}
189
					} else {
190
						FieldDeclaration fieldDeclaration = fields[i];
191
						if (fieldDeclaration.initialization != null && 
192
								fieldDeclaration.initialization.sourceStart <= astNode.sourceStart &&
193
								astNode.sourceEnd <= fieldDeclaration.initialization.sourceEnd) {
194
							// completion is inside a field initializer
195
							searchVisibleVariablesAndMethods(scope, visibleLocalVariables, visibleFields, visibleMethods, notInJavadoc);
196
							break done;
197
						}  
198
					}
202
					}
199
				}
203
				}
200
			}
204
			}
205
		} finally {
206
			this.lookupEnvironment.unitBeingCompleted = previousUnitBeingCompleted;
201
		}
207
		}
202
	}
208
	}
203
	
209
	
(-)src/org/eclipse/jdt/core/tests/util/Util.java (-4 / +24 lines)
Lines 152-157 Link Here
152
    return result;
152
    return result;
153
}
153
}
154
public static void compile(String[] pathsAndContents, Map options, String outputPath) {
154
public static void compile(String[] pathsAndContents, Map options, String outputPath) {
155
	compile(pathsAndContents, options, null, outputPath);
156
}
157
public static void compile(String[] pathsAndContents, Map options, String[] classpath, String outputPath) {
155
        IProblemFactory problemFactory = new DefaultProblemFactory(Locale.getDefault());
158
        IProblemFactory problemFactory = new DefaultProblemFactory(Locale.getDefault());
156
        Requestor requestor =
159
        Requestor requestor =
157
            new Requestor(
160
            new Requestor(
Lines 161-167 Link Here
161
                false /* show warning token*/);
164
                false /* show warning token*/);
162
        requestor.outputPath = outputPath.endsWith(File.separator) ? outputPath : outputPath + File.separator;
165
        requestor.outputPath = outputPath.endsWith(File.separator) ? outputPath : outputPath + File.separator;
163
166
164
        INameEnvironment nameEnvironment = new FileSystem(getJavaClassLibs(), new String[] {}, null);
167
        String[] classLibs = getJavaClassLibs();
168
        if (classpath != null) {
169
        	int length = classpath.length;
170
        	int classLibsLength = classLibs.length;
171
        	System.arraycopy(classpath, 0, classpath = new String[classLibsLength + length], classLibsLength, length);
172
        	System.arraycopy(classLibs, 0, classpath, 0, classLibsLength);
173
        } else {
174
        	classpath = classLibs;
175
        }
176
        
177
        INameEnvironment nameEnvironment = new FileSystem(classpath, new String[] {}, null);
165
        IErrorHandlingPolicy errorHandlingPolicy =
178
        IErrorHandlingPolicy errorHandlingPolicy =
166
            new IErrorHandlingPolicy() {
179
            new IErrorHandlingPolicy() {
167
                public boolean proceedOnErrors() {
180
                public boolean proceedOnErrors() {
Lines 316-329 Link Here
316
    compile(pathsAndContents, getCompileOptions(compliance), folderPath);
329
    compile(pathsAndContents, getCompileOptions(compliance), folderPath);
317
}
330
}
318
public static void createJar(String[] pathsAndContents, Map options, String jarPath) throws IOException {
331
public static void createJar(String[] pathsAndContents, Map options, String jarPath) throws IOException {
319
	createJar(pathsAndContents, null, options, jarPath);
332
	createJar(pathsAndContents, null, options, null, jarPath);
320
}
333
}
321
public static void createJar(String[] pathsAndContents, String[] extraPathsAndContents, Map options, String jarPath) throws IOException {
334
public static void createJar(String[] pathsAndContents, String[] extraPathsAndContents, Map options, String jarPath) throws IOException {
335
	createJar(pathsAndContents, extraPathsAndContents, options, null, jarPath);
336
}
337
public static void createJar(String[] pathsAndContents, String[] extraPathsAndContents, Map options, String jarPath, String[] classpath) throws IOException {
338
	createJar(pathsAndContents, extraPathsAndContents, options, classpath, jarPath);
339
}
340
341
public static void createJar(String[] pathsAndContents, String[] extraPathsAndContents, Map options, String[] classpath, String jarPath) throws IOException {
322
    String classesPath = getOutputDirectory() + File.separator + "classes";
342
    String classesPath = getOutputDirectory() + File.separator + "classes";
323
    File classesDir = new File(classesPath);
343
    File classesDir = new File(classesPath);
324
    flushDirectoryContent(classesDir);
344
    flushDirectoryContent(classesDir);
325
	if (pathsAndContents != null) {
345
	if (pathsAndContents != null) {
326
		compile(pathsAndContents, options, classesPath);
346
		compile(pathsAndContents, options, classpath, classesPath);
327
	}
347
	}
328
	for (int i = 0, l = extraPathsAndContents == null ? 0 : extraPathsAndContents.length; i < l; /* inc in loop */) {
348
	for (int i = 0, l = extraPathsAndContents == null ? 0 : extraPathsAndContents.length; i < l; /* inc in loop */) {
329
		File  outputFile = new File(classesPath, extraPathsAndContents[i++]);
349
		File  outputFile = new File(classesPath, extraPathsAndContents[i++]);
Lines 336-342 Link Here
336
	createJar(javaPathsAndContents, null, jarPath, compliance);
356
	createJar(javaPathsAndContents, null, jarPath, compliance);
337
}
357
}
338
public static void createJar(String[] javaPathsAndContents, String[] extraPathsAndContents, String jarPath, String compliance) throws IOException {
358
public static void createJar(String[] javaPathsAndContents, String[] extraPathsAndContents, String jarPath, String compliance) throws IOException {
339
	createJar(javaPathsAndContents, extraPathsAndContents, getCompileOptions(compliance), jarPath);
359
	createJar(javaPathsAndContents, extraPathsAndContents, getCompileOptions(compliance), null, jarPath);
340
}
360
}
341
public static void createSourceZip(String[] pathsAndContents, String zipPath) throws IOException {
361
public static void createSourceZip(String[] pathsAndContents, String zipPath) throws IOException {
342
    String sourcesPath = getOutputDirectory() + File.separator + "sources";
362
    String sourcesPath = getOutputDirectory() + File.separator + "sources";

Return to bug 237469