<commit_before>package jce.codemanipulation.ecore;

import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.corext.refactoring.rename.RenameCompilationUnitProcessor;
import org.eclipse.ltk.core.refactoring.participants.RenameRefactoring;

import eme.generator.GeneratedEcoreMetamodel;
import jce.codemanipulation.AbstractCodeManipulator;
import jce.properties.EcorificationProperties;
import jce.properties.TextProperty;
import jce.util.MetamodelSearcher;
import jce.util.PackageFilter;
import jce.util.PathHelper;
import jce.util.RefactoringUtil;

/**
 * Code manipulator that moves the original Ecore factory implementation class into a new subpackage.
 * @author <NAME>
 */
@SuppressWarnings("restriction") // TODO (LOW) This class uses LTK classes & methods that are not marked as API
public class FactoryRenamer extends AbstractCodeManipulator {
    private final GeneratedEcoreMetamodel metamodel;

    /**
     * Simple constructor, sets the metamodel and the properties.
     * @param metamodel is the extracted Ecore metamodel.
     * @param properties are the {@link EcorificationProperties}.
     */
    public FactoryRenamer(GeneratedEcoreMetamodel metamodel, EcorificationProperties properties) {
        super(properties);
        this.metamodel = metamodel;
    }

    /**
     * Checks whether a {@link ICompilationUnit} is an Ecore factory implementation class.
     */
    private boolean isEcoreFactory(ICompilationUnit unit) throws JavaModelException {
        String fullName = getPackageMemberName(unit); // get name of the type
        String packageName = packageHelper.getLastSegment(packageHelper.cutLastSegments(fullName, 2));
        if (fullName.endsWith(PathHelper.capitalize(packageName) + "FactoryImpl")) { // if has factory name
            String modelName = packageHelper.cutFirstSegment(fullName); // without ecore package
            return MetamodelSearcher.findEClass(modelName, metamodel.getRoot()) == null; // search metamodel counterpart
        }
        return false; // Does not have Ecore implementation name and package
    }

    @Override
    protected List<IPackageFragment> filterPackages(IProject project, EcorificationProperties properties) {
        return PackageFilter.startsWith(project, properties.get(TextProperty.ECORE_PACKAGE));
    }

    @Override
    protected void manipulate(ICompilationUnit unit) throws JavaModelException {
        if (isEcoreFactory(unit)) {
            String name = unit.getElementName();
            name = packageHelper.cutLastSegment(name);
            name += "2"; // TODO
            System.err.println(name); // TODO
            try {
                RenameCompilationUnitProcessor processor = new RenameCompilationUnitProcessor(unit);
                System.err.println(processor.checkNewElementName(name)); // TODO
                processor.setNewElementName(name);
                RenameRefactoring refactoring = new RenameRefactoring(processor);
                RefactoringUtil.applyRefactoring(refactoring, monitor);
            } catch (CoreException exception) {
                logger.fatal("Renaming " + unit.getElementName() + " failed!", exception);
            }
        }
    }
}<commit_msg>EDIT: Implemented the use of the factory prefix.<commit_after>package jce.codemanipulation.ecore;

import static jce.properties.TextProperty.FACTORY_SUFFIX;

import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.corext.refactoring.rename.RenameCompilationUnitProcessor;
import org.eclipse.ltk.core.refactoring.participants.RenameRefactoring;

import eme.generator.GeneratedEcoreMetamodel;
import jce.codemanipulation.AbstractCodeManipulator;
import jce.properties.EcorificationProperties;
import jce.properties.TextProperty;
import jce.util.MetamodelSearcher;
import jce.util.PackageFilter;
import jce.util.PathHelper;
import jce.util.RefactoringUtil;

/**
 * Code manipulator that moves the original Ecore factory implementation class into a new subpackage.
 * @author <NAME>
 */
@SuppressWarnings("restriction") // TODO (LOW) This class uses LTK classes & methods that are not marked as API
public class FactoryRenamer extends AbstractCodeManipulator {
    private final GeneratedEcoreMetamodel metamodel;

    /**
     * Simple constructor, sets the metamodel and the properties.
     * @param metamodel is the extracted Ecore metamodel.
     * @param properties are the {@link EcorificationProperties}.
     */
    public FactoryRenamer(GeneratedEcoreMetamodel metamodel, EcorificationProperties properties) {
        super(properties);
        this.metamodel = metamodel;
    }

    /**
     * Checks whether a {@link ICompilationUnit} is an Ecore factory implementation class.
     */
    private boolean isEcoreFactory(ICompilationUnit unit) throws JavaModelException { // TODO (HIGH) allow interfaces
        String fullName = getPackageMemberName(unit); // get name of the type
        String packageName = packageHelper.getLastSegment(packageHelper.cutLastSegments(fullName, 2));
        if (fullName.endsWith(PathHelper.capitalize(packageName) + "FactoryImpl")) { // if has factory name
            String modelName = packageHelper.cutFirstSegment(fullName); // without ecore package
            return MetamodelSearcher.findEClass(modelName, metamodel.getRoot()) == null; // search metamodel counterpart
        }
        return false; // Does not have Ecore implementation name and package
    }

    @Override
    protected List<IPackageFragment> filterPackages(IProject project, EcorificationProperties properties) {
        return PackageFilter.startsWith(project, properties.get(TextProperty.ECORE_PACKAGE));
    }

    @Override
    protected void manipulate(ICompilationUnit unit) throws JavaModelException {
        if (isEcoreFactory(unit)) {
            String name = unit.getElementName();
            String newName = packageHelper.cutLastSegment(name) + properties.get(FACTORY_SUFFIX); // new name of class
            newName = packageHelper.append(newName, packageHelper.getLastSegment(name)); // add file extension
            try {
                RenameCompilationUnitProcessor processor = new RenameCompilationUnitProcessor(unit);
                if (processor.checkNewElementName(newName).isOK()) { // if new name is okay
                    processor.setNewElementName(newName); // set new name
                }
                RenameRefactoring refactoring = new RenameRefactoring(processor); // create refactoring
                RefactoringUtil.applyRefactoring(refactoring, monitor); // apply refactoring
                monitor.beginTask("Renamed factory: " + name, 0);
            } catch (CoreException exception) {
                logger.fatal("Renaming " + unit.getElementName() + " failed!", exception);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.08451871288496064, 'fraction_numerical': 0.0005524098881369977, 'mean_word_length': 3.5864471184293856, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6887477', 'n_tokens_mistral': 1981, 'n_tokens_neox': 1895, 'n_words': 461}