private void addNewClause( Collection<String> elements, Collection<String> additional, Project project, boolean isExtends) throws IncorrectOperationException { if (elements.isEmpty() && additional.isEmpty()) return; StringBuilder classText = new StringBuilder(); classText.append("class A "); classText.append(isExtends ? "extends " : "implements "); for (String str : elements) { classText.append(str); classText.append(", "); } for (String str : additional) { classText.append(str); classText.append(", "); } classText.delete(classText.length() - 2, classText.length()); classText.append(" {}"); final GrTypeDefinition definition = GroovyPsiElementFactory.getInstance(project).createTypeDefinition(classText.toString()); GroovyPsiElement clause = isExtends ? definition.getExtendsClause() : definition.getImplementsClause(); assert clause != null; PsiElement addedClause = myClass.addBefore(clause, myClass.getBody()); JavaCodeStyleManager.getInstance(project).shortenClassReferences(addedClause); }
private void doMoveClass(PsiSubstitutor substitutor, GrMemberInfo info) { GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(myProject); GrTypeDefinition aClass = (GrTypeDefinition) info.getMember(); if (Boolean.FALSE.equals(info.getOverrides())) { final GrReferenceList sourceReferenceList = info.getSourceReferenceList(); LOG.assertTrue(sourceReferenceList != null); GrCodeReferenceElement ref = mySourceClass.equals(sourceReferenceList.getParent()) ? removeFromReferenceList(sourceReferenceList, aClass) : findReferenceToClass(sourceReferenceList, aClass); if (ref != null && !myTargetSuperClass.isInheritor(aClass, false)) { replaceMovedMemberTypeParameters( ref, PsiUtil.typeParametersIterable(mySourceClass), substitutor, elementFactory); GrReferenceList referenceList; if (myTargetSuperClass.isInterface()) { referenceList = myTargetSuperClass.getExtendsClause(); if (referenceList == null) { GrExtendsClause newClause = GroovyPsiElementFactory.getInstance(myProject).createExtendsClause(); PsiElement anchor = myTargetSuperClass.getTypeParameterList() != null ? myTargetSuperClass.getTypeParameterList() : myTargetSuperClass.getNameIdentifierGroovy(); referenceList = (GrReferenceList) myTargetSuperClass.addAfter(newClause, anchor); addSpacesAround(referenceList); } } else { referenceList = myTargetSuperClass.getImplementsClause(); if (referenceList == null) { GrImplementsClause newClause = GroovyPsiElementFactory.getInstance(myProject).createImplementsClause(); PsiElement anchor = myTargetSuperClass.getExtendsClause() != null ? myTargetSuperClass.getExtendsClause() : myTargetSuperClass.getTypeParameterList() != null ? myTargetSuperClass.getTypeParameterList() : myTargetSuperClass.getNameIdentifierGroovy(); referenceList = (GrReferenceList) myTargetSuperClass.addAfter(newClause, anchor); addSpacesAround(referenceList); } } assert referenceList != null; referenceList.add(ref); } } else { replaceMovedMemberTypeParameters( aClass, PsiUtil.typeParametersIterable(mySourceClass), substitutor, elementFactory); // fixReferencesToStatic(aClass, movedMembers); PsiMember movedElement = (PsiMember) myTargetSuperClass.addAfter(aClass, null); // movedElement = (PsiMember)CodeStyleManager.getInstance(myProject).reformat(movedElement); myMembersAfterMove.add(movedElement); deleteMemberWithDocComment(aClass); } }
private static void getImplementListsInner( GrTypeDefinition grType, List<PsiClassType> result, Set<PsiClass> visited) { if (!visited.add(grType)) return; final PsiClassType[] implementsTypes = getReferenceListTypes(grType.getImplementsClause()); List<PsiClassType> fromDelegates = getImplementsFromDelegate(grType, visited); if (fromDelegates != null) { result.addAll(fromDelegates); } result.addAll(Arrays.asList(implementsTypes)); if (!grType.isInterface() && !ContainerUtil.or(implementsTypes, IS_GROOVY_OBJECT) && !ContainerUtil.or(getReferenceListTypes(grType.getExtendsClause()), IS_GROOVY_OBJECT)) { result.add(getGroovyObjectType(grType)); } }
public ChangeExtendsImplementsQuickFix(@NotNull GrTypeDefinition aClass) { myClass = aClass; myExtendsClause = aClass.getExtendsClause(); myImplementsClause = aClass.getImplementsClause(); }
private static void startTemplate( GrTypeParameterList oldTypeParameterList, final Project project, final GrTypeDefinition psiClass, final GrTypeDefinition targetClass, boolean includeClassName) { PsiElementFactory jfactory = JavaPsiFacade.getElementFactory(project); final GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(project); GrCodeReferenceElement ref = elementFactory.createCodeReferenceElementFromClass(psiClass); try { if (psiClass.isInterface()) { GrImplementsClause clause = targetClass.getImplementsClause(); if (clause == null) { clause = (GrImplementsClause) targetClass.addAfter( elementFactory.createImplementsClause(), targetClass.getNameIdentifierGroovy()); } ref = (GrCodeReferenceElement) clause.add(ref); } else { GrExtendsClause clause = targetClass.getExtendsClause(); if (clause == null) { clause = (GrExtendsClause) targetClass.addAfter( elementFactory.createExtendsClause(), targetClass.getNameIdentifierGroovy()); } ref = (GrCodeReferenceElement) clause.add(ref); } if (psiClass.hasTypeParameters() || includeClassName) { final Editor editor = CodeInsightUtil.positionCursor( project, targetClass.getContainingFile(), targetClass.getLBrace()); final TemplateBuilderImpl templateBuilder = editor == null || ApplicationManager.getApplication().isUnitTestMode() ? null : (TemplateBuilderImpl) TemplateBuilderFactory.getInstance().createTemplateBuilder(targetClass); if (includeClassName && templateBuilder != null) { templateBuilder.replaceElement(targetClass.getNameIdentifier(), targetClass.getName()); } if (oldTypeParameterList != null) { for (PsiTypeParameter parameter : oldTypeParameterList.getTypeParameters()) { final PsiElement param = ref.getTypeArgumentList() .add(elementFactory.createTypeElement(jfactory.createType(parameter))); if (templateBuilder != null) { templateBuilder.replaceElement(param, param.getText()); } } } final GrTypeParameterList typeParameterList = targetClass.getTypeParameterList(); assert typeParameterList != null; typeParameterList.replace(oldTypeParameterList); if (templateBuilder != null) { templateBuilder.setEndVariableBefore(ref); final Template template = templateBuilder.buildTemplate(); template.addEndVariable(); final PsiFile containingFile = targetClass.getContainingFile(); PsiDocumentManager.getInstance(project) .doPostponedOperationsAndUnblockDocument(editor.getDocument()); final TextRange textRange = targetClass.getTextRange(); final int startClassOffset = textRange.getStartOffset(); editor.getDocument().deleteString(textRange.getStartOffset(), textRange.getEndOffset()); CreateFromUsageBaseFix.startTemplate( editor, template, project, new TemplateEditingAdapter() { @Override public void templateFinished(Template template, boolean brokenOff) { chooseAndImplement( psiClass, project, PsiTreeUtil.getParentOfType( containingFile.findElementAt(startClassOffset), GrTypeDefinition.class), editor); } }, getTitle(psiClass)); } } } catch (IncorrectOperationException e) { LOG.error(e); } }