### Eclipse Workspace Patch 1.0 #P org.eclipse.gmf.common Index: META-INF/MANIFEST.MF =================================================================== RCS file: /cvsroot/technology/org.eclipse.gmf/plugins/org.eclipse.gmf.common/META-INF/MANIFEST.MF,v retrieving revision 1.29 diff -u -r1.29 MANIFEST.MF --- META-INF/MANIFEST.MF 14 Nov 2006 18:16:39 -0000 1.29 +++ META-INF/MANIFEST.MF 1 Dec 2006 21:27:15 -0000 @@ -22,7 +22,8 @@ org.eclipse.ui.forms;bundle-version="[3.2.0,4.0.0)", org.eclipse.jdt.core;bundle-version="[3.2.0,4.0.0)", org.eclipse.jface.text;bundle-version="[3.2.0,4.0.0)", - org.eclipse.jface;bundle-version="[3.2.0,4.0.0)" + org.eclipse.jface;bundle-version="[3.2.0,4.0.0)", + org.eclipse.jdt.ui Bundle-Vendor: %providerName Bundle-RequiredExecutionEnvironment: J2SE-1.5 Bundle-Activator: org.eclipse.gmf.internal.common.Activator 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.14 diff -u -r1.14 GeneratorBase.java --- src/org/eclipse/gmf/internal/common/codegen/GeneratorBase.java 13 Nov 2006 13:22:15 -0000 1.14 +++ src/org/eclipse/gmf/internal/common/codegen/GeneratorBase.java 1 Dec 2006 21:27:15 -0000 @@ -39,6 +39,7 @@ import org.eclipse.emf.ecore.EObject; import org.eclipse.gmf.common.UnexpectedBehaviourException; import org.eclipse.gmf.common.codegen.ImportAssistant; +import org.eclipse.gmf.common.codegen.OrganizeImportsPostprocessor; import org.eclipse.gmf.internal.common.Activator; import org.eclipse.jdt.core.IClasspathEntry; import org.eclipse.jdt.core.ICompilationUnit; @@ -51,6 +52,7 @@ import org.eclipse.jdt.core.formatter.CodeFormatter; import org.eclipse.jface.text.Document; import org.eclipse.jface.text.IDocument; +import org.eclipse.text.edits.MalformedTreeException; import org.eclipse.text.edits.TextEdit; /** @@ -60,6 +62,7 @@ public abstract class GeneratorBase implements Runnable { private CodeFormatter myCodeFormatter; + private OrganizeImportsPostprocessor myImportsPostprocessor; private IProgressMonitor myProgress = new NullProgressMonitor(); // myDestRoot.getJavaProject().getElementName() == myDestProject.getName() @@ -68,6 +71,7 @@ private final List myExceptions; private IStatus myRunStatus = Status.CANCEL_STATUS; private TextMerger myMerger; + private boolean isToRestoreExistingImports = true; protected abstract void customRun() throws InterruptedException, UnexpectedBehaviourException; @@ -312,7 +316,7 @@ IProgressMonitor pm = getNextStepMonitor(); try { setProgressTaskName(className); - pm.beginTask(null, 4); + pm.beginTask(null, 5); String genText = emitter.generate(new SubProgressMonitor(pm, 1), input); IPackageFragment pf = myDestRoot.createPackageFragment(packageName, true, new SubProgressMonitor(pm, 1)); ICompilationUnit cu = pf.getCompilationUnit(className + ".java"); //$NON-NLS-1$ @@ -325,7 +329,9 @@ } genText = formatCode(genText); if (!genText.equals(oldContents)) { - pf.createCompilationUnit(cu.getElementName(), genText, true, new SubProgressMonitor(pm, 1)); + ICompilationUnit newCU = pf.createCompilationUnit(cu.getElementName(), genText, true, new SubProgressMonitor(pm, 1)); + getImportsPostrocessor().organizeImports(newCU, isToRestoreExistingImports, new SubProgressMonitor(pm, 1)); + newCU.save(new SubProgressMonitor(pm, 1), true); } else { pm.worked(1); } @@ -336,8 +342,10 @@ } catch (UnexpectedBehaviourException ex) { handleUnexpected(ex); } catch (CoreException ex) { - handleException(ex); - } finally { + handleException(ex); + } catch (MalformedTreeException ex) { + handleException(ex); + } finally { pm.done(); } } @@ -442,7 +450,18 @@ } return myCodeFormatter; } - + + private OrganizeImportsPostprocessor getImportsPostrocessor() { + if (myImportsPostprocessor == null) { + myImportsPostprocessor = new OrganizeImportsPostprocessor(); + } + return myImportsPostprocessor; + } + + public void setIfToRestoreExistingImports(boolean restore) { + isToRestoreExistingImports = restore; + } + private final void clearExceptionsList(){ myExceptions.clear(); } Index: src/org/eclipse/gmf/common/codegen/PackageReferencesCollector.java =================================================================== RCS file: src/org/eclipse/gmf/common/codegen/PackageReferencesCollector.java diff -N src/org/eclipse/gmf/common/codegen/PackageReferencesCollector.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/gmf/common/codegen/PackageReferencesCollector.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,134 @@ +package org.eclipse.gmf.common.codegen; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.ListIterator; + +import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.ASTVisitor; +import org.eclipse.jdt.core.dom.ImportDeclaration; +import org.eclipse.jdt.core.dom.Name; +import org.eclipse.jdt.core.dom.PackageDeclaration; +import org.eclipse.jdt.core.dom.QualifiedName; +import org.eclipse.jdt.core.dom.SimpleName; +import org.eclipse.jdt.core.dom.SimpleType; +import org.eclipse.jdt.core.dom.TypeDeclaration; + +public class PackageReferencesCollector extends ASTVisitor { + + public static void collect(ASTNode node, Collection resultingQualifiedTypeReferences, Collection resultingSimpleTypeReferences, Collection resultingImportDeclarations) { + node.accept(new PackageReferencesCollector(resultingQualifiedTypeReferences, resultingSimpleTypeReferences, resultingImportDeclarations)); + } + + private Collection mySimpleTypeReferences; + private Collection myQualifiedTypeReferences; + private Collection myImportDeclarations; + private Collection myKnownPackages = new HashSet(); + + private PackageReferencesCollector(Collection resultingTypeReferences, Collection resultingSimpleTypeReferences, Collection resultingImportDeclarations) { + super(true); + myQualifiedTypeReferences = resultingTypeReferences; + mySimpleTypeReferences = resultingSimpleTypeReferences; + myImportDeclarations = resultingImportDeclarations; + } + + private void addQualifiedReference(QualifiedName node) { + myKnownPackages.add(node.getQualifier().getFullyQualifiedName()); + myQualifiedTypeReferences.add(node); + } + + private void addSimpleReference(SimpleName name) { + mySimpleTypeReferences.add(name); + } + + private void addImport(Name name) { + if (name.isQualifiedName()) { + QualifiedName qName = (QualifiedName) name; + myImportDeclarations.add(qName.getName().getFullyQualifiedName()); + myKnownPackages.add(qName.getQualifier().getFullyQualifiedName()); + } else { + addSimpleReference((SimpleName) name); + } + } + + protected void typeRefFound(Name node) { + Name name = node; + if (node.isQualifiedName()) { + name = hackRecognizeTypesAndPackagesForTheirLettersCase((QualifiedName) node); + if (name == null) { + return; + } + } + if (name.isQualifiedName()) { + addQualifiedReference((QualifiedName) name); + } else { + addSimpleReference((SimpleName) name); + } + } + + protected Name hackRecognizeTypesAndPackagesForTheirLettersCase(QualifiedName node) { + Name name = node; + List qualifiers = new ArrayList(); + while (name.isQualifiedName()) { + qualifiers.add(qualifiers.size(), name); + name = ((QualifiedName) name).getQualifier(); + } + qualifiers.add(name); + for (ListIterator it=qualifiers.listIterator(qualifiers.size()); it.hasPrevious();) { + Name packagePart = it.previous(); + SimpleName lastPart = packagePart.isSimpleName() ? (SimpleName)packagePart : ((QualifiedName)packagePart).getName(); + char[] letters = lastPart.getFullyQualifiedName().toCharArray(); + if (letters.length > 0) { + if (Character.isUpperCase(letters[0])) { + //XXX: Type reference is recognized for its first Upper letter and at least one Lower + for (int i=0; i Traverses the ast tree and extracts any full qualified names to be import statements, + * with respect to the name conflicts possible.

+ *

Afterwards these imports are organized with the help of {@link ImportRewrite} manager.

+ *

Changes are applied directly to the given ICompilationUnit's buffer, using code + * formatting settings as specified in the JDT UI preferences. Saving of the unit is + * left for the user consideration.

+ * + * @param icu the compilation unit containing valid code + * @param restoreExistingImports specifies if the existing imports should be kept or removed. + * @param monitor the progress monitor used to report progress and request cancelation, + * or null if none + * + * @throws CoreException the exception is thrown if the rewrite fails. + * + * @see ImportRewrite + */ + public void organizeImports(ICompilationUnit icu, boolean restoreExistingImports, 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, restoreExistingImports, progress); + + try { + importsEdit.apply(document); + } catch (BadLocationException e) { + throw new CoreException(new Status(IStatus.ERROR, Activator.getID(), "Unable to apply imports text changes", e)); + } + icu.getBuffer().setContents(document.get()); + } + + /** + * Organizes qualified names in document. + *

Traverses the ast tree and extracts any full qualified names to be import statements, + * with respect to the name conflicts possible.

+ *

Afterwards these imports are organized with the help of {@link ImportRewrite} manager.

+ * + * @param astRoot the parsed traversable ast tree, should contain no errors + * @param restoreExistingImports specifies if the existing imports should be kept or removed. + * @param monitor the progress monitor used to report progress and request cancelation, + * or null if none + * + * @return text edit object describing the changes to the document corresponding to the changes + * recorded by rewriter + * + * @throws CoreException the exception is thrown if the rewrite fails. + * + * @see ImportRewrite + */ + public TextEdit organizeImports(CompilationUnit astRoot, boolean restoreExistingImports, IProgressMonitor progress) throws CoreException { + MultiTextEdit result = new MultiTextEdit(); + + Set oldSingleImports = new HashSet(); + Set oldDemandImports = new HashSet(); + + if (DEBUG) { + collectExistingImports(astRoot, oldSingleImports, oldDemandImports); + } + + if (!checkForNoSyntaxErrors(astRoot)) { + throw new CoreException(new Status(IStatus.ERROR, Activator.getID(), "Imports are unable to be organized due to syntax errors in the compilation unit")); + } + + Collection qualifiedTypeReferences = new ArrayList(); + Collection simpleTypeReferences = new ArrayList(); + Collection importsAdded = new ArrayList(); + + PackageReferencesCollector.collect(astRoot, qualifiedTypeReferences, simpleTypeReferences, importsAdded); + + ImportRewrite importRewrite = createImportRewrite(astRoot, restoreExistingImports); + ImportRewrite.ImportRewriteContext context = new ReferencedTypesAwareImportRewriteContext(simpleTypeReferences, importRewrite); + + Iterator refIterator = qualifiedTypeReferences.iterator(); + while (refIterator.hasNext()) { + Name typeRef = refIterator.next(); + if (typeRef.isQualifiedName()) { + QualifiedName qualifiedName = (QualifiedName) typeRef; + SimpleName simpleName = qualifiedName.getName(); + boolean added = addImport(simpleName.getIdentifier(), qualifiedName.getFullyQualifiedName(), importRewrite, context, importsAdded); + if (added) { + Name qualifier = qualifiedName.getQualifier(); + int qualifierStart = qualifier.getStartPosition(); + int simpleNameStart = simpleName.getStartPosition(); + try { + result.addChild(new ReplaceEdit(qualifierStart, simpleNameStart - qualifierStart, "")); + } catch (MalformedTreeException e) { + throw new CoreException(new Status(IStatus.ERROR, Activator.getID(), "Unable to produce correct text changes for replacing full name: "+qualifiedName, e)); + } + } + } else { + SimpleName simpleName = (SimpleName) typeRef; + addImport(simpleName.getIdentifier(), simpleName.getFullyQualifiedName(), importRewrite, context, importsAdded); + } + } + + TextEdit edit = importRewrite.rewriteImports(progress); + try { + result.addChild(edit); + } catch (MalformedTreeException e) { + throw new CoreException(new Status(IStatus.ERROR, Activator.getID(), "Text changes conflict while organizing imports", e)); + } + + if (DEBUG) { + determineImportDifferences(importRewrite, oldSingleImports, oldDemandImports); + } + + return result; + } + + private boolean addImport(String typeName, String fullName, ImportRewrite importRewrite, ImportRewrite.ImportRewriteContext context, Collection importsAdded) { + boolean resultIsOk = importRewrite.addImport(fullName, context).equals(typeName); + if (resultIsOk && !importsAdded.contains(fullName)) { + importsAdded.add(fullName); + } + return resultIsOk; + } + + private void collectExistingImports(CompilationUnit astRoot, Set oldSingleImports, Set oldDemandImports) { + List imports = astRoot.imports(); + for (int i = 0; i < imports.size(); i++) { + ImportDeclaration curr = (ImportDeclaration) imports.get(i); + String id = curr.getName().getFullyQualifiedName(); + if (curr.isOnDemand()) { + oldDemandImports.add(id); + } else { + oldSingleImports.add(id); + } + } + } + + private boolean checkForNoSyntaxErrors(CompilationUnit astRoot) { + IProblem[] problems = astRoot.getProblems(); + for (int i = 0; i < problems.length; i++) { + IProblem curr = problems[i]; + if (curr.isError() && (curr.getID() & IProblem.Syntax) != 0) { + return false; + } + } + return true; + } + + private class ReferencedTypesAwareImportRewriteContext extends ImportRewrite.ImportRewriteContext { + private Collection mySimpleTypesReferenced; + private ImportRewrite myImportRewrite; + + public ReferencedTypesAwareImportRewriteContext(Collection simpleTypesReferenced, ImportRewrite importRewrite) { + mySimpleTypesReferenced = simpleTypesReferenced; + myImportRewrite = importRewrite; + } + + public int findInContext(String qualifier, String name, int kind) { + int result = myImportRewrite.getDefaultImportRewriteContext().findInContext(qualifier, name, kind); + if (result == RES_NAME_UNKNOWN) { + for (Iterator it = mySimpleTypesReferenced.iterator(); it.hasNext();) { + SimpleName next = it.next(); + if (name.equals(next.getIdentifier())) { + return RES_NAME_CONFLICT; + } + } + } + return result; + } + }; + + /** + * Returns a {@link ImportRewrite} using {@link ImportRewrite#create(CompilationUnit, boolean)} and + * configures the rewriter with the settings as specified in the JDT UI preferences. + * + * @param astRoot the AST root to create the rewriter on + * @param restoreExistingImports specifies if the existing imports should be kept or removed. + * @return the new rewriter configured with the settings as specified in the JDT UI preferences. + * + * @see ImportRewrite#create(CompilationUnit, boolean) + */ + public ImportRewrite createImportRewrite(CompilationUnit astRoot, boolean restoreExistingImports) { + return configureImportRewrite(ImportRewrite.create(astRoot, restoreExistingImports)); + } + + private static ImportRewrite configureImportRewrite(ImportRewrite rewrite) { + IJavaProject project = rewrite.getCompilationUnit().getJavaProject(); + String order = PreferenceConstants.getPreference(PreferenceConstants.ORGIMPORTS_IMPORTORDER, project); + rewrite.setImportOrder(SEMICOLON_PATTERN.split(order, 0)); + + String thres = PreferenceConstants.getPreference(PreferenceConstants.ORGIMPORTS_ONDEMANDTHRESHOLD, project); + try { + int num = Integer.parseInt(thres); + if (num == 0) + num = 1; + rewrite.setOnDemandImportThreshold(num); + } catch (NumberFormatException e) { + // ignore + } + String thresStatic = PreferenceConstants.getPreference(PreferenceConstants.ORGIMPORTS_STATIC_ONDEMANDTHRESHOLD, project); + try { + int num = Integer.parseInt(thresStatic); + if (num == 0) + num = 1; + rewrite.setStaticOnDemandImportThreshold(num); + } catch (NumberFormatException e) { + // ignore + } + return rewrite; + } + + private void determineImportDifferences(ImportRewrite importsStructure, Set oldSingleImports, Set oldDemandImports) { + ArrayList importsAdded = new ArrayList(); + importsAdded.addAll(Arrays.asList(importsStructure.getCreatedImports())); + importsAdded.addAll(Arrays.asList(importsStructure.getCreatedStaticImports())); + Object[] content = oldSingleImports.toArray(); + for (int i = 0; i < content.length; i++) { + String importName = (String) content[i]; + if (importsAdded.remove(importName)) { + oldSingleImports.remove(importName); + } + } + content = oldDemandImports.toArray(); + for (int i = 0; i < content.length; i++) { + String importName = (String) content[i]; + if (importsAdded.remove(importName + ".*")) { //$NON-NLS-1$ + oldDemandImports.remove(importName); + } + } + int fNumberOfImportsAdded = importsAdded.size(); + int fNumberOfImportsRemoved = oldSingleImports.size() + oldDemandImports.size(); + Activator.log(new Status(IStatus.INFO, Activator.getID(), 0, "[imports added]: "+fNumberOfImportsAdded, null)); + Activator.log(new Status(IStatus.INFO, Activator.getID(), 0, "[imports removed]: "+fNumberOfImportsRemoved, null)); + } + + private static final Pattern SEMICOLON_PATTERN = Pattern.compile(";"); //$NON-NLS-1$ + static boolean DEBUG = false; +} #P org.eclipse.gmf.tests Index: src/org/eclipse/gmf/tests/AllTests.java =================================================================== RCS file: /cvsroot/technology/org.eclipse.gmf/tests/org.eclipse.gmf.tests/src/org/eclipse/gmf/tests/AllTests.java,v retrieving revision 1.71 diff -u -r1.71 AllTests.java --- src/org/eclipse/gmf/tests/AllTests.java 21 Nov 2006 19:47:35 -0000 1.71 +++ src/org/eclipse/gmf/tests/AllTests.java 1 Dec 2006 21:27:16 -0000 @@ -35,6 +35,7 @@ import org.eclipse.gmf.tests.gen.LabelSupportTest; import org.eclipse.gmf.tests.gen.MapModeStrategyTest; import org.eclipse.gmf.tests.gen.ModelLoadHelperTest; +import org.eclipse.gmf.tests.gen.OrganizeImportsPostprocessorTest; import org.eclipse.gmf.tests.gen.RTFigureTest; import org.eclipse.gmf.tests.gen.ShapePropertiesTest; import org.eclipse.gmf.tests.gen.StandaloneMapModeTest; @@ -140,6 +141,8 @@ suite.addTest(AllValidateTests.suite()); suite.addTestSuite(GenModelGraphAnalyzerTest.class); + suite.addTestSuite(OrganizeImportsPostprocessorTest.class); + //$JUnit-END$ suite.addTest(new CleanupTest("testCleanup") { protected void performCleanup() throws Exception { Index: src/org/eclipse/gmf/tests/gen/OrganizeImportsPostprocessorTest.java =================================================================== RCS file: src/org/eclipse/gmf/tests/gen/OrganizeImportsPostprocessorTest.java diff -N src/org/eclipse/gmf/tests/gen/OrganizeImportsPostprocessorTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/gmf/tests/gen/OrganizeImportsPostprocessorTest.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,953 @@ +package org.eclipse.gmf.tests.gen; + +import java.io.ByteArrayInputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import junit.framework.TestCase; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IProjectDescription; +import org.eclipse.core.resources.IWorkspace; +import org.eclipse.core.resources.IWorkspaceRoot; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.draw2d.RectangleFigure; +import org.eclipse.gmf.common.codegen.OrganizeImportsPostprocessor; +import org.eclipse.jdt.core.IClasspathEntry; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ASTParser; +import org.eclipse.jdt.core.dom.ASTVisitor; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.FieldDeclaration; +import org.eclipse.jdt.core.dom.ImportDeclaration; +import org.eclipse.jdt.core.dom.MethodDeclaration; +import org.eclipse.jdt.core.dom.QualifiedName; +import org.eclipse.jdt.core.dom.SimpleName; +import org.eclipse.jdt.core.dom.SingleVariableDeclaration; +import org.eclipse.jdt.core.dom.Type; +import org.eclipse.jdt.core.dom.TypeDeclaration; +import org.eclipse.jdt.core.dom.VariableDeclarationStatement; + +public class OrganizeImportsPostprocessorTest extends TestCase { + + private static final String nl = System.getProperty("line.separator"); + + protected void setUp() throws Exception { + super.setUp(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + public void testFullQualifiedFields() throws Exception { + String className = "TestFullQualifiedFields"; + String[] fieldTypes = new String[] { List.class.getName(), RectangleFigure.class.getName(), getInnerClassAwareName(Map.Entry.class), Map.class.getName(), getInnerClassAwareName(Iterator.class)+"_eINSTANCE" }; + String code = generateClassCode(className, null, null, null, fieldTypes, null, null, null); + ICompilationUnit icu = (ICompilationUnit) JavaProjectHelper.createJavaFile(className+".java", code); + + OrganizeImportsPostprocessor processor = new OrganizeImportsPostprocessor(); + processor.organizeImports(icu, true, 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 fieldList = new ArrayList(fieldTypes.length); + + assertEquals("Failed to generate enough import statements", fieldTypes.length, imports.size()); + fieldList.clear(); + fieldList.addAll(Arrays.asList(fieldTypes)); + for (Iterator it=imports.iterator(); it.hasNext();) { + String nextImport = ((ImportDeclaration) it.next()).getName().getFullyQualifiedName(); + assertTrue("Unexpected import found: "+nextImport, fieldList.remove(nextImport)); + } + assertTrue("Failed to generate import for "+fieldList.size()+" more types", fieldList.isEmpty()); + + fieldList.clear(); + fieldList.addAll(Arrays.asList(fieldTypes)); + cu.accept(new ASTVisitor(){ + public boolean visit(FieldDeclaration node) { + node.getType().accept(new ExpectedSimpleNamesVisitor(fieldList)); + return super.visit(node); + } + }); + assertTrue("Failed to find references for "+fieldList.size()+" more types", fieldList.isEmpty()); + } + + public void testFullQualifiedMethodParams() throws Exception { + String className = "TestFullQualifiedMethodParams"; + String[] methodParams = new String[] { List.class.getName(), RectangleFigure.class.getName(), getInnerClassAwareName(Map.Entry.class), Map.class.getName() }; + String code = generateClassCode(className, null, null, null, null, methodParams, null, null); + ICompilationUnit icu = (ICompilationUnit) JavaProjectHelper.createJavaFile(className+".java", code); + + OrganizeImportsPostprocessor processor = new OrganizeImportsPostprocessor(); + processor.organizeImports(icu, true, 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 methodsList = new ArrayList(); + + assertEquals("Failed to generate enough import statements", methodParams.length, imports.size()); + methodsList.addAll(Arrays.asList(methodParams)); + for (Iterator it=imports.iterator(); it.hasNext();) { + String nextImport = ((ImportDeclaration) it.next()).getName().getFullyQualifiedName(); + assertTrue("Unexpected import found: "+nextImport, methodsList.remove(nextImport)); + } + assertTrue("Failed to generate import for "+methodsList.size()+" more types", methodsList.isEmpty()); + + methodsList.clear(); + methodsList.addAll(Arrays.asList(methodParams)); + cu.accept(new ASTVisitor(){ + public boolean visit(MethodDeclaration node) { + for (Iterator it = node.parameters().iterator(); it.hasNext();) { + SingleVariableDeclaration next = (SingleVariableDeclaration) it.next(); + next.getType().accept(new ExpectedSimpleNamesVisitor(methodsList)); + } + return super.visit(node); + } + }); + assertTrue("Failed to find references for "+methodsList.size()+" more types", methodsList.isEmpty()); + } + + public void testFullQualifiedLocalVariables() throws Exception { + String className = "TestFullQualifiedLocalVariables"; + String[] localVars = new String[] { List.class.getName(), RectangleFigure.class.getName(), getInnerClassAwareName(Map.Entry.class), Map.class.getName() }; + String code = generateClassCode(className, null, null, null, null, null, null, localVars); + ICompilationUnit icu = (ICompilationUnit) JavaProjectHelper.createJavaFile(className+".java", code); + + OrganizeImportsPostprocessor processor = new OrganizeImportsPostprocessor(); + processor.organizeImports(icu, true, 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 varsList = new ArrayList(localVars.length); + + assertEquals("Failed to generate enough import statements", localVars.length, imports.size()); + varsList.addAll(Arrays.asList(localVars)); + for (Iterator it=imports.iterator(); it.hasNext();) { + String nextImport = ((ImportDeclaration) it.next()).getName().getFullyQualifiedName(); + assertTrue("Unexpected import found: "+nextImport, varsList.remove(nextImport)); + } + assertTrue("Failed to generate import for "+varsList.size()+" more types", varsList.isEmpty()); + + varsList.clear(); + varsList.addAll(Arrays.asList(localVars)); + cu.accept(new ASTVisitor(){ + public boolean visit(MethodDeclaration node) { + node.getBody().accept(new ASTVisitor() { + public boolean visit(VariableDeclarationStatement node) { + node.getType().accept(new ExpectedSimpleNamesVisitor(varsList)); + return super.visit(node); + } + }); + return super.visit(node); + } + }); + assertTrue("Failed to find references for "+varsList.size()+" more types", varsList.isEmpty()); + } + + public void testFullQualifiedSupers() throws Exception { + String className = "TestFullQualifiedSupers"; + String superClass = RectangleFigure.class.getName(); + String[] superInterfaces = new String[] { Collection.class.getName(), Iterator.class.getName() }; + String code = generateClassCode(className, null, superClass, superInterfaces, null, null, null, null); + ICompilationUnit icu = (ICompilationUnit) JavaProjectHelper.createJavaFile(className+".java", code); + + OrganizeImportsPostprocessor processor = new OrganizeImportsPostprocessor(); + processor.organizeImports(icu, true, 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 supersList = new ArrayList(superInterfaces.length+1); + + assertEquals("Failed to generate enough import statements", superInterfaces.length+1, imports.size()); + supersList.clear(); + supersList.add(superClass); + supersList.addAll(Arrays.asList(superInterfaces)); + for (Iterator it=imports.iterator(); it.hasNext();) { + String nextImport = ((ImportDeclaration) it.next()).getName().getFullyQualifiedName(); + assertTrue("Unexpected import found: "+nextImport, supersList.remove(nextImport)); + } + assertTrue("Failed to generate import for "+supersList.size()+" more types", supersList.isEmpty()); + + supersList.clear(); + supersList.add(superClass); + supersList.addAll(Arrays.asList(superInterfaces)); + final ASTVisitor typeVisitor = new ExpectedSimpleNamesVisitor(supersList); + cu.accept(new ASTVisitor(){ + public boolean visit(TypeDeclaration node) { + node.getSuperclassType().accept(typeVisitor); + for (Iterator it=node.superInterfaceTypes().iterator(); it.hasNext();) { + ((Type) it.next()).accept(typeVisitor); + } + return super.visit(node); + } + }); + assertTrue("Failed to find references for "+supersList.size()+" more types", supersList.isEmpty()); + } + + public void testFieldConflictWithDeclaredType() throws Exception { + String className = "FieldConflictWithDeclaredType"; + String[] fieldTypes = new String[] { Collection.class.getName() }; + StringBuffer code = new StringBuffer(); + String mainClass = generateClassCode(className, null, null, null, fieldTypes, null, null, null); + String innerClass = generateClassCode(Collection.class.getSimpleName(), null, null, null, null, null, null, null); + code.append(mainClass).insert(mainClass.length()-1, innerClass); + ICompilationUnit icu = (ICompilationUnit) JavaProjectHelper.createJavaFile(className+".java", code.toString()); + + OrganizeImportsPostprocessor processor = new OrganizeImportsPostprocessor(); + processor.organizeImports(icu, true, null); + icu.save(null, true); + + ASTParser parser = ASTParser.newParser(AST.JLS3); + parser.setSource(icu); + CompilationUnit cu = (CompilationUnit) parser.createAST(null); + List imports = cu.imports(); + + assertTrue("Failed not to generate conflicted import statements", imports.isEmpty()); + assertEquals("Failed to preserve conflicted types qualified", code.toString(), icu.getBuffer().getContents()); + + final List fieldsList = new ArrayList(fieldTypes.length); + fieldsList.clear(); + fieldsList.addAll(Arrays.asList(fieldTypes)); + cu.accept(new ASTVisitor(){ + public boolean visit(FieldDeclaration node) { + node.getType().accept(new ASTVisitor() { + public boolean visit(QualifiedName node) { + boolean found = false; + for (Iterator it = fieldsList.iterator(); it.hasNext();) { + String nextFullTypeName = it.next(); + if (nextFullTypeName.equals(node.getFullyQualifiedName())) { + it.remove(); + found = true; + break; + } + } + assertTrue("Unexpected simple type found "+node.getFullyQualifiedName(), found); + return false; + } + }); + return super.visit(node); + } + }); + assertTrue("Failed to find references for "+fieldsList.size()+" more types", fieldsList.isEmpty()); + } + + public void testImportConflictWithDeclaredType() throws Exception { + String className = "ImportConflictWithDeclaredType"; + String[] importedTypes = new String[] { Collection.class.getName() }; + StringBuffer code = new StringBuffer(); + String mainClass = generateClassCode(className, importedTypes, null, null, null, null, null, null); + String innerClass = generateClassCode(Collection.class.getSimpleName(), null, null, null, null, null, null, null); + code.append(mainClass).insert(mainClass.length()-1, innerClass); + ICompilationUnit icu = (ICompilationUnit) JavaProjectHelper.createJavaFile(className+".java", code.toString()); + + OrganizeImportsPostprocessor processor = new OrganizeImportsPostprocessor(); + processor.organizeImports(icu, true, 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 importsList = new ArrayList(importedTypes.length); + + assertEquals("Failed to generate enough import statements", importedTypes.length, imports.size()); + importsList.clear(); + importsList.addAll(Arrays.asList(importedTypes)); + for (Iterator it=imports.iterator(); it.hasNext();) { + String nextImport = ((ImportDeclaration) it.next()).getName().getFullyQualifiedName(); + assertTrue("Unexpected import found: "+nextImport, importsList.remove(nextImport)); + } + + assertTrue("Failed to generate import for "+importsList.size()+" more types", importsList.isEmpty()); + assertEquals("Failed to preserve conflicted types qualified", code.toString(), icu.getBuffer().getContents()); + assertTrue("Failed to find references for "+importsList.size()+" more types", importsList.isEmpty()); + } + + public void testImportConflictWithQualifiedTypeRef() throws Exception { + String className = "ImportConflictWithQualifiedTypeRef"; + String[] importedTypes = new String[] { Collection.class.getName() }; + StringBuffer code = new StringBuffer(); + String mainClass = generateClassCode(className, importedTypes, null, null, null, null, null, null); + String innerClass = generateClassCode(Collection.class.getSimpleName(), null, Collection.class.getName(), null, null, null, null, null); + code.append(mainClass).insert(mainClass.length()-1, innerClass); + ICompilationUnit icu = (ICompilationUnit) JavaProjectHelper.createJavaFile(className+".java", code.toString()); + + OrganizeImportsPostprocessor processor = new OrganizeImportsPostprocessor(); + processor.organizeImports(icu, true, 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 importsList = new ArrayList(importedTypes.length); + + assertEquals("Failed to generate enough import statements", importedTypes.length, imports.size()); + importsList.clear(); + importsList.addAll(Arrays.asList(importedTypes)); + for (Iterator it=imports.iterator(); it.hasNext();) { + String nextImport = ((ImportDeclaration) it.next()).getName().getFullyQualifiedName(); + assertTrue("Unexpected import found: "+nextImport, importsList.remove(nextImport)); + } + + assertTrue("Failed to generate import for "+importsList.size()+" more types", importsList.isEmpty()); + //assertEquals("Failed to preserve conflicted types qualified", code.toString(), icu.getBuffer().getContents()); + assertTrue("Failed to find references for "+importsList.size()+" more types", importsList.isEmpty()); + } + + public void testImportConflictWithSuperTypeRef() throws Exception { + String className = "ImportConflictWithSuperTypeRef"; + String[] importedTypes = new String[] { Collection.class.getName() }; + String conflicted = "my.own.Collection"; + String code = generateClassCode(className, importedTypes, conflicted, null, null, null, null, null); + ICompilationUnit icu = (ICompilationUnit) JavaProjectHelper.createJavaFile(className+".java", code); + + OrganizeImportsPostprocessor processor = new OrganizeImportsPostprocessor(); + processor.organizeImports(icu, true, 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 importsList = new ArrayList(importedTypes.length); + + assertEquals("Failed to generate enough import statements", importedTypes.length, imports.size()); + importsList.clear(); + importsList.addAll(Arrays.asList(importedTypes)); + for (Iterator it=imports.iterator(); it.hasNext();) { + String nextImport = ((ImportDeclaration) it.next()).getName().getFullyQualifiedName(); + assertTrue("Unexpected import found: "+nextImport, importsList.remove(nextImport)); + } + + assertTrue("Failed to generate import for "+importsList.size()+" more types", importsList.isEmpty()); + assertEquals("Failed to preserve conflicted types qualified", code.toString(), icu.getBuffer().getContents()); + assertTrue("Failed to find references for "+importsList.size()+" more types", importsList.isEmpty()); + } + + public void testLocalVarConflictWithDeclaredType() throws Exception { + String className = "TestFullQualifiedLocalVariables"; + String[] localVars = new String[] { Collection.class.getName() }; + StringBuffer code = new StringBuffer(); + String mainClass = generateClassCode(className, null, null, null, null, null, null, null); + String innerClass = generateClassCode(Collection.class.getSimpleName(), null, null, null, null, null, null, localVars); + code.append(mainClass).insert(mainClass.length()-1, innerClass); + ICompilationUnit icu = (ICompilationUnit) JavaProjectHelper.createJavaFile(className+".java", code.toString()); + + OrganizeImportsPostprocessor processor = new OrganizeImportsPostprocessor(); + processor.organizeImports(icu, true, null); + icu.save(null, true); + + ASTParser parser = ASTParser.newParser(AST.JLS3); + parser.setSource(icu); + CompilationUnit cu = (CompilationUnit) parser.createAST(null); + List imports = cu.imports(); + + assertTrue("Failed not to generate conflicting imports", imports.isEmpty()); + + final List varsList = new ArrayList(Arrays.asList(localVars)); + cu.accept(new ASTVisitor(){ + public boolean visit(MethodDeclaration node) { + node.getBody().accept(new ASTVisitor() { + public boolean visit(VariableDeclarationStatement node) { + node.getType().accept(new ASTVisitor() { + public boolean visit(QualifiedName node) { + boolean found = false; + for (Iterator it = varsList.iterator(); it.hasNext();) { + String nextFullTypeName = it.next(); + if (nextFullTypeName.equals(node.getFullyQualifiedName())) { + it.remove(); + found = true; + break; + } + } + assertTrue("Unexpected simple type found "+node.getFullyQualifiedName(), found); + return false; + } + }); + return super.visit(node); + } + }); + return super.visit(node); + } + }); + assertTrue("Failed to find references for "+varsList.size()+" more types", varsList.isEmpty()); + } + + public void testNotSpoilingExistingImports() throws Exception { + String className = "NotSpoilingExistingImports"; + String[] existingImports = new String[] { ArrayList.class.getName(), Map.class.getName() }; + String[] fieldTypes = new String[] { List.class.getName(), RectangleFigure.class.getName(), getInnerClassAwareName(Map.Entry.class), Map.class.getName(), Map.class.getSimpleName(), ArrayList.class.getSimpleName() }; + String code = generateClassCode(className, existingImports, null, null, fieldTypes, null, null, null); + ICompilationUnit icu = (ICompilationUnit) JavaProjectHelper.createJavaFile(className+".java", code); + + OrganizeImportsPostprocessor processor = new OrganizeImportsPostprocessor(); + processor.organizeImports(icu, true, 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 hadImportsList = new ArrayList(Arrays.asList(existingImports)); + final List uniqueImports = new ArrayList(hadImportsList); + for (int i=0; i allImports = new ArrayList(); + 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) { + fail("Unexpected fully qualified field found: "+node.getFullyQualifiedName()); + return false; + } + + public boolean visit(SimpleName node) { + String name = "."+node.getFullyQualifiedName(); + boolean found = false; + for (Iterator 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 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() }; + String code = generateClassCode(className, existingImports, null, null, null, null, null, null); + ICompilationUnit icu = (ICompilationUnit) JavaProjectHelper.createJavaFile(className+".java", code); + + OrganizeImportsPostprocessor processor = new OrganizeImportsPostprocessor(); + processor.organizeImports(icu, true, null); + icu.save(null, true); + + ASTParser parser = ASTParser.newParser(AST.JLS3); + parser.setSource(icu); + CompilationUnit cu = (CompilationUnit) parser.createAST(null); + List imports = cu.imports(); + + assertFalse("Failed not to remove unused imports", imports.isEmpty()); + assertEquals("Failed to preserve everything if there is unused imports", code, icu.getBuffer().getContents()); + } + + public void testNotToImportIfInOnDemandImports() throws Exception { + String className = "TestNotToImportIfInOnDemandImports"; + String[] existingImports = new String[] { "java.util.*" }; + String[] fieldTypes = new String[] { ArrayList.class.getName() }; + String code = generateClassCode(className, existingImports, null, null, fieldTypes, null, null, null); + ICompilationUnit icu = (ICompilationUnit) JavaProjectHelper.createJavaFile(className+".java", code); + + OrganizeImportsPostprocessor processor = new OrganizeImportsPostprocessor(); + processor.organizeImports(icu, true, null); + icu.save(null, true); + + ASTParser parser = ASTParser.newParser(AST.JLS3); + parser.setSource(icu); + CompilationUnit cu = (CompilationUnit) parser.createAST(null); + List imports = cu.imports(); + + assertEquals("Failed not to add extra import if on-demand presents", 1, imports.size()); + assertTrue("Failed to preserve on-demand imports", ((ImportDeclaration)imports.get(0)).isOnDemand()); + assertEquals("Failed to preserve right on-demand imports", existingImports[0], ((ImportDeclaration)imports.get(0)).getName().getFullyQualifiedName()+".*"); + + final List fieldList = new ArrayList(); + fieldList.addAll(Arrays.asList(fieldTypes)); + cu.accept(new ASTVisitor(){ + public boolean visit(FieldDeclaration node) { + node.getType().accept(new ExpectedSimpleNamesVisitor(fieldList)); + return super.visit(node); + } + }); + assertTrue("Failed to find references for "+fieldList.size()+" more types", fieldList.isEmpty()); + } + + private static class ExpectedSimpleNamesVisitor extends ASTVisitor { + private Collection myExpectedFullNames; + + public ExpectedSimpleNamesVisitor(Collection expectedFullNames) { + myExpectedFullNames = expectedFullNames; + } + + public boolean visit(QualifiedName node) { + fail("Unexpected fully qualified field found: "+node.getFullyQualifiedName()); + return false; + } + + public boolean visit(SimpleName node) { + String dottedName = "."+node.getFullyQualifiedName(); + boolean found = false; + for (Iterator it = myExpectedFullNames.iterator(); it.hasNext();) { + String nextFullTypeName = it.next(); + if (nextFullTypeName.endsWith(dottedName)) { + it.remove(); + found = true; + break; + } + } + assertTrue("Unexpected simple type found "+node.getFullyQualifiedName(), found); + return false; + } + } + + private String generateClassCode(String className, String[] imports, String extendsType, String[] implementsTypes, String[] fieldTypes, String[] methodParameters, String[] methodReturns, String[] localVariables) { + StringBuffer buf = new StringBuffer(); + if (imports != null && imports.length > 0) { + for (int i=0; i 0) { + buf.append(" implements "); + for (int i=0; i 0) { + for (int i=0; i 0) { + buf.append(" public void methodWithParams("); + for (int i=0; i 0) { + for (int i=0; i 0) { + buf.append(" public void methodWithLocalVariables() {").append(nl); + for (int i=0; i fieldList = new ArrayList(typeRefs.length); + + fieldList.clear(); + fieldList.addAll(Arrays.asList(existedImports)); + assertEquals("Failed to generate enough import statements", typeRefs.length, imports.size()); + for (Iterator it=imports.iterator(); it.hasNext();) { + String nextImport = ((ImportDeclaration) it.next()).getName().getFullyQualifiedName(); + assertTrue("Unexpected import found: "+nextImport, fieldList.remove(nextImport)); + } + assertTrue("Failed to generate import for "+fieldList.size()+" more types", fieldList.isEmpty()); + + fieldList.clear(); + fieldList.addAll(Arrays.asList(existedImports)); + cu.accept(new ASTVisitor(){ + public boolean visit(FieldDeclaration node) { + node.getType().accept(new ExpectedSimpleNamesVisitor(fieldList) { + public boolean visit(SimpleName node) { + String dottedName = "."+node.getFullyQualifiedName(); + boolean found = false; + for (Iterator it = fieldList.iterator(); it.hasNext();) { + String nextFullTypeName = it.next(); + if (nextFullTypeName.endsWith(dottedName)) { + found = true; + break; + } + } + assertTrue("Unexpected simple type found "+node.getFullyQualifiedName(), found); + return false; + } + }); + return super.visit(node); + } + }); + } + + public void testQualifiedConstantsInMethodCalls() throws Exception { + String testClassName = "QualifiedConstantsInMethodCalls"; + final List typeNameRefs = new ArrayList(Arrays.asList(new String[] { + "javax.dummy.Castings", + "javax.dummy.Classes", + "javax.dummy.Constants", + "javax.dummy.Methods", + })); + + StringBuffer buf = new StringBuffer(); + buf.append("public class ").append(testClassName).append(" {").append(nl); + buf.append(nl); + buf.append(" private static int method1(Object[] values) {").append(nl); + buf.append(" return method2(").append(typeNameRefs.get(2)).append(".CONSTANT,").append(nl); + buf.append(" ").append("(").append(typeNameRefs.get(0)).append(") casting,").append(nl); + buf.append(" ").append(typeNameRefs.get(1)).append(".class,").append(nl); + buf.append(" values.length,").append(nl); + buf.append(" local.fVar1.fVar2.length,").append(nl); + buf.append(" localV.fVar1.fVar2.length,").append(nl); + buf.append(" not.to.replace.pack1.A,").append(nl); + buf.append(" (not.to.replace.pack2.B) b,").append(nl); + buf.append(" ").append(typeNameRefs.get(3)).append(".method());").append(nl); + buf.append(" }").append(nl); + buf.append(nl); + buf.append("}").append(nl); + + ICompilationUnit icu = (ICompilationUnit) JavaProjectHelper.createJavaFile(testClassName+".java", buf.toString()); + new OrganizeImportsPostprocessor().organizeImports(icu, true, null); + icu.save(null, true); + + ASTParser parser = ASTParser.newParser(AST.JLS3); + parser.setSource(icu); + CompilationUnit cu = (CompilationUnit) parser.createAST(null); + List imports = cu.imports(); + + assertEquals("Failed to generate enough import statements: "+imports, typeNameRefs.size(), imports.size()); + for (int i=0; i a1 = new ArrayList(); +// ArrayList a2 = new ArrayList(); +// ArrayList a3 = new ArrayList(); +// ImportReferencesCollector.collect(cu, a1, a2, a3); +// System.err.println(a1); +// System.err.println(a2); +// System.err.println(a3); + } + + public void testFullQualifiedArrays() throws Exception { + String testClassName = "FullQualifiedArrays"; + List typeRefs = Arrays.asList(new Class[] { + ArrayList.class, + List.class, + Map.class, + Map.Entry.class, + }); + final String[] conflictedTypeNames = new String[] { + "javax.some.Map", + }; + final List typeNameRefs = new ArrayList(typeRefs.size()+conflictedTypeNames.length); + for (Iterator it = typeRefs.iterator(); it.hasNext();) { + typeNameRefs.add(getInnerClassAwareName(it.next())); + } + String localVariable = typeNameRefs.get(2)+".SMTH.ourEntries"; + //String localVariable2 = "org.eclipse.draw2d.Graphics"; + typeNameRefs.add(localVariable); + //typeNameRefs.add(localVariable2); + typeNameRefs.addAll(Arrays.asList(conflictedTypeNames)); + + StringBuffer buf = new StringBuffer(); + buf.append("import ").append(typeNameRefs.get(0)).append(";").append(nl); + buf.append(nl); + buf.append("public class ").append(testClassName).append(" {").append(nl); + buf.append(nl); + buf.append(" private ").append(typeNameRefs.get(1)).append("[] list;").append(nl); + buf.append(" private ").append(typeNameRefs.get(2)).append("[] entry = new ").append(typeNameRefs.get(4)).append("[0];").append(nl); + buf.append(" private ").append(typeNameRefs.get(3)).append("[] maps;").append(nl); + buf.append(" private ").append(typeRefs.get(0).getSimpleName()).append("[] arrayLists;").append(nl); + buf.append(" private ").append(typeNameRefs.get(5)).append(" conflictEntry;").append(nl); + buf.append(" private int style = setLineStyle(").append(localVariable).append(".LINE_DASH);"); + buf.append(nl); + buf.append(" private static int setLineStyle(int style) {"); + buf.append(" return 0;"); + buf.append(" }"); + buf.append(nl); + buf.append("}").append(nl); + + ICompilationUnit icu = (ICompilationUnit) JavaProjectHelper.createJavaFile(testClassName+".java", buf.toString()); + new OrganizeImportsPostprocessor().organizeImports(icu, true, null); + icu.save(null, true); + + ASTParser parser = ASTParser.newParser(AST.JLS3); + parser.setSource(icu); + CompilationUnit cu = (CompilationUnit) parser.createAST(null); + List imports = cu.imports(); + + assertEquals("Failed to generate enough import statements: "+imports, typeRefs.size(), imports.size()); + for (int i=0; i