Search in sources :

Example 1 with PsiClassReferenceType

use of com.intellij.psi.impl.source.PsiClassReferenceType in project GsonFormat by zzz40500.

the class ConvertBridge method evalFieldEntity.

private FieldEntity evalFieldEntity(FieldEntity fieldEntity, PsiType type) {
    if (type instanceof PsiPrimitiveType) {
        if (fieldEntity == null) {
            fieldEntity = new FieldEntity();
        }
        fieldEntity.setType(type.getPresentableText());
        return fieldEntity;
    } else if (type instanceof PsiArrayType) {
        if (fieldEntity == null) {
            fieldEntity = new IterableFieldEntity();
        }
        IterableFieldEntity iterableFieldEntity = (IterableFieldEntity) fieldEntity;
        iterableFieldEntity.setDeep(iterableFieldEntity.getDeep() + 1);
        return evalFieldEntity(fieldEntity, ((PsiArrayType) type).getComponentType());
    } else if (type instanceof PsiClassReferenceType) {
        PsiClass psi = ((PsiClassReferenceType) type).resolveGenerics().getElement();
        if (isCollection(psi)) {
            if (fieldEntity == null) {
                fieldEntity = new IterableFieldEntity();
            }
            IterableFieldEntity iterableFieldEntity = (IterableFieldEntity) fieldEntity;
            iterableFieldEntity.setDeep(iterableFieldEntity.getDeep() + 1);
            PsiType[] parameters = ((PsiClassReferenceType) type).getParameters();
            if (parameters.length > 0) {
                PsiType parameter = parameters[0];
                if (parameter instanceof PsiWildcardType) {
                    if (((PsiWildcardType) parameter).isExtends()) {
                        final PsiType extendsBound = ((PsiWildcardType) parameter).getExtendsBound();
                        evalFieldEntity(fieldEntity, extendsBound);
                    }
                    if (((PsiWildcardType) parameter).isSuper()) {
                        final PsiType superBound = ((PsiWildcardType) parameter).getSuperBound();
                        evalFieldEntity(fieldEntity, superBound);
                    }
                } else if (parameter instanceof PsiClassReferenceType) {
                    PsiClass element = ((PsiClassReferenceType) parameter).resolveGenerics().getElement();
                    handleClassReferenceType(fieldEntity, element);
                }
            }
            return fieldEntity;
        } else {
            if (fieldEntity == null) {
                fieldEntity = new FieldEntity();
            }
            handleClassReferenceType(fieldEntity, psi);
            return fieldEntity;
        }
    }
    if (fieldEntity == null) {
        fieldEntity = new IterableFieldEntity();
    }
    return fieldEntity;
}
Also used : IterableFieldEntity(org.gsonformat.intellij.entity.IterableFieldEntity) FieldEntity(org.gsonformat.intellij.entity.FieldEntity) IterableFieldEntity(org.gsonformat.intellij.entity.IterableFieldEntity) PsiClassReferenceType(com.intellij.psi.impl.source.PsiClassReferenceType)

Example 2 with PsiClassReferenceType

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

the class AnnotationsHighlightUtil method checkApplicability.

@Nullable
public static HighlightInfo checkApplicability(@NotNull PsiAnnotation annotation, @NotNull LanguageLevel level, @NotNull PsiFile file) {
    if (ANY_ANNOTATION_ALLOWED.accepts(annotation)) {
        return null;
    }
    PsiJavaCodeReferenceElement nameRef = annotation.getNameReferenceElement();
    if (nameRef == null)
        return null;
    PsiAnnotationOwner owner = annotation.getOwner();
    PsiAnnotation.TargetType[] targets = AnnotationTargetUtil.getTargetsForLocation(owner);
    if (owner == null || targets.length == 0) {
        String message = JavaErrorMessages.message("annotation.not.allowed.here");
        return annotationError(annotation, message);
    }
    if (!(owner instanceof PsiModifierList)) {
        HighlightInfo info = HighlightUtil.checkFeature(annotation, HighlightUtil.Feature.TYPE_ANNOTATIONS, level, file);
        if (info != null)
            return info;
    }
    PsiAnnotation.TargetType applicable = AnnotationTargetUtil.findAnnotationTarget(annotation, targets);
    if (applicable == PsiAnnotation.TargetType.UNKNOWN)
        return null;
    if (applicable == null) {
        String target = JavaErrorMessages.message("annotation.target." + targets[0]);
        String message = JavaErrorMessages.message("annotation.not.applicable", nameRef.getText(), target);
        return annotationError(annotation, message);
    }
    if (applicable == PsiAnnotation.TargetType.TYPE_USE) {
        if (owner instanceof PsiClassReferenceType) {
            PsiJavaCodeReferenceElement ref = ((PsiClassReferenceType) owner).getReference();
            HighlightInfo info = checkReferenceTarget(annotation, ref);
            if (info != null)
                return info;
        } else if (owner instanceof PsiModifierList) {
            PsiElement nextElement = PsiTreeUtil.skipSiblingsForward((PsiModifierList) owner, PsiComment.class, PsiWhiteSpace.class, PsiTypeParameterList.class);
            if (nextElement instanceof PsiTypeElement) {
                PsiTypeElement typeElement = (PsiTypeElement) nextElement;
                PsiType type = typeElement.getType();
                if (PsiType.VOID.equals(type)) {
                    String message = JavaErrorMessages.message("annotation.not.allowed.void");
                    return annotationError(annotation, message);
                }
                if (!(type instanceof PsiPrimitiveType)) {
                    PsiJavaCodeReferenceElement ref = getOutermostReferenceElement(typeElement.getInnermostComponentReferenceElement());
                    HighlightInfo info = checkReferenceTarget(annotation, ref);
                    if (info != null)
                        return info;
                }
            }
        } else if (owner instanceof PsiTypeElement) {
            PsiElement context = PsiTreeUtil.skipParentsOfType((PsiTypeElement) owner, PsiTypeElement.class);
            if (context instanceof PsiClassObjectAccessExpression) {
                String message = JavaErrorMessages.message("annotation.not.allowed.class");
                return annotationError(annotation, message);
            }
        }
    }
    return null;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) PsiClassReferenceType(com.intellij.psi.impl.source.PsiClassReferenceType) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with PsiClassReferenceType

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

the class PsiNewExpressionImpl method doGetType.

@Nullable
private PsiType doGetType(@Nullable PsiAnnotation stopAt) {
    PsiType type = null;
    SmartList<PsiAnnotation> annotations = new SmartList<>();
    boolean stop = false;
    for (ASTNode child = getFirstChildNode(); child != null; child = child.getTreeNext()) {
        IElementType elementType = child.getElementType();
        if (elementType == JavaElementType.ANNOTATION) {
            PsiAnnotation annotation = (PsiAnnotation) child.getPsi();
            annotations.add(annotation);
            if (annotation == stopAt)
                stop = true;
        } else if (elementType == JavaElementType.JAVA_CODE_REFERENCE) {
            assert type == null : this;
            type = new PsiClassReferenceType((PsiJavaCodeReferenceElement) child.getPsi(), null);
            if (stop)
                return type;
        } else if (ElementType.PRIMITIVE_TYPE_BIT_SET.contains(elementType)) {
            assert type == null : this;
            PsiElementFactory factory = JavaPsiFacade.getInstance(getProject()).getElementFactory();
            PsiAnnotation[] copy = ContainerUtil.copyAndClear(annotations, PsiAnnotation.ARRAY_FACTORY, true);
            type = factory.createPrimitiveTypeFromText(child.getText()).annotate(TypeAnnotationProvider.Static.create(copy));
            if (stop)
                return type;
        } else if (elementType == JavaTokenType.LBRACKET) {
            assert type != null : this;
            PsiAnnotation[] copy = ContainerUtil.copyAndClear(annotations, PsiAnnotation.ARRAY_FACTORY, true);
            type = type.createArrayType().annotate(TypeAnnotationProvider.Static.create(copy));
            if (stop)
                return type;
        } else if (elementType == JavaElementType.ANONYMOUS_CLASS) {
            PsiElementFactory factory = JavaPsiFacade.getInstance(getProject()).getElementFactory();
            PsiClass aClass = (PsiClass) child.getPsi();
            PsiSubstitutor substitutor = aClass instanceof PsiTypeParameter ? PsiSubstitutor.EMPTY : factory.createRawSubstitutor(aClass);
            PsiAnnotation[] copy = ContainerUtil.copyAndClear(annotations, PsiAnnotation.ARRAY_FACTORY, true);
            type = factory.createType(aClass, substitutor, PsiUtil.getLanguageLevel(aClass)).annotate(TypeAnnotationProvider.Static.create(copy));
            if (stop)
                return type;
        }
    }
    // stop == true means annotation is misplaced
    return stop ? null : type;
}
Also used : PsiClassReferenceType(com.intellij.psi.impl.source.PsiClassReferenceType) IElementType(com.intellij.psi.tree.IElementType) ASTNode(com.intellij.lang.ASTNode) SmartList(com.intellij.util.SmartList) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with PsiClassReferenceType

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

the class PsiAnnotationImpl method getOwner.

@Nullable
@Override
public PsiAnnotationOwner getOwner() {
    PsiElement parent = getParent();
    if (parent instanceof PsiAnnotationOwner) {
        return (PsiAnnotationOwner) parent;
    }
    if (parent instanceof PsiNewExpression) {
        return ((PsiNewExpression) parent).getOwner(this);
    }
    if (parent instanceof PsiReferenceExpression) {
        PsiElement ctx = parent.getParent();
        if (ctx instanceof PsiMethodReferenceExpression) {
            return new PsiClassReferenceType((PsiJavaCodeReferenceElement) parent, null);
        }
    } else if (parent instanceof PsiJavaCodeReferenceElement) {
        PsiElement ctx = PsiTreeUtil.skipParentsOfType(parent, PsiJavaCodeReferenceElement.class);
        if (ctx instanceof PsiReferenceList || ctx instanceof PsiNewExpression || ctx instanceof PsiTypeElement || ctx instanceof PsiAnonymousClass) {
            return new PsiClassReferenceType((PsiJavaCodeReferenceElement) parent, null);
        }
    }
    PsiTypeElement typeElement = null;
    PsiElement anchor = null;
    if (parent instanceof PsiMethod) {
        typeElement = ((PsiMethod) parent).getReturnTypeElement();
        anchor = ((PsiMethod) parent).getParameterList();
    } else if (parent instanceof PsiField || parent instanceof PsiParameter || parent instanceof PsiLocalVariable) {
        typeElement = ((PsiVariable) parent).getTypeElement();
        anchor = ((PsiVariable) parent).getNameIdentifier();
    }
    if (typeElement != null && anchor != null) {
        return JavaSharedImplUtil.getType(typeElement, anchor, this);
    }
    return null;
}
Also used : PsiClassReferenceType(com.intellij.psi.impl.source.PsiClassReferenceType) JavaStubPsiElement(com.intellij.psi.impl.source.JavaStubPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with PsiClassReferenceType

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

the class ClsTypeElementImpl method calculateBaseType.

@NotNull
private PsiType calculateBaseType() {
    PsiType result = PsiJavaParserFacadeImpl.getPrimitiveType(myTypeText);
    if (result != null)
        return result;
    ClsElementImpl childElement = myChild.getValue();
    if (childElement instanceof ClsTypeElementImpl) {
        if (isArray()) {
            switch(myVariance) {
                case VARIANCE_NONE:
                    return ((PsiTypeElement) childElement).getType().createArrayType();
                case VARIANCE_EXTENDS:
                    return PsiWildcardType.createExtends(getManager(), ((PsiTypeElement) childElement).getType());
                case VARIANCE_SUPER:
                    return PsiWildcardType.createSuper(getManager(), ((PsiTypeElement) childElement).getType());
                default:
                    assert false : myVariance;
                    return null;
            }
        } else {
            assert isVarArgs() : this;
            return new PsiEllipsisType(((PsiTypeElement) childElement).getType());
        }
    }
    if (childElement instanceof ClsJavaCodeReferenceElementImpl) {
        PsiClassReferenceType psiClassReferenceType = new PsiClassReferenceType((PsiJavaCodeReferenceElement) childElement, null);
        switch(myVariance) {
            case VARIANCE_NONE:
                return psiClassReferenceType;
            case VARIANCE_EXTENDS:
                return PsiWildcardType.createExtends(getManager(), psiClassReferenceType);
            case VARIANCE_SUPER:
                return PsiWildcardType.createSuper(getManager(), psiClassReferenceType);
            case VARIANCE_INVARIANT:
                return PsiWildcardType.createUnbounded(getManager());
            default:
                assert false : myVariance;
                return null;
        }
    }
    assert childElement == null : this;
    return PsiWildcardType.createUnbounded(getManager());
}
Also used : PsiClassReferenceType(com.intellij.psi.impl.source.PsiClassReferenceType) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiClassReferenceType (com.intellij.psi.impl.source.PsiClassReferenceType)10 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3 LightClassReference (com.intellij.psi.impl.light.LightClassReference)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)1 ASTNode (com.intellij.lang.ASTNode)1 PsiElement (com.intellij.psi.PsiElement)1 PsiType (com.intellij.psi.PsiType)1 ClsJavaCodeReferenceElementImpl (com.intellij.psi.impl.compiled.ClsJavaCodeReferenceElementImpl)1 JavaClassReferenceListElementType (com.intellij.psi.impl.java.stubs.JavaClassReferenceListElementType)1 JavaStubPsiElement (com.intellij.psi.impl.source.JavaStubPsiElement)1 PsiImmediateClassType (com.intellij.psi.impl.source.PsiImmediateClassType)1 PsiJavaCodeReferenceElementImpl (com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 IElementType (com.intellij.psi.tree.IElementType)1 SmartList (com.intellij.util.SmartList)1 FieldEntity (org.gsonformat.intellij.entity.FieldEntity)1 IterableFieldEntity (org.gsonformat.intellij.entity.IterableFieldEntity)1 GroovyMapContentProvider (org.jetbrains.plugins.groovy.extensions.GroovyMapContentProvider)1