PsiMethod (@NotNull PsiClass targetClass, @Nullable PsiClass parentClass, @Nullable PsiMember enclosingContext, @NotNull PsiMethod method) { if (targetClass.equals(parentClass)) { method = (PsiMethod)targetClass.addAfter(method, enclosingContext); } else { PsiElement anchor = enclosingContext; while (anchor != null && anchor.getParent() != null && !anchor.getParent().equals(targetClass)) { anchor = anchor.getParent(); } if (anchor != null && anchor.getParent() == null) anchor = null; if (anchor != null) { method = (PsiMethod)targetClass.addAfter(method, anchor); } else { method = (PsiMethod)targetClass.add(method); } } return method; }