Search in sources :

Example 6 with PsiDocTagValue

use of com.intellij.psi.javadoc.PsiDocTagValue in project intellij-community by JetBrains.

the class MoveInstanceMethodProcessor method findUsages.

@NotNull
protected UsageInfo[] findUsages() {
    final PsiManager manager = myMethod.getManager();
    final GlobalSearchScope searchScope = GlobalSearchScope.allScope(manager.getProject());
    final List<UsageInfo> usages = new ArrayList<>();
    for (PsiReference ref : ReferencesSearch.search(myMethod, searchScope, false)) {
        final PsiElement element = ref.getElement();
        if (element instanceof PsiReferenceExpression) {
            boolean isInternal = PsiTreeUtil.isAncestor(myMethod, element, true);
            usages.add(new MethodCallUsageInfo((PsiReferenceExpression) element, isInternal));
        } else if (element instanceof PsiDocTagValue) {
            usages.add(new JavadocUsageInfo((PsiDocTagValue) element));
        } else {
            throw new UnknownReferenceTypeException(element.getLanguage());
        }
    }
    if (myTargetClass.isInterface() && !PsiUtil.isLanguageLevel8OrHigher(myTargetClass)) {
        addInheritorUsages(myTargetClass, searchScope, usages);
    }
    final PsiCodeBlock body = myMethod.getBody();
    if (body != null) {
        body.accept(new JavaRecursiveElementWalkingVisitor() {

            @Override
            public void visitNewExpression(PsiNewExpression expression) {
                if (MoveInstanceMembersUtil.getClassReferencedByThis(expression) != null) {
                    usages.add(new InternalUsageInfo(expression));
                }
                super.visitNewExpression(expression);
            }

            @Override
            public void visitReferenceExpression(PsiReferenceExpression expression) {
                if (MoveInstanceMembersUtil.getClassReferencedByThis(expression) != null) {
                    usages.add(new InternalUsageInfo(expression));
                } else if (!expression.isQualified()) {
                    final PsiElement resolved = expression.resolve();
                    if (myTargetVariable.equals(resolved)) {
                        usages.add(new InternalUsageInfo(expression));
                    }
                }
                super.visitReferenceExpression(expression);
            }
        });
    }
    return usages.toArray(new UsageInfo[usages.size()]);
}
Also used : PsiDocTagValue(com.intellij.psi.javadoc.PsiDocTagValue) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) UsageInfo(com.intellij.usageView.UsageInfo) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with PsiDocTagValue

use of com.intellij.psi.javadoc.PsiDocTagValue in project intellij-community by JetBrains.

the class ExtractClassProcessor method findUsagesForField.

private void findUsagesForField(PsiField field, List<FixableUsageInfo> usages) {
    final PsiManager psiManager = field.getManager();
    final Project project = psiManager.getProject();
    final GlobalSearchScope scope = GlobalSearchScope.allScope(project);
    final String qualifiedName = getQualifiedName();
    @NonNls String getter = null;
    if (myGenerateAccessors) {
        getter = GenerateMembersUtil.suggestGetterName(field);
    } else {
        final PsiMethod fieldGetter = PropertyUtil.findPropertyGetter(sourceClass, field.getName(), false, false);
        if (fieldGetter != null && isInMovedElement(fieldGetter)) {
            getter = fieldGetter.getName();
        }
    }
    @NonNls String setter = null;
    if (myGenerateAccessors) {
        setter = GenerateMembersUtil.suggestSetterName(field);
    } else {
        final PsiMethod fieldSetter = PropertyUtil.findPropertySetter(sourceClass, field.getName(), false, false);
        if (fieldSetter != null && isInMovedElement(fieldSetter)) {
            setter = fieldSetter.getName();
        }
    }
    final boolean isStatic = field.hasModifierProperty(PsiModifier.STATIC);
    for (PsiReference reference : ReferencesSearch.search(field, scope)) {
        final PsiElement element = reference.getElement();
        if (isInMovedElement(element)) {
            continue;
        }
        if (element instanceof PsiReferenceExpression) {
            final PsiReferenceExpression exp = (PsiReferenceExpression) element;
            if (PsiUtil.isIncrementDecrementOperation(exp.getParent())) {
                usages.add(isStatic ? new ReplaceStaticVariableIncrementDecrement(exp, qualifiedName) : new ReplaceInstanceVariableIncrementDecrement(exp, delegateFieldName, setter, getter, field.getName()));
            } else if (RefactoringUtil.isAssignmentLHS(exp)) {
                usages.add(isStatic ? new ReplaceStaticVariableAssignment(exp, qualifiedName) : new ReplaceInstanceVariableAssignment(PsiTreeUtil.getParentOfType(exp, PsiAssignmentExpression.class), delegateFieldName, setter, getter, field.getName()));
            } else {
                usages.add(isStatic ? new ReplaceStaticVariableAccess(exp, qualifiedName, enumConstants.contains(field)) : new ReplaceInstanceVariableAccess(exp, delegateFieldName, getter, field.getName()));
            }
            if (!isStatic) {
                delegationRequired = true;
            }
        } else if (element instanceof PsiDocTagValue) {
            usages.add(new BindJavadocReference(element, qualifiedName, field.getName()));
        }
    }
}
Also used : NonNls(org.jetbrains.annotations.NonNls) PsiDocTagValue(com.intellij.psi.javadoc.PsiDocTagValue) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope)

Example 8 with PsiDocTagValue

use of com.intellij.psi.javadoc.PsiDocTagValue in project intellij-community by JetBrains.

the class JavaChangeSignatureUsageSearcher method findSimpleUsagesWithoutParameters.

private PsiMethod[] findSimpleUsagesWithoutParameters(final PsiMethod method, final ArrayList<UsageInfo> result, boolean isToModifyArgs, boolean isToThrowExceptions, boolean isOriginal) {
    GlobalSearchScope projectScope = GlobalSearchScope.projectScope(method.getProject());
    PsiMethod[] overridingMethods = OverridingMethodsSearch.search(method).toArray(PsiMethod.EMPTY_ARRAY);
    for (PsiMethod overridingMethod : overridingMethods) {
        result.add(new OverriderUsageInfo(overridingMethod, method, isOriginal, isToModifyArgs, isToThrowExceptions));
    }
    boolean needToChangeCalls = !myChangeInfo.isGenerateDelegate() && (myChangeInfo.isNameChanged() || myChangeInfo.isParameterSetOrOrderChanged() || myChangeInfo.isExceptionSetOrOrderChanged() || myChangeInfo.isVisibilityChanged());
    if (needToChangeCalls) {
        int parameterCount = method.getParameterList().getParametersCount();
        PsiReference[] refs = MethodReferencesSearch.search(method, projectScope, true).toArray(PsiReference.EMPTY_ARRAY);
        for (PsiReference ref : refs) {
            PsiElement element = ref.getElement();
            boolean isToCatchExceptions = isToThrowExceptions && needToCatchExceptions(RefactoringUtil.getEnclosingMethod(element));
            if (!isToCatchExceptions) {
                if (RefactoringUtil.isMethodUsage(element)) {
                    PsiExpressionList list = RefactoringUtil.getArgumentListByMethodReference(element);
                    if (list == null || !method.isVarArgs() && list.getExpressions().length != parameterCount)
                        continue;
                }
            }
            if (RefactoringUtil.isMethodUsage(element)) {
                result.add(new MethodCallUsageInfo(element, isToModifyArgs, isToCatchExceptions));
            } else if (element instanceof PsiDocTagValue) {
                result.add(new UsageInfo(element));
            } else if (element instanceof PsiMethod && ((PsiMethod) element).isConstructor()) {
                if (JavaLanguage.INSTANCE.equals(element.getLanguage())) {
                    DefaultConstructorImplicitUsageInfo implicitUsageInfo = new DefaultConstructorImplicitUsageInfo((PsiMethod) element, ((PsiMethod) element).getContainingClass(), method);
                    result.add(implicitUsageInfo);
                }
            } else if (element instanceof PsiClass) {
                LOG.assertTrue(method.isConstructor());
                final PsiClass psiClass = (PsiClass) element;
                if (JavaLanguage.INSTANCE.equals(psiClass.getLanguage())) {
                    if (myChangeInfo instanceof JavaChangeInfoImpl) {
                        if (shouldPropagateToNonPhysicalMethod(method, result, psiClass, ((JavaChangeInfoImpl) myChangeInfo).propagateParametersMethods)) {
                            continue;
                        }
                        if (shouldPropagateToNonPhysicalMethod(method, result, psiClass, ((JavaChangeInfoImpl) myChangeInfo).propagateExceptionsMethods)) {
                            continue;
                        }
                    }
                    result.add(new NoConstructorClassUsageInfo(psiClass));
                }
            } else if (ref instanceof PsiCallReference) {
                result.add(new CallReferenceUsageInfo((PsiCallReference) ref));
            } else if (element instanceof PsiMethodReferenceExpression && MethodReferenceUsageInfo.needToExpand(myChangeInfo)) {
                result.add(new MethodReferenceUsageInfo(element, method, isToModifyArgs, isToCatchExceptions));
            } else {
                result.add(new MoveRenameUsageInfo(element, ref, method));
            }
        }
    //if (method.isConstructor() && parameterCount == 0) {
    //    RefactoringUtil.visitImplicitConstructorUsages(method.getContainingClass(),
    //                                                   new DefaultConstructorUsageCollector(result));
    //}
    } else if (myChangeInfo.isParameterTypesChanged()) {
        PsiReference[] refs = MethodReferencesSearch.search(method, projectScope, true).toArray(PsiReference.EMPTY_ARRAY);
        for (PsiReference reference : refs) {
            final PsiElement element = reference.getElement();
            if (element instanceof PsiDocTagValue) {
                result.add(new UsageInfo(reference));
            } else if (element instanceof XmlElement) {
                result.add(new MoveRenameUsageInfo(reference, method));
            } else if (element instanceof PsiMethodReferenceExpression) {
                result.add(new UsageInfo(reference));
            }
        }
    }
    // Conflicts
    detectLocalsCollisionsInMethod(method, result, isOriginal);
    for (final PsiMethod overridingMethod : overridingMethods) {
        detectLocalsCollisionsInMethod(overridingMethod, result, isOriginal);
    }
    return overridingMethods;
}
Also used : MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) PsiDocTagValue(com.intellij.psi.javadoc.PsiDocTagValue) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) NoConstructorClassUsageInfo(com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo) DefaultConstructorImplicitUsageInfo(com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo) XmlElement(com.intellij.psi.xml.XmlElement) UsageInfo(com.intellij.usageView.UsageInfo) UnresolvableCollisionUsageInfo(com.intellij.refactoring.rename.UnresolvableCollisionUsageInfo) NoConstructorClassUsageInfo(com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) DefaultConstructorImplicitUsageInfo(com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo)

Example 9 with PsiDocTagValue

use of com.intellij.psi.javadoc.PsiDocTagValue in project intellij-community by JetBrains.

the class PsiDocParamRef method getReference.

@Override
public PsiReference getReference() {
    final PsiDocComment comment = PsiTreeUtil.getParentOfType(this, PsiDocComment.class);
    if (comment == null)
        return null;
    final PsiJavaDocumentedElement owner = comment.getOwner();
    if (!(owner instanceof PsiMethod) && !(owner instanceof PsiClass))
        return null;
    final ASTNode valueToken = findChildByType(JavaDocTokenType.DOC_TAG_VALUE_TOKEN);
    if (valueToken == null)
        return null;
    final String name = valueToken.getText();
    PsiElement reference = null;
    final PsiElement firstChild = getFirstChild();
    if (firstChild instanceof PsiDocToken && ((PsiDocToken) firstChild).getTokenType().equals(JavaDocTokenType.DOC_TAG_VALUE_LT)) {
        final PsiTypeParameter[] typeParameters = ((PsiTypeParameterListOwner) owner).getTypeParameters();
        for (PsiTypeParameter typeParameter : typeParameters) {
            if (typeParameter.getName().equals(name)) {
                reference = typeParameter;
            }
        }
    } else if (owner instanceof PsiMethod) {
        final PsiParameter[] parameters = ((PsiMethod) owner).getParameterList().getParameters();
        for (PsiParameter parameter : parameters) {
            if (parameter.getName().equals(name)) {
                reference = parameter;
            }
        }
    }
    final PsiElement resultReference = reference;
    return new PsiJavaReference() {

        @Override
        public PsiElement resolve() {
            return resultReference;
        }

        @Override
        @NotNull
        public String getCanonicalText() {
            return valueToken.getText();
        }

        @Override
        public PsiElement handleElementRename(String newElementName) {
            final CharTable charTableByTree = SharedImplUtil.findCharTableByTree(getNode());
            LeafElement newElement = Factory.createSingleLeafElement(JavaDocTokenType.DOC_TAG_VALUE_TOKEN, newElementName, charTableByTree, getManager());
            replaceChild(valueToken, newElement);
            return PsiDocParamRef.this;
        }

        @Override
        public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
            if (isReferenceTo(element))
                return PsiDocParamRef.this;
            if (!(element instanceof PsiParameter)) {
                throw new IncorrectOperationException("Unsupported operation");
            }
            return handleElementRename(((PsiParameter) element).getName());
        }

        @Override
        public boolean isReferenceTo(PsiElement element) {
            if (!(element instanceof PsiNamedElement))
                return false;
            PsiNamedElement namedElement = (PsiNamedElement) element;
            if (!getCanonicalText().equals(namedElement.getName()))
                return false;
            return getManager().areElementsEquivalent(resolve(), element);
        }

        @Override
        @NotNull
        public PsiElement[] getVariants() {
            final PsiElement firstChild = getFirstChild();
            Set<String> usedNames = new HashSet<>();
            for (PsiDocTag tag : comment.getTags()) {
                if (tag.getName().equals("param")) {
                    PsiDocTagValue valueElement = tag.getValueElement();
                    if (valueElement != null) {
                        usedNames.add(valueElement.getText());
                    }
                }
            }
            PsiNamedElement[] result = PsiNamedElement.EMPTY_ARRAY;
            if (firstChild instanceof PsiDocToken && ((PsiDocToken) firstChild).getTokenType().equals(JavaDocTokenType.DOC_TAG_VALUE_LT)) {
                result = ((PsiTypeParameterListOwner) owner).getTypeParameters();
            } else if (owner instanceof PsiMethod) {
                result = ((PsiMethod) owner).getParameterList().getParameters();
            }
            List<PsiElement> filtered = new ArrayList<>();
            for (PsiNamedElement namedElement : result) {
                if (!usedNames.contains(namedElement.getName())) {
                    filtered.add(namedElement);
                }
            }
            return filtered.toArray(new PsiElement[filtered.size()]);
        }

        @Override
        public boolean isSoft() {
            return false;
        }

        @Override
        public TextRange getRangeInElement() {
            final int startOffsetInParent = valueToken.getPsi().getStartOffsetInParent();
            return new TextRange(startOffsetInParent, startOffsetInParent + valueToken.getTextLength());
        }

        @Override
        public PsiElement getElement() {
            return PsiDocParamRef.this;
        }

        @Override
        public void processVariants(@NotNull PsiScopeProcessor processor) {
            for (final PsiElement element : getVariants()) {
                if (!processor.execute(element, ResolveState.initial())) {
                    return;
                }
            }
        }

        @Override
        @NotNull
        public JavaResolveResult advancedResolve(boolean incompleteCode) {
            return resultReference == null ? JavaResolveResult.EMPTY : new CandidateInfo(resultReference, PsiSubstitutor.EMPTY);
        }

        @Override
        @NotNull
        public JavaResolveResult[] multiResolve(boolean incompleteCode) {
            return resultReference == null ? JavaResolveResult.EMPTY_ARRAY : new JavaResolveResult[] { new CandidateInfo(resultReference, PsiSubstitutor.EMPTY) };
        }
    };
}
Also used : PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) CandidateInfo(com.intellij.psi.infos.CandidateInfo) ArrayList(java.util.ArrayList) PsiDocToken(com.intellij.psi.javadoc.PsiDocToken) PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) CharTable(com.intellij.util.CharTable) NotNull(org.jetbrains.annotations.NotNull) ASTNode(com.intellij.lang.ASTNode) HashSet(java.util.HashSet) PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) TextRange(com.intellij.openapi.util.TextRange) PsiDocTagValue(com.intellij.psi.javadoc.PsiDocTagValue) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 10 with PsiDocTagValue

use of com.intellij.psi.javadoc.PsiDocTagValue in project intellij-community by JetBrains.

the class DocDataHandler method getTextFromNode.

// since doctag value may be inside doc comment we specially build text including skipped nodes
private static String getTextFromNode(final PsiElement node, String text1) {
    PsiElement nextSibling = node.getNextSibling();
    if (nextSibling instanceof PsiDocTagValue) {
        text1 += nextSibling.getText();
        nextSibling = nextSibling.getNextSibling();
        if (nextSibling instanceof PsiDocToken && ((PsiDocToken) nextSibling).getTokenType() == JavaDocTokenType.DOC_COMMENT_DATA) {
            text1 += nextSibling.getText();
        }
    }
    return text1;
}
Also used : PsiDocToken(com.intellij.psi.javadoc.PsiDocToken) PsiDocTagValue(com.intellij.psi.javadoc.PsiDocTagValue) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiDocTagValue (com.intellij.psi.javadoc.PsiDocTagValue)14 PsiDocTag (com.intellij.psi.javadoc.PsiDocTag)7 NotNull (org.jetbrains.annotations.NotNull)4 Project (com.intellij.openapi.project.Project)3 PsiDocToken (com.intellij.psi.javadoc.PsiDocToken)3 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)3 UsageInfo (com.intellij.usageView.UsageInfo)3 ASTNode (com.intellij.lang.ASTNode)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiElement (com.intellij.psi.PsiElement)2 PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)2 Annotation (com.intellij.lang.annotation.Annotation)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 Pair (com.intellij.openapi.util.Pair)1 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)1 PsiDocParamRef (com.intellij.psi.impl.source.javadoc.PsiDocParamRef)1 CandidateInfo (com.intellij.psi.infos.CandidateInfo)1 PsiInlineDocTag (com.intellij.psi.javadoc.PsiInlineDocTag)1 PsiScopeProcessor (com.intellij.psi.scope.PsiScopeProcessor)1