eclipse-jdt / eclipse.jdt.ui

Eclipse Public License 2.0
35 stars 86 forks source link

Move to new file for static nested record throws NPE #1419

Closed stephan-herrmann closed 2 months ago

stephan-herrmann commented 3 months ago

I had this rocket-science record defined as a nested type:

static record Pair<T> (T left, T right) {}

Then I decided this should be a util available for others too: Refactor > Move to new file:

java.lang.NullPointerException: Cannot invoke "org.eclipse.jdt.core.dom.VariableDeclarationFragment.getParent()" because "fragment" is null
    at org.eclipse.jdt.internal.corext.refactoring.structure.MemberVisibilityAdjustor$IncomingMemberVisibilityAdjustment.rewriteVisibility(MemberVisibilityAdjustor.java:171)
    at org.eclipse.jdt.internal.corext.refactoring.structure.MemberVisibilityAdjustor$IncomingMemberVisibilityAdjustment.rewriteVisibility(MemberVisibilityAdjustor.java:201)
    at org.eclipse.jdt.internal.corext.refactoring.structure.MemberVisibilityAdjustor.rewriteVisibility(MemberVisibilityAdjustor.java:1064)
    at org.eclipse.jdt.internal.corext.refactoring.structure.MoveInnerToTopRefactoring.createChangeManager(MoveInnerToTopRefactoring.java:840)
    at org.eclipse.jdt.internal.corext.refactoring.structure.MoveInnerToTopRefactoring.checkFinalConditions(MoveInnerToTopRefactoring.java:731)
    at org.eclipse.ltk.core.refactoring.CheckConditionsOperation.run(CheckConditionsOperation.java:86)
    at org.eclipse.ltk.core.refactoring.CreateChangeOperation.run(CreateChangeOperation.java:116)
jjohnstn commented 2 months ago

Hi @stephan-herrmann I realize it is quite late from when you opened the issue but do you happen to have a test scenario? I tried a simple class which has the inner static record and moving to a new file works fine for me. I can easily add a null check on the fragment variable but that is not the right solution as it is supposed to be changing the visibility of a member.

stephan-herrmann commented 2 months ago

@jjohnstn please try this:

package test;

public class Foo {
    static record Pair<T> (T left, T right) {}

    String test (Pair<String> p) {
        return p.left();
    }
}

When I tried this right now, I happened to be in Build id: I20231217-1800

jjohnstn commented 2 months ago

Thanks @stephan-herrmann that did it. My test is similar but I didn't access the record component.