Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 58667 Details for
Bug 173462
Import rewriter should replace all fully qualified names with short names where possible on re-generation
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
[patch]
To avoid unsafe file saving during adding manual imports it was delegated to the postprocessor itself.
patch.txt (text/plain), 14.54 KB, created by
Anna Karjakina
on 2007-02-09 12:28:08 EST
(
hide
)
Description:
To avoid unsafe file saving during adding manual imports it was delegated to the postprocessor itself.
Filename:
MIME Type:
Creator:
Anna Karjakina
Created:
2007-02-09 12:28:08 EST
Size:
14.54 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.gmf.common >Index: src/org/eclipse/gmf/internal/common/codegen/OrganizeImportsPostprocessor.java >=================================================================== >RCS file: /cvsroot/technology/org.eclipse.gmf/plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/OrganizeImportsPostprocessor.java,v >retrieving revision 1.2 >diff -u -r1.2 OrganizeImportsPostprocessor.java >--- src/org/eclipse/gmf/internal/common/codegen/OrganizeImportsPostprocessor.java 7 Feb 2007 11:51:07 -0000 1.2 >+++ src/org/eclipse/gmf/internal/common/codegen/OrganizeImportsPostprocessor.java 9 Feb 2007 17:25:47 -0000 >@@ -78,6 +78,8 @@ > * > * @param icu > * the compilation unit containing <b>valid</b> code >+ * @param declaredImportsAsStrings >+ * imports added in previous file revision (default is null) > * @param monitor > * the progress monitor used to report progress and request > * cancelation, or <code>null</code> if none >@@ -87,14 +89,18 @@ > * > * @see ImportRewrite > */ >- public void organizeImports(ICompilationUnit icu, IProgressMonitor progress) throws CoreException { >+ public void organizeImports(ICompilationUnit icu, IProgressMonitor progress) throws CoreException { >+ organizeImports(icu, null, progress); >+ } >+ >+ public void organizeImports(ICompilationUnit icu, String[] declaredImportsAsStrings, IProgressMonitor progress) throws CoreException { > IDocument document = new Document(icu.getBuffer().getContents()); > > ASTParser parser = ASTParser.newParser(AST.JLS3); > parser.setSource(icu); > CompilationUnit cu = (CompilationUnit) parser.createAST(progress); > >- TextEdit importsEdit = organizeImports(cu, progress); >+ TextEdit importsEdit = organizeImports(cu, declaredImportsAsStrings, progress); > > try { > importsEdit.apply(document); >@@ -119,6 +125,8 @@ > * > * @param astRoot > * the parsed traversable ast tree, should contain no errors >+ * @param declaredImports >+ * imports added in previous file revision (default is null) > * @param monitor > * the progress monitor used to report progress and request > * cancelation, or <code>null</code> if none >@@ -131,15 +139,20 @@ > * > * @see ImportRewrite > */ >- public TextEdit organizeImports(CompilationUnit astRoot, IProgressMonitor progress) throws CoreException { >+ public TextEdit organizeImports(CompilationUnit astRoot, IProgressMonitor progress) throws CoreException { >+ return organizeImports(astRoot, null, progress); >+ } >+ >+ public TextEdit organizeImports(CompilationUnit astRoot, String[] declaredImports, IProgressMonitor progress) throws CoreException { > MultiTextEdit result = new MultiTextEdit(); > > Set<String> oldSingleImports = new HashSet<String>(); > Set<String> oldDemandImports = new HashSet<String>(); > >- if (isDebug()) { >- collectExistingImports(astRoot, oldSingleImports, oldDemandImports); >- } >+ String[] customImports = substract(declaredImports, astRoot.imports()); >+ if (isDebug()) { >+ collectExistingImports(astRoot, oldSingleImports, oldDemandImports, customImports); >+ } > > if (!checkForNoSyntaxErrors(astRoot)) { > throw new CoreException(new Status(IStatus.ERROR, Activator.getID(), 0, "Imports are unable to be organized due to syntax errors in the compilation unit", null)); >@@ -151,7 +164,8 @@ > > PackageReferencesCollector.collect(astRoot, qualifiedTypeReferences, simpleTypeReferences, importsAdded); > >- ImportRewrite importRewrite = createImportRewrite(astRoot); >+ ImportRewrite importRewrite = createImportRewrite(astRoot); >+ copyImports(importRewrite, customImports); > ImportRewrite.ImportRewriteContext context = new ReferencedTypesAwareImportRewriteContext(simpleTypeReferences, importRewrite); > > Iterator<Name> refIterator = qualifiedTypeReferences.iterator(); >@@ -191,6 +205,31 @@ > return result; > } > >+ /* >+ * Since we do organizeImports prior to merge, we must ensure >+ * imports added manually are known to OrganizeImportsProcessor >+ */ >+ private static void copyImports(ImportRewrite importRewrite, String[] importsToCopy) { >+ if (importsToCopy == null || importsToCopy.length == 0) { >+ return; >+ } >+ for (int i = 0; i < importsToCopy.length; i++) { >+ importRewrite.addImport(importsToCopy[i]); >+ } >+ } >+ >+ private String[] substract(String[] declaredImports, List list) { >+ if (declaredImports == null || declaredImports.length == 0) { >+ return declaredImports; >+ } >+ List<String> result = new ArrayList<String>(Arrays.asList(declaredImports)); >+ for (int i=0; i<list.size(); i++) { >+ ImportDeclaration next = (ImportDeclaration) list.get(i); >+ result.remove(next.getName().getFullyQualifiedName()); >+ } >+ return result.toArray(new String[result.size()]); >+ } >+ > private boolean addImport(String typeName, String fullName, ImportRewrite importRewrite, ImportRewrite.ImportRewriteContext context, Collection<String> importsAdded) { > boolean resultIsOk = importRewrite.addImport(fullName, context).equals(typeName); > if (resultIsOk && !importsAdded.contains(fullName)) { >@@ -199,8 +238,18 @@ > return resultIsOk; > } > >- private void collectExistingImports(CompilationUnit astRoot, Set<String> oldSingleImports, Set<String> oldDemandImports) { >- List imports = astRoot.imports(); >+ private void collectExistingImports(CompilationUnit astRoot, Set<String> oldSingleImports, Set<String> oldDemandImports, String[] declaredImports) { >+ List imports = new ArrayList(astRoot.imports()); >+ if (declaredImports != null && declaredImports.length > 0) { >+ for (int i=0; i<declaredImports.length; i++) { >+ String curr = declaredImports[i]; >+ if (curr.endsWith("*")) { >+ oldDemandImports.add(curr); >+ } else { >+ oldSingleImports.add(curr); >+ } >+ } >+ } > for (int i = 0; i < imports.size(); i++) { > ImportDeclaration curr = (ImportDeclaration) imports.get(i); > String id = curr.getName().getFullyQualifiedName(); >Index: src/org/eclipse/gmf/internal/common/codegen/GeneratorBase.java >=================================================================== >RCS file: /cvsroot/technology/org.eclipse.gmf/plugins/org.eclipse.gmf.common/src/org/eclipse/gmf/internal/common/codegen/GeneratorBase.java,v >retrieving revision 1.26 >diff -u -r1.26 GeneratorBase.java >--- src/org/eclipse/gmf/internal/common/codegen/GeneratorBase.java 7 Feb 2007 17:41:36 -0000 1.26 >+++ src/org/eclipse/gmf/internal/common/codegen/GeneratorBase.java 9 Feb 2007 17:25:47 -0000 >@@ -322,11 +322,15 @@ > ICompilationUnit cu = pf.getCompilationUnit(className + ".java"); //$NON-NLS-1$ > if (cu.exists()) { > final String oldContents = cu.getSource(); >- IImportDeclaration[] declaredImports = cu.getImports(); >+ IImportDeclaration[] declaredImports = cu.getImports(); > cu.getBuffer().setContents(genText); > try { >- copyImports(cu, declaredImports, new SubProgressMonitor(pm, 1)); >- getImportsPostrocessor().organizeImports(cu, new SubProgressMonitor(pm, 1)); >+ //Since we do organizeImports prior to merge, we must ensure imports added manually are known to OrganizeImportsProcessor >+ String[] declaredImportsAsStrings = new String[declaredImports.length]; >+ for (int i=0; i<declaredImports.length; i++) { >+ declaredImportsAsStrings[i] = declaredImports[i].getElementName(); >+ } >+ getImportsPostrocessor().organizeImports(cu, declaredImportsAsStrings, new SubProgressMonitor(pm, 1)); > } catch (CoreException e) { > cu.save(new SubProgressMonitor(pm, 1), true); // save to investigate contents > throw e; >@@ -342,7 +346,7 @@ > } > } else { > cu = pf.createCompilationUnit(cu.getElementName(), genText, true, new SubProgressMonitor(pm, 1)); >- getImportsPostrocessor().organizeImports(cu, new SubProgressMonitor(pm, 1)); >+ getImportsPostrocessor().organizeImports(cu, null, new SubProgressMonitor(pm, 1)); > String newContents = formatCode(cu.getSource()); > cu.getBuffer().setContents(newContents); > cu.save(new SubProgressMonitor(pm, 2), true); >@@ -360,29 +364,6 @@ > } > } > >- /* >- * Since we do organizeImports prior to merge, we must ensure >- * imports added manually are known to OrganizeImportsProcessor >- */ >- private static void copyImports(ICompilationUnit cu, IImportDeclaration[] importsToCopy, IProgressMonitor progress) throws JavaModelException { >- if (importsToCopy == null || importsToCopy.length == 0) { >- return; >- } >- progress.beginTask(null, importsToCopy.length + 1); >- final String[] imports = new String[importsToCopy.length]; >- final int[] flags = new int[imports.length]; >- for (int i = 0; i < importsToCopy.length; i++) { >- imports[i] = importsToCopy[i].getElementName(); >- flags[i] = importsToCopy[i].getFlags(); >- } >- // ensure resource is in sync with buffer (otherwize NPE from CreateElementInCUOperation) >- cu.save(new SubProgressMonitor(progress, 1), true); >- for (int i = 0; i < imports.length; i++) { >- cu.createImport(imports[i], null, flags[i], new SubProgressMonitor(progress, 1)); >- } >- progress.done(); >- } >- > protected final void doGenerateBinaryFile(BinaryEmitter emitter, Path outputPath, Object[] params) throws InterruptedException, UnexpectedBehaviourException { > IProgressMonitor pm = getNextStepMonitor(); > setProgressTaskName(outputPath.lastSegment()); >#P org.eclipse.gmf.tests >Index: src/org/eclipse/gmf/tests/gen/OrganizeImportsPostprocessorTest.java >=================================================================== >RCS file: /cvsroot/technology/org.eclipse.gmf/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/gen/OrganizeImportsPostprocessorTest.java,v >retrieving revision 1.4 >diff -u -r1.4 OrganizeImportsPostprocessorTest.java >--- src/org/eclipse/gmf/tests/gen/OrganizeImportsPostprocessorTest.java 7 Feb 2007 11:51:05 -0000 1.4 >+++ src/org/eclipse/gmf/tests/gen/OrganizeImportsPostprocessorTest.java 9 Feb 2007 17:25:49 -0000 >@@ -533,6 +533,77 @@ > assertTrue("Failed to find references for "+allImports.size()+" more types", allImports.isEmpty()); > } > >+ public void testNotSpoilingCustomImports() throws Exception { >+ String className = "NotSpoilingCustomImports"; >+ final String conflict = "my.namespace."+ArrayList.class.getSimpleName(); >+ String[] customImports = new String[] { ArrayList.class.getName() }; >+ String[] fieldTypes = new String[] { Map.class.getName(), ArrayList.class.getName(), ArrayList.class.getSimpleName(), conflict }; >+ String code = generateClassCode(className, null, null, null, fieldTypes, null, null, null, null); >+ ICompilationUnit icu = JavaProjectHelper.createJavaFile(className+".java", code); >+ >+ OrganizeImportsPostprocessor processor = new OrganizeImportsPostprocessor(); >+ processor.organizeImports(icu, customImports, null); >+ icu.save(null, true); >+ >+ ASTParser parser = ASTParser.newParser(AST.JLS3); >+ parser.setSource(icu); >+ CompilationUnit cu = (CompilationUnit) parser.createAST(null); >+ List imports = cu.imports(); >+ >+ final List<String> hadImportsList = new ArrayList<String>(Arrays.asList(customImports)); >+ final List<String> uniqueImports = new ArrayList<String>(hadImportsList); >+ for (int i=0; i<fieldTypes.length; i++) { >+ if (!conflict.equals(fieldTypes[i]) && !uniqueImports.contains(fieldTypes[i]) && fieldTypes[i].indexOf('.') != -1) { >+ uniqueImports.add(fieldTypes[i]); >+ } >+ } >+ >+ final List<String> allImports = new ArrayList<String>(); >+ allImports.addAll(uniqueImports); >+ assertEquals("Failed to generate enough import statements", allImports.size(), imports.size()); >+ for (Iterator it=imports.iterator(); it.hasNext();) { >+ String nextImport = ((ImportDeclaration) it.next()).getName().getFullyQualifiedName(); >+ assertTrue("Unexpected import found: "+nextImport, allImports.remove(nextImport)); >+ } >+ assertTrue("Failed to generate import for "+allImports.size()+" more types", allImports.isEmpty()); >+ >+ allImports.clear(); >+ allImports.addAll(uniqueImports); >+ cu.accept(new ASTVisitor(){ >+ public boolean visit(FieldDeclaration node) { >+ node.getType().accept(new ASTVisitor(){ >+ public boolean visit(QualifiedName node) { >+ assertEquals("Unexpected fully qualified field found: "+node.getFullyQualifiedName(), conflict, node.getFullyQualifiedName()); >+ return false; >+ } >+ >+ public boolean visit(SimpleName node) { >+ String name = "."+node.getFullyQualifiedName(); >+ boolean found = false; >+ for (Iterator<String> it=uniqueImports.iterator(); it.hasNext();) { >+ String nextFullTypeName = it.next(); >+ if (nextFullTypeName.endsWith(name)) { >+ found = true; >+ break; >+ } >+ } >+ assertTrue("Unexpected simple type found "+name, found); >+ for (Iterator<String> it=allImports.iterator(); it.hasNext();) { >+ String nextFullTypeName = it.next(); >+ if (nextFullTypeName.endsWith(name)) { >+ it.remove(); >+ break; >+ } >+ } >+ return false; >+ } >+ }); >+ return super.visit(node); >+ } >+ }); >+ assertTrue("Failed to find references for "+allImports.size()+" more types", allImports.isEmpty()); >+ } >+ > public void testNotToRemoveEvenUnusedImports() throws Exception { > String className = "TestNotToRemoveEvenUnusedImports"; > String[] existingImports = new String[] { ArrayList.class.getName() };
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 173462
: 58667