PsiMethod (PsiClass clazz, @NotNull PsiMethod newMethod, Editor editor) { PsiMethod methodHashCode = PsiAdapter.findHashCodeMethod(clazz); PsiMethod methodEquals = PsiAdapter.findEqualsMethod(clazz); // if both methods exist determine the last method in the javafile PsiMethod method; if (methodEquals != null && methodHashCode != null) { if (methodEquals.getTextOffset() > methodHashCode.getTextOffset()) { method = methodEquals; } else { method = methodHashCode; } } else { method = methodHashCode != null ? methodHashCode : methodEquals; } if (method != null) { // insert after the equals/hashCode method newMethod = (PsiMethod) clazz.addAfter(newMethod, method); } else { // no equals/hashCode so insert at caret newMethod = InsertAtCaretStrategy.getInstance().insertNewMethod(clazz, newMethod, editor); } return newMethod; }