@Override public void visitClass(@NotNull PsiClass aClass) { // note: no call to super, to avoid drill-down final String name = aClass.getName(); if (!PsiKeyword.ASSERT.equals(name)) { return; } final PsiTypeParameterList params = aClass.getTypeParameterList(); if (params != null) { params.accept(this); } registerClassError(aClass); }
private static void invoke(final PsiClass aClass, Editor editor) { final PsiTypeParameterList typeParameterList = aClass.getTypeParameterList(); Project project = aClass.getProject(); if (typeParameterList == null) { final String message = RefactoringBundle.getCannotRefactorMessage( RefactoringBundle.message("changeClassSignature.no.type.parameters")); CommonRefactoringUtil.showErrorHint( project, editor, message, REFACTORING_NAME, HelpID.CHANGE_CLASS_SIGNATURE); return; } if (!CommonRefactoringUtil.checkReadOnlyStatus(project, aClass)) return; ChangeClassSignatureDialog dialog = new ChangeClassSignatureDialog(aClass); dialog.show(); }
private static boolean invoke(final PsiClass aClass, Editor editor) { final PsiTypeParameterList typeParameterList = aClass.getTypeParameterList(); Project project = aClass.getProject(); if (typeParameterList == null) { final String message = RefactoringBundle.getCannotRefactorMessage( RefactoringBundle.message("changeClassSignature.no.type.parameters")); CommonRefactoringUtil.showErrorHint( project, editor, message, REFACTORING_NAME, HelpID.CHANGE_CLASS_SIGNATURE); return false; } if (!CommonRefactoringUtil.checkReadOnlyStatus(project, aClass)) return false; ChangeClassSignatureDialog dialog = new ChangeClassSignatureDialog(aClass, true); // if (!ApplicationManager.getApplication().isUnitTestMode()){ return dialog.showAndGet(); // }else { // dialog.showAndGetOk() // } }
public void replace(final ReplacementInfo info, ReplaceOptions options) { PsiElement elementToReplace = info.getMatch(0); PsiElement elementParent = elementToReplace.getParent(); String replacementToMake = info.getReplacement(); Project project = myContext.getProject(); PsiElement el = findRealSubstitutionElement(elementToReplace); boolean listContext = isListContext(el); if (el instanceof PsiAnnotation && !StringUtil.startsWithChar(replacementToMake, '@')) { replacementToMake = "@" + replacementToMake; } PsiElement[] statements = ReplacerUtil.createTreeForReplacement( replacementToMake, el instanceof PsiMember && !isSymbolReplacement(el) ? PatternTreeContext.Class : PatternTreeContext.Block, myContext); if (listContext) { if (statements.length > 1) { elementParent.addRangeBefore( statements[0], statements[statements.length - 1], elementToReplace); } else if (statements.length == 1) { PsiElement replacement = getMatchExpr(statements[0], elementToReplace); handleModifierList(el, replacement); replacement = handleSymbolReplacement(replacement, el); if (replacement instanceof PsiTryStatement) { final List<PsiCatchSection> unmatchedCatchSections = el.getUserData(JavaMatchingVisitor.UNMATCHED_CATCH_SECTION_CONTENT_VAR_KEY); final PsiCatchSection[] catches = ((PsiTryStatement) replacement).getCatchSections(); if (unmatchedCatchSections != null) { for (int i = unmatchedCatchSections.size() - 1; i >= 0; --i) { final PsiParameter parameter = unmatchedCatchSections.get(i).getParameter(); final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory(); final PsiCatchSection catchSection = elementFactory.createCatchSection(parameter.getType(), parameter.getName(), null); catchSection.getCatchBlock().replace(unmatchedCatchSections.get(i).getCatchBlock()); replacement.addAfter(catchSection, catches[catches.length - 1]); replacement.addBefore(createWhiteSpace(replacement), replacement.getLastChild()); } } } try { final PsiElement inserted = elementParent.addBefore(replacement, elementToReplace); if (replacement instanceof PsiComment && (elementParent instanceof PsiIfStatement || elementParent instanceof PsiLoopStatement)) { elementParent.addAfter(createSemicolon(replacement), inserted); } } catch (IncorrectOperationException e) { elementToReplace.replace(replacement); } } } else if (statements.length > 0) { PsiElement replacement = ReplacerUtil.copySpacesAndCommentsBefore( elementToReplace, statements, replacementToMake, elementParent); replacement = getMatchExpr(replacement, elementToReplace); if (replacement instanceof PsiStatement && !(replacement.getLastChild() instanceof PsiJavaToken) && !(replacement.getLastChild() instanceof PsiComment)) { // assert w/o ; final PsiElement prevLastChildInParent = replacement.getLastChild().getPrevSibling(); if (prevLastChildInParent != null) { elementParent.addRangeBefore(replacement.getFirstChild(), prevLastChildInParent, el); } else { elementParent.addBefore(replacement.getFirstChild(), el); } el.getNode().getTreeParent().removeChild(el.getNode()); } else { // preserve comments handleModifierList(el, replacement); if (replacement instanceof PsiClass) { // modifier list final PsiStatement[] searchStatements = getCodeBlock().getStatements(); if (searchStatements.length > 0 && searchStatements[0] instanceof PsiDeclarationStatement && ((PsiDeclarationStatement) searchStatements[0]).getDeclaredElements()[0] instanceof PsiClass) { final PsiClass replaceClazz = (PsiClass) replacement; final PsiClass queryClazz = (PsiClass) ((PsiDeclarationStatement) searchStatements[0]).getDeclaredElements()[0]; final PsiClass clazz = (PsiClass) el; if (replaceClazz.getExtendsList().getTextLength() == 0 && queryClazz.getExtendsList().getTextLength() == 0 && clazz.getExtendsList().getTextLength() != 0) { replaceClazz.addBefore( clazz.getExtendsList().getPrevSibling(), replaceClazz.getExtendsList()); // whitespace replaceClazz .getExtendsList() .addRange( clazz.getExtendsList().getFirstChild(), clazz.getExtendsList().getLastChild()); } if (replaceClazz.getImplementsList().getTextLength() == 0 && queryClazz.getImplementsList().getTextLength() == 0 && clazz.getImplementsList().getTextLength() != 0) { replaceClazz.addBefore( clazz.getImplementsList().getPrevSibling(), replaceClazz.getImplementsList()); // whitespace replaceClazz .getImplementsList() .addRange( clazz.getImplementsList().getFirstChild(), clazz.getImplementsList().getLastChild()); } if (replaceClazz.getTypeParameterList().getTextLength() == 0 && queryClazz.getTypeParameterList().getTextLength() == 0 && clazz.getTypeParameterList().getTextLength() != 0) { // skip < and > replaceClazz.getTypeParameterList().replace(clazz.getTypeParameterList()); } } } replacement = handleSymbolReplacement(replacement, el); el.replace(replacement); } } else { final PsiElement nextSibling = el.getNextSibling(); el.delete(); if (nextSibling instanceof PsiWhiteSpace && nextSibling.isValid()) { nextSibling.delete(); } } if (listContext) { final int matchSize = info.getMatchesCount(); for (int i = 0; i < matchSize; ++i) { PsiElement matchElement = info.getMatch(i); PsiElement element = findRealSubstitutionElement(matchElement); if (element == null) continue; PsiElement firstToDelete = element; PsiElement lastToDelete = element; PsiElement prevSibling = element.getPrevSibling(); PsiElement nextSibling = element.getNextSibling(); if (prevSibling instanceof PsiWhiteSpace) { firstToDelete = prevSibling; prevSibling = prevSibling != null ? prevSibling.getPrevSibling() : null; } else if (prevSibling == null && nextSibling instanceof PsiWhiteSpace) { lastToDelete = nextSibling; } if (nextSibling instanceof XmlText && i + 1 < matchSize) { final PsiElement next = info.getMatch(i + 1); if (next != null && next == nextSibling.getNextSibling()) { lastToDelete = nextSibling; } } if (element instanceof PsiExpression) { final PsiElement parent = element.getParent().getParent(); if ((parent instanceof PsiCall || parent instanceof PsiAnonymousClass) && prevSibling instanceof PsiJavaToken && ((PsiJavaToken) prevSibling).getTokenType() == JavaTokenType.COMMA) { firstToDelete = prevSibling; } } else if (element instanceof PsiParameter && prevSibling instanceof PsiJavaToken && ((PsiJavaToken) prevSibling).getTokenType() == JavaTokenType.COMMA) { firstToDelete = prevSibling; } element.getParent().deleteChildRange(firstToDelete, lastToDelete); } } }
private static boolean processDeclarationsInClassNotCached( @NotNull PsiClass aClass, @NotNull PsiScopeProcessor processor, @NotNull ResolveState state, @Nullable Set<PsiClass> visited, PsiElement last, @NotNull PsiElement place, boolean isRaw, @NotNull LanguageLevel languageLevel) { if (visited == null) visited = new THashSet<PsiClass>(); if (!visited.add(aClass)) return true; processor.handleEvent(PsiScopeProcessor.Event.SET_DECLARATION_HOLDER, aClass); final ElementClassHint classHint = processor.getHint(ElementClassHint.KEY); final NameHint nameHint = processor.getHint(NameHint.KEY); if (classHint == null || classHint.shouldProcess(ElementClassHint.DeclarationKind.FIELD)) { if (nameHint != null) { final PsiField fieldByName = aClass.findFieldByName(nameHint.getName(state), false); if (fieldByName != null && !processor.execute(fieldByName, state)) return false; } else { final PsiField[] fields = aClass.getFields(); for (final PsiField field : fields) { if (!processor.execute(field, state)) return false; } } } PsiElementFactory factory = JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory(); if (classHint == null || classHint.shouldProcess(ElementClassHint.DeclarationKind.METHOD)) { PsiSubstitutor baseSubstitutor = state.get(PsiSubstitutor.KEY); final PsiMethod[] methods = nameHint != null ? aClass.findMethodsByName(nameHint.getName(state), false) : aClass.getMethods(); for (final PsiMethod method : methods) { PsiSubstitutor finalSubstitutor = checkRaw(isRaw, factory, method, baseSubstitutor); ResolveState methodState = finalSubstitutor == baseSubstitutor ? state : state.put(PsiSubstitutor.KEY, finalSubstitutor); if (!processor.execute(method, methodState)) return false; } } if (classHint == null || classHint.shouldProcess(ElementClassHint.DeclarationKind.CLASS)) { if (last != null && last.getParent() == aClass) { // Parameters final PsiTypeParameterList list = aClass.getTypeParameterList(); if (list != null && !list.processDeclarations(processor, ResolveState.initial(), last, place)) return false; } if (!(last instanceof PsiReferenceList) && !(last instanceof PsiModifierList)) { // Inners if (nameHint != null) { final PsiClass inner = aClass.findInnerClassByName(nameHint.getName(state), false); if (inner != null) { if (!processor.execute(inner, state)) return false; } } else { final PsiClass[] inners = aClass.getInnerClasses(); for (final PsiClass inner : inners) { if (!processor.execute(inner, state)) return false; } } } } return last instanceof PsiReferenceList || processSuperTypes( aClass, processor, visited, last, place, state, isRaw, factory, languageLevel); }
private static boolean processCachedMembersByName( @NotNull PsiClass aClass, @NotNull PsiScopeProcessor processor, @NotNull ResolveState state, @Nullable Set<PsiClass> visited, PsiElement last, @NotNull PsiElement place, boolean isRaw, @NotNull PsiSubstitutor substitutor, @NotNull MembersMap value, String name, @NotNull LanguageLevel languageLevel) { final ElementClassHint classHint = processor.getHint(ElementClassHint.KEY); PsiElementFactory factory = JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory(); if (classHint == null || classHint.shouldProcess(ElementClassHint.DeclarationKind.FIELD)) { final PsiField fieldByName = aClass.findFieldByName(name, false); if (fieldByName != null) { processor.handleEvent(PsiScopeProcessor.Event.SET_DECLARATION_HOLDER, aClass); if (!processor.execute(fieldByName, state)) return false; } else { final Map<String, List<Pair<PsiMember, PsiSubstitutor>>> allFieldsMap = value.get(MemberType.FIELD); final List<Pair<PsiMember, PsiSubstitutor>> list = allFieldsMap.get(name); if (list != null) { for (final Pair<PsiMember, PsiSubstitutor> candidate : list) { PsiMember candidateField = candidate.getFirst(); PsiSubstitutor finalSubstitutor = obtainFinalSubstitutor( candidateField.getContainingClass(), candidate.getSecond(), aClass, substitutor, factory, languageLevel); processor.handleEvent( PsiScopeProcessor.Event.SET_DECLARATION_HOLDER, candidateField.getContainingClass()); if (!processor.execute(candidateField, state.put(PsiSubstitutor.KEY, finalSubstitutor))) return false; } } } } if (classHint == null || classHint.shouldProcess(ElementClassHint.DeclarationKind.CLASS)) { if (last != null && last.getParent() == aClass) { if (last instanceof PsiClass) { if (!processor.execute(last, state)) return false; } // Parameters final PsiTypeParameterList list = aClass.getTypeParameterList(); if (list != null && !list.processDeclarations(processor, state, last, place)) return false; } if (!(last instanceof PsiReferenceList)) { final PsiClass classByName = aClass.findInnerClassByName(name, false); if (classByName != null) { processor.handleEvent(PsiScopeProcessor.Event.SET_DECLARATION_HOLDER, aClass); if (!processor.execute(classByName, state)) return false; } else { Map<String, List<Pair<PsiMember, PsiSubstitutor>>> allClassesMap = value.get(MemberType.CLASS); List<Pair<PsiMember, PsiSubstitutor>> list = allClassesMap.get(name); if (list != null) { for (final Pair<PsiMember, PsiSubstitutor> candidate : list) { PsiMember inner = candidate.getFirst(); PsiClass containingClass = inner.getContainingClass(); if (containingClass != null) { PsiSubstitutor finalSubstitutor = obtainFinalSubstitutor( containingClass, candidate.getSecond(), aClass, substitutor, factory, languageLevel); processor.handleEvent( PsiScopeProcessor.Event.SET_DECLARATION_HOLDER, containingClass); if (!processor.execute(inner, state.put(PsiSubstitutor.KEY, finalSubstitutor))) return false; } } } } } } if (classHint == null || classHint.shouldProcess(ElementClassHint.DeclarationKind.METHOD)) { if (processor instanceof MethodResolverProcessor) { final MethodResolverProcessor methodResolverProcessor = (MethodResolverProcessor) processor; if (methodResolverProcessor.isConstructor()) { final PsiMethod[] constructors = aClass.getConstructors(); methodResolverProcessor.handleEvent( PsiScopeProcessor.Event.SET_DECLARATION_HOLDER, aClass); for (PsiMethod constructor : constructors) { if (!methodResolverProcessor.execute(constructor, state)) return false; } return true; } } Map<String, List<Pair<PsiMember, PsiSubstitutor>>> allMethodsMap = value.get(MemberType.METHOD); List<Pair<PsiMember, PsiSubstitutor>> list = allMethodsMap.get(name); if (list != null) { for (final Pair<PsiMember, PsiSubstitutor> candidate : list) { ProgressIndicatorProvider.checkCanceled(); PsiMethod candidateMethod = (PsiMethod) candidate.getFirst(); if (processor instanceof MethodResolverProcessor) { if (candidateMethod.isConstructor() != ((MethodResolverProcessor) processor).isConstructor()) continue; } final PsiClass containingClass = candidateMethod.getContainingClass(); if (visited != null && visited.contains(candidateMethod.getContainingClass())) { continue; } PsiSubstitutor finalSubstitutor = obtainFinalSubstitutor( containingClass, candidate.getSecond(), aClass, substitutor, factory, languageLevel); finalSubstitutor = checkRaw(isRaw, factory, candidateMethod, finalSubstitutor); processor.handleEvent(PsiScopeProcessor.Event.SET_DECLARATION_HOLDER, containingClass); if (!processor.execute(candidateMethod, state.put(PsiSubstitutor.KEY, finalSubstitutor))) return false; } if (visited != null) { for (Pair<PsiMember, PsiSubstitutor> aList : list) { visited.add(aList.getFirst().getContainingClass()); } } } } return true; }