<commit_before>/*
 * Copyright 2003-2006 <NAME>, <NAME>
 *
 * 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.siyeh.ig.initialization;

import com.intellij.codeInsight.daemon.GroupNames;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.ExpressionInspection;
import com.siyeh.InspectionGadgetsBundle;
import org.jetbrains.annotations.NotNull;

public class NonFinalStaticVariableUsedInClassInitializationInspection
        extends ExpressionInspection{

    public String getDisplayName(){
        return InspectionGadgetsBundle.message(
                "non.final.static.variable.initialization.display.name");
    }

    public String getGroupDisplayName(){
        return GroupNames.INITIALIZATION_GROUP_NAME;
    }

    @NotNull
    public String buildErrorString(Object... infos){
        return InspectionGadgetsBundle.message(
                "non.final.static.variable.initialization.problem.descriptor");
    }

    public BaseInspectionVisitor buildVisitor(){
        return new NonFinalStaticVariableUsedInClassInitializationVisitor();
    }

    private static class NonFinalStaticVariableUsedInClassInitializationVisitor
            extends BaseInspectionVisitor{

        public void visitReferenceExpression(PsiReferenceExpression expression){
            super.visitReferenceExpression(expression);
            if(!isInClassInitialization(expression)){
                return;
            }
            final PsiElement referent = expression.resolve();
            if(!(referent instanceof PsiField)){
                return;
            }
            final PsiField field = (PsiField) referent;
            if(!field.hasModifierProperty(PsiModifier.STATIC)){
                return;
            }
            if(field.hasModifierProperty(PsiModifier.FINAL)){
                return;
            }
            registerError(expression);
        }

        private static boolean isInClassInitialization(
                PsiExpression expression){
            final PsiClassInitializer initializer =
                    PsiTreeUtil.getParentOfType(expression,
                                                PsiClassInitializer.class);
            if(initializer != null &&
               initializer.hasModifierProperty(PsiModifier.STATIC)){
                if(!PsiUtil.isOnAssignmentLeftHand(expression)){
                    return true;
                }
            }
            final PsiField field =
                    PsiTreeUtil.getParentOfType(expression, PsiField.class);
            return field != null &&
                   field.hasModifierProperty(PsiModifier.STATIC);
        }
    }
}<commit_msg>IDEADEV-5254<commit_after>/*
 * Copyright 2003-2006 <NAME>, <NAME>
 *
 * 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.siyeh.ig.initialization;

import com.intellij.codeInsight.daemon.GroupNames;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import org.jetbrains.annotations.NotNull;

public class NonFinalStaticVariableUsedInClassInitializationInspection
        extends BaseInspection {

    public String getDisplayName(){
        return InspectionGadgetsBundle.message(
                "non.final.static.variable.initialization.display.name");
    }

    public String getGroupDisplayName(){
        return GroupNames.INITIALIZATION_GROUP_NAME;
    }

    @NotNull
    public String buildErrorString(Object... infos){
        return InspectionGadgetsBundle.message(
                "non.final.static.variable.initialization.problem.descriptor");
    }

    public BaseInspectionVisitor buildVisitor(){
        return new NonFinalStaticVariableUsedInClassInitializationVisitor();
    }

    private static class NonFinalStaticVariableUsedInClassInitializationVisitor
            extends BaseInspectionVisitor{

        public void visitReferenceExpression(PsiReferenceExpression expression){
            super.visitReferenceExpression(expression);
            if(!isInClassInitialization(expression)){
                return;
            }
            final PsiElement referent = expression.resolve();
            if(!(referent instanceof PsiField)){
                return;
            }
            final PsiField field = (PsiField) referent;
            if(!field.hasModifierProperty(PsiModifier.STATIC)){
                return;
            }
            if(field.hasModifierProperty(PsiModifier.FINAL)){
                return;
            }
            registerError(expression);
        }

        private static boolean isInClassInitialization(
                PsiExpression expression){
            final PsiClass expressionClass =
                    PsiTreeUtil.getParentOfType(expression, PsiClass.class);
            final PsiMember member =
                    PsiTreeUtil.getParentOfType(expression,
                            PsiClassInitializer.class, PsiField.class);
            if (member == null) {
                return false;
            }
            final PsiClass memberClass = member.getContainingClass();
            if (!memberClass.equals(expressionClass)) {
                return false;
            }
            if (!member.hasModifierProperty(PsiModifier.STATIC)) {
                return false;
            }
            if (member instanceof PsiClassInitializer) {
                return !PsiUtil.isOnAssignmentLeftHand(expression);
            }
            return true;
        }
    }
}
{'fraction_non_alphanumeric': 0.07525757802000896, 'fraction_numerical': 0.004180976556667164, 'mean_word_length': 2.4383983572895276, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '146755', 'n_tokens_mistral': 1684, 'n_tokens_neox': 1628, 'n_words': 392}