Skip to content

The use of the Java Stream leads to failures in 'Move Method' refactoring. #4289

Closed

Description

@assdfsdafasfa

When using a tool to detect Feature Envy and perform automated refactoring, if the refactored code contains syntax errors, should I attempt to fix them manually?
Before Refactoring:

import java.util.ArrayList;
import javax.print.attribute.standard.Destination;
import static java.util.stream.Collectors.toList;
public class SourceClass {
    TargetClass t;
    private static class Config { }
    private final Config config = new Config();
    public void processData(TargetClass tarClass) {
        new ArrayList<Integer>().stream().filter(this::isPositive).collect(toList());
        tarClass.m();
    }
    private boolean isPositive(Integer number) {
        return number != null && number > 0;
    }
}
public class TargetClass {
    public void m() {}
}

After Refactoring:

public class TargetClass {

	public void processData(SourceClass sourceClass) {
	    new ArrayList<Integer>().stream().filter(sourceClass::isPositive).collect(toList());
	}
}

Error: The type SourceClass does not define isPositive(Integer) that is applicable here

Activity

  1. fbricon commented on Dec 19, 2025

    @fbricon
    Collaborator

    From the example you gave, if you move the processData method to TargetClass, where TargetClass is in another file, the boolean isPositive(Integer number) method's visibility in SourceClass is changed from private to package protected, which makes it visible to TargetClass. Code compiles fine.

    Maybe record a screencast of your workflow so we get a better understanding of your issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions