Honza-cz commented on code in PR #6595:
URL: https://github.com/apache/netbeans/pull/6595#discussion_r1370700314
##########
java/java.hints/src/org/netbeans/modules/java/hints/introduce/ScanStatement.java:
##########
@@ -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;
Review Comment:
I was checking this use case with nested lambda. It is true, that flag is
set for whole lambda. On the other hand it seems to fix the issue. I realized a
new occurrence of issue, tested on netbeans 19:
```
static void dummy(final String first) {
String unused = "unused";
String second = "world";
Runnable r = () -> {
Runnable g = () -> {
final String m = first + second + m1;
System.out.println("m" + m);
};
};
}
```
select `first + second + m1` => extract => non compilable code:
```
static void dummy(final String first) {
String unused = "unused";
String second = "world";
Runnable r = () -> {
Runnable g = () -> {
final String m = aaa();
System.out.println("m" + m);
};
};
}
private static String aaa() {
return first + second + m1;
}
```
with my version it create valid code:
```
static void dummy(final String first) {
String unused = "unused";
String second = "world";
Runnable r = () -> {
Runnable g = () -> {
final String m = aaa(first, second);
System.out.println("m" + m);
};
};
}
private static String aaa(final String first, String second) {
return first + second + m1;
}
```
I was testing nested lambada and it works fine, just some a sample:
```
public static void main(String[] args) {
String unused = "unused";
String first = "hello";
String second = "world";
Function<String,Integer> f1 = t -> {
Function<String,String> f2 = u -> {
final String m = first + second + m1;
System.out.println("m" + m);
final Function<String, Integer> len = s -> (u + s + m + first +
m1).length();
System.out.println("len" + len.apply("hello"));
return (u + m + first + m1);
};
return f2.apply(t).length();
};
}
```
=> various extract to method:
```
public static void main(String[] args) {
String unused = "unused";
String first = "hello";
String second = "world";
Function<String, Integer> f1 = t -> {
Function<String, String> f2 = u -> {
final String m = z1(first, second);
System.out.println("m" + m);
Function<String, Integer> len = z3(u, m, first);
System.out.println("len" + len.apply("hello"));
return (u + m + first + m1);
};
return z4(f2, t);
};
}
private static int z4(Function<String, String> f2, String t) {
return f2.apply(t).length();
}
private static Function<String, Integer> z3(String u, final String m, String
first) {
final Function<String, Integer> len = s -> z2(u, s, m, first);
return len;
}
private static int z2(String u, String s, final String m, String first) {
return (u + s + m + first + m1).length();
}
private static String z1(String first, String second) {
return first + second + m1;
}
```
What didn't work if I select whole statement with return `return
f2.apply(t).length();` but this is another issue which I was not able to fix
yet (and announced it in the description above)
I admit that `isLambda` flag is a bit magic flag, but it works in case of
lambda or non-lambda code. At least I was not able to find a use case, which
could be broken by this flag.
Do you want me to dig deeper?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists