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

(-)model/org/eclipse/jdt/internal/core/SearchableEnvironment.java (-3 / +15 lines)
Lines 24-29 Link Here
24
import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
24
import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
25
import org.eclipse.jdt.internal.core.search.BasicSearchEngine;
25
import org.eclipse.jdt.internal.core.search.BasicSearchEngine;
26
import org.eclipse.jdt.internal.core.search.IRestrictedAccessTypeRequestor;
26
import org.eclipse.jdt.internal.core.search.IRestrictedAccessTypeRequestor;
27
import org.eclipse.jdt.internal.core.util.Util;
27
28
28
/**
29
/**
29
 *	This class provides a <code>SearchableBuilderEnvironment</code> for code assist which
30
 *	This class provides a <code>SearchableBuilderEnvironment</code> for code assist which
Lines 35-40 Link Here
35
	public NameLookup nameLookup;
36
	public NameLookup nameLookup;
36
	protected ICompilationUnit unitToSkip;
37
	protected ICompilationUnit unitToSkip;
37
	protected org.eclipse.jdt.core.ICompilationUnit[] workingCopies;
38
	protected org.eclipse.jdt.core.ICompilationUnit[] workingCopies;
39
	protected WorkingCopyOwner owner;
38
40
39
	protected JavaProject project;
41
	protected JavaProject project;
40
	protected IJavaSearchScope searchScope;
42
	protected IJavaSearchScope searchScope;
Lines 58-63 Link Here
58
	 */
60
	 */
59
	public SearchableEnvironment(JavaProject project, WorkingCopyOwner owner) throws JavaModelException {
61
	public SearchableEnvironment(JavaProject project, WorkingCopyOwner owner) throws JavaModelException {
60
		this(project, owner == null ? null : JavaModelManager.getJavaModelManager().getWorkingCopies(owner, true/*add primary WCs*/));
62
		this(project, owner == null ? null : JavaModelManager.getJavaModelManager().getWorkingCopies(owner, true/*add primary WCs*/));
63
		this.owner = owner;
61
	}
64
	}
62
65
63
	private static int convertSearchFilterToModelFilter(int searchFilter) {
66
	private static int convertSearchFilterToModelFilter(int searchFilter) {
Lines 85-90 Link Here
85
	protected NameEnvironmentAnswer find(String typeName, String packageName) {
88
	protected NameEnvironmentAnswer find(String typeName, String packageName) {
86
		if (packageName == null)
89
		if (packageName == null)
87
			packageName = IPackageFragment.DEFAULT_PACKAGE_NAME;
90
			packageName = IPackageFragment.DEFAULT_PACKAGE_NAME;
91
		if (this.owner != null) {
92
			String source = this.owner.findSource(typeName, packageName);
93
			if (source != null) {
94
				ICompilationUnit cu = new BasicCompilationUnit(source.toCharArray(), CharOperation.splitOn('.', packageName.toCharArray()), typeName + Util.defaultJavaExtension());
95
				return new NameEnvironmentAnswer(cu, null);
96
			}
97
		}
88
		NameLookup.Answer answer =
98
		NameLookup.Answer answer =
89
			this.nameLookup.findType(
99
			this.nameLookup.findType(
90
				typeName,
100
				typeName,
Lines 98-104 Link Here
98
				try {
108
				try {
99
					return new NameEnvironmentAnswer((IBinaryType) ((BinaryType) answer.type).getElementInfo(), answer.restriction);
109
					return new NameEnvironmentAnswer((IBinaryType) ((BinaryType) answer.type).getElementInfo(), answer.restriction);
100
				} catch (JavaModelException npe) {
110
				} catch (JavaModelException npe) {
101
					return null;
111
					// fall back to using owner
102
				}
112
				}
103
			} else { //SourceType
113
			} else { //SourceType
104
				try {
114
				try {
Lines 123-129 Link Here
123
					}
133
					}
124
					return new NameEnvironmentAnswer(sourceTypes, answer.restriction);
134
					return new NameEnvironmentAnswer(sourceTypes, answer.restriction);
125
				} catch (JavaModelException npe) {
135
				} catch (JavaModelException npe) {
126
					return null;
136
					// fall back to using owner
127
				}
137
				}
128
			}
138
			}
129
		}
139
		}
Lines 501-507 Link Here
501
				pkgName[i] = new String(parentPackageName[i]);
511
				pkgName[i] = new String(parentPackageName[i]);
502
			pkgName[length] = new String(subPackageName);
512
			pkgName[length] = new String(subPackageName);
503
		}
513
		}
504
		return this.nameLookup.isPackage(pkgName);
514
		return 
515
			(this.owner != null && this.owner.isPackage(pkgName))
516
			|| this.nameLookup.isPackage(pkgName);
505
	}
517
	}
506
518
507
	/**
519
	/**
(-)model/org/eclipse/jdt/core/WorkingCopyOwner.java (+74 lines)
Lines 89-94 Link Here
89
	public IProblemRequestor getProblemRequestor(ICompilationUnit workingCopy) {
89
	public IProblemRequestor getProblemRequestor(ICompilationUnit workingCopy) {
90
		return null;
90
		return null;
91
	}
91
	}
92
	
93
	/**
94
	 * Returns the source of the compilation unit that defines the given type in
95
	 * the given package, or <code>null</code> if the type is unknown to this
96
	 * owner.
97
	 * <p>This method is called before the normal lookup (i.e. before looking 
98
	 * at the project's classpath and before looking at the working copies of this 
99
	 * owner.)</p>
100
	 * <p>This allows to provide types that are not normally available, or to hide 
101
	 * types that would normally be available by returning an empty source for 
102
	 * the given type and package.</p>
103
	 * <p>Example of use:
104
	 * <pre>
105
	 * WorkingCopyOwner owner = new WorkingCopyOwner() {
106
	 *   public String findSource(String typeName, String packageName) {
107
	 *     if ("to.be".equals(packageName) && "Generated".equals(typeName)) {
108
	 *       return
109
	 *         "package to.be;\n" +
110
	 *         "public class Generated {\n" +
111
	 *         "}";
112
	 *     }
113
	 *     return super.findSource(typeName, packageName);
114
	 *   }
115
	 *   public boolean isPackage(String[] pkg) {
116
	 *     switch (pkg.length) {
117
	 *     case 1:
118
	 *       return "to".equals(pkg[0]);
119
	 *     case 2:
120
	 *       return "to".equals(pkg[0]) && "be".equals(pkg[1]);
121
	 *     }
122
	 *     return false;
123
	 *   }
124
	 * };
125
	 * // Working copy on X.java with the following contents:
126
	 * //    public class X extends to.be.Generated {
127
	 * //    }
128
	 * ICompilationUnit workingCopy = ... 
129
	 * ASTParser parser = ASTParser.newParser(AST.JLS3);
130
	 * parser.setSource(workingCopy);
131
	 * parser.setResolveBindings(true);
132
	 * parser.setWorkingCopyOwner(owner);
133
	 * CompilationUnit cu = (CompilationUnit) parser.createAST(null);
134
	 * assert cu.getProblems().length == 0;
135
	 * </pre>
136
	 * </p>
137
	 * 
138
	 * @param typeName the simple name of the type to lookup
139
	 * @param packageName the dot-separated name of the package of type
140
	 * @return the source of the compilation unit that defines the given type in
141
	 * the given package, or <code>null</code> if the type is unknown
142
	 * @see #isPackage(String[])
143
	 * @since 3.5
144
	 */
145
	public String findSource(String typeName, String packageName) {
146
		return null;
147
	}
148
	
149
	/**
150
	 * Returns whether the given package segments represent a package.
151
	 * <p>This method is called before the normal lookup (i.e. before looking 
152
	 * at the project's classpath and before looking at the working copies of this 
153
	 * owner.)</p>
154
	 * <p>This allows to provide packages that are not normally available.</p>
155
	 * <p>If <code>false</code> is returned, then normal lookup is used on 
156
	 * this package.</p>
157
	 * 
158
	 * @param pkg the segments of a package to lookup
159
	 * @return whether the given package segments represent a package.
160
	 * @see #findSource(String, String)
161
	 * @since 3.5
162
	 */
163
	public boolean isPackage(String[] pkg) {
164
		return false;
165
	}
92
166
93
	/**
167
	/**
94
	 * Returns a new working copy with the given name using this working copy owner to
168
	 * Returns a new working copy with the given name using this working copy owner to
(-)src/org/eclipse/jdt/core/tests/model/AbstractJavaModelTests.java (+8 lines)
Lines 696-701 Link Here
696
		assertEquals("Unexpected member value pair", expected, actual);
696
		assertEquals("Unexpected member value pair", expected, actual);
697
	}
697
	}
698
698
699
	protected void assertProblems(String message, String expected, IProblem[] problems, char[] source) {
700
		ProblemRequestor pbRequestor = new ProblemRequestor();
701
		pbRequestor.unitSource = source;
702
		for (int i = 0, length = problems.length; i < length; i++) {
703
			pbRequestor.acceptProblem(problems[i]);
704
		}
705
		assertProblems(message, expected, pbRequestor);
706
	}
699
	protected void assertProblems(String message, String expected, ProblemRequestor problemRequestor) {
707
	protected void assertProblems(String message, String expected, ProblemRequestor problemRequestor) {
700
		String actual = org.eclipse.jdt.core.tests.util.Util.convertToIndependantLineDelimiter(problemRequestor.problems.toString());
708
		String actual = org.eclipse.jdt.core.tests.util.Util.convertToIndependantLineDelimiter(problemRequestor.problems.toString());
701
		String independantExpectedString = org.eclipse.jdt.core.tests.util.Util.convertToIndependantLineDelimiter(expected);
709
		String independantExpectedString = org.eclipse.jdt.core.tests.util.Util.convertToIndependantLineDelimiter(expected);
(-)src/org/eclipse/jdt/core/tests/model/WorkingCopyOwnerTests.java (+71 lines)
Lines 79-84 Link Here
79
		super.tearDown();
79
		super.tearDown();
80
	}
80
	}
81
81
82
	private void assertProblems(String expectedProblems, String path, String source, WorkingCopyOwner owner) throws JavaModelException {
83
		this.workingCopy = getWorkingCopy(path, source);
84
		ASTParser parser = ASTParser.newParser(AST.JLS3);
85
		parser.setSource(this.workingCopy);
86
		parser.setResolveBindings(true);
87
		parser.setWorkingCopyOwner(owner);
88
		CompilationUnit cu = (CompilationUnit) parser.createAST(null);
89
		assertProblems("Unexpected problems", expectedProblems, cu.getProblems(), source.toCharArray());
90
	}
91
	
82
	protected void assertTypeBindingsEqual(String message, String expected, ITypeBinding[] types) {
92
	protected void assertTypeBindingsEqual(String message, String expected, ITypeBinding[] types) {
83
		StringBuffer buffer = new StringBuffer();
93
		StringBuffer buffer = new StringBuffer();
84
		if (types == null) {
94
		if (types == null) {
Lines 617-622 Link Here
617
	}
627
	}
618
628
619
	/*
629
	/*
630
	 * Ensures that a type that is otherwise missing can be looked up using the working copy owner.
631
	 */
632
	public void testFindType1() throws Exception {
633
		WorkingCopyOwner owner = new WorkingCopyOwner() {
634
			public String findSource(String typeName, String packageName) {
635
				if ("to.be".equals(packageName) && "Generated".equals(typeName)) {
636
					return
637
						"package to.be;\n" +
638
						"public class Generated {\n" +
639
						"}";
640
				}
641
				return super.findSource(typeName, packageName);
642
			}
643
			public boolean isPackage(String[] pkg) {
644
				switch (pkg.length) {
645
				case 1:
646
					return "to".equals(pkg[0]);
647
				case 2:
648
					return "to".equals(pkg[0]) && "be".equals(pkg[1]);
649
				}
650
				return false;
651
			}
652
		};
653
		assertProblems(
654
			"",
655
			"/P/X.java",
656
			"public class X extends to.be.Generated {\n" +
657
			"}", 
658
			owner);
659
	}
660
661
	/*
662
	 * Ensures that a type that is otherwise available can be hidden using the working copy owner.
663
	 */
664
	public void testFindType2() throws Exception {
665
		try {
666
			createFile("/P/Y.java", "public class Y {}");
667
			WorkingCopyOwner owner = new WorkingCopyOwner() {
668
				public String findSource(String typeName, String packageName) {
669
					if ("".equals(packageName) && "Y".equals(typeName)) {
670
						return "";
671
					}
672
					return super.findSource(typeName, packageName);
673
				}
674
			};
675
			assertProblems(
676
				"1. ERROR in /P/X.java (at line 1)\n" + 
677
				"	public class X extends Y {\n" + 
678
				"	                       ^\n" + 
679
				"Y cannot be resolved to a type\n" + 
680
				"----------\n",
681
				"/P/X.java",
682
				"public class X extends Y {\n" +
683
				"}", 
684
				owner);
685
		} finally {
686
			deleteFile("/P/Y.java");
687
		}
688
	}
689
690
	/*
620
	 * Ensures that getCorrespondingResource() returns a non-null value for a primary working copy.
691
	 * Ensures that getCorrespondingResource() returns a non-null value for a primary working copy.
621
	 * (regression test for bug 44065 NPE during hot code replace)
692
	 * (regression test for bug 44065 NPE during hot code replace)
622
	 */
693
	 */

Return to bug 257528