Search in sources :

Example 1 with PsiJavaCodeReferenceElement

use of com.intellij.psi.PsiJavaCodeReferenceElement in project qi4j-sdk by Qi4j.

the class ConcernsAnnotationDeclaredCorrectlyInspection method checkClass.

@Override
public final ProblemDescriptor[] checkClass(@NotNull PsiClass psiClass, @NotNull InspectionManager manager, boolean isOnTheFly) {
    // If class does not have @Concerns, ignore
    PsiAnnotation concernsAnnotation = getConcernsAnnotation(psiClass);
    if (concernsAnnotation == null) {
        return null;
    }
    // If @Concerns declared in class, suggest remove @Concerns annotation
    if (!psiClass.isInterface()) {
        String message = message("concerns.annotation.declared.correctly.error.annotation.declared.in.class");
        RemoveConcernsAnnotationFix fix = new RemoveConcernsAnnotationFix(concernsAnnotation);
        ProblemDescriptor problemDescriptor = manager.createProblemDescriptor(concernsAnnotation, message, fix, GENERIC_ERROR_OR_WARNING);
        return new ProblemDescriptor[] { problemDescriptor };
    }
    // If @Concerns annotation is empty, ignore
    List<PsiAnnotationMemberValue> concernsAnnotationValue = getConcernsAnnotationValue(concernsAnnotation);
    if (concernsAnnotationValue.isEmpty()) {
        return null;
    }
    // If ConcernOfClass is not resolved, ignore
    Project project = psiClass.getProject();
    GlobalSearchScope searchScope = determineSearchScope(psiClass);
    PsiClass concernOfClass = getConcernOfClass(project, searchScope);
    if (concernOfClass == null) {
        return null;
    }
    List<ProblemDescriptor> problems = new LinkedList<ProblemDescriptor>();
    for (PsiAnnotationMemberValue concernClassAnnotationValue : concernsAnnotationValue) {
        PsiJavaCodeReferenceElement concernClassReference = getConcernClassReference(concernClassAnnotationValue);
        // If it's not a class reference, ignore
        if (concernClassReference == null) {
            continue;
        }
        // If class reference can't be resolved, ignore
        PsiClass concernClass = (PsiClass) concernClassReference.resolve();
        if (concernClass == null) {
            continue;
        }
        // If concern class does not inherit concern class, suggest remove that reference.
        if (!concernClass.isInheritor(concernOfClass, true)) {
            String message = Qi4jResourceBundle.message("concerns.annotation.declared.correctly.error.concern.class.does.not.extend.ConcernOf", concernClass.getQualifiedName());
            RemoveInvalidConcernClassReferenceFix fix = new RemoveInvalidConcernClassReferenceFix(concernClassAnnotationValue, concernClassReference);
            ProblemDescriptor problemDescriptor = manager.createProblemDescriptor(concernClassAnnotationValue, message, fix, GENERIC_ERROR_OR_WARNING);
            problems.add(problemDescriptor);
        } else {
        // TODO: Test whether it is a generic concern
        // TODO: Test whether it is a specific concern
        }
    }
    return problems.toArray(new ProblemDescriptor[problems.size()]);
}
Also used : ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) PsiJavaCodeReferenceElement(com.intellij.psi.PsiJavaCodeReferenceElement) PsiClass(com.intellij.psi.PsiClass) LinkedList(java.util.LinkedList) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiAnnotation(com.intellij.psi.PsiAnnotation) PsiAnnotationMemberValue(com.intellij.psi.PsiAnnotationMemberValue)

Example 2 with PsiJavaCodeReferenceElement

use of com.intellij.psi.PsiJavaCodeReferenceElement in project qi4j-sdk by Qi4j.

the class SideEffectsAnnotationDeclaredCorrectlyInspection method checkClass.

@Override
public final ProblemDescriptor[] checkClass(@NotNull PsiClass psiClass, @NotNull InspectionManager manager, boolean isOnTheFly) {
    // If class does not have @SideEffects, ignore
    PsiAnnotation sideEffectsAnnotation = getSideEffectsAnnotation(psiClass);
    if (sideEffectsAnnotation == null) {
        return null;
    }
    // If @SideEffects declared in class, suggest remove @SideEffects annotation
    if (!psiClass.isInterface()) {
        String message = message("side.effects.annotation.declared.correctly.error.annotation.declared.in.class");
        RemoveSideEffectsAnnotationFix fix = new RemoveSideEffectsAnnotationFix(sideEffectsAnnotation);
        ProblemDescriptor problemDescriptor = manager.createProblemDescriptor(sideEffectsAnnotation, message, fix, GENERIC_ERROR_OR_WARNING);
        return new ProblemDescriptor[] { problemDescriptor };
    }
    // If @SideEffects annotation is empty, ignore
    List<PsiAnnotationMemberValue> sideEffectsAnnotationValue = getSideEffectsAnnotationValue(sideEffectsAnnotation);
    if (sideEffectsAnnotationValue.isEmpty()) {
        return null;
    }
    // If SideEffectOf is not resolved, ignore
    Project project = psiClass.getProject();
    GlobalSearchScope searchScope = determineSearchScope(psiClass);
    PsiClass sideEffectOfClass = Qi4jSideEffectUtil.getGenericSideEffectClass(project, searchScope);
    if (sideEffectOfClass == null) {
        return null;
    }
    List<ProblemDescriptor> problems = new LinkedList<ProblemDescriptor>();
    for (PsiAnnotationMemberValue sideEffectClassReferenceWrapper : sideEffectsAnnotationValue) {
        PsiJavaCodeReferenceElement sideEffectClassReference = getSideEffectClassReference(sideEffectClassReferenceWrapper);
        // If it's not a class reference, ignore
        if (sideEffectClassReference == null) {
            continue;
        }
        // If class reference can't be resolved, ignore
        PsiClass sideEffectClass = (PsiClass) sideEffectClassReference.resolve();
        if (sideEffectClass == null) {
            continue;
        }
        // If side effect class does not inherit SideEffectOf class, suggest remove that reference.
        if (!sideEffectClass.isInheritor(sideEffectOfClass, true)) {
            String message = Qi4jResourceBundle.message("side.effects.annotation.declared.correctly.error.side.effect.does.not.extend.side.effect.of", sideEffectClass.getQualifiedName());
            RemoveAnnotationValueFix fix = new RemoveAnnotationValueFix(sideEffectClassReferenceWrapper, sideEffectClassReference);
            ProblemDescriptor problemDescriptor = manager.createProblemDescriptor(sideEffectClassReferenceWrapper, message, fix, GENERIC_ERROR_OR_WARNING);
            problems.add(problemDescriptor);
        } else {
        // TODO: Test whether it is a generic side effect
        // TODO: Test whether it is a specific side effect
        }
    }
    return problems.toArray(new ProblemDescriptor[problems.size()]);
}
Also used : ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) PsiJavaCodeReferenceElement(com.intellij.psi.PsiJavaCodeReferenceElement) PsiClass(com.intellij.psi.PsiClass) LinkedList(java.util.LinkedList) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiAnnotation(com.intellij.psi.PsiAnnotation) PsiAnnotationMemberValue(com.intellij.psi.PsiAnnotationMemberValue)

Example 3 with PsiJavaCodeReferenceElement

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

the class PsiJavaCodeReferenceCodeFragmentImpl method getReferenceElement.

@Override
public PsiJavaCodeReferenceElement getReferenceElement() {
    final CompositeElement treeElement = calcTreeElement();
    LOG.assertTrue(treeElement.getFirstChildNode().getElementType() == JavaElementType.JAVA_CODE_REFERENCE);
    return (PsiJavaCodeReferenceElement) SourceTreeToPsiMap.treeElementToPsi(treeElement.getFirstChildNode());
}
Also used : PsiJavaCodeReferenceElement(com.intellij.psi.PsiJavaCodeReferenceElement) CompositeElement(com.intellij.psi.impl.source.tree.CompositeElement)

Example 4 with PsiJavaCodeReferenceElement

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

the class GrThrowsClauseImpl method add.

@Override
public PsiElement add(@NotNull PsiElement element) throws IncorrectOperationException {
    if (element instanceof GrCodeReferenceElement || element instanceof PsiJavaCodeReferenceElement) {
        if (findChildByClass(GrCodeReferenceElement.class) == null) {
            getNode().addLeaf(GroovyTokenTypes.kTHROWS, "throws", null);
        } else {
            PsiElement lastChild = getLastChild();
            lastChild = PsiUtil.skipWhitespacesAndComments(lastChild, false);
            if (!lastChild.getNode().getElementType().equals(GroovyTokenTypes.mCOMMA)) {
                getNode().addLeaf(GroovyTokenTypes.mCOMMA, ",", null);
            }
        }
        if (element instanceof PsiJavaCodeReferenceElement) {
            element = GroovyPsiElementFactory.getInstance(getProject()).createCodeReferenceElementFromText(element.getText());
        }
    }
    return super.add(element);
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) PsiJavaCodeReferenceElement(com.intellij.psi.PsiJavaCodeReferenceElement) PsiElement(com.intellij.psi.PsiElement)

Example 5 with PsiJavaCodeReferenceElement

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

the class GrThrowsClauseImpl method getReferenceElements.

@Override
@NotNull
public PsiJavaCodeReferenceElement[] getReferenceElements() {
    PsiClassType[] types = getReferencedTypes();
    if (types.length == 0)
        return PsiJavaCodeReferenceElement.EMPTY_ARRAY;
    PsiManagerEx manager = getManager();
    List<PsiJavaCodeReferenceElement> result = ContainerUtil.newArrayList();
    for (PsiClassType type : types) {
        PsiClassType.ClassResolveResult resolveResult = type.resolveGenerics();
        PsiClass resolved = resolveResult.getElement();
        if (resolved != null) {
            result.add(new LightClassReference(manager, type.getCanonicalText(), resolved, resolveResult.getSubstitutor()));
        }
    }
    return result.toArray(new PsiJavaCodeReferenceElement[result.size()]);
}
Also used : PsiClassType(com.intellij.psi.PsiClassType) LightClassReference(com.intellij.psi.impl.light.LightClassReference) PsiJavaCodeReferenceElement(com.intellij.psi.PsiJavaCodeReferenceElement) PsiClass(com.intellij.psi.PsiClass) PsiManagerEx(com.intellij.psi.impl.PsiManagerEx) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiJavaCodeReferenceElement (com.intellij.psi.PsiJavaCodeReferenceElement)14 PsiClass (com.intellij.psi.PsiClass)7 PsiElement (com.intellij.psi.PsiElement)4 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)3 PsiAnnotationMemberValue (com.intellij.psi.PsiAnnotationMemberValue)3 LinkedList (java.util.LinkedList)3 Nullable (org.jetbrains.annotations.Nullable)3 Project (com.intellij.openapi.project.Project)2 PsiAnnotation (com.intellij.psi.PsiAnnotation)2 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 Result (com.intellij.openapi.application.Result)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 PsiClassType (com.intellij.psi.PsiClassType)1 PsiImportStaticStatement (com.intellij.psi.PsiImportStaticStatement)1 PsiJavaFile (com.intellij.psi.PsiJavaFile)1 PsiManagerEx (com.intellij.psi.impl.PsiManagerEx)1 PsiImportStatementStub (com.intellij.psi.impl.java.stubs.PsiImportStatementStub)1 LightClassReference (com.intellij.psi.impl.light.LightClassReference)1 PsiJavaCodeReferenceElementImpl (com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl)1