blob: d267fcd9cec7204446ba71a53222a3107e0dbd85 [file] [log] [blame]
/*
* Copyright 2000-2009 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInspection.concurrencyAnnotations;
import com.intellij.codeInsight.daemon.GroupNames;
import com.intellij.codeInspection.BaseJavaBatchLocalInspectionTool;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.*;
import com.intellij.psi.javadoc.PsiDocTag;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
public class StaticGuardedByInstanceInspection extends BaseJavaBatchLocalInspectionTool {
@Override
@NotNull
public String getGroupDisplayName() {
return GroupNames.CONCURRENCY_ANNOTATION_ISSUES;
}
@Override
@Nls
@NotNull
public String getDisplayName() {
return "Static member guarded by instance field or this";
}
@Override
@NotNull
public String getShortName() {
return "StaticGuardedByInstance";
}
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
return new Visitor(holder);
}
private static class Visitor extends JavaElementVisitor {
private final ProblemsHolder myHolder;
public Visitor(ProblemsHolder holder) {
myHolder = holder;
}
@Override
public void visitAnnotation(PsiAnnotation annotation) {
super.visitAnnotation(annotation);
if (!JCiPUtil.isGuardedByAnnotation(annotation)) {
return;
}
final PsiMember member = PsiTreeUtil.getParentOfType(annotation, PsiMember.class);
if (member == null) {
return;
}
if (!member.hasModifierProperty(PsiModifier.STATIC)) {
return;
}
final String guardValue = JCiPUtil.getGuardValue(annotation);
if (guardValue == null) {
return;
}
final PsiAnnotationMemberValue guardRef = annotation.findAttributeValue("value");
if (guardRef == null) {
return;
}
if ("this".equals(guardValue)) {
registerError(guardRef);
}
final PsiClass containingClass = PsiTreeUtil.getParentOfType(annotation, PsiClass.class);
if (containingClass == null) {
return;
}
final PsiField guardField = containingClass.findFieldByName(guardValue, true);
if (guardField == null) {
return;
}
if (guardField.hasModifierProperty(PsiModifier.STATIC)) {
return;
}
registerError(guardRef);
}
@Override
public void visitDocTag(PsiDocTag psiDocTag) {
super.visitDocTag(psiDocTag);
if (!JCiPUtil.isGuardedByTag(psiDocTag)) {
return;
}
final PsiMember member = PsiTreeUtil.getParentOfType(psiDocTag, PsiMember.class);
if (member == null) {
return;
}
if (!member.hasModifierProperty(PsiModifier.STATIC)) {
return;
}
final String guardValue = JCiPUtil.getGuardValue(psiDocTag);
if ("this".equals(guardValue)) {
registerError(psiDocTag);
}
final PsiClass containingClass = PsiTreeUtil.getParentOfType(psiDocTag, PsiClass.class);
if (containingClass == null) {
return;
}
final PsiField guardField = containingClass.findFieldByName(guardValue, true);
if (guardField == null) {
return;
}
if (guardField.hasModifierProperty(PsiModifier.STATIC)) {
return;
}
myHolder.registerProblem(psiDocTag, "Static member guarded by instance \"" + guardValue + "\" #loc");
}
private void registerError(PsiElement element) {
myHolder.registerProblem(element, "Static member guarded by instance #ref #loc");
}
}
}