Skip to content

Commit 0b13c1d

Browse files
committed
Fix for #1357: rename package includes star imports
1 parent a3129b3 commit 0b13c1d

File tree

4 files changed

+52
-20
lines changed

4 files changed

+52
-20
lines changed

base/org.eclipse.jdt.groovy.core/src/org/codehaus/jdt/groovy/integration/internal/GroovyIndexingVisitor.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2020 the original author or authors.
2+
* Copyright 2009-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import org.codehaus.groovy.ast.GenericsType;
2323
import org.codehaus.groovy.ast.ImportNode;
2424
import org.codehaus.groovy.ast.MethodNode;
25+
import org.codehaus.groovy.ast.PackageNode;
2526
import org.codehaus.groovy.ast.Parameter;
2627
import org.codehaus.groovy.ast.expr.AnnotationConstantExpression;
2728
import org.codehaus.groovy.ast.expr.ArrayExpression;
@@ -40,10 +41,12 @@
4041
import org.codehaus.groovy.ast.expr.TupleExpression;
4142
import org.codehaus.groovy.ast.expr.VariableExpression;
4243
import org.codehaus.groovy.syntax.Types;
44+
import org.eclipse.jdt.core.compiler.CharOperation;
4345
import org.eclipse.jdt.groovy.core.util.DepthFirstVisitor;
4446
import org.eclipse.jdt.groovy.core.util.GroovyUtils;
4547
import org.eclipse.jdt.groovy.search.AccessorSupport;
4648
import org.eclipse.jdt.internal.compiler.ISourceElementRequestor;
49+
import org.eclipse.jdt.internal.compiler.ast.ImportReference;
4750

4851
/**
4952
* Visits a {@link ModuleNode} and passes it to an indexing element requestor,
@@ -61,10 +64,22 @@ class GroovyIndexingVisitor extends DepthFirstVisitor {
6164

6265
// NOTE: Expected entry point is visitModule(ModuleNode).
6366

67+
@Override
68+
public void visitPackage(final PackageNode node) {
69+
char[][] tokens = CharOperation.splitOn('.', node.getName().toCharArray(), 0, node.getName().length() - 1);
70+
requestor.acceptPackage(new ImportReference(tokens, new long[tokens.length], false, 0));
71+
super.visitPackage(node);
72+
}
73+
6474
@Override
6575
public void visitImport(final ImportNode node) {
66-
if (node.getType() != null && node.getEnd() > 0) {
67-
visitTypeReference(node.getType(), false, true);
76+
if (node.getEnd() > 0) {
77+
if (node.getType() == null) { String name = node.getPackageName(); // includes trailing '.'
78+
char[][] tokens = CharOperation.splitOn('.', name.toCharArray(), 0, name.length() - 1);
79+
requestor.acceptUnknownReference(tokens, node.getNameStart(), node.getNameEnd());
80+
} else {
81+
visitTypeReference(node.getType(), false, true);
82+
}
6883
}
6984
super.visitImport(node);
7085
}
@@ -363,11 +378,6 @@ private void visitTypeParameters(final GenericsType[] generics, final String typ
363378

364379
private static char[][] splitName(final ClassNode type, final boolean useQualifiedName) {
365380
String name = useQualifiedName ? type.getName() : type.getNameWithoutPackage();
366-
String[] nameArr = name.split("\\.");
367-
char[][] nameCharArr = new char[nameArr.length][];
368-
for (int i = 0; i < nameArr.length; i += 1) {
369-
nameCharArr[i] = nameArr[i].toCharArray();
370-
}
371-
return nameCharArr;
381+
return CharOperation.splitOn('.', name.toCharArray());
372382
}
373383
}

base/org.eclipse.jdt.groovy.core/src/org/codehaus/jdt/groovy/internal/compiler/ast/GroovyCompilationUnitDeclaration.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ private void createImportDeclarations(ModuleNode moduleNode) {
10061006
nameEndOffset = importPackage.getNameEnd() + 1;
10071007
nameStartOffset = importPackage.getNameStart();
10081008
}
1009-
char[][] splits = CharOperation.splitOn('.', importPackage.getPackageName().substring(0, importPackage.getPackageName().length() - 1).toCharArray());
1009+
char[][] splits = CharOperation.splitOn('.', importPackage.getPackageName().toCharArray(), 0, importPackage.getPackageName().length() - 1);
10101010
ImportReference ref = new ImportReference(splits, positionsFor(splits, nameStartOffset, nameEndOffset), true, Flags.AccDefault);
10111011
ref.annotations = createAnnotations(importPackage.getAnnotations());
10121012

@@ -1022,6 +1022,7 @@ private void createImportDeclarations(ModuleNode moduleNode) {
10221022
ref.declarationSourceStart = importPackage.getStart();
10231023
ref.declarationSourceEnd = ref.sourceEnd;
10241024
}
1025+
ref.trailingStarPosition = ref.sourceEnd;
10251026

10261027
importReferences.put(lexicalKey(ref), ref);
10271028
}
@@ -1038,9 +1039,9 @@ private void createImportDeclarations(ModuleNode moduleNode) {
10381039
long[] positions = positionsFor(splits, nameStartOffset, nameEndOffset);
10391040
ImportReference ref;
10401041
if (importNode.getAlias() == null || importNode.getAlias().length() < 1 || importNode.getAlias().equals(importNode.getFieldName())) {
1041-
ref = new ImportReference(splits, positions, false, Flags.AccDefault | Flags.AccStatic);
1042+
ref = new ImportReference(splits, positions, false, Flags.AccStatic);
10421043
} else {
1043-
ref = new AliasImportReference(importNode.getAlias().toCharArray(), splits, positions, false, Flags.AccDefault | Flags.AccStatic);
1044+
ref = new AliasImportReference(importNode.getAlias().toCharArray(), splits, positions, false, Flags.AccStatic);
10441045
}
10451046
ref.annotations = createAnnotations(importNode.getAnnotations());
10461047

@@ -1071,7 +1072,7 @@ private void createImportDeclarations(ModuleNode moduleNode) {
10711072
}
10721073
char[][] splits = CharOperation.splitOn('.', classname.toCharArray());
10731074
long[] positions = positionsFor(splits, nameStartOffset, nameEndOffset);
1074-
ImportReference ref = new ImportReference(splits, positions, true, Flags.AccDefault | Flags.AccStatic);
1075+
ImportReference ref = new ImportReference(splits, positions, true, Flags.AccStatic);
10751076
ref.annotations = createAnnotations(importNode.getAnnotations());
10761077

10771078
ref.sourceEnd = Math.max(endOffset - 1, ref.sourceStart); // For error reporting, Eclipse wants -1
@@ -1086,6 +1087,7 @@ private void createImportDeclarations(ModuleNode moduleNode) {
10861087
ref.declarationSourceStart = importNode.getStart();
10871088
ref.declarationSourceEnd = ref.sourceEnd;
10881089
}
1090+
ref.trailingStarPosition = ref.sourceEnd;
10891091

10901092
importReferences.put(lexicalKey(ref), ref);
10911093
}

base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/PackageReferenceSearchRequestor.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2009-2017 the original author or authors.
2+
* Copyright 2009-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* https://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -53,16 +53,14 @@ public VisitStatus acceptASTNode(ASTNode node, TypeLookupResult result, IJavaEle
5353
if (name.startsWith(packageName)) {
5454
notifyRequestor(enclosingElement, node.getStart());
5555
}
56-
5756
} else if (node instanceof ImportNode) {
5857
ImportNode i = ((ImportNode) node);
5958
String name = i.getClassName(); // "java.util.Map$Entry" or null
6059
if (i.isStar() && !i.isStatic()) name = i.getPackageName(); // "java.util."
6160

6261
if (name.startsWith(packageName)) {
63-
notifyRequestor(enclosingElement, i.getType().getStart());
62+
notifyRequestor(enclosingElement, i.getNameStart());
6463
}
65-
6664
} else if (node instanceof ClassNode && enclosingElement.getElementType() != IJavaElement.IMPORT_DECLARATION) {
6765
String name = ((ClassNode) node).getName(); // "java.util.Map$Entry"
6866

@@ -76,7 +74,6 @@ public VisitStatus acceptASTNode(ASTNode node, TypeLookupResult result, IJavaEle
7674
}
7775
}
7876
}
79-
8077
} catch (CoreException e) {
8178
Util.log(e, "Error accepting " + node.getClass().getSimpleName() + " for " + enclosingElement);
8279
}

ide-test/org.codehaus.groovy.eclipse.refactoring.test/src/org/codehaus/groovy/eclipse/refactoring/test/rename/RenamePackageTests.groovy

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2020 the original author or authors.
2+
* Copyright 2009-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -175,4 +175,27 @@ final class RenamePackageTests extends RenameRefactoringTestSuite {
175175
|'''.stripMargin()
176176
))
177177
}
178+
179+
@Test // star import reference
180+
void testRenamePackage6() {
181+
renamePackage(new TestSource(
182+
pack: 'p', name: 'H.java',
183+
contents: 'package p; interface H { }',
184+
finalContents: 'package q; interface H { }'
185+
), new TestSource(
186+
pack: 'x', name: 'I.groovy',
187+
contents: '''\
188+
|package x
189+
|import p.*
190+
|interface I extends H {
191+
|}
192+
|'''.stripMargin(),
193+
finalContents: '''\
194+
|package x
195+
|import q.*
196+
|interface I extends H {
197+
|}
198+
|'''.stripMargin()
199+
))
200+
}
178201
}

0 commit comments

Comments
 (0)