<commit_before>package nl.han.ica.core.strategies.solvers;

import japa.parser.ASTHelper;
import japa.parser.ast.body.BodyDeclaration;
import japa.parser.ast.body.ModifierSet;
import japa.parser.ast.body.VariableDeclarator;
import japa.parser.ast.body.VariableDeclaratorId;
import japa.parser.ast.expr.IntegerLiteralExpr;
import japa.parser.ast.visitor.VoidVisitorAdapter;
import net.sourceforge.pmd.RuleViolation;
import nl.han.ica.core.ast.ASTStrategyHelper;
import nl.han.ica.core.ast.FieldDeclarationVisitor;


public class ReplaceMagicNumberSolver extends StrategySolver {

    private String replaceName = "MAGIC";

    public ReplaceMagicNumberSolver(RuleViolation ruleViolation) {
        super(ruleViolation);
    }

    @Override
    public void rewriteAST() {
        System.out.println(ruleViolation.toString());
        ReplaceMagicNumberLiteralExprVisitor expressionVisitor = new ReplaceMagicNumberLiteralExprVisitor();
        compilationUnit.accept(expressionVisitor, ruleViolation);

        IntegerLiteralExpr literalExpr = expressionVisitor.getIntegerLiteralViolationExpr();
        if (literalExpr != null) {
            addStaticFinalField(literalExpr.getValue());
            literalExpr.setValue(replaceName + literalExpr.getValue());
        }
    }

    public void setReplaceName(String replaceName) {
        this.replaceName = replaceName;
    }

    private void addStaticFinalField(String magicNumber) {

        VariableDeclarator variableDeclarator = new VariableDeclarator(new VariableDeclaratorId(replaceName + magicNumber),
                new IntegerLiteralExpr(magicNumber));

        int modifier = ModifierSet.addModifier(ModifierSet.PRIVATE, ModifierSet.STATIC);
        modifier = ModifierSet.addModifier(modifier, ModifierSet.FINAL);

        BodyDeclaration fieldDeclaration = ASTHelper.createFieldDeclaration(modifier, ASTHelper.INT_TYPE, variableDeclarator);
        fieldDeclaration.setBeginLine(4);
        fieldDeclaration.setEndLine(5);

        FieldDeclarationVisitor fieldVisitor = new FieldDeclarationVisitor();
        compilationUnit.accept(fieldVisitor, null);
        ASTStrategyHelper.insertMember(compilationUnit.getTypes().get(0),
                fieldDeclaration, fieldVisitor.getNumberOfFields());
    }


    private static class ReplaceMagicNumberLiteralExprVisitor extends VoidVisitorAdapter {

        private IntegerLiteralExpr integerLiteralViolationExpr = null;

        @Override
        public void visit(IntegerLiteralExpr n, Object arg) {
            System.out.println("IntegerLiteral: " + n.getBeginLine() + " " + n.getBeginColumn());
            RuleViolation violation = (RuleViolation) arg;
            if (n.getBeginLine() == violation.getBeginLine() &&
                    n.getBeginColumn() == violation.getBeginColumn()) {
                integerLiteralViolationExpr = n;
            }
        }

        public IntegerLiteralExpr getIntegerLiteralViolationExpr() {
            return integerLiteralViolationExpr;
        }

    }
}<commit_msg>removed system.out.println<commit_after>package nl.han.ica.core.strategies.solvers;

import japa.parser.ASTHelper;
import japa.parser.ast.body.BodyDeclaration;
import japa.parser.ast.body.ModifierSet;
import japa.parser.ast.body.VariableDeclarator;
import japa.parser.ast.body.VariableDeclaratorId;
import japa.parser.ast.expr.IntegerLiteralExpr;
import japa.parser.ast.visitor.VoidVisitorAdapter;
import net.sourceforge.pmd.RuleViolation;
import nl.han.ica.core.ast.ASTStrategyHelper;
import nl.han.ica.core.ast.FieldDeclarationVisitor;


public class ReplaceMagicNumberSolver extends StrategySolver {

    private String replaceName = "MAGIC";

    public ReplaceMagicNumberSolver(RuleViolation ruleViolation) {
        super(ruleViolation);
    }

    @Override
    public void rewriteAST() {
        System.out.println(ruleViolation.toString());
        ReplaceMagicNumberLiteralExprVisitor expressionVisitor = new ReplaceMagicNumberLiteralExprVisitor();
        compilationUnit.accept(expressionVisitor, ruleViolation);

        IntegerLiteralExpr literalExpr = expressionVisitor.getIntegerLiteralViolationExpr();
        if (literalExpr != null) {
            addStaticFinalField(literalExpr.getValue());
            literalExpr.setValue(replaceName + literalExpr.getValue());
        }
    }

    public void setReplaceName(String replaceName) {
        this.replaceName = replaceName;
    }

    private void addStaticFinalField(String magicNumber) {

        VariableDeclarator variableDeclarator = new VariableDeclarator(new VariableDeclaratorId(replaceName + magicNumber),
                new IntegerLiteralExpr(magicNumber));

        int modifier = ModifierSet.addModifier(ModifierSet.PRIVATE, ModifierSet.STATIC);
        modifier = ModifierSet.addModifier(modifier, ModifierSet.FINAL);

        BodyDeclaration fieldDeclaration = ASTHelper.createFieldDeclaration(modifier, ASTHelper.INT_TYPE, variableDeclarator);
        fieldDeclaration.setBeginLine(4);
        fieldDeclaration.setEndLine(5);

        FieldDeclarationVisitor fieldVisitor = new FieldDeclarationVisitor();
        compilationUnit.accept(fieldVisitor, null);
        ASTStrategyHelper.insertMember(compilationUnit.getTypes().get(0),
                fieldDeclaration, fieldVisitor.getNumberOfFields());
    }


    private static class ReplaceMagicNumberLiteralExprVisitor extends VoidVisitorAdapter {

        private IntegerLiteralExpr integerLiteralViolationExpr = null;

        @Override
        public void visit(IntegerLiteralExpr n, Object arg) {
            RuleViolation violation = (RuleViolation) arg;
            if (n.getBeginLine() == violation.getBeginLine() &&
                    n.getBeginColumn() == violation.getBeginColumn()) {
                integerLiteralViolationExpr = n;
            }
        }

        public IntegerLiteralExpr getIntegerLiteralViolationExpr() {
            return integerLiteralViolationExpr;
        }

    }
}
{'fraction_non_alphanumeric': 0.08295625942684766, 'fraction_numerical': 0.001005530417295123, 'mean_word_length': 3.6443579766536964, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4743460', 'n_tokens_mistral': 1565, 'n_tokens_neox': 1538, 'n_words': 262}