Search in sources :

Example 6 with RefactoringEventData

use of com.intellij.refactoring.listeners.RefactoringEventData in project intellij-community by JetBrains.

the class InlineParameterHandler method inlineElement.

public void inlineElement(final Project project, final Editor editor, final PsiElement psiElement) {
    final PsiParameter psiParameter = (PsiParameter) psiElement;
    final PsiParameterList parameterList = (PsiParameterList) psiParameter.getParent();
    if (!(parameterList.getParent() instanceof PsiMethod)) {
        return;
    }
    final int index = parameterList.getParameterIndex(psiParameter);
    final PsiMethod method = (PsiMethod) parameterList.getParent();
    String errorMessage = getCannotInlineMessage(psiParameter, method);
    if (errorMessage != null) {
        CommonRefactoringUtil.showErrorHint(project, editor, errorMessage, RefactoringBundle.message("inline.parameter.refactoring"), null);
        return;
    }
    final Ref<PsiExpression> refInitializer = new Ref<>();
    final Ref<PsiExpression> refConstantInitializer = new Ref<>();
    final Ref<PsiCallExpression> refMethodCall = new Ref<>();
    final List<PsiReference> occurrences = Collections.synchronizedList(new ArrayList<PsiReference>());
    final Collection<PsiFile> containingFiles = Collections.synchronizedSet(new HashSet<PsiFile>());
    containingFiles.add(psiParameter.getContainingFile());
    boolean[] result = new boolean[1];
    if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> {
        result[0] = ReferencesSearch.search(method).forEach(psiReference -> {
            PsiElement element = psiReference.getElement();
            final PsiElement parent = element.getParent();
            if (parent instanceof PsiCallExpression) {
                final PsiCallExpression methodCall = (PsiCallExpression) parent;
                occurrences.add(psiReference);
                containingFiles.add(element.getContainingFile());
                final PsiExpression[] expressions = methodCall.getArgumentList().getExpressions();
                if (expressions.length <= index)
                    return false;
                PsiExpression argument = expressions[index];
                if (!refInitializer.isNull()) {
                    return argument != null && PsiEquivalenceUtil.areElementsEquivalent(refInitializer.get(), argument) && PsiEquivalenceUtil.areElementsEquivalent(refMethodCall.get(), methodCall);
                }
                if (InlineToAnonymousConstructorProcessor.isConstant(argument) || getReferencedFinalField(argument) != null) {
                    if (refConstantInitializer.isNull()) {
                        refConstantInitializer.set(argument);
                    } else if (!isSameConstant(argument, refConstantInitializer.get())) {
                        return false;
                    }
                } else if (!isRecursiveReferencedParameter(argument, psiParameter)) {
                    if (!refConstantInitializer.isNull())
                        return false;
                    refInitializer.set(argument);
                    refMethodCall.set(methodCall);
                }
            }
            return true;
        });
    }, "Searching for Method Usages", true, project)) {
        return;
    }
    final PsiReference reference = TargetElementUtil.findReference(editor);
    final PsiReferenceExpression refExpr = reference instanceof PsiReferenceExpression ? ((PsiReferenceExpression) reference) : null;
    final PsiCodeBlock codeBlock = PsiTreeUtil.getParentOfType(refExpr, PsiCodeBlock.class);
    if (codeBlock != null) {
        final PsiElement[] defs = DefUseUtil.getDefs(codeBlock, psiParameter, refExpr);
        if (defs.length == 1) {
            final PsiElement def = defs[0];
            if (def instanceof PsiReferenceExpression && PsiUtil.isOnAssignmentLeftHand((PsiExpression) def)) {
                final PsiExpression rExpr = ((PsiAssignmentExpression) def.getParent()).getRExpression();
                if (rExpr != null) {
                    PsiExpression toInline = InlineLocalHandler.getDefToInline(psiParameter, refExpr, codeBlock);
                    if (toInline != null) {
                        final PsiElement[] refs = DefUseUtil.getRefs(codeBlock, psiParameter, toInline);
                        if (InlineLocalHandler.checkRefsInAugmentedAssignmentOrUnaryModified(refs, def) == null) {
                            new WriteCommandAction(project) {

                                @Override
                                protected void run(@NotNull Result result) throws Throwable {
                                    for (final PsiElement ref : refs) {
                                        InlineUtil.inlineVariable(psiParameter, rExpr, (PsiJavaCodeReferenceElement) ref);
                                    }
                                    def.getParent().delete();
                                }
                            }.execute();
                            return;
                        }
                    }
                }
            }
        }
    }
    if (occurrences.isEmpty()) {
        CommonRefactoringUtil.showErrorHint(project, editor, "Method has no usages", RefactoringBundle.message("inline.parameter.refactoring"), null);
        return;
    }
    if (!result[0]) {
        CommonRefactoringUtil.showErrorHint(project, editor, "Cannot find constant initializer for parameter", RefactoringBundle.message("inline.parameter.refactoring"), null);
        return;
    }
    if (!refInitializer.isNull()) {
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            final InlineParameterExpressionProcessor processor = new InlineParameterExpressionProcessor(refMethodCall.get(), method, psiParameter, refInitializer.get(), method.getProject().getUserData(InlineParameterExpressionProcessor.CREATE_LOCAL_FOR_TESTS));
            processor.run();
        } else {
            final boolean createLocal = ReferencesSearch.search(psiParameter).findAll().size() > 1;
            InlineParameterDialog dlg = new InlineParameterDialog(refMethodCall.get(), method, psiParameter, refInitializer.get(), createLocal);
            dlg.show();
        }
        return;
    }
    if (refConstantInitializer.isNull()) {
        CommonRefactoringUtil.showErrorHint(project, editor, "Cannot find constant initializer for parameter", RefactoringBundle.message("inline.parameter.refactoring"), null);
        return;
    }
    final Ref<Boolean> isNotConstantAccessible = new Ref<>();
    final PsiExpression constantExpression = refConstantInitializer.get();
    constantExpression.accept(new JavaRecursiveElementVisitor() {

        @Override
        public void visitReferenceExpression(PsiReferenceExpression expression) {
            super.visitReferenceExpression(expression);
            final PsiElement resolved = expression.resolve();
            if (resolved instanceof PsiMember && !PsiUtil.isAccessible((PsiMember) resolved, method, null)) {
                isNotConstantAccessible.set(Boolean.TRUE);
            }
        }
    });
    if (!isNotConstantAccessible.isNull() && isNotConstantAccessible.get()) {
        CommonRefactoringUtil.showErrorHint(project, editor, "Constant initializer is not accessible in method body", RefactoringBundle.message("inline.parameter.refactoring"), null);
        return;
    }
    for (PsiReference psiReference : ReferencesSearch.search(psiParameter)) {
        final PsiElement element = psiReference.getElement();
        if (element instanceof PsiExpression && PsiUtil.isAccessedForWriting((PsiExpression) element)) {
            CommonRefactoringUtil.showErrorHint(project, editor, "Inline parameter which has write usages is not supported", RefactoringBundle.message("inline.parameter.refactoring"), null);
            return;
        }
    }
    if (!ApplicationManager.getApplication().isUnitTestMode()) {
        String occurencesString = RefactoringBundle.message("occurrences.string", occurrences.size());
        String question = RefactoringBundle.message("inline.parameter.confirmation", psiParameter.getName(), constantExpression.getText()) + " " + occurencesString;
        RefactoringMessageDialog dialog = new RefactoringMessageDialog(REFACTORING_NAME, question, HelpID.INLINE_VARIABLE, "OptionPane.questionIcon", true, project);
        if (!dialog.showAndGet()) {
            return;
        }
    }
    final RefactoringEventData data = new RefactoringEventData();
    data.addElement(psiElement.copy());
    CommandProcessor.getInstance().executeCommand(project, () -> {
        project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(REFACTORING_ID, data);
        SameParameterValueInspection.InlineParameterValueFix.inlineSameParameterValue(method, psiParameter, constantExpression);
        project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(REFACTORING_ID, null);
    }, REFACTORING_NAME, null);
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Result(com.intellij.openapi.application.Result) RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) RefactoringMessageDialog(com.intellij.refactoring.util.RefactoringMessageDialog) Ref(com.intellij.openapi.util.Ref)

Example 7 with RefactoringEventData

use of com.intellij.refactoring.listeners.RefactoringEventData in project intellij-community by JetBrains.

the class InlineSuperClassRefactoringProcessor method getAfterData.

@Nullable
@Override
protected RefactoringEventData getAfterData(@NotNull UsageInfo[] usages) {
    final RefactoringEventData data = new RefactoringEventData();
    data.addElements(myTargetClasses);
    return data;
}
Also used : RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with RefactoringEventData

use of com.intellij.refactoring.listeners.RefactoringEventData in project intellij-community by JetBrains.

the class InlineMethodProcessor method getBeforeData.

@Nullable
@Override
protected RefactoringEventData getBeforeData() {
    final RefactoringEventData data = new RefactoringEventData();
    data.addElement(myMethod);
    return data;
}
Also used : RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with RefactoringEventData

use of com.intellij.refactoring.listeners.RefactoringEventData in project intellij-community by JetBrains.

the class ExtractSuperClassUtil method createAfterData.

public static RefactoringEventData createAfterData(final PsiClass subClass) {
    RefactoringEventData data = new RefactoringEventData();
    data.addElement(subClass);
    return data;
}
Also used : RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData)

Example 10 with RefactoringEventData

use of com.intellij.refactoring.listeners.RefactoringEventData in project android by JetBrains.

the class UnusedResourcesProcessor method getBeforeData.

@Nullable
@Override
protected RefactoringEventData getBeforeData() {
    final RefactoringEventData beforeData = new RefactoringEventData();
    beforeData.addElements(myElements);
    return beforeData;
}
Also used : RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

RefactoringEventData (com.intellij.refactoring.listeners.RefactoringEventData)51 Nullable (org.jetbrains.annotations.Nullable)36 NotNull (org.jetbrains.annotations.NotNull)6 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)5 Project (com.intellij.openapi.project.Project)5 UsageInfo (com.intellij.usageView.UsageInfo)5 ArrayList (java.util.ArrayList)5 Result (com.intellij.openapi.application.Result)4 Logger (com.intellij.openapi.diagnostic.Logger)4 com.intellij.psi (com.intellij.psi)4 RefactoringEventListener (com.intellij.refactoring.listeners.RefactoringEventListener)4 MultiMap (com.intellij.util.containers.MultiMap)4 HighlightManager (com.intellij.codeInsight.highlighting.HighlightManager)3 ApplicationManager (com.intellij.openapi.application.ApplicationManager)3 CommandProcessor (com.intellij.openapi.command.CommandProcessor)3 EditorColors (com.intellij.openapi.editor.colors.EditorColors)3 EditorColorsManager (com.intellij.openapi.editor.colors.EditorColorsManager)3 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)3 WindowManager (com.intellij.openapi.wm.WindowManager)3 ReferencesSearch (com.intellij.psi.search.searches.ReferencesSearch)3