Skip to content

Commit

Permalink
"extract lambda to method" should create valid code with appropriate …
Browse files Browse the repository at this point in the history
…method params
  • Loading branch information
Honza committed Oct 5, 2023
1 parent ceec539 commit db8b8b3
Showing 1 changed file with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import org.netbeans.api.java.source.CompilationInfo;
Expand Down Expand Up @@ -76,6 +77,7 @@ final class ScanStatement extends ErrorAwareTreePathScanner<Void, Void> {
private boolean secondPass = false;
private boolean stopSecondPass = false;
private final AtomicBoolean cancel;
private boolean isLambda = false;

/**
* Nesting level for local classes and lambdas. Ignore returns in nested scopes
Expand Down Expand Up @@ -114,12 +116,18 @@ public Void scan(Tree tree, Void p) {
}
return null;
}

@Override
public Void visitLambdaExpression(LambdaExpressionTree node, Void p) {
nesting++;
if (node.equals(firstInSelection)) {
phase = PHASE_INSIDE_SELECTION;
}
isLambda = true;

super.visitLambdaExpression(node, p);
nesting--;
isLambda = false;
return null;
}

Expand Down Expand Up @@ -161,7 +169,12 @@ public Void visitVariable(VariableTree node, Void p) {
public Void visitIdentifier(IdentifierTree node, Void p) {
Element e = info.getTrees().getElement(getCurrentPath());
if (e != null) {
if (IntroduceHint.LOCAL_VARIABLES.contains(e.getKind())) {

if (isLambda && treesSeensInSelection.contains(node) && e.getKind().equals(ElementKind.LOCAL_VARIABLE)){
usedLocalVariables.put((VariableElement) e, !localVariables.contains(e));
}

if (IntroduceHint.LOCAL_VARIABLES.contains(e.getKind()) && (e.getKind()!=ElementKind.PARAMETER) || isLambda) {
switch (phase) {
case PHASE_INSIDE_SELECTION:
if (localVariables.contains(e) && usedLocalVariables.get(e) == null) {
Expand Down Expand Up @@ -356,4 +369,4 @@ String verifyExits(boolean exitsFromAllBranches) {
return null;
}

}
}

0 comments on commit db8b8b3

Please sign in to comment.