### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core Index: dom/org/eclipse/jdt/core/dom/rewrite/ImportRewrite.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/rewrite/ImportRewrite.java,v retrieving revision 1.18 diff -u -r1.18 ImportRewrite.java --- dom/org/eclipse/jdt/core/dom/rewrite/ImportRewrite.java 21 Jan 2010 16:46:38 -0000 1.18 +++ dom/org/eclipse/jdt/core/dom/rewrite/ImportRewrite.java 1 Feb 2010 15:42:03 -0000 @@ -137,6 +137,7 @@ private String[] createdStaticImports; private boolean filterImplicitImports; + private boolean mainTypeTypesAreImplicitImports; /** * Creates a {@link ImportRewrite} from a {@link ICompilationUnit}. If restoreExistingImports @@ -221,6 +222,7 @@ this.restoreExistingImports= false; } this.filterImplicitImports= true; + this.mainTypeTypesAreImplicitImports= true; this.defaultContext= new ImportRewriteContext() { public int findInContext(String qualifier, String name, int kind) { @@ -303,15 +305,45 @@ } /** - * Specifies that implicit imports (types in default package, package java.lang or - * in the same package as the rewrite compilation unit should not be created except if necessary - * to resolve an on-demand import conflict. The filter is enabled by default. - * @param filterImplicitImports if set, implicit imports will be filtered. + * Specifies that implicit imports (for types in java.lang, types in the same package as the rewrite + * compilation unit, and types in the compilation unit's main type) should not be created, except if necessary to + * resolve an on-demand import conflict. + *

+ * The filter is enabled by default. + *

+ *

+ * Note: {@link #setMainTypeTypesAreImplicitImports(boolean)} can be used to stop filtering types in the compilation + * unit's main type. + *

+ * + * @param filterImplicitImports + * if true, implicit imports will be filtered + * + * @see #setMainTypeTypesAreImplicitImports(boolean) */ public void setFilterImplicitImports(boolean filterImplicitImports) { this.filterImplicitImports= filterImplicitImports; } + /** + * Sets whether types in the compilation unit's main type are considered as implicit imports when + * {@link #setFilterImplicitImports(boolean)} is enabled. + *

+ * By default, the option is enabled, i.e. types in the compilation unit's main type are considered as implicit + * imports. Note that this interpretation is only correct for type references inside direct children of the main + * type + *

+ * + * @param mainTypeTypesAreImplicitImports + * the mainTypeTypesAreImplicitImports to set + * + * @see #setFilterImplicitImports(boolean) + * @since 3.6 + */ + public void setMainTypeTypesAreImplicitImports(boolean mainTypeTypesAreImplicitImports) { + this.mainTypeTypesAreImplicitImports= mainTypeTypesAreImplicitImports; + } + private static int compareImport(char prefix, String qualifier, String name, String curr) { if (curr.charAt(0) != prefix || !curr.endsWith(name)) { return ImportRewriteContext.RES_NAME_UNKNOWN; @@ -995,6 +1027,7 @@ ImportRewriteAnalyzer computer= new ImportRewriteAnalyzer(this.compilationUnit, usedAstRoot, this.importOrder, this.importOnDemandThreshold, this.staticImportOnDemandThreshold, this.restoreExistingImports); computer.setFilterImplicitImports(this.filterImplicitImports); + computer.setMainTypeTypesAreImplicitImports(this.mainTypeTypesAreImplicitImports); if (this.addedImports != null) { for (int i= 0; i < this.addedImports.size(); i++) { Index: dom/org/eclipse/jdt/internal/core/dom/rewrite/ImportRewriteAnalyzer.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ImportRewriteAnalyzer.java,v retrieving revision 1.21 diff -u -r1.21 ImportRewriteAnalyzer.java --- dom/org/eclipse/jdt/internal/core/dom/rewrite/ImportRewriteAnalyzer.java 7 Jan 2010 20:18:49 -0000 1.21 +++ dom/org/eclipse/jdt/internal/core/dom/rewrite/ImportRewriteAnalyzer.java 1 Feb 2010 15:42:03 -0000 @@ -53,6 +53,7 @@ private final int staticImportOnDemandThreshold; private boolean filterImplicitImports; + private boolean mainTypeTypesAreImplicitImports; private boolean findAmbiguousImports; private int flags= 0; @@ -68,6 +69,7 @@ this.staticImportOnDemandThreshold= staticThreshold; this.filterImplicitImports= true; + this.mainTypeTypesAreImplicitImports= true; this.findAmbiguousImports= true; //!restoreExistingImports; this.packageEntries= new ArrayList(20); @@ -238,18 +240,42 @@ } /** - * Sets that implicit imports (types in default package, CU- package and - * 'java.lang') should not be created. Note that this is a heuristic filter and can - * lead to missing imports, e.g. in cases where a type is forced to be specified - * due to a name conflict. - * By default, the filter is enabled. - * @param filterImplicitImports The filterImplicitImports to set + * Specifies that implicit imports (for types in java.lang, types in the same package as the rewrite + * compilation unit and types in the compilation unit's main type) should not be created, except if necessary to + * resolve an on-demand import conflict. + *

+ * The filter is enabled by default. + *

+ *

+ * Note: {@link #setMainTypeTypesAreImplicitImports(boolean)} can be used to stop filtering types in the compilation + * unit's main type. + *

+ * + * @param filterImplicitImports + * if true, implicit imports will be filtered + * + * @see #setMainTypeTypesAreImplicitImports(boolean) */ public void setFilterImplicitImports(boolean filterImplicitImports) { this.filterImplicitImports= filterImplicitImports; } /** + * Sets whether types in the compilation unit's main type are considered as implicit imports when + * {@link #setFilterImplicitImports(boolean)} is enabled. By default, the option is enabled, i.e. types in the + * compilation unit's main type are considered as implicit imports. + * + * @param mainTypeTypesAreImplicitImports + * the mainTypeTypesAreImplicitImports to set + * + * @see #setFilterImplicitImports(boolean) + * @since 3.6 + */ + public void setMainTypeTypesAreImplicitImports(boolean mainTypeTypesAreImplicitImports) { + this.mainTypeTypesAreImplicitImports= mainTypeTypesAreImplicitImports; + } + + /** * When set searches for imports that can not be folded into on-demand * imports but must be specified explicitly * @param findAmbiguousImports The new value @@ -385,14 +411,20 @@ return bestMatch; } - private static boolean isImplicitImport(String qualifier, ICompilationUnit cu) { + private boolean isImplicitImport(String qualifier) { if (JAVA_LANG.equals(qualifier)) { return true; } + ICompilationUnit cu= this.compilationUnit; String packageName= cu.getParent().getElementName(); if (qualifier.equals(packageName)) { return true; } + + if (!this.mainTypeTypesAreImplicitImports) { + return false; + } + String mainTypeName= JavaCore.removeJavaLikeExtension(cu.getElementName()); if (packageName.length() == 0) { return qualifier.equals(mainTypeName); @@ -524,7 +556,7 @@ PackageEntry pack= (PackageEntry) this.packageEntries.get(i); int nImports= pack.getNumberOfImports(); - if (this.filterImplicitImports && !pack.isStatic() && isImplicitImport(pack.getName(), this.compilationUnit)) { + if (this.filterImplicitImports && !pack.isStatic() && isImplicitImport(pack.getName())) { pack.removeAllNew(onDemandConflicts); nImports= pack.getNumberOfImports(); } #P org.eclipse.jdt.ui Index: core extension/org/eclipse/jdt/internal/corext/codemanipulation/OrganizeImportsOperation.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/codemanipulation/OrganizeImportsOperation.java,v retrieving revision 1.94 diff -u -r1.94 OrganizeImportsOperation.java --- core extension/org/eclipse/jdt/internal/corext/codemanipulation/OrganizeImportsOperation.java 8 Jul 2009 12:36:11 -0000 1.94 +++ core extension/org/eclipse/jdt/internal/corext/codemanipulation/OrganizeImportsOperation.java 1 Feb 2010 15:42:07 -0000 @@ -425,6 +425,7 @@ } ImportRewrite importsRewrite= StubUtility.createImportRewrite(astRoot, false); + importsRewrite.setMainTypeTypesAreImplicitImports(false); Set/**/ oldSingleImports= new HashSet(); Set/**/ oldDemandImports= new HashSet();