Skip to content

Commit

Permalink
Fix for #1244: support imports for default-package elements
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Apr 15, 2021
1 parent e5dcc3b commit 6e4dba2
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2020 the original author or authors.
* Copyright 2009-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -954,6 +954,7 @@ final class AnnotationCompletionTests extends CompletionTestSuite {
| public static final String VALUE = 'nls'
|}
|'''.stripMargin()
setJavaPreference(CODEASSIST_ADDIMPORT, false)
checkProposalApplication(contents, expected, getIndexOf(contents, '(V'), 'VALUE', false)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.codehaus.groovy.eclipse.test.actions

import groovy.transform.NotYetImplemented

import org.junit.Test

/**
Expand Down Expand Up @@ -470,6 +472,52 @@ final class AliasingOrganizeImportsTests extends OrganizeImportsTestSuite {
doContentsCompareTest(contents)
}

@Test // https://github.com/groovy/groovy-eclipse/issues/1244
void testRetainStaticAlias12() {
addJavaSource('class C { static Object f }', 'C', '')
String contents = '''\
|import static C.f as v
|print v
|'''
doContentsCompareTest(contents)
}

@Test // https://github.com/groovy/groovy-eclipse/issues/1244
void testRetainStaticAlias13() {
addJavaSource('class C { static void m() {} }', 'C', '')
String contents = '''\
|import static C.m as f
|f('x')
|'''
doContentsCompareTest(contents)
}

@Test // https://github.com/groovy/groovy-eclipse/issues/1244
void testRetainStaticAlias14() {
addJavaSource('class C { static void m() {} }', 'C', '')
String contents = '''\
|import static C.m as f
|f 'x'
|'''
doContentsCompareTest(contents)
}

@Test @NotYetImplemented // https://github.com/groovy/groovy-eclipse/issues/732
void testRetainStaticAlias15() {
addJavaSource('class A { public static boolean isSomething(String s) { return true; } }', 'A', 'p')
addJavaSource('class B { public static boolean isPoorlyNamed(Iterable i) { return true; } }', 'B', 'p')

String contents = '''\
|import static p.A.isSomething
|import static p.B.isPoorlyNamed as isSomething
|String s
|Iterable i
|isSomething(s)
|isSomething(i)
|'''
doContentsCompareTest(contents)
}

@Test
void testRemoveStaticAlias1() {
String originalContents = '''\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,39 @@ final class OrganizeImportsTests extends OrganizeImportsTestSuite {
doContentsCompareTest(contents)
}

@Test // https://github.com/groovy/groovy-eclipse/issues/1244
void testRetainImport20() {
addJavaSource('class C { static Object f }', 'C', '')

String contents = '''\
|import static C.f
|print f
|'''
doContentsCompareTest(contents)
}

@Test // https://github.com/groovy/groovy-eclipse/issues/1244
void testRetainImport21() {
addJavaSource('class C { static void m() {} }', 'C', '')

String contents = '''\
|import static C.m
|m('x')
|'''
doContentsCompareTest(contents)
}

@Test // https://github.com/groovy/groovy-eclipse/issues/1244
void testRetainImport22() {
addJavaSource('class C { static void m() {} }', 'C', '')

String contents = '''\
|import static C.m
|m 'x'
|'''
doContentsCompareTest(contents)
}

@Test
void testChoices() {
String contents = '''\
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2000, 2018 IBM Corporation and others.
*
Expand Down Expand Up @@ -1176,7 +1177,8 @@ public String addStaticImport(String declaringTypeName, String simpleName, boole
*/
public String addStaticImport(String declaringTypeName, String simpleName, boolean isField, ImportRewriteContext context) {
String key = declaringTypeName + '.' + simpleName;
if (declaringTypeName.indexOf('.') == -1) {
// GROOVY add -- Groovy allows static import of default-package elements
if (declaringTypeName.indexOf('.') == -1 && !this.compilationUnit.getClass().getName().contains("Groovy")) { //$NON-NLS-1$
return key;
}
if (context == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2000, 2018 IBM Corporation and others.
*
Expand Down Expand Up @@ -1176,7 +1177,8 @@ public String addStaticImport(String declaringTypeName, String simpleName, boole
*/
public String addStaticImport(String declaringTypeName, String simpleName, boolean isField, ImportRewriteContext context) {
String key = declaringTypeName + '.' + simpleName;
if (declaringTypeName.indexOf('.') == -1) {
// GROOVY add -- Groovy allows static import of default-package elements
if (declaringTypeName.indexOf('.') == -1 && !this.compilationUnit.getClass().getName().contains("Groovy")) { //$NON-NLS-1$
return key;
}
if (context == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2000, 2019 IBM Corporation and others.
*
Expand Down Expand Up @@ -1176,7 +1177,8 @@ public String addStaticImport(String declaringTypeName, String simpleName, boole
*/
public String addStaticImport(String declaringTypeName, String simpleName, boolean isField, ImportRewriteContext context) {
String key = declaringTypeName + '.' + simpleName;
if (declaringTypeName.indexOf('.') == -1) {
// GROOVY add -- Groovy allows static import of default-package elements
if (declaringTypeName.indexOf('.') == -1 && !this.compilationUnit.getClass().getName().contains("Groovy")) { //$NON-NLS-1$
return key;
}
if (context == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2000, 2019 IBM Corporation and others.
*
Expand Down Expand Up @@ -1176,7 +1177,8 @@ public String addStaticImport(String declaringTypeName, String simpleName, boole
*/
public String addStaticImport(String declaringTypeName, String simpleName, boolean isField, ImportRewriteContext context) {
String key = declaringTypeName + '.' + simpleName;
if (declaringTypeName.indexOf('.') == -1) {
// GROOVY add -- Groovy allows static import of default-package elements
if (declaringTypeName.indexOf('.') == -1 && !this.compilationUnit.getClass().getName().contains("Groovy")) { //$NON-NLS-1$
return key;
}
if (context == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2000, 2019 IBM Corporation and others.
*
Expand Down Expand Up @@ -1176,7 +1177,8 @@ public String addStaticImport(String declaringTypeName, String simpleName, boole
*/
public String addStaticImport(String declaringTypeName, String simpleName, boolean isField, ImportRewriteContext context) {
String key = declaringTypeName + '.' + simpleName;
if (declaringTypeName.indexOf('.') == -1) {
// GROOVY add -- Groovy allows static import of default-package elements
if (declaringTypeName.indexOf('.') == -1 && !this.compilationUnit.getClass().getName().contains("Groovy")) { //$NON-NLS-1$
return key;
}
if (context == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2000, 2019 IBM Corporation and others.
*
Expand Down Expand Up @@ -1176,7 +1177,8 @@ public String addStaticImport(String declaringTypeName, String simpleName, boole
*/
public String addStaticImport(String declaringTypeName, String simpleName, boolean isField, ImportRewriteContext context) {
String key = declaringTypeName + '.' + simpleName;
if (declaringTypeName.indexOf('.') == -1) {
// GROOVY add -- Groovy allows static import of default-package elements
if (declaringTypeName.indexOf('.') == -1 && !this.compilationUnit.getClass().getName().contains("Groovy")) { //$NON-NLS-1$
return key;
}
if (context == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2000, 2019 IBM Corporation and others.
*
Expand Down Expand Up @@ -1176,7 +1177,8 @@ public String addStaticImport(String declaringTypeName, String simpleName, boole
*/
public String addStaticImport(String declaringTypeName, String simpleName, boolean isField, ImportRewriteContext context) {
String key = declaringTypeName + '.' + simpleName;
if (declaringTypeName.indexOf('.') == -1) {
// GROOVY add -- Groovy allows static import of default-package elements
if (declaringTypeName.indexOf('.') == -1 && !this.compilationUnit.getClass().getName().contains("Groovy")) { //$NON-NLS-1$
return key;
}
if (context == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2000, 2019 IBM Corporation and others.
*
Expand Down Expand Up @@ -1177,7 +1178,8 @@ public String addStaticImport(String declaringTypeName, String simpleName, boole
*/
public String addStaticImport(String declaringTypeName, String simpleName, boolean isField, ImportRewriteContext context) {
String key = declaringTypeName + '.' + simpleName;
if (declaringTypeName.indexOf('.') == -1) {
// GROOVY add -- Groovy allows static import of default-package elements
if (declaringTypeName.indexOf('.') == -1 && !this.compilationUnit.getClass().getName().contains("Groovy")) { //$NON-NLS-1$
return key;
}
if (context == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2000, 2020 IBM Corporation and others.
*
Expand Down Expand Up @@ -1177,7 +1178,8 @@ public String addStaticImport(String declaringTypeName, String simpleName, boole
*/
public String addStaticImport(String declaringTypeName, String simpleName, boolean isField, ImportRewriteContext context) {
String key = declaringTypeName + '.' + simpleName;
if (declaringTypeName.indexOf('.') == -1) {
// GROOVY add -- Groovy allows static import of default-package elements
if (declaringTypeName.indexOf('.') == -1 && !this.compilationUnit.getClass().getName().contains("Groovy")) { //$NON-NLS-1$
return key;
}
if (context == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2000, 2020 IBM Corporation and others.
*
Expand Down Expand Up @@ -1177,7 +1178,8 @@ public String addStaticImport(String declaringTypeName, String simpleName, boole
*/
public String addStaticImport(String declaringTypeName, String simpleName, boolean isField, ImportRewriteContext context) {
String key = declaringTypeName + '.' + simpleName;
if (declaringTypeName.indexOf('.') == -1) {
// GROOVY add -- Groovy allows static import of default-package elements
if (declaringTypeName.indexOf('.') == -1 && !this.compilationUnit.getClass().getName().contains("Groovy")) { //$NON-NLS-1$
return key;
}
if (context == null) {
Expand Down Expand Up @@ -1317,7 +1319,7 @@ public final TextEdit rewriteImports(IProgressMonitor monitor) throws CoreExcept

CompilationUnit usedAstRoot= this.astRoot;
if (usedAstRoot == null) {
ASTParser parser= ASTParser.newParser(AST.JLS15);
ASTParser parser= ASTParser.newParser(AST.JLS_Latest);
parser.setSource(this.compilationUnit);
parser.setFocalPosition(0); // reduced AST
parser.setResolveBindings(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2000, 2021 IBM Corporation and others.
*
Expand Down Expand Up @@ -1176,7 +1177,8 @@ public String addStaticImport(String declaringTypeName, String simpleName, boole
*/
public String addStaticImport(String declaringTypeName, String simpleName, boolean isField, ImportRewriteContext context) {
String key = declaringTypeName + '.' + simpleName;
if (declaringTypeName.indexOf('.') == -1) {
// GROOVY add -- Groovy allows static import of default-package elements
if (declaringTypeName.indexOf('.') == -1 && !this.compilationUnit.getClass().getName().contains("Groovy")) { //$NON-NLS-1$
return key;
}
if (context == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GROOVY PATCHED
/*******************************************************************************
* Copyright (c) 2000, 2018 IBM Corporation and others.
*
Expand Down Expand Up @@ -1176,7 +1177,8 @@ public String addStaticImport(String declaringTypeName, String simpleName, boole
*/
public String addStaticImport(String declaringTypeName, String simpleName, boolean isField, ImportRewriteContext context) {
String key = declaringTypeName + '.' + simpleName;
if (declaringTypeName.indexOf('.') == -1) {
// GROOVY add -- Groovy allows static import of default-package elements
if (declaringTypeName.indexOf('.') == -1 && !this.compilationUnit.getClass().getName().contains("Groovy")) { //$NON-NLS-1$
return key;
}
if (context == null) {
Expand Down

0 comments on commit 6e4dba2

Please sign in to comment.