2 * Copyright 2000-2014 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.jetbrains.python.inspections;
18 import com.intellij.codeInspection.LocalInspectionToolSession;
19 import com.intellij.codeInspection.LocalQuickFix;
20 import com.intellij.codeInspection.ProblemHighlightType;
21 import com.intellij.codeInspection.ProblemsHolder;
22 import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel;
23 import com.intellij.openapi.util.text.StringUtil;
24 import com.intellij.psi.PsiElement;
25 import com.intellij.psi.PsiElementVisitor;
26 import com.intellij.psi.PsiReference;
27 import com.jetbrains.python.PyBundle;
28 import com.jetbrains.python.PyNames;
29 import com.jetbrains.python.codeInsight.controlflow.ScopeOwner;
30 import com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil;
31 import com.jetbrains.python.codeInsight.stdlib.PyNamedTupleType;
32 import com.jetbrains.python.inspections.quickfix.PyAddPropertyForFieldQuickFix;
33 import com.jetbrains.python.inspections.quickfix.PyMakePublicQuickFix;
34 import com.jetbrains.python.inspections.quickfix.PyRenameElementQuickFix;
35 import com.jetbrains.python.psi.*;
36 import com.jetbrains.python.psi.types.PyModuleType;
37 import com.jetbrains.python.psi.types.PyType;
38 import com.jetbrains.python.refactoring.PyRefactoringUtil;
39 import com.jetbrains.python.testing.pytest.PyTestUtil;
40 import org.jetbrains.annotations.Nls;
41 import org.jetbrains.annotations.NotNull;
42 import org.jetbrains.annotations.Nullable;
45 import java.util.ArrayList;
46 import java.util.Collection;
47 import java.util.List;
52 * Inspection to detect situations, where
53 * protected member (i.e. class member with a name beginning with an underscore)
54 * is access outside the class or a descendant of the class where it's defined.
56 public class PyProtectedMemberInspection extends PyInspection {
57 public boolean ignoreTestFunctions = true;
62 public String getDisplayName() {
63 return PyBundle.message("INSP.NAME.protected.member.access");
68 public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder,
70 @NotNull LocalInspectionToolSession session) {
71 return new Visitor(holder, session);
75 private class Visitor extends PyInspectionVisitor {
76 public Visitor(@Nullable ProblemsHolder holder, @NotNull LocalInspectionToolSession session) {
77 super(holder, session);
81 public void visitPyImportElement(PyImportElement node) {
82 final PyStatement statement = node.getContainingImportStatement();
83 if (!(statement instanceof PyFromImportStatement)) return;
84 final PyReferenceExpression importReferenceExpression = node.getImportReferenceExpression();
85 final PyReferenceExpression importSource = ((PyFromImportStatement)statement).getImportSource();
86 if (importReferenceExpression != null && importSource != null)
87 checkReference(importReferenceExpression, importSource);
91 public void visitPyReferenceExpression(PyReferenceExpression node) {
92 final PyExpression qualifier = node.getQualifier();
93 if (qualifier == null || PyNames.CANONICAL_SELF.equals(qualifier.getText())) return;
94 checkReference(node, qualifier);
97 private void checkReference(@NotNull final PyReferenceExpression node, @NotNull final PyExpression qualifier) {
98 if (myTypeEvalContext.getType(qualifier) instanceof PyNamedTupleType) return;
99 final String name = node.getName();
100 final List<LocalQuickFix> quickFixes = new ArrayList<LocalQuickFix>();
101 quickFixes.add(new PyRenameElementQuickFix());
103 if (name != null && name.startsWith("_") && !name.startsWith("__") && !name.endsWith("__")) {
104 final PsiReference reference = node.getReference(getResolveContext());
105 if (reference == null) return;
106 for (final PyInspectionExtension inspectionExtension : PyInspectionExtension.EP_NAME.getExtensions()) {
107 if (inspectionExtension.ignoreProtectedSymbol(node, myTypeEvalContext)) {
111 final PsiElement resolvedExpression = reference.resolve();
112 final PyClass resolvedClass = getClassOwner(resolvedExpression);
113 if (resolvedExpression instanceof PyTargetExpression) {
114 final String newName = StringUtil.trimLeading(name, '_');
115 if (resolvedClass != null) {
116 final String qFixName = resolvedClass.getProperties().containsKey(newName) ?
117 PyBundle.message("QFIX.use.property") : PyBundle.message("QFIX.add.property");
118 quickFixes.add(new PyAddPropertyForFieldQuickFix(qFixName));
120 final Collection<String> usedNames = PyRefactoringUtil.collectUsedNames(resolvedClass);
121 if (!usedNames.contains(newName)) {
122 quickFixes.add(new PyMakePublicQuickFix());
127 final PyClass parentClass = getClassOwner(node);
128 if (parentClass != null) {
129 if (PyTestUtil.isPyTestClass(parentClass) && ignoreTestFunctions) return;
131 if (parentClass.isSubclass(resolvedClass, null))
134 PyClass outerClass = getClassOwner(parentClass);
135 while (outerClass != null) {
136 if (outerClass.isSubclass(resolvedClass, null))
139 outerClass = getClassOwner(outerClass);
142 final PyType type = myTypeEvalContext.getType(qualifier);
143 final String bundleKey = type instanceof PyModuleType ? "INSP.protected.member.$0.access.module" : "INSP.protected.member.$0.access";
144 registerProblem(node, PyBundle.message(bundleKey, name), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, null, quickFixes.toArray(new LocalQuickFix[quickFixes.size()-1]));
149 private PyClass getClassOwner(@Nullable PsiElement element) {
150 for (ScopeOwner owner = ScopeUtil.getScopeOwner(element); owner != null; owner = ScopeUtil.getScopeOwner(owner)) {
151 if (owner instanceof PyClass) {
152 return (PyClass)owner;
161 public JComponent createOptionsPanel() {
162 MultipleCheckboxOptionsPanel panel = new MultipleCheckboxOptionsPanel(this);
163 panel.addCheckbox("Ignore test functions", "ignoreTestFunctions");