Search in sources :

Example 1 with JavaRecursiveElementWalkingVisitor

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

the class AppEngineForbiddenCodeInspection method checkFile.

@Override
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull final InspectionManager manager, final boolean isOnTheFly) {
    final Project project = manager.getProject();
    Module module = ModuleUtilCore.findModuleForPsiElement(file);
    final AppEngineStandardFacet appEngineStandardFacet = AppEngineStandardFacet.getAppEngineFacetByModule(module);
    if (appEngineStandardFacet == null) {
        return null;
    }
    if (appEngineStandardFacet.getRuntimeJavaVersion().atLeast(JavaVersion.JAVA_1_8)) {
        return null;
    }
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    final List<ProblemDescriptor> problems = new ArrayList<ProblemDescriptor>();
    file.accept(new JavaRecursiveElementWalkingVisitor() {

        CloudSdkInternals sdkInternals = CloudSdkInternals.getInstance();

        @Override
        public void visitDocComment(PsiDocComment comment) {
        }

        @Override
        public void visitMethod(PsiMethod method) {
            final PsiModifierList modifierList = method.getModifierList();
            if (modifierList.hasModifierProperty(PsiModifier.NATIVE)) {
                if (!isNativeMethodAllowed(method)) {
                    problems.add(manager.createProblemDescriptor(modifierList, "Native methods aren't allowed in App Engine application", isOnTheFly, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
                }
            }
            super.visitMethod(method);
        }

        @Override
        public void visitNewExpression(PsiNewExpression expression) {
            final PsiJavaCodeReferenceElement classReference = expression.getClassReference();
            if (classReference != null) {
                final PsiElement resolved = classReference.resolve();
                if (resolved instanceof PsiClass) {
                    final String qualifiedName = ((PsiClass) resolved).getQualifiedName();
                    if (qualifiedName != null && sdkInternals.isMethodInBlacklist(qualifiedName, "new")) {
                        final String message = "App Engine application should not create new instances of '" + qualifiedName + "' class";
                        problems.add(manager.createProblemDescriptor(classReference, message, isOnTheFly, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
                    }
                }
            }
            super.visitNewExpression(expression);
        }

        @Override
        public void visitMethodCallExpression(PsiMethodCallExpression expression) {
            final PsiReferenceExpression methodExpression = expression.getMethodExpression();
            final PsiElement element = methodExpression.resolve();
            if (element instanceof PsiMethod) {
                final PsiMethod method = (PsiMethod) element;
                final PsiClass psiClass = method.getContainingClass();
                if (psiClass != null) {
                    final String qualifiedName = psiClass.getQualifiedName();
                    final String methodName = method.getName();
                    if (qualifiedName != null && sdkInternals.isMethodInBlacklist(qualifiedName, methodName)) {
                        final String message = "AppEngine application should not call '" + StringUtil.getShortName(qualifiedName) + "" + methodName + "' method";
                        problems.add(manager.createProblemDescriptor(methodExpression, message, isOnTheFly, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
                    }
                }
            }
            super.visitMethodCallExpression(expression);
        }

        @Override
        public void visitReferenceElement(PsiJavaCodeReferenceElement reference) {
            final PsiElement resolved = reference.resolve();
            if (resolved instanceof PsiClass) {
                final PsiFile psiFile = resolved.getContainingFile();
                if (psiFile != null) {
                    final VirtualFile virtualFile = psiFile.getVirtualFile();
                    if (virtualFile != null && !fileIndex.isInSource(virtualFile)) {
                        final List<OrderEntry> list = fileIndex.getOrderEntriesForFile(virtualFile);
                        for (OrderEntry entry : list) {
                            if (entry instanceof JdkOrderEntry) {
                                final String className = ClassUtil.getJVMClassName((PsiClass) resolved);
                                if (className != null && !sdkInternals.isClassInWhiteList(className)) {
                                    problems.add(manager.createProblemDescriptor(reference, "Class '" + className + "' is not included in App Engine JRE White List", isOnTheFly, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
                                }
                            }
                        }
                    }
                }
            }
            super.visitReferenceElement(reference);
        }
    });
    return problems.toArray(new ProblemDescriptor[problems.size()]);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiMethod(com.intellij.psi.PsiMethod) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) PsiJavaCodeReferenceElement(com.intellij.psi.PsiJavaCodeReferenceElement) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression) ArrayList(java.util.ArrayList) CloudSdkInternals(com.google.cloud.tools.intellij.appengine.sdk.CloudSdkInternals) JdkOrderEntry(com.intellij.openapi.roots.JdkOrderEntry) OrderEntry(com.intellij.openapi.roots.OrderEntry) PsiFile(com.intellij.psi.PsiFile) ArrayList(java.util.ArrayList) PsiModifierList(com.intellij.psi.PsiModifierList) List(java.util.List) PsiElement(com.intellij.psi.PsiElement) JavaRecursiveElementWalkingVisitor(com.intellij.psi.JavaRecursiveElementWalkingVisitor) PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) JdkOrderEntry(com.intellij.openapi.roots.JdkOrderEntry) PsiClass(com.intellij.psi.PsiClass) PsiNewExpression(com.intellij.psi.PsiNewExpression) PsiModifierList(com.intellij.psi.PsiModifierList) PsiMethodCallExpression(com.intellij.psi.PsiMethodCallExpression) Project(com.intellij.openapi.project.Project) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) AppEngineStandardFacet(com.google.cloud.tools.intellij.appengine.facet.standard.AppEngineStandardFacet) Module(com.intellij.openapi.module.Module)

Example 2 with JavaRecursiveElementWalkingVisitor

use of com.intellij.psi.JavaRecursiveElementWalkingVisitor in project component-runtime by Talend.

the class SuggestionServiceImpl method getFamilyFromPackageInfo.

private String getFamilyFromPackageInfo(final PsiPackage psiPackage, final Module module) {
    return of(FilenameIndex.getFilesByName(psiPackage.getProject(), "package-info.java", GlobalSearchScope.moduleScope(module))).map(psiFile -> {
        if (!PsiJavaFile.class.cast(psiFile).getPackageName().equals(psiPackage.getQualifiedName())) {
            return null;
        }
        final String[] family = { null };
        PsiJavaFile.class.cast(psiFile).accept(new JavaRecursiveElementWalkingVisitor() {

            @Override
            public void visitAnnotation(final PsiAnnotation annotation) {
                super.visitAnnotation(annotation);
                if (!COMPONENTS.equals(annotation.getQualifiedName())) {
                    return;
                }
                final PsiAnnotationMemberValue familyAttribute = annotation.findAttributeValue("family");
                if (familyAttribute == null) {
                    return;
                }
                family[0] = removeQuotes(familyAttribute.getText());
            }
        });
        return family[0];
    }).filter(Objects::nonNull).findFirst().orElseGet(() -> {
        final PsiPackage parent = psiPackage.getParentPackage();
        if (parent == null) {
            return null;
        }
        return getFamilyFromPackageInfo(parent, module);
    });
}
Also used : PsiPackage(com.intellij.psi.PsiPackage) PsiType(com.intellij.psi.PsiType) JavaPsiFacade(com.intellij.psi.JavaPsiFacade) FilenameIndex(com.intellij.psi.search.FilenameIndex) DISPLAY_NAME(org.talend.sdk.component.intellij.completion.properties.Suggestion.DISPLAY_NAME) CompletionParameters(com.intellij.codeInsight.completion.CompletionParameters) Collections.singletonList(java.util.Collections.singletonList) PsiClass(com.intellij.psi.PsiClass) PsiEnumConstant(com.intellij.psi.PsiEnumConstant) PsiClassReferenceType(com.intellij.psi.impl.source.PsiClassReferenceType) Project(com.intellij.openapi.project.Project) JavaRecursiveElementWalkingVisitor(com.intellij.psi.JavaRecursiveElementWalkingVisitor) ROOT(java.util.Locale.ROOT) Module(com.intellij.openapi.module.Module) PLACEHOLDER(org.talend.sdk.component.intellij.completion.properties.Suggestion.PLACEHOLDER) Suggestion(org.talend.sdk.component.intellij.completion.properties.Suggestion) LookupElement(com.intellij.codeInsight.lookup.LookupElement) Collections.emptyList(java.util.Collections.emptyList) Optional.ofNullable(java.util.Optional.ofNullable) Stream.of(java.util.stream.Stream.of) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiJavaFile(com.intellij.psi.PsiJavaFile) Objects(java.util.Objects) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Stream(java.util.stream.Stream) AnnotationUtil(com.intellij.codeInsight.AnnotationUtil) PsiAnnotation(com.intellij.psi.PsiAnnotation) JvmModifier(com.intellij.lang.jvm.JvmModifier) Collections(java.util.Collections) PsiAnnotationMemberValue(com.intellij.psi.PsiAnnotationMemberValue) Objects(java.util.Objects) PsiPackage(com.intellij.psi.PsiPackage) PsiAnnotation(com.intellij.psi.PsiAnnotation) JavaRecursiveElementWalkingVisitor(com.intellij.psi.JavaRecursiveElementWalkingVisitor) PsiAnnotationMemberValue(com.intellij.psi.PsiAnnotationMemberValue)

Aggregations

Module (com.intellij.openapi.module.Module)2 Project (com.intellij.openapi.project.Project)2 JavaRecursiveElementWalkingVisitor (com.intellij.psi.JavaRecursiveElementWalkingVisitor)2 PsiClass (com.intellij.psi.PsiClass)2 List (java.util.List)2 AppEngineStandardFacet (com.google.cloud.tools.intellij.appengine.facet.standard.AppEngineStandardFacet)1 CloudSdkInternals (com.google.cloud.tools.intellij.appengine.sdk.CloudSdkInternals)1 AnnotationUtil (com.intellij.codeInsight.AnnotationUtil)1 CompletionParameters (com.intellij.codeInsight.completion.CompletionParameters)1 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)1 JvmModifier (com.intellij.lang.jvm.JvmModifier)1 JdkOrderEntry (com.intellij.openapi.roots.JdkOrderEntry)1 OrderEntry (com.intellij.openapi.roots.OrderEntry)1 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 JavaPsiFacade (com.intellij.psi.JavaPsiFacade)1 PsiAnnotation (com.intellij.psi.PsiAnnotation)1 PsiAnnotationMemberValue (com.intellij.psi.PsiAnnotationMemberValue)1 PsiElement (com.intellij.psi.PsiElement)1