Skip to content

Commit

Permalink
[3921] Correctly handling opens directive in package rename.
Browse files Browse the repository at this point in the history
  • Loading branch information
jlahoda committed Apr 17, 2022
1 parent e623b46 commit c843263
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -449,16 +449,26 @@ public Tree visitImport(ImportTree node, Element p) {

@Override
public Tree visitExports(ExportsTree node, Element p) {
renamePackage(node.getPackageName());
return super.visitExports(node, p);
}

@Override
public Tree visitOpens(OpensTree node, Element p) {
renamePackage(node.getPackageName());
return super.visitOpens(node, p);
}

private void renamePackage(ExpressionTree packageName) {
if (!workingCopy.getTreeUtilities().isSynthetic(getCurrentPath())) {
final Element el = workingCopy.getTrees().getElement(new TreePath(getCurrentPath(), node.getPackageName()));
final Element el = workingCopy.getTrees().getElement(new TreePath(getCurrentPath(), packageName));
if (el != null && el.getKind() == ElementKind.PACKAGE && isThisPackageMoving((PackageElement)el)) {
Tree nju = make.Identifier(getTargetPackageName(el));
rewrite(node.getPackageName(), nju);
rewrite(packageName, nju);
}
}
return super.visitExports(node, p);
}

private boolean containsAnyOf(Element el, EnumSet<Modifier> neededMods) {
for (Modifier mod : neededMods) {
if(el.getModifiers().contains(mod)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
package org.netbeans.modules.refactoring.java.test;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.netbeans.modules.java.source.parsing.JavacParser;
import org.netbeans.modules.refactoring.api.Problem;
import org.netbeans.modules.refactoring.api.RefactoringSession;
Expand All @@ -37,8 +39,10 @@
*/
public class RenamePackageTest extends RefactoringTestBase {

private static final Set<String> MODULAR_TESTS = new HashSet<>(Arrays.asList("testModuleOpens"));

public RenamePackageTest(String name) {
super(name, "1.8");
super(name, MODULAR_TESTS.contains(name) ? "9" : "1.8");
}

static {
Expand Down Expand Up @@ -129,6 +133,26 @@ public void test218766() throws Exception {
+ "}"));
}

public void testModuleOpens() throws Exception {
writeFilesAndWaitForScan(src,
new File("module-info.java", "module m {\n"
+ "exports t;\n"
+ "opens t;\n"
+ "}"),
new File("t/A.java", "package t;\n"
+ "public class A {\n"
+ "}"));
performRenameFolder(src.getFileObject("t"), "u", false);
verifyContent(src,
new File("module-info.java", "module m {\n"
+ "exports u;\n"
+ "opens u;\n"
+ "}"),
new File("u/A.java", "package u;\n"
+ "public class A {\n"
+ "}"));
}


private void performRenameFolder(FileObject source, final String newname, boolean searchInComments, Problem... expectedProblems) throws Exception {
final RenameRefactoring[] r = new RenameRefactoring[1];
Expand Down

0 comments on commit c843263

Please sign in to comment.