Skip to content

Commit 1c2ffa1

Browse files
authored
Merge pull request #8666 from lahodaj/create-local-variable-in-lambda-in-field
Fixing create local variable fix for lambdas inside field initializers.
2 parents c5ecbce + ac96f5d commit 1c2ffa1

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

java/java.hints/src/org/netbeans/modules/java/hints/errors/CreateElement.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ private static List<Fix> analyzeImpl(CompilationInfo info, String diagnosticKey,
170170
TreePath parent = null;
171171
TreePath firstClass = null;
172172
TreePath firstMethod = null;
173+
TreePath firstLambda = null;
173174
TreePath firstVar = null;
174175
TreePath firstInitializer = null;
175176
TreePath methodInvocation = null;
@@ -194,6 +195,9 @@ private static List<Fix> analyzeImpl(CompilationInfo info, String diagnosticKey,
194195
firstClass = path;
195196
if (leafKind == Kind.METHOD && firstMethod == null && firstClass == null)
196197
firstMethod = path;
198+
if (leafKind == Kind.LAMBDA_EXPRESSION && firstMethod == null &&
199+
firstClass == null && firstLambda == null && firstInitializer == null)
200+
firstLambda = path;
197201
//static/dynamic initializer:
198202
if ( leafKind == Kind.BLOCK && TreeUtilities.CLASS_TREE_KINDS.contains(path.getParentPath().getLeaf().getKind())
199203
&& firstMethod == null && firstClass == null)
@@ -495,7 +499,7 @@ private static List<Fix> analyzeImpl(CompilationInfo info, String diagnosticKey,
495499
int identifierPos = (int) info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), errorPath.getLeaf());
496500
if (ee != null && fixTypes.contains(ElementKind.PARAMETER) && !Utilities.isMethodHeaderInsideGuardedBlock(info, (MethodTree) firstMethod.getLeaf()))
497501
result.add(new AddParameterOrLocalFix(info, type, simpleName, ElementKind.PARAMETER, identifierPos).toEditorFix());
498-
if ((firstMethod != null || firstInitializer != null) && fixTypes.contains(ElementKind.LOCAL_VARIABLE) && ErrorFixesFakeHint.enabled(ErrorFixesFakeHint.FixKind.CREATE_LOCAL_VARIABLE))
502+
if ((firstMethod != null || firstInitializer != null || firstLambda != null) && fixTypes.contains(ElementKind.LOCAL_VARIABLE) && ErrorFixesFakeHint.enabled(ErrorFixesFakeHint.FixKind.CREATE_LOCAL_VARIABLE))
499503
result.add(new AddParameterOrLocalFix(info, type, simpleName, ElementKind.LOCAL_VARIABLE, identifierPos).toEditorFix());
500504
if (fixTypes.contains(ElementKind.RESOURCE_VARIABLE))
501505
result.add(new AddParameterOrLocalFix(info, type, simpleName, ElementKind.RESOURCE_VARIABLE, identifierPos).toEditorFix());

java/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/AddParameterOrLocalFixTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,27 @@ public void testTWR2() throws Exception {
468468
"AddParameterOrLocalFix:in:java.io.FileInputStream:RESOURCE_VARIABLE");
469469
}
470470

471+
public void testLocalInLambdaInField() throws Exception {
472+
performFixTest("test/Test.java",
473+
"""
474+
package test;
475+
public class Test {
476+
Runnable r = () -> {
477+
i|i = 0;
478+
};
479+
}
480+
""",
481+
"AddParameterOrLocalFix:ii:int:LOCAL_VARIABLE",
482+
"""
483+
package test;
484+
public class Test {
485+
Runnable r = () -> {
486+
int ii = 0;
487+
};
488+
}
489+
""".replaceAll("[ \t\n]+", " "));
490+
}
491+
471492
@Override
472493
protected List<Fix> computeFixes(CompilationInfo info, String diagnosticCode, int pos, TreePath path) throws Exception {
473494
List<Fix> fixes = super.computeFixes(info, diagnosticCode, pos, path);

0 commit comments

Comments
 (0)