private PsiMethod generateMethodFor(
      PsiField field, String methodName, String parameterName, PsiElementFactory elementFactory) {
    PsiMethod withMethod =
        elementFactory.createMethod(
            methodName, elementFactory.createType(field.getContainingClass()));
    PsiParameter parameter = elementFactory.createParameter(parameterName, field.getType());
    withMethod.getParameterList().add(parameter);
    PsiUtil.setModifierProperty(withMethod, PsiModifier.PUBLIC, true);

    return withMethod;
  }
  private static LookupElementBuilder createOverridingLookupElement(
      boolean implemented,
      final PsiMethod baseMethod,
      PsiClass baseClass,
      PsiSubstitutor substitutor) {

    RowIcon icon =
        new RowIcon(
            baseMethod.getIcon(0),
            implemented ? AllIcons.Gutter.ImplementingMethod : AllIcons.Gutter.OverridingMethod);
    return createGenerateMethodElement(
        baseMethod,
        substitutor,
        icon,
        baseClass.getName(),
        new InsertHandler<LookupElement>() {
          @Override
          public void handleInsert(InsertionContext context, LookupElement item) {
            removeLookupString(context);

            final PsiClass parent =
                PsiTreeUtil.findElementOfClassAtOffset(
                    context.getFile(), context.getStartOffset(), PsiClass.class, false);
            if (parent == null) return;

            List<PsiMethod> prototypes =
                OverrideImplementUtil.overrideOrImplementMethod(parent, baseMethod, false);
            insertGenerationInfos(
                context, OverrideImplementUtil.convert2GenerationInfos(prototypes));
          }
        });
  }
 private static void addSuperSignatureElements(
     final PsiClass parent,
     boolean implemented,
     CompletionResultSet result,
     Set<MethodSignature> addedSignatures) {
   for (CandidateInfo candidate :
       OverrideImplementExploreUtil.getMethodsToOverrideImplement(parent, implemented)) {
     PsiMethod baseMethod = (PsiMethod) candidate.getElement();
     PsiClass baseClass = baseMethod.getContainingClass();
     PsiSubstitutor substitutor = candidate.getSubstitutor();
     if (!baseMethod.isConstructor()
         && baseClass != null
         && addedSignatures.add(baseMethod.getSignature(substitutor))) {
       result.addElement(
           createOverridingLookupElement(implemented, baseMethod, baseClass, substitutor));
     }
   }
 }
  private PsiMethod generateMethodBodyFor(
      PsiMethod method,
      String propertyName,
      String parameterName,
      PsiElementFactory elementFactory) {
    StringBuilder methodBodyBuilder =
        new StringBuilder()
            .append("{\n")
            .append("this.")
            .append(propertyName)
            .append("=")
            .append(parameterName)
            .append(";\n")
            .append("return this;\n")
            .append("}\n");

    PsiCodeBlock methodBody =
        elementFactory.createCodeBlockFromText(methodBodyBuilder.toString(), null);
    method.getBody().replace(methodBody);
    method = (PsiMethod) CodeStyleManager.getInstance(method.getProject()).reformat(method);
    return method;
  }
  private static void addGetterSetterElements(
      CompletionResultSet result, PsiClass parent, Set<MethodSignature> addedSignatures) {
    int count = 0;
    for (PsiField field : parent.getFields()) {
      if (field instanceof PsiEnumConstant) continue;

      List<PsiMethod> prototypes = ContainerUtil.newSmartList();
      Collections.addAll(
          prototypes, GetterSetterPrototypeProvider.generateGetterSetters(field, true));
      Collections.addAll(
          prototypes, GetterSetterPrototypeProvider.generateGetterSetters(field, false));
      for (final PsiMethod prototype : prototypes) {
        if (parent.findMethodBySignature(prototype, false) == null
            && addedSignatures.add(prototype.getSignature(PsiSubstitutor.EMPTY))) {
          Icon icon = prototype.getIcon(Iconable.ICON_FLAG_VISIBILITY);
          result.addElement(
              createGenerateMethodElement(
                  prototype,
                  PsiSubstitutor.EMPTY,
                  icon,
                  "",
                  new InsertHandler<LookupElement>() {
                    @Override
                    public void handleInsert(InsertionContext context, LookupElement item) {
                      removeLookupString(context);

                      insertGenerationInfos(
                          context,
                          Collections.singletonList(new PsiGenerationInfo<PsiMethod>(prototype)));
                    }
                  }));

          if (count++ > 100) return;
        }
      }
    }
  }
  private static LookupElementBuilder createGenerateMethodElement(
      PsiMethod prototype,
      PsiSubstitutor substitutor,
      Icon icon,
      String typeText,
      InsertHandler<LookupElement> insertHandler) {
    String methodName = prototype.getName();

    String visibility = VisibilityUtil.getVisibilityModifier(prototype.getModifierList());
    String modifiers = (visibility == PsiModifier.PACKAGE_LOCAL ? "" : visibility + " ");

    PsiType type = substitutor.substitute(prototype.getReturnType());
    String signature =
        modifiers + (type == null ? "" : type.getPresentableText() + " ") + methodName;

    String parameters =
        PsiFormatUtil.formatMethod(
            prototype, substitutor, PsiFormatUtilBase.SHOW_PARAMETERS, PsiFormatUtilBase.SHOW_NAME);

    String overrideSignature =
        " @Override "
            + signature; // leading space to make it a middle match, under all annotation
                         // suggestions
    LookupElementBuilder element =
        LookupElementBuilder.create(prototype, signature)
            .withLookupString(methodName)
            .withLookupString(signature)
            .withLookupString(overrideSignature)
            .withInsertHandler(insertHandler)
            .appendTailText(parameters, false)
            .appendTailText(" {...}", true)
            .withTypeText(typeText)
            .withIcon(icon);
    element.putUserData(GENERATE_ELEMENT, true);
    return element;
  }