diff --git a/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/actions/OrganizeImportsTests.groovy b/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/actions/OrganizeImportsTests.groovy index 43e631932e..27838e86bb 100644 --- a/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/actions/OrganizeImportsTests.groovy +++ b/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/actions/OrganizeImportsTests.groovy @@ -1536,6 +1536,47 @@ final class OrganizeImportsTests extends OrganizeImportsTestSuite { doContentsCompareTest(contents) } + @Test + void testOrganizeWithExtraImports6() { + addConfigScript '''\ + withConfig(configuration) { + imports { + star 'groovy.transform' + alias 'Regexp', 'java.util.regex.Pattern' + } + } + ''' + + addGroovySource '''\ + @Canonical + class One { + String string + Number number + private Regexp pattern + void setPattern(Regexp pattern) { + this.pattern = pattern + } + } + '''.stripIndent(), 'One', 'main' + + String contents = '''\ + @CompileStatic + final class Tests { + @org.junit.Test + void testCtors() { + One one = new One('value') // error: Cannot find matching method + Two two = new Two('value') + } + } + @Canonical @CompileStatic + class Two { + String value + } + '''.stripIndent() + + doContentsCompareTest(contents) + } + @Test @NotYetImplemented void testOrganizeWithInterleavedComments() { String originalContents = '''\ diff --git a/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/refactoring/actions/OrganizeGroovyImports.java b/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/refactoring/actions/OrganizeGroovyImports.java index 764ce1ef57..bb3437c843 100644 --- a/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/refactoring/actions/OrganizeGroovyImports.java +++ b/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/refactoring/actions/OrganizeGroovyImports.java @@ -156,7 +156,8 @@ public TextEdit calculateMissingImports() { importsSlatedForRemoval.put(className, imp); } else { String fullName = className + " as " + imp.getAlias(); - rewriter.addImport(fullName, FORCE_RETENTION); + rewriter.addImport(fullName, imp.getEnd() > 0 ? FORCE_RETENTION + : rewriter.getDefaultImportRewriteContext()); importsSlatedForRemoval.put(fullName, imp); } } else { @@ -656,7 +657,7 @@ private void handleTypeReference(ClassNode node, boolean isAnnotation) { visitTypeParameters(generics, name); } - if (!node.isResolved() && node.redirect() != current) { + if (!node.isResolved() && !current.getModule().getClasses().contains(node.redirect())) { // aliases come through as unresolved types if (ALIASED_IMPORT.matcher(name).find()) { doNotRemoveImport(name);