void (@NotNull Project project, @NotNull PsiElement element, @NotNull ModPsiUpdater updater) { if (!(element.getParent() instanceof PsiClass aClass)) { return; } @NonNls final StringBuilder methodText = new StringBuilder(); if (PsiUtil.isLanguageLevel5OrHigher(aClass) && JavaCodeStyleSettings.getInstance(aClass.getContainingFile()).INSERT_OVERRIDE_ANNOTATION) { methodText.append("@java.lang.Override "); } methodText.append("protected "); final String name = aClass.getName(); if (name != null) { methodText.append(name); } else if (aClass instanceof PsiAnonymousClass) { final PsiClassType baseClassType = ((PsiAnonymousClass)aClass).getBaseClassType(); methodText.append(baseClassType.getCanonicalText()); } else { methodText.append(CommonClassNames.JAVA_LANG_OBJECT); } final PsiTypeParameterList typeParameterList = aClass.getTypeParameterList(); if (typeParameterList != null) { methodText.append(typeParameterList.getText()); } methodText.append(" clone() {}"); final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); final PsiMethod method = (PsiMethod)aClass.add(factory.createMethodFromText(methodText.toString(), aClass)); final PsiClassType exceptionType = factory.createTypeByFQClassName("java.lang.CloneNotSupportedException", element.getResolveScope()); final PsiMethod superMethod = MethodUtils.getSuper(method); boolean throwException = false; if (superMethod != null) { if (superMethod.hasModifierProperty(PsiModifier.PUBLIC)) { method.getModifierList().setModifierProperty(PsiModifier.PUBLIC, true); } for (PsiClassType thrownType : superMethod.getThrowsList().getReferencedTypes()) { if (thrownType.equals(exceptionType)) { throwException = true; break; } } if (throwException) { final PsiJavaCodeReferenceElement exceptionReference = factory.createReferenceElementByType(exceptionType); method.getThrowsList().add(exceptionReference); } else { final PsiJavaCodeReferenceElement errorReference = factory.createFQClassNameReferenceElement("java.lang.AssertionError", element.getResolveScope()); method.getThrowsList().add(errorReference); } } final String throwableName = throwException ? "java.lang.CloneNotSupportedException" : "java.lang.AssertionError"; final PsiStatement statement = factory.createStatementFromText("throw new " + throwableName + "();", element); final PsiCodeBlock body = method.getBody(); assert body != null; body.add(statement); final PsiExpression superCloneCall = factory.createExpressionFromText("super.clone()", aClass); for (PsiMethodCallExpression cloneCall : collectCallsToClone(aClass)) { cloneCall.replace(superCloneCall); } GenerateMembersUtil.positionCaret(updater, method, true); }