fully qualified class name should not be treated as instance access
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInspection / accessStaticViaInstance / AccessStaticViaInstance.java
blob76795f29023a08185516f18dd206616d0b92b338
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.accessStaticViaInstance;
18 import com.intellij.codeInsight.daemon.JavaErrorMessages;
19 import com.intellij.codeInsight.daemon.impl.analysis.HighlightMessageUtil;
20 import com.intellij.codeInsight.daemon.impl.analysis.HighlightUtil;
21 import com.intellij.codeInsight.daemon.impl.quickfix.AccessStaticViaInstanceFix;
22 import com.intellij.codeInspection.BaseJavaLocalInspectionTool;
23 import com.intellij.codeInspection.InspectionsBundle;
24 import com.intellij.codeInspection.ProblemsHolder;
25 import com.intellij.psi.*;
26 import org.jetbrains.annotations.NonNls;
27 import org.jetbrains.annotations.NotNull;
29 /**
30 * User: anna
31 * Date: 15-Nov-2005
33 public class AccessStaticViaInstance extends BaseJavaLocalInspectionTool {
34 @NotNull
35 public String getGroupDisplayName() {
36 return "";
39 @NotNull
40 public String getDisplayName() {
41 return InspectionsBundle.message("access.static.via.instance");
44 @NotNull
45 @NonNls
46 public String getShortName() {
47 return "AccessStaticViaInstance";
50 public boolean isEnabledByDefault() {
51 return true;
54 @NotNull
55 public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
56 return new JavaElementVisitor() {
57 @Override public void visitReferenceExpression(PsiReferenceExpression expression) {
58 checkAccessStaticMemberViaInstanceReference(expression, holder);
63 private static void checkAccessStaticMemberViaInstanceReference(PsiReferenceExpression expr, ProblemsHolder holder) {
64 JavaResolveResult result = expr.advancedResolve(false);
65 PsiElement resolved = result.getElement();
67 if (!(resolved instanceof PsiMember)) return;
68 PsiExpression qualifierExpression = expr.getQualifierExpression();
69 if (qualifierExpression == null) return;
71 if (qualifierExpression instanceof PsiReferenceExpression) {
72 final PsiElement qualifierResolved = ((PsiReferenceExpression)qualifierExpression).resolve();
73 if (qualifierResolved instanceof PsiClass || qualifierResolved instanceof PsiPackage) {
74 return;
77 if (!((PsiMember)resolved).hasModifierProperty(PsiModifier.STATIC)) return;
79 String description = JavaErrorMessages.message("static.member.accessed.via.instance.reference",
80 HighlightUtil.formatType(qualifierExpression.getType()),
81 HighlightMessageUtil.getSymbolName(resolved, result.getSubstitutor()));
82 holder.registerProblem(expr, description, new AccessStaticViaInstanceFix(expr, result));