Search in sources :

Example 11 with PsiInvalidElementAccessException

use of com.intellij.psi.PsiInvalidElementAccessException in project google-cloud-intellij by GoogleCloudPlatform.

the class ApiParameterInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new EndpointPsiElementVisitor() {

        @Override
        public void visitParameter(PsiParameter psiParameter) {
            if (!EndpointUtilities.isEndpointClass(psiParameter)) {
                return;
            }
            // Check if method is public or non-static
            PsiElement psiElement = psiParameter.getDeclarationScope();
            if (psiElement instanceof PsiMethod) {
                if (!EndpointUtilities.isApiMethod((PsiMethod) psiElement)) {
                    return;
                }
                if (((PsiMethod) psiElement).isConstructor()) {
                    return;
                }
            } else {
                return;
            }
            Project project;
            try {
                project = psiParameter.getContainingFile().getProject();
                if (project == null) {
                    return;
                }
            } catch (PsiInvalidElementAccessException ex) {
                LOG.error("Cannot determine project with parameter " + psiParameter.getText(), ex);
                return;
            }
            // Check if parameter is an API Parameter
            PsiType psiType = psiParameter.getType();
            if (!isApiParameter(psiType, project)) {
                return;
            }
            // Check that API parameter has Named Resource (@Named)
            if (!hasParameterName(psiParameter)) {
                holder.registerProblem(psiParameter, "Missing parameter name. Parameter type (" + psiType.getPresentableText() + ") is not an entity type and thus should be annotated with @Named.", new MyQuickFix());
            }
        }
    };
}
Also used : PsiInvalidElementAccessException(com.intellij.psi.PsiInvalidElementAccessException) Project(com.intellij.openapi.project.Project) PsiParameter(com.intellij.psi.PsiParameter) PsiMethod(com.intellij.psi.PsiMethod) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiInvalidElementAccessException (com.intellij.psi.PsiInvalidElementAccessException)11 NotNull (org.jetbrains.annotations.NotNull)8 Project (com.intellij.openapi.project.Project)6 PsiElement (com.intellij.psi.PsiElement)4 PsiMethod (com.intellij.psi.PsiMethod)4 PsiParameter (com.intellij.psi.PsiParameter)3 PsiType (com.intellij.psi.PsiType)3 PsiFile (com.intellij.psi.PsiFile)2 TestStateStorage (com.intellij.execution.TestStateStorage)1 TestStateInfo (com.intellij.execution.testframework.sm.runner.states.TestStateInfo)1 Document (com.intellij.openapi.editor.Document)1 PsiAnnotation (com.intellij.psi.PsiAnnotation)1 PsiArrayType (com.intellij.psi.PsiArrayType)1 PsiDirectory (com.intellij.psi.PsiDirectory)1 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)1 PsiModifierList (com.intellij.psi.PsiModifierList)1 PsiNamedElement (com.intellij.psi.PsiNamedElement)1 PsiManagerEx (com.intellij.psi.impl.PsiManagerEx)1 LightElement (com.intellij.psi.impl.light.LightElement)1 PsiFileImpl (com.intellij.psi.impl.source.PsiFileImpl)1