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
{
30 public String
getGroupDisplayName() {
31 return GroupNames
.CONCURRENCY_ANNOTATION_ISSUES
;
36 public String
getDisplayName() {
37 return "Static member guarded by instance field or this";
41 public String
getShortName() {
42 return "StaticGuardedByInstance";
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
) {
57 public void visitAnnotation(PsiAnnotation annotation
) {
58 super.visitAnnotation(annotation
);
59 if (!JCiPUtil
.isGuardedByAnnotation(annotation
)) {
62 final PsiMember member
= PsiTreeUtil
.getParentOfType(annotation
, PsiMember
.class);
66 if (!member
.hasModifierProperty(PsiModifier
.STATIC
)) {
69 final String guardValue
= JCiPUtil
.getGuardValue(annotation
);
70 if (guardValue
== null) {
74 final PsiAnnotationMemberValue guardRef
= annotation
.findAttributeValue("value");
75 if (guardRef
== null) {
78 if ("this".equals(guardValue
)) {
79 registerError(guardRef
);
81 final PsiClass containingClass
= PsiTreeUtil
.getParentOfType(annotation
, PsiClass
.class);
82 if (containingClass
== null) {
85 final PsiField guardField
= containingClass
.findFieldByName(guardValue
, true);
86 if (guardField
== null) {
89 if (guardField
.hasModifierProperty(PsiModifier
.STATIC
)) {
92 registerError(guardRef
);
95 public void visitDocTag(PsiDocTag psiDocTag
) {
96 super.visitDocTag(psiDocTag
);
97 if (!JCiPUtil
.isGuardedByTag(psiDocTag
)) {
100 final PsiMember member
= PsiTreeUtil
.getParentOfType(psiDocTag
, PsiMember
.class);
101 if (member
== null) {
104 if (!member
.hasModifierProperty(PsiModifier
.STATIC
)) {
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) {
116 final PsiField guardField
= containingClass
.findFieldByName(guardValue
, true);
117 if (guardField
== null) {
120 if (guardField
.hasModifierProperty(PsiModifier
.STATIC
)) {
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
) {