<commit_before>package jce.codemanipulation.origin;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.internal.corext.refactoring.sef.SelfEncapsulateFieldRefactoring;

import jce.properties.EcorificationProperties;
import jce.util.RefactoringUtil;
import jce.util.logging.MonitorFactory;

/**
 * {@link ASTVisitor} class for the encapsulation of fields. Encapsulates all field of an class upon visiting.
 * @author <NAME>
 */
@SuppressWarnings("restriction") // TODO (LOW) This class uses LTK classes & methods that are not marked as API
public class FieldEncapsulationVisitor extends ASTVisitor {
    private static final Logger logger = LogManager.getLogger(FieldEncapsulationVisitor.class.getName());
    private IProgressMonitor monitor;

    /**
     * Basic constructor.
     * @param properties are the Ecorification properties.
     */
    public FieldEncapsulationVisitor(EcorificationProperties properties) {
        monitor = MonitorFactory.createProgressMonitor(logger, properties);
    }

    /**
     * Encapsulates a specific {@link IField}.
     * @param field is the specific {@link IField}.
     */
    public void encapsulateField(IField field) {
        try {
            SelfEncapsulateFieldRefactoring refactoring = new SelfEncapsulateFieldRefactoring(field);
            RefactoringUtil.applyRefactoring(refactoring, monitor);
        } catch (JavaModelException exception) {
            exception.printStackTrace();
        }
    }

    @Override
    public boolean visit(TypeDeclaration node) {
        if (!node.isInterface()) { // if is class, manipulate inheritance:
            for (FieldDeclaration field : node.getFields()) { // for every field:
                VariableDeclarationFragment fragment = (VariableDeclarationFragment) field.fragments().get(0);
                IJavaElement element = fragment.resolveBinding().getJavaElement(); // parse FieldDeclaration to IField
                if (element instanceof IField) {
                    encapsulateField((IField) element); // Encapsulate if casted succesful.
                } else {
                    throw new ClassCastException("IJavaElement is not IField: " + element + " is " + element.getClass().getName());
                }
            }
        }
        return super.visit(node);
    }
}<commit_msg>EDIT: Added check against unresolved variable bindings. Currently I am not sure whether that check can be triggered in a perfect project or whether the edge case only exists because other problems.<commit_after>package jce.codemanipulation.origin;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.internal.corext.refactoring.sef.SelfEncapsulateFieldRefactoring;

import jce.properties.EcorificationProperties;
import jce.util.RefactoringUtil;
import jce.util.logging.MonitorFactory;

/**
 * {@link ASTVisitor} class for the encapsulation of fields. Encapsulates all field of an class upon visiting.
 * @author <NAME>
 */
@SuppressWarnings("restriction") // TODO (LOW) This class uses LTK classes & methods that are not marked as API
public class FieldEncapsulationVisitor extends ASTVisitor {
    private static final Logger logger = LogManager.getLogger(FieldEncapsulationVisitor.class.getName());
    private IProgressMonitor monitor;

    /**
     * Basic constructor.
     * @param properties are the Ecorification properties.
     */
    public FieldEncapsulationVisitor(EcorificationProperties properties) {
        monitor = MonitorFactory.createProgressMonitor(logger, properties);
    }

    /**
     * Encapsulates a specific {@link IField}.
     * @param field is the specific {@link IField}.
     */
    public void encapsulateField(IField field) {
        try {
            SelfEncapsulateFieldRefactoring refactoring = new SelfEncapsulateFieldRefactoring(field);
            RefactoringUtil.applyRefactoring(refactoring, monitor);
        } catch (JavaModelException exception) {
            exception.printStackTrace();
        }
    }

    @Override
    public boolean visit(TypeDeclaration node) {
        if (!node.isInterface()) { // if is class, manipulate inheritance:
            for (FieldDeclaration field : node.getFields()) { // for every field:
                VariableDeclarationFragment fragment = (VariableDeclarationFragment) field.fragments().get(0);
                IVariableBinding binding = fragment.resolveBinding(); // resolve binding of fragment
                if (binding == null) {
                    logger.error("Could not resolve binding: " + fragment + " of " + field);
                } else {
                    IJavaElement element = binding.getJavaElement(); // parse FieldDeclaration to IField
                    if (element instanceof IField) {
                        encapsulateField((IField) element); // Encapsulate if casted succesful.
                    } else {
                        throw new ClassCastException("IJavaElement is not IField: " + element + " is " + element.getClass().getName());
                    }
                }
            }
        }
        return super.visit(node);
    }
}
{'fraction_non_alphanumeric': 0.09110738255033557, 'fraction_numerical': 0.0010067114093959733, 'mean_word_length': 3.1082012405237767, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2655259', 'n_tokens_mistral': 1564, 'n_tokens_neox': 1521, 'n_words': 391}