Skip to content

Commit

Permalink
Fix move of record to new file refactoring when component accessed (e…
Browse files Browse the repository at this point in the history
…clipse-jdt#1453)

- add check in MemberVisibilityAdjustor.rewriteVisibility() to ignore
  record fields
- add new test to MoveInnerToNewTests16
- fixes eclipse-jdt#1419
  • Loading branch information
jjohnstn authored Jun 17, 2024
1 parent d2721bd commit 76cb0af
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,14 @@ protected final void rewriteVisibility(final MemberVisibilityAdjustor adjustor,
Assert.isNotNull(rewrite);
Assert.isNotNull(root);
final int visibility= fKeyword != null ? fKeyword.toFlagValue() : Modifier.NONE;
if (fMember instanceof IField && !Flags.isEnum(fMember.getFlags())) {
if (fMember instanceof IField && !Flags.isEnum(fMember.getFlags()) && !Flags.isRecord(fMember.getFlags())) {
final VariableDeclarationFragment fragment= ASTNodeSearchUtil.getFieldDeclarationFragmentNode((IField) fMember, root);
final FieldDeclaration declaration= (FieldDeclaration) fragment.getParent();
VariableDeclarationFragment[] fragmentsToChange= new VariableDeclarationFragment[] { fragment };
VariableDeclarationRewrite.rewriteModifiers(declaration, fragmentsToChange, visibility, ModifierRewrite.VISIBILITY_MODIFIERS, rewrite, group);
if (status != null)
adjustor.fStatus.merge(status);
} else if (fMember != null) {
} else if (fMember != null && !Flags.isRecord(fMember.getFlags())) {
final BodyDeclaration declaration= ASTNodeSearchUtil.getBodyDeclarationNode(fMember, root);
if (declaration != null) {
ModifierRewrite.create(rewrite, declaration).setVisibility(visibility, group);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package p1;

public record Foo(Foo.Bar bar) {
static record Bar<T> (T left, T right) {}

public String foo(Bar<String> in) {
return in.left();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
*
*/
package p1;

record Bar<T> (T left, T right) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package p1;

public record Foo(Bar bar) {
public String foo(Bar<String> in) {
return in.left();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2021 IBM Corporation and others.
* Copyright (c) 2019, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -82,6 +82,39 @@ public void moveInnerRecord() throws Exception{
assertTrue("activation was supposed to be successful" + preconditionResult.toString(), preconditionResult.isOK());


RefactoringStatus checkInputResult= ref.checkFinalConditions(new NullProgressMonitor());
assertFalse("precondition was supposed to pass", checkInputResult.hasError());
performChange(ref, false);

assertEquals("p1 files", 2, packP1.getChildren().length);

String expectedSource= getFileContents(getRefactoringPath() + getName() + outDir + p1Name + "/Foo.java");
assertEqualLines("incorrect update of Foo", expectedSource, packP1.getCompilationUnit("Foo.java").getSource());

expectedSource= getFileContents(getRefactoringPath() + getName() + outDir + p1Name + "/Bar.java");
assertEqualLines("incorrect creation of Bar", expectedSource, packP1.getCompilationUnit("Bar.java").getSource());

}

@Test
public void moveInnerRecord2() throws Exception{
ParticipantTesting.reset();
final String p1Name= "p1";
final String inDir= "/in/";
final String outDir= "/out/";

IPackageFragment packP1= createPackage(p1Name);
ICompilationUnit p1Foo= createCu(packP1, getName() + inDir + p1Name + "/Foo.java", "Foo.java");
IType fooType= p1Foo.getTypes()[0];
IType barType= fooType.getTypes()[0];

assertTrue("should be enabled", RefactoringAvailabilityTester.isMoveInnerAvailable(barType));
MoveInnerToTopRefactoring ref= ((RefactoringAvailabilityTester.isMoveInnerAvailable(barType)) ? new MoveInnerToTopRefactoring(barType, JavaPreferencesSettings.getCodeGenerationSettings(barType.getJavaProject())) : null);
assertNotNull("MoveInnerToTopRefactoring should not be null", ref);
RefactoringStatus preconditionResult= ref.checkInitialConditions(new NullProgressMonitor());
assertTrue("activation was supposed to be successful" + preconditionResult.toString(), preconditionResult.isOK());


RefactoringStatus checkInputResult= ref.checkFinalConditions(new NullProgressMonitor());
assertFalse("precondition was supposed to pass", checkInputResult.hasError());
performChange(ref, false);
Expand Down

0 comments on commit 76cb0af

Please sign in to comment.