Search in sources :

Example 1 with SourceJavaCodeReference

use of com.intellij.psi.impl.source.SourceJavaCodeReference in project intellij-community by JetBrains.

the class JavaSourceUtil method fullyQualifyReference.

public static void fullyQualifyReference(@NotNull CompositeElement reference, @NotNull PsiClass targetClass) {
    if (((SourceJavaCodeReference) reference).isQualified()) {
        // qualified reference
        final PsiClass parentClass = targetClass.getContainingClass();
        if (parentClass == null)
            return;
        final ASTNode qualifier = reference.findChildByRole(ChildRole.QUALIFIER);
        if (qualifier instanceof SourceJavaCodeReference) {
            ((SourceJavaCodeReference) qualifier).fullyQualify(parentClass);
        }
    } else {
        // unqualified reference, need to qualify with package name
        final String qName = targetClass.getQualifiedName();
        if (qName == null) {
            // todo: local classes?
            return;
        }
        final int i = qName.lastIndexOf('.');
        if (i > 0) {
            final String prefix = qName.substring(0, i);
            final PsiManager manager = reference.getManager();
            final PsiJavaParserFacade parserFacade = JavaPsiFacade.getInstance(manager.getProject()).getParserFacade();
            final TreeElement qualifier;
            if (reference instanceof PsiReferenceExpression) {
                qualifier = (TreeElement) parserFacade.createExpressionFromText(prefix, null).getNode();
            } else {
                qualifier = (TreeElement) parserFacade.createReferenceFromText(prefix, null).getNode();
            }
            if (qualifier != null) {
                final CharTable systemCharTab = SharedImplUtil.findCharTableByTree(qualifier);
                final LeafElement dot = Factory.createSingleLeafElement(JavaTokenType.DOT, ".", 0, 1, systemCharTab, manager);
                qualifier.rawInsertAfterMe(dot);
                reference.addInternal(qualifier, dot, null, Boolean.FALSE);
            }
        }
    }
}
Also used : SourceJavaCodeReference(com.intellij.psi.impl.source.SourceJavaCodeReference) ASTNode(com.intellij.lang.ASTNode) LighterASTNode(com.intellij.lang.LighterASTNode) CharTable(com.intellij.util.CharTable)

Example 2 with SourceJavaCodeReference

use of com.intellij.psi.impl.source.SourceJavaCodeReference in project intellij-community by JetBrains.

the class JavaReferenceAdjuster method process.

@Override
public ASTNode process(@NotNull ASTNode element, boolean addImports, boolean incompleteCode, boolean useFqInJavadoc, boolean useFqInCode) {
    IElementType elementType = element.getElementType();
    if ((elementType == JavaElementType.JAVA_CODE_REFERENCE || elementType == JavaElementType.REFERENCE_EXPRESSION) && !isAnnotated(element)) {
        IElementType parentType = element.getTreeParent().getElementType();
        if (elementType == JavaElementType.JAVA_CODE_REFERENCE || incompleteCode || parentType == JavaElementType.REFERENCE_EXPRESSION || parentType == JavaElementType.METHOD_REF_EXPRESSION) {
            PsiJavaCodeReferenceElement ref = (PsiJavaCodeReferenceElement) element.getPsi();
            PsiReferenceParameterList parameterList = ref.getParameterList();
            if (parameterList != null) {
                PsiTypeElement[] typeParameters = parameterList.getTypeParameterElements();
                for (PsiTypeElement typeParameter : typeParameters) {
                    process(typeParameter.getNode(), addImports, incompleteCode, useFqInJavadoc, useFqInCode);
                }
            }
            boolean rightKind = true;
            if (elementType == JavaElementType.JAVA_CODE_REFERENCE) {
                PsiJavaCodeReferenceElementImpl impl = (PsiJavaCodeReferenceElementImpl) element;
                int kind = impl.getKind(impl.getContainingFile());
                rightKind = kind == PsiJavaCodeReferenceElementImpl.CLASS_NAME_KIND || kind == PsiJavaCodeReferenceElementImpl.CLASS_OR_PACKAGE_NAME_KIND;
            }
            if (rightKind) {
                // annotations may jump out of reference (see PsiJavaCodeReferenceImpl#setAnnotations()) so they should be processed first
                List<PsiAnnotation> annotations = PsiTreeUtil.getChildrenOfTypeAsList(ref, PsiAnnotation.class);
                for (PsiAnnotation annotation : annotations) {
                    process(annotation.getNode(), addImports, incompleteCode, useFqInJavadoc, useFqInCode);
                }
                boolean isInsideDocComment = TreeUtil.findParent(element, JavaDocElementType.DOC_COMMENT) != null;
                boolean isShort = !ref.isQualified();
                if (isInsideDocComment ? !useFqInJavadoc : !useFqInCode) {
                    // short name already, no need to change
                    if (isShort)
                        return element;
                }
                PsiElement refElement;
                if (!incompleteCode) {
                    refElement = ref.resolve();
                } else {
                    PsiResolveHelper helper = JavaPsiFacade.getInstance(ref.getManager().getProject()).getResolveHelper();
                    final SourceJavaCodeReference reference = (SourceJavaCodeReference) element;
                    refElement = helper.resolveReferencedClass(reference.getClassNameText(), ref);
                }
                if (refElement instanceof PsiClass) {
                    PsiClass psiClass = (PsiClass) refElement;
                    if (isInsideDocComment ? useFqInJavadoc : useFqInCode) {
                        String qName = psiClass.getQualifiedName();
                        if (qName == null)
                            return element;
                        PsiFile file = ref.getContainingFile();
                        if (file instanceof PsiJavaFile) {
                            if (ImportHelper.isImplicitlyImported(qName, (PsiJavaFile) file)) {
                                if (isShort)
                                    return element;
                                return makeShortReference((CompositeElement) element, psiClass, addImports);
                            }
                            String thisPackageName = ((PsiJavaFile) file).getPackageName();
                            if (ImportHelper.hasPackage(qName, thisPackageName)) {
                                if (!isShort) {
                                    return makeShortReference((CompositeElement) element, psiClass, addImports);
                                }
                            }
                        }
                        return replaceReferenceWithFQ(element, psiClass);
                    } else {
                        int oldLength = element.getTextLength();
                        ASTNode treeElement = makeShortReference((CompositeElement) element, psiClass, addImports);
                        if (treeElement.getTextLength() == oldLength && psiClass.getContainingClass() != null) {
                            PsiElement qualifier = ref.getQualifier();
                            if (qualifier instanceof PsiJavaCodeReferenceElement && ((PsiJavaCodeReferenceElement) qualifier).resolve() instanceof PsiClass) {
                                process(qualifier.getNode(), addImports, incompleteCode, useFqInJavadoc, useFqInCode);
                            }
                        }
                        return treeElement;
                    }
                }
            }
        }
    }
    for (ASTNode child = element.getFirstChildNode(); child != null; child = child.getTreeNext()) {
        //noinspection AssignmentToForLoopParameter
        child = process(child, addImports, incompleteCode, useFqInJavadoc, useFqInCode);
    }
    return element;
}
Also used : PsiJavaCodeReferenceElementImpl(com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl) IElementType(com.intellij.psi.tree.IElementType) SourceJavaCodeReference(com.intellij.psi.impl.source.SourceJavaCodeReference) ASTNode(com.intellij.lang.ASTNode)

Aggregations

ASTNode (com.intellij.lang.ASTNode)2 SourceJavaCodeReference (com.intellij.psi.impl.source.SourceJavaCodeReference)2 LighterASTNode (com.intellij.lang.LighterASTNode)1 PsiJavaCodeReferenceElementImpl (com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl)1 IElementType (com.intellij.psi.tree.IElementType)1 CharTable (com.intellij.util.CharTable)1