Summary: | An API to incrementaly generate compilation units for binding resolution environment | ||||||
---|---|---|---|---|---|---|---|
Product: | [Eclipse Project] JDT | Reporter: | Satish C Gupta <satish.gupta> | ||||
Component: | Core | Assignee: | Jerome Lanneluc <jerome_lanneluc> | ||||
Status: | VERIFIED FIXED | QA Contact: | |||||
Severity: | enhancement | ||||||
Priority: | P3 | CC: | kent_johnson, Olivier_Thomann, philippe_mulet | ||||
Version: | 3.4.2 | ||||||
Target Milestone: | 3.5 M6 | ||||||
Hardware: | PC | ||||||
OS: | Windows XP | ||||||
Whiteboard: | |||||||
Attachments: |
|
Description
Satish C Gupta
2008-12-04 08:33:54 EST
For 2) since this is a separate request, could you please enter a separate bug report. For 1), would these 2 APIs on org.eclipse.jdt.core.WorkingCopyOwner work for you? /** * Hook to add more types that are not available through normal lookup (i.e. * types that are not available on the project's classpath, nor through the * working copies of this owner). * <p>Example of use: * <pre> * WorkingCopyOwner owner = new WorkingCopyOwner() { * public String findSource(String typeName, String packageName) { * if ("to.be".equals(packageName) && "Generated".equals(typeName)) { * return * "package to.be;\n" + * "public class Generated {\n" + * "}"; * } * return super.findSource(typeName, packageName); * } * public boolean isPackage(String[] pkg) { * switch (pkg.length) { * case 1: * return "to".equals(pkg[0]); * case 2: * return "to".equals(pkg[0]) && "be".equals(pkg[1]); * } * return false; * } * }; * ICompilationUnit workingCopy = * getWorkingCopy( * "/P/X.java", * "public class X extends to.be.Generated {\n" + * "}"); * ASTParser parser = ASTParser.newParser(AST.JLS3); * parser.setSource(workingCopy); * parser.setResolveBindings(true); * parser.setWorkingCopyOwner(owner); * CompilationUnit cu = (CompilationUnit) parser.createAST(null); * assert cu.getProblems().length == 0; * </pre> * </p> * * @param typeName the simple name of the type to lookup * @param packageName the dot-separated name of the package of type * @see #isPackage(String[]) * @since 3.5 */ public String findSource(String typeName, String packageName) /** * Hook to add more packages that are not available through normal lookup (i.e. * packages that are not available on the project's classpath, nor through the * working copies of this owner). * * @param pkg the segments of a package to lookup * @see #findSource(String, String) * @since 3.5 */ public boolean isPackage(String[] pkg) Re: public String findSource(String typeName, String packageName) It should answer 'null' to indicate deferring to standard lookup. Should clarify that this type lookup could be answering CU source (could contain multiple types). Re: public boolean isPackage(String[] pkg) This should be a 3-way question. YES / NO / DUNNO. Only if DUNNO would it defer to standard lookup to find out. Note: my previous comment assumed these new hooks were called before doing standard lookup. Talking with Jerome, it wasn't the intent. I think it should do hook pre-lookup still to match the mode where working copies are being supplied to hide existing resources. The suggested API seems sufficient, except it is not clear which getWorkingCopy() (which class has this function) is being called in: * ICompilationUnit workingCopy = * getWorkingCopy( * "/P/X.java", * "public class X extends to.be.Generated {\n" + * "}"); I am most probably missing something here, but I couldn't locate a getWorkingCopy() that takes a unit-name and source string and creates a ICompilationUnit. Looking at Philippe's I realize that there are two scenarios: 1) Ability to supply classes not yet found by standard lookup: This is what my current need is. When a class to.be.Generated (in the API documentation in comment #1 ) is looked up and found non-existing, I need a hook to be able to generate it. However, when the class to.be.Generated is looked up again, as in processing following, the standard lookup should be able to find to.be.Generated without requiring to.be.Generated to be generated again: ICompilationUnit workingCopy = getWorkingCopy( "/P/X.java", "public class X extends to.be.Generated {\n" + " public static class FuzzyX extends to.be.Generated {\n" + " }" + "}"); 2) Ability to supply classes that hide classes visible in standard lookup: I don't need this ability at present, but I can see that it can be pretty useful in certain situations. I guess the question is how to indicate JDT to call findSource() before or after its standard lookup. (In reply to comment #4) > The suggested API seems sufficient, except it is not clear which > getWorkingCopy() This was meant to be pseudo-code (ie. there are many ways to create a working copy, but this is out of the scope of this API). I tried to clarify that in the new version of the API below. Also, I tried to clarify when the findSource() and isPackage() methods are called. /** * Returns the source of the compilation unit that defines the given type in * the given package, or <code>null</code> if the type is unknown to this * owner. * <p>This method is called before the normal lookup (i.e. before looking * at the project's classpath and before looking at the working copies of this * owner.)</p> * <p>This allows to provide types that are not normally available, or to hide * types that would normally be available by returning an empty source for * the given type and package.</p> * <p>Example of use: * <pre> * WorkingCopyOwner owner = new WorkingCopyOwner() { * public String findSource(String typeName, String packageName) { * if ("to.be".equals(packageName) && "Generated".equals(typeName)) { * return * "package to.be;\n" + * "public class Generated {\n" + * "}"; * } * return super.findSource(typeName, packageName); * } * public boolean isPackage(String[] pkg) { * switch (pkg.length) { * case 1: * return "to".equals(pkg[0]); * case 2: * return "to".equals(pkg[0]) && "be".equals(pkg[1]); * } * return false; * } * }; * // Working copy on X.java with the following contents: * // public class X extends to.be.Generated { * // } * ICompilationUnit workingCopy = ... * ASTParser parser = ASTParser.newParser(AST.JLS3); * parser.setSource(workingCopy); * parser.setResolveBindings(true); * parser.setWorkingCopyOwner(owner); * CompilationUnit cu = (CompilationUnit) parser.createAST(null); * assert cu.getProblems().length == 0; * </pre> * </p> * * @param typeName the simple name of the type to lookup * @param packageName the dot-separated name of the package of type * @return the source of the compilation unit that defines the given type in * the given package, or <code>null</code> if the type is unknown * @see #isPackage(String[]) * @since 3.5 */ public String findSource(String typeName, String packageName) { return null; } /** * Returns whether the given package segments represent a package. * <p>This method is called before the normal lookup (i.e. before looking * at the project's classpath and before looking at the working copies of this * owner.)</p> * <p>This allows to provide packages that are not normally available.</p> * <p>If <code>false</code> is returned, then normal lookup is used on * this package.</p> * * @param pkg the segments of a package to lookup * @return whether the given package segments represent a package. * @see #findSource(String, String) * @since 3.5 */ public boolean isPackage(String[] pkg) { return false; } Created attachment 120323 [details]
Corresponding implementation and tests
The API provided in the patch is sufficient for me. Thanks, +satish API, implementation and tests released for 3.5M6 Verified for 3.5M6 using I20090310-0100 (looking at source code). |