Search in sources :

Example 1 with PsiJavaCodeReferenceElementImpl

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

the class ImportHelper method addNamesToImport.

private static void addNamesToImport(@NotNull Set<Pair<String, Boolean>> names, @NotNull List<PsiElement> comments, @NotNull PsiElement scope, @NotNull String thisPackageName, PsiFile context) {
    if (scope instanceof PsiImportList)
        return;
    final LinkedList<PsiElement> stack = new LinkedList<>();
    stack.add(scope);
    while (!stack.isEmpty()) {
        final PsiElement child = stack.removeFirst();
        if (child instanceof PsiImportList) {
            for (PsiElement element : child.getChildren()) {
                if (element == null) {
                    continue;
                }
                ASTNode node = element.getNode();
                if (node == null) {
                    continue;
                }
                IElementType elementType = node.getElementType();
                if (!ElementType.IMPORT_STATEMENT_BASE_BIT_SET.contains(elementType) && !JavaJspElementType.WHITE_SPACE_BIT_SET.contains(elementType)) {
                    comments.add(element);
                }
            }
            continue;
        }
        if (child instanceof PsiLiteralExpression)
            continue;
        ContainerUtil.addAll(stack, child.getChildren());
        for (final PsiReference reference : child.getReferences()) {
            if (!(reference instanceof PsiJavaReference))
                continue;
            final PsiJavaReference javaReference = (PsiJavaReference) reference;
            if (javaReference instanceof JavaClassReference && ((JavaClassReference) javaReference).getContextReference() != null)
                continue;
            PsiJavaCodeReferenceElement referenceElement = null;
            if (reference instanceof PsiJavaCodeReferenceElement) {
                referenceElement = (PsiJavaCodeReferenceElement) child;
                if (referenceElement.getQualifier() != null) {
                    continue;
                }
                if (reference instanceof PsiJavaCodeReferenceElementImpl && ((PsiJavaCodeReferenceElementImpl) reference).getKind(((PsiJavaCodeReferenceElementImpl) reference).getContainingFile()) == PsiJavaCodeReferenceElementImpl.CLASS_IN_QUALIFIED_NEW_KIND) {
                    continue;
                }
            }
            final JavaResolveResult resolveResult = javaReference.advancedResolve(true);
            PsiElement refElement = resolveResult.getElement();
            if (refElement == null && referenceElement != null) {
                // might be uncomplete code
                refElement = ResolveClassUtil.resolveClass(referenceElement, referenceElement.getContainingFile());
            }
            if (refElement == null)
                continue;
            PsiElement currentFileResolveScope = resolveResult.getCurrentFileResolveScope();
            if (!(currentFileResolveScope instanceof PsiImportStatementBase))
                continue;
            if (context != null && (!currentFileResolveScope.isValid() || currentFileResolveScope instanceof JspxImportStatement && context != ((JspxImportStatement) currentFileResolveScope).getDeclarationFile())) {
                continue;
            }
            if (referenceElement != null) {
                if (currentFileResolveScope instanceof PsiImportStaticStatement) {
                    PsiImportStaticStatement importStaticStatement = (PsiImportStaticStatement) currentFileResolveScope;
                    String name = importStaticStatement.getImportReference().getCanonicalText();
                    if (importStaticStatement.isOnDemand()) {
                        String refName = referenceElement.getReferenceName();
                        if (refName != null)
                            name = name + "." + refName;
                    }
                    names.add(Pair.create(name, Boolean.TRUE));
                    continue;
                }
            }
            if (refElement instanceof PsiClass) {
                String qName = ((PsiClass) refElement).getQualifiedName();
                if (hasPackage(qName, thisPackageName))
                    continue;
                names.add(Pair.create(qName, Boolean.FALSE));
            }
        }
    }
}
Also used : JspxImportStatement(com.intellij.psi.impl.source.jsp.jspJava.JspxImportStatement) JavaClassReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReference) PsiJavaCodeReferenceElementImpl(com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl) IElementType(com.intellij.psi.tree.IElementType) ASTNode(com.intellij.lang.ASTNode)

Example 2 with PsiJavaCodeReferenceElementImpl

use of com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl 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)

Example 3 with PsiJavaCodeReferenceElementImpl

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

the class PsiClassReferenceListStubImpl method getReferencedTypes.

@NotNull
@Override
public PsiClassType[] getReferencedTypes() {
    if (myTypes != null)
        return myTypes;
    if (myNames.length == 0) {
        myTypes = PsiClassType.EMPTY_ARRAY;
        return myTypes;
    }
    PsiClassType[] types = new PsiClassType[myNames.length];
    final boolean compiled = ((JavaClassReferenceListElementType) getStubType()).isCompiled(this);
    if (compiled) {
        for (int i = 0; i < types.length; i++) {
            types[i] = new PsiClassReferenceType(new ClsJavaCodeReferenceElementImpl(getPsi(), myNames[i]), null);
        }
    } else {
        final PsiElementFactory factory = JavaPsiFacade.getInstance(getProject()).getElementFactory();
        int nullCount = 0;
        final PsiReferenceList psi = getPsi();
        for (int i = 0; i < types.length; i++) {
            try {
                final PsiJavaCodeReferenceElement ref = factory.createReferenceFromText(myNames[i], psi);
                ((PsiJavaCodeReferenceElementImpl) ref).setKindWhenDummy(PsiJavaCodeReferenceElementImpl.CLASS_NAME_KIND);
                types[i] = factory.createType(ref);
            } catch (IncorrectOperationException e) {
                types[i] = null;
                nullCount++;
            }
        }
        if (nullCount > 0) {
            PsiClassType[] newTypes = new PsiClassType[types.length - nullCount];
            int cnt = 0;
            for (PsiClassType type : types) {
                if (type != null)
                    newTypes[cnt++] = type;
            }
            types = newTypes;
        }
    }
    myTypes = types;
    return types.clone();
}
Also used : ClsJavaCodeReferenceElementImpl(com.intellij.psi.impl.compiled.ClsJavaCodeReferenceElementImpl) JavaClassReferenceListElementType(com.intellij.psi.impl.java.stubs.JavaClassReferenceListElementType) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PsiClassReferenceType(com.intellij.psi.impl.source.PsiClassReferenceType) PsiJavaCodeReferenceElementImpl(com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with PsiJavaCodeReferenceElementImpl

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

the class PsiImportStatementStubImpl method getRegularReference.

@Nullable
private PsiJavaCodeReferenceElement getRegularReference() {
    final PsiJavaCodeReferenceElement refElement = createReference();
    if (refElement == null)
        return null;
    ((PsiJavaCodeReferenceElementImpl) refElement).setKindWhenDummy(isOnDemand() ? PsiJavaCodeReferenceElementImpl.CLASS_FQ_OR_PACKAGE_NAME_KIND : PsiJavaCodeReferenceElementImpl.CLASS_FQ_NAME_KIND);
    return refElement;
}
Also used : PsiJavaCodeReferenceElement(com.intellij.psi.PsiJavaCodeReferenceElement) PsiJavaCodeReferenceElementImpl(com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiJavaCodeReferenceElementImpl (com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl)4 ASTNode (com.intellij.lang.ASTNode)2 IElementType (com.intellij.psi.tree.IElementType)2 PsiJavaCodeReferenceElement (com.intellij.psi.PsiJavaCodeReferenceElement)1 ClsJavaCodeReferenceElementImpl (com.intellij.psi.impl.compiled.ClsJavaCodeReferenceElementImpl)1 JavaClassReferenceListElementType (com.intellij.psi.impl.java.stubs.JavaClassReferenceListElementType)1 PsiClassReferenceType (com.intellij.psi.impl.source.PsiClassReferenceType)1 SourceJavaCodeReference (com.intellij.psi.impl.source.SourceJavaCodeReference)1 JspxImportStatement (com.intellij.psi.impl.source.jsp.jspJava.JspxImportStatement)1 JavaClassReference (com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReference)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1