enhanced API for nullness
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInspection / concurrencyAnnotations / StaticGuardedByInstanceInspection.java
blob41623606c6ee6d3c200a9d7acedd430550596aca
1 /*
2 * Copyright 2000-2009 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.intellij.codeInspection.concurrencyAnnotations;
18 import com.intellij.codeInsight.daemon.GroupNames;
19 import com.intellij.codeInspection.BaseJavaLocalInspectionTool;
20 import com.intellij.codeInspection.ProblemsHolder;
21 import com.intellij.psi.*;
22 import com.intellij.psi.javadoc.PsiDocTag;
23 import com.intellij.psi.util.PsiTreeUtil;
24 import org.jetbrains.annotations.Nls;
25 import org.jetbrains.annotations.NotNull;
27 public class StaticGuardedByInstanceInspection extends BaseJavaLocalInspectionTool {
29 @NotNull
30 public String getGroupDisplayName() {
31 return GroupNames.CONCURRENCY_ANNOTATION_ISSUES;
34 @Nls
35 @NotNull
36 public String getDisplayName() {
37 return "Static member guarded by instance field or this";
40 @NotNull
41 public String getShortName() {
42 return "StaticGuardedByInstance";
45 @NotNull
46 public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
47 return new Visitor(holder);
50 private static class Visitor extends JavaElementVisitor {
51 private final ProblemsHolder myHolder;
53 public Visitor(ProblemsHolder holder) {
54 myHolder = holder;
57 public void visitAnnotation(PsiAnnotation annotation) {
58 super.visitAnnotation(annotation);
59 if (!JCiPUtil.isGuardedByAnnotation(annotation)) {
60 return;
62 final PsiMember member = PsiTreeUtil.getParentOfType(annotation, PsiMember.class);
63 if (member == null) {
64 return;
66 if (!member.hasModifierProperty(PsiModifier.STATIC)) {
67 return;
69 final String guardValue = JCiPUtil.getGuardValue(annotation);
70 if (guardValue == null) {
71 return;
74 final PsiAnnotationMemberValue guardRef = annotation.findAttributeValue("value");
75 if (guardRef == null) {
76 return;
78 if ("this".equals(guardValue)) {
79 registerError(guardRef);
81 final PsiClass containingClass = PsiTreeUtil.getParentOfType(annotation, PsiClass.class);
82 if (containingClass == null) {
83 return;
85 final PsiField guardField = containingClass.findFieldByName(guardValue, true);
86 if (guardField == null) {
87 return;
89 if (guardField.hasModifierProperty(PsiModifier.STATIC)) {
90 return;
92 registerError(guardRef);
95 public void visitDocTag(PsiDocTag psiDocTag) {
96 super.visitDocTag(psiDocTag);
97 if (!JCiPUtil.isGuardedByTag(psiDocTag)) {
98 return;
100 final PsiMember member = PsiTreeUtil.getParentOfType(psiDocTag, PsiMember.class);
101 if (member == null) {
102 return;
104 if (!member.hasModifierProperty(PsiModifier.STATIC)) {
105 return;
107 final String guardValue = JCiPUtil.getGuardValue(psiDocTag);
109 if ("this".equals(guardValue)) {
110 registerError(psiDocTag);
112 final PsiClass containingClass = PsiTreeUtil.getParentOfType(psiDocTag, PsiClass.class);
113 if (containingClass == null) {
114 return;
116 final PsiField guardField = containingClass.findFieldByName(guardValue, true);
117 if (guardField == null) {
118 return;
120 if (guardField.hasModifierProperty(PsiModifier.STATIC)) {
121 return;
123 myHolder.registerProblem(psiDocTag, "Static member guarded by instance \"" + guardValue + "\" #loc");
126 private void registerError(PsiElement element) {
127 myHolder.registerProblem(element, "Static member guarded by instance #ref #loc");
130 public void visitReferenceExpression(PsiReferenceExpression expression) {