@@ -73,6 +73,14 @@ public boolean isAvailable(@NotNull Project project, @NotNull Editor editor, @No
7373 return false ;
7474 }
7575
76+ //not directly inside an implicitly declared class
77+ PsiClass targetClass = PsiUtil .getContainingClass (lambdaExpression );
78+ PsiMethod method = PsiTreeUtil .getParentOfType (lambdaExpression , PsiMethod .class , true );
79+ if (targetClass == null ||
80+ (targetClass instanceof PsiImplicitClass && method != null && method .hasModifierProperty (PsiModifier .STATIC ))) {
81+ return false ;
82+ }
83+
7684 PsiExpression asMethodReference = LambdaCanBeMethodReferenceInspection
7785 .canBeMethodReferenceProblem (body , lambdaExpression .getParameterList ().getParameters (), functionalInterfaceType , null );
7886 if (asMethodReference != null ) return false ;
@@ -82,8 +90,7 @@ public boolean isAvailable(@NotNull Project project, @NotNull Editor editor, @No
8290 wrapper .prepareAndCheckExitStatements (toExtract , body );
8391 PsiVariable [] outputVariables = wrapper .getOutputVariables ();
8492 List <PsiVariable > inputVariables = wrapper .getInputVariables (body , toExtract , outputVariables );
85- return inputVariables .stream ()
86- .allMatch (variable -> variable instanceof PsiParameter && ((PsiParameter )variable ).getDeclarationScope () == lambdaExpression );
93+ return ContainerUtil .and (inputVariables , variable -> variable instanceof PsiParameter && ((PsiParameter )variable ).getDeclarationScope () == lambdaExpression );
8794 }
8895 catch (PrepareFailedException | ControlFlowWrapper .ExitStatementsNotSameException ignored ) {
8996 }
@@ -102,7 +109,8 @@ public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull Ps
102109 PsiElement [] elements = body .getStatements ();
103110
104111 HashSet <PsiField > usedFields = new HashSet <>();
105- boolean canBeStatic = CommonJavaRefactoringUtil .canBeStatic (targetClass , lambdaExpression , elements , usedFields ) && usedFields .isEmpty ();
112+ boolean canBeStatic = CommonJavaRefactoringUtil .canBeStatic (targetClass , lambdaExpression , elements , usedFields ) &&
113+ usedFields .isEmpty () && !(targetClass instanceof PsiImplicitClass );
106114 PsiElementFactory elementFactory = JavaPsiFacade .getElementFactory (targetClass .getProject ());
107115 PsiType functionalInterfaceType = lambdaExpression .getFunctionalInterfaceType ();
108116
@@ -144,7 +152,7 @@ private static void startInplaceRename(Editor editor, PsiMethod method, PsiMetho
144152 PsiIdentifier nameIdentifier = method .getNameIdentifier ();
145153 if (nameIdentifier == null ) return ;
146154 nameIdentifier = CodeInsightUtilCore .forcePsiPostprocessAndRestoreElement (nameIdentifier );
147-
155+ if ( nameIdentifier == null ) return ;
148156 //try to navigate to reference name
149157 editor .getCaretModel ().moveToOffset (ObjectUtils .notNull (methodReference .getReferenceNameElement (), nameIdentifier ).getTextOffset ());
150158
0 commit comments