Search in sources :

Example 36 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class MakeMethodOrClassStaticProcessor method findExternalReferences.

protected void findExternalReferences(final PsiMethod method, final ArrayList<UsageInfo> result) {
    for (PsiReference ref : ReferencesSearch.search(method)) {
        PsiElement element = ref.getElement();
        PsiElement qualifier = null;
        if (element instanceof PsiReferenceExpression) {
            qualifier = ((PsiReferenceExpression) element).getQualifierExpression();
            if (qualifier instanceof PsiThisExpression)
                qualifier = null;
        }
        if (!PsiTreeUtil.isAncestor(myMember, element, true) || qualifier != null) {
            result.add(new UsageInfo(element));
        }
        processExternalReference(element, method, result);
    }
}
Also used : UsageInfo(com.intellij.usageView.UsageInfo)

Example 37 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class MakeMethodStaticProcessor method findAdditionalMembers.

@Override
protected boolean findAdditionalMembers(final Set<UsageInfo> toMakeStatic) {
    if (!toMakeStatic.isEmpty()) {
        myAdditionalMethods = new ArrayList<>();
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            for (UsageInfo usageInfo : toMakeStatic) {
                myAdditionalMethods.add((PsiMethod) usageInfo.getElement());
            }
        } else {
            final JavaCallerChooser chooser = new MakeStaticJavaCallerChooser(myMember, myProject, methods -> myAdditionalMethods.addAll(methods)) {

                @Override
                protected ArrayList<UsageInfo> getTopLevelItems() {
                    return new ArrayList<>(toMakeStatic);
                }
            };
            TreeUtil.expand(chooser.getTree(), 2);
            if (!chooser.showAndGet()) {
                return false;
            }
        }
    }
    return true;
}
Also used : JavaCallerChooser(com.intellij.refactoring.changeSignature.inCallers.JavaCallerChooser) ArrayList(java.util.ArrayList) UsageInfo(com.intellij.usageView.UsageInfo)

Example 38 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class SearchInLibsTest method testFindInPathInLibraryDirActuallySearchesInTheirSourcesToo.

public void testFindInPathInLibraryDirActuallySearchesInTheirSourcesToo() throws Exception {
    FindModel model = new FindModel();
    final PsiClass aClass = myJavaFacade.findClass("LibraryClass1");
    assertNotNull(aClass);
    model.setDirectoryName(aClass.getContainingFile().getContainingDirectory().getVirtualFile().getPath());
    model.setCaseSensitive(true);
    model.setCustomScope(false);
    model.setStringToFind("LibraryClass1");
    model.setProjectScope(false);
    List<UsageInfo> usages = Collections.synchronizedList(new ArrayList<>());
    CommonProcessors.CollectProcessor<UsageInfo> consumer = new CommonProcessors.CollectProcessor<>(usages);
    FindUsagesProcessPresentation presentation = FindInProjectUtil.setupProcessPresentation(getProject(), false, FindInProjectUtil.setupViewPresentation(false, model));
    FindInProjectUtil.findUsages(model, getProject(), consumer, presentation);
    assertSize(2, usages);
}
Also used : FindModel(com.intellij.find.FindModel) FindUsagesProcessPresentation(com.intellij.usages.FindUsagesProcessPresentation) PsiClass(com.intellij.psi.PsiClass) CommonProcessors(com.intellij.util.CommonProcessors) UsageInfo(com.intellij.usageView.UsageInfo)

Example 39 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class ExtractLightMethodObjectHandler method extractLightMethodObject.

@Nullable
public static ExtractedData extractLightMethodObject(final Project project, @Nullable PsiElement originalContext, @NotNull final PsiCodeFragment fragment, final String methodName) throws PrepareFailedException {
    final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
    PsiElement[] elements = completeToStatementArray(fragment, elementFactory);
    if (elements == null) {
        elements = CodeInsightUtil.findStatementsInRange(fragment, 0, fragment.getTextLength());
    }
    if (elements.length == 0) {
        return null;
    }
    if (originalContext == null) {
        return null;
    }
    PsiFile file = originalContext.getContainingFile();
    final PsiFile copy = PsiFileFactory.getInstance(project).createFileFromText(file.getName(), file.getFileType(), file.getText(), file.getModificationStamp(), false);
    if (originalContext instanceof PsiKeyword && PsiModifier.PRIVATE.equals(originalContext.getText())) {
        final PsiNameIdentifierOwner identifierOwner = PsiTreeUtil.getParentOfType(originalContext, PsiNameIdentifierOwner.class);
        if (identifierOwner != null) {
            final PsiElement identifier = identifierOwner.getNameIdentifier();
            if (identifier != null) {
                originalContext = identifier;
            }
        }
    }
    final TextRange range = originalContext.getTextRange();
    PsiElement originalAnchor = CodeInsightUtil.findElementInRange(copy, range.getStartOffset(), range.getEndOffset(), originalContext.getClass());
    if (originalAnchor == null) {
        final PsiElement elementAt = copy.findElementAt(range.getStartOffset());
        if (elementAt != null && elementAt.getClass() == originalContext.getClass()) {
            originalAnchor = PsiTreeUtil.skipSiblingsForward(elementAt, PsiWhiteSpace.class);
        }
    }
    final PsiClass containingClass = PsiTreeUtil.getParentOfType(originalAnchor, PsiClass.class, false);
    if (containingClass == null) {
        return null;
    }
    PsiElement anchor = RefactoringUtil.getParentStatement(originalAnchor, false);
    if (anchor == null) {
        if (PsiTreeUtil.getParentOfType(originalAnchor, PsiCodeBlock.class) != null) {
            anchor = originalAnchor;
        }
    }
    final PsiElement container;
    if (anchor == null) {
        container = ((PsiClassInitializer) containingClass.add(elementFactory.createClassInitializer())).getBody();
        anchor = container.getLastChild();
    } else {
        container = anchor.getParent();
    }
    final PsiElement firstElementCopy = container.addRangeBefore(elements[0], elements[elements.length - 1], anchor);
    final PsiElement[] elementsCopy = CodeInsightUtil.findStatementsInRange(copy, firstElementCopy.getTextRange().getStartOffset(), anchor.getTextRange().getStartOffset());
    if (elementsCopy.length == 0) {
        return null;
    }
    if (elementsCopy[elementsCopy.length - 1] instanceof PsiExpressionStatement) {
        final PsiExpression expr = ((PsiExpressionStatement) elementsCopy[elementsCopy.length - 1]).getExpression();
        if (!(expr instanceof PsiAssignmentExpression)) {
            PsiType expressionType = GenericsUtil.getVariableTypeByExpressionType(expr.getType());
            if (expressionType instanceof PsiDisjunctionType) {
                expressionType = ((PsiDisjunctionType) expressionType).getLeastUpperBound();
            }
            if (isValidVariableType(expressionType)) {
                final String uniqueResultName = JavaCodeStyleManager.getInstance(project).suggestUniqueVariableName("result", elementsCopy[0], true);
                final String statementText = expressionType.getCanonicalText() + " " + uniqueResultName + " = " + expr.getText() + ";";
                elementsCopy[elementsCopy.length - 1] = elementsCopy[elementsCopy.length - 1].replace(elementFactory.createStatementFromText(statementText, elementsCopy[elementsCopy.length - 1]));
            }
        }
    }
    LOG.assertTrue(elementsCopy[0].getParent() == container, "element: " + elementsCopy[0].getText() + "; container: " + container.getText());
    final int startOffsetInContainer = elementsCopy[0].getStartOffsetInParent();
    final ControlFlow controlFlow;
    try {
        controlFlow = ControlFlowFactory.getInstance(project).getControlFlow(container, LocalsOrMyInstanceFieldsControlFlowPolicy.getInstance(), false, false);
    } catch (AnalysisCanceledException e) {
        return null;
    }
    List<PsiVariable> variables = ControlFlowUtil.getUsedVariables(controlFlow, controlFlow.getStartOffset(elementsCopy[0]), controlFlow.getEndOffset(elementsCopy[elementsCopy.length - 1]));
    variables = ContainerUtil.filter(variables, variable -> {
        PsiElement variableScope = PsiUtil.getVariableCodeBlock(variable, null);
        return variableScope != null && PsiTreeUtil.isAncestor(variableScope, elementsCopy[elementsCopy.length - 1], true);
    });
    final String outputVariables = StringUtil.join(variables, variable -> "\"variable: \" + " + variable.getName(), " +");
    PsiStatement outStatement = elementFactory.createStatementFromText("System.out.println(" + outputVariables + ");", anchor);
    outStatement = (PsiStatement) container.addAfter(outStatement, elementsCopy[elementsCopy.length - 1]);
    copy.accept(new JavaRecursiveElementWalkingVisitor() {

        private void makePublic(PsiMember method) {
            if (method.hasModifierProperty(PsiModifier.PRIVATE)) {
                VisibilityUtil.setVisibility(method.getModifierList(), PsiModifier.PUBLIC);
            }
        }

        @Override
        public void visitMethod(PsiMethod method) {
            super.visitMethod(method);
            makePublic(method);
        }

        @Override
        public void visitField(PsiField field) {
            super.visitField(field);
            makePublic(field);
        }
    });
    final ExtractMethodObjectProcessor extractMethodObjectProcessor = new ExtractMethodObjectProcessor(project, null, elementsCopy, "") {

        @Override
        protected AbstractExtractDialog createExtractMethodObjectDialog(MyExtractMethodProcessor processor) {
            return new LightExtractMethodObjectDialog(this, methodName);
        }

        @Override
        protected boolean isFoldingApplicable() {
            return false;
        }
    };
    extractMethodObjectProcessor.getExtractProcessor().setShowErrorDialogs(false);
    final ExtractMethodObjectProcessor.MyExtractMethodProcessor extractProcessor = extractMethodObjectProcessor.getExtractProcessor();
    if (extractProcessor.prepare()) {
        if (extractProcessor.showDialog()) {
            try {
                extractProcessor.doExtract();
                final UsageInfo[] usages = extractMethodObjectProcessor.findUsages();
                extractMethodObjectProcessor.performRefactoring(usages);
                extractMethodObjectProcessor.runChangeSignature();
            } catch (IncorrectOperationException e) {
                LOG.error(e);
            }
            if (extractMethodObjectProcessor.isCreateInnerClass()) {
                extractMethodObjectProcessor.changeInstanceAccess(project);
            }
            final PsiElement method = extractMethodObjectProcessor.getMethod();
            LOG.assertTrue(method != null);
            method.delete();
        }
    } else {
        return null;
    }
    final int startOffset = startOffsetInContainer + container.getTextRange().getStartOffset();
    final String generatedCall = copy.getText().substring(startOffset, outStatement.getTextOffset());
    return new ExtractedData(generatedCall, (PsiClass) CodeStyleManager.getInstance(project).reformat(extractMethodObjectProcessor.getInnerClass()), originalAnchor);
}
Also used : IncorrectOperationException(com.intellij.util.IncorrectOperationException) StringUtil(com.intellij.openapi.util.text.StringUtil) VisibilityUtil(com.intellij.util.VisibilityUtil) UsageInfo(com.intellij.usageView.UsageInfo) TextRange(com.intellij.openapi.util.TextRange) InputVariables(com.intellij.refactoring.extractMethod.InputVariables) ContainerUtil(com.intellij.util.containers.ContainerUtil) CodeInsightUtil(com.intellij.codeInsight.CodeInsightUtil) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) VariableData(com.intellij.refactoring.util.VariableData) Nullable(org.jetbrains.annotations.Nullable) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) List(java.util.List) RefactoringUtil(com.intellij.refactoring.util.RefactoringUtil) PrepareFailedException(com.intellij.refactoring.extractMethod.PrepareFailedException) Project(com.intellij.openapi.project.Project) PsiUtil(com.intellij.psi.util.PsiUtil) AbstractExtractDialog(com.intellij.refactoring.extractMethod.AbstractExtractDialog) com.intellij.psi(com.intellij.psi) Logger(com.intellij.openapi.diagnostic.Logger) NotNull(org.jetbrains.annotations.NotNull) com.intellij.psi.controlFlow(com.intellij.psi.controlFlow) UsageInfo(com.intellij.usageView.UsageInfo) TextRange(com.intellij.openapi.util.TextRange) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Nullable(org.jetbrains.annotations.Nullable)

Example 40 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class InheritanceToDelegationProcessor method performRefactoring.

protected void performRefactoring(@NotNull UsageInfo[] usages) {
    try {
        for (UsageInfo aUsage : usages) {
            InheritanceToDelegationUsageInfo usage = (InheritanceToDelegationUsageInfo) aUsage;
            if (usage instanceof UnqualifiedNonDelegatedMemberUsageInfo) {
                delegateUsageFromClass(usage.getElement(), ((NonDelegatedMemberUsageInfo) usage).nonDelegatedMember, usage.getDelegateFieldAccessible());
            } else {
                upcastToDelegation(usage.getElement(), usage.getDelegateFieldAccessible());
            }
        }
        myAbstractDelegatedMethods = new HashSet<>();
        addInnerClass();
        addField(usages);
        delegateMethods();
        addImplementingInterfaces();
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : IncorrectOperationException(com.intellij.util.IncorrectOperationException) UsageInfo(com.intellij.usageView.UsageInfo)

Aggregations

UsageInfo (com.intellij.usageView.UsageInfo)283 PsiElement (com.intellij.psi.PsiElement)70 NotNull (org.jetbrains.annotations.NotNull)57 ArrayList (java.util.ArrayList)41 MoveRenameUsageInfo (com.intellij.refactoring.util.MoveRenameUsageInfo)38 IncorrectOperationException (com.intellij.util.IncorrectOperationException)34 PsiFile (com.intellij.psi.PsiFile)33 MultiMap (com.intellij.util.containers.MultiMap)30 VirtualFile (com.intellij.openapi.vfs.VirtualFile)29 Nullable (org.jetbrains.annotations.Nullable)20 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)19 DefaultConstructorImplicitUsageInfo (com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo)17 NoConstructorClassUsageInfo (com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo)17 Project (com.intellij.openapi.project.Project)16 HashSet (com.intellij.util.containers.HashSet)15 TextRange (com.intellij.openapi.util.TextRange)12 PsiReference (com.intellij.psi.PsiReference)12 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)11 PsiClass (com.intellij.psi.PsiClass)11 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)11