void () { JavaRecursiveElementWalkingVisitor visitor = new JavaRecursiveElementWalkingVisitor() { @Override public void visitReferenceElement(@NotNull PsiJavaCodeReferenceElement reference) { super.visitReferenceElement(reference); final PsiElement resolved = reference.resolve(); if (resolved instanceof PsiTypeParameter typeParameter) { final PsiTypeParameterListOwner owner = typeParameter.getOwner(); if (owner != null && !PsiTreeUtil.isAncestor(myAnonOrLocalClass, owner, false) && (CommonJavaRefactoringUtil.isInStaticContext(owner, myTargetClass) || myMakeStatic)) { myTypeParametersToCreate.add(typeParameter); } } } }; myAnonOrLocalClass.accept(visitor); for (VariableInfo info : myVariableInfos) { PsiTypeElement typeElement = info.variable.getTypeElement(); if (typeElement != null) { typeElement.accept(visitor); } } }