<commit_before>package jce;

import static jce.properties.TextProperty.ECORE_PACKAGE;
import static jce.properties.TextProperty.PROJECT_SUFFIX;

import java.io.File;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.codegen.ecore.genmodel.GenModel;

import eme.EcoreMetamodelExtraction;
import eme.generator.GeneratedEcoreMetamodel;
import eme.generator.saving.SavingInformation;
import eme.properties.BinaryProperty;
import eme.properties.ExtractionProperties;
import eme.properties.TextProperty;
import jce.codegen.GenModelGenerator;
import jce.codegen.ModelCodeGenerator;
import jce.codegen.WrapperGenerator;
import jce.codegen.XtendLibraryHelper;
import jce.manipulation.FieldEncapsulator;
import jce.manipulation.ImportOrganizer;
import jce.manipulation.InheritanceManipulator;
import jce.manipulation.MemberRemover;
import jce.properties.EcorificationProperties;
import jce.util.MonitorFactory;
import jce.util.ResourceRefresher;

/**
 * Main class for Java code ecorification.
 * @author <NAME>
 */
public class JavaCodeEcorification {
    private static final Logger logger = LogManager.getLogger(JavaCodeEcorification.class.getName());
    private final FieldEncapsulator fieldEncapsulator;
    private final GenModelGenerator genModelGenerator;
    private final InheritanceManipulator inheritanceManipulator;
    private final EcoreMetamodelExtraction metamodelGenerator;
    private final EcorificationProperties properties;
    private final WrapperGenerator wrapperGenerator;
    private final ImportOrganizer importOrganizer;

    /**
     * Basic constructor.
     */
    public JavaCodeEcorification() {
        properties = new EcorificationProperties();
        metamodelGenerator = new EcoreMetamodelExtraction();
        genModelGenerator = new GenModelGenerator();
        wrapperGenerator = new WrapperGenerator(properties);
        fieldEncapsulator = new FieldEncapsulator(properties);
        importOrganizer = new ImportOrganizer(properties);
        inheritanceManipulator = new InheritanceManipulator(properties);
        configureExtraction(metamodelGenerator.getProperties());

    }

    /**
     * Starts the ecorification for a specific Java project. Initializes the different steps of the Ecorification
     * pipeline: The extraction of an Ecore metamodel, the Ecore model code generation, the wrapper generation and the
     * origin code adaption.
     * @param originalProject is the specific Java project as {@link IProject}.
     */
    public void start(IProject originalProject) {
        // 0. initialize:
        check(originalProject);
        logger.info("Starting Ecorification...");
        // 1. generate metamodel, GenModel, model code and make Project copy:
        GeneratedEcoreMetamodel metamodel = metamodelGenerator.extractAndSaveFrom(originalProject);
        GenModel genModel = genModelGenerator.generate(metamodel);
        IProject project = getProject(metamodel.getSavingInformation()); // Retrieve output project
        ModelCodeGenerator.generate(genModel, properties);
        // 2. generate wrappers:
        XtendLibraryHelper.addXtendLibs(project, properties);
        ResourceRefresher.refresh(project);
        wrapperGenerator.buildWrappers(metamodel, project);
        // 3. adapt origin code:
        fieldEncapsulator.manipulate(project);
        new MemberRemover(metamodel, properties).manipulate(project);
        importOrganizer.manipulate(project);
        inheritanceManipulator.manipulate(project);
        // 4. build project and make changes visible in the Eclipse IDE:
        rebuild(project, properties);
        ResourceRefresher.refresh(project);
        logger.info("Ecorification complete!");
    }

    /**
     * Checks whether a specific {@link IProject} is valid (neither null nor nonexistent)
     * @param project is the specific {@link IProject}.
     */
    private void check(IProject project) {
        if (project == null) {
            throw new IllegalArgumentException("Project can't be null!");
        } else if (!project.exists()) {
            throw new IllegalArgumentException("Project " + project.toString() + "does not exist!");
        }
    }

    /**
     * Configures the extraction properties. JCE Properties are referenced directly, EME properties are referenced witht
     * the class name.
     */
    private void configureExtraction(ExtractionProperties properties) {
        properties.set(TextProperty.SAVING_STRATEGY, "CopyProject");
        properties.set(TextProperty.PROJECT_SUFFIX, this.properties.get(PROJECT_SUFFIX));
        properties.set(TextProperty.DEFAULT_PACKAGE, this.properties.get(ECORE_PACKAGE));
        properties.set(TextProperty.DATATYPE_PACKAGE, "datatypes");
        properties.set(BinaryProperty.DUMMY_CLASS, false);
    }

    /**
     * Gets {@link IProject} from {@link SavingInformation}.
     */
    private IProject getProject(SavingInformation information) {
        String name = information.getProjectName(); // get project name
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        for (IProject project : root.getProjects()) { // for every project
            if (project.getName().equals(name)) { // compare with name
                ResourceRefresher.refresh(project);
                return project;
            }
        }
        return null;
    }

    /**
     * Tries to build the project.
     */
    private void rebuild(IProject project, EcorificationProperties properties) {
        IProgressMonitor monitor = MonitorFactory.createProgressMonitor(logger, properties);
        ResourceRefresher.refresh(project);
        try { // TODO (MEDIUM) fix Xtend build.
            project.build(IncrementalProjectBuilder.CLEAN_BUILD, monitor);
            project.build(IncrementalProjectBuilder.FULL_BUILD, monitor);
            IFolder xtendFolder = project.getFolder("src" + File.separator + "main" + File.separator + "xtend-gen");
            ResourceRefresher.refresh(project);
            xtendFolder.delete(true, monitor);
        } catch (CoreException exception) {
            exception.printStackTrace();
        }
    }
}<commit_msg>EDIT: Added the import manipulation of the Ecore code to the JCE pipeline, reduced the building process to a minimal procedure.<commit_after>package jce;

import static jce.properties.TextProperty.ECORE_PACKAGE;
import static jce.properties.TextProperty.PROJECT_SUFFIX;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.codegen.ecore.genmodel.GenModel;

import eme.EcoreMetamodelExtraction;
import eme.generator.GeneratedEcoreMetamodel;
import eme.generator.saving.SavingInformation;
import eme.properties.BinaryProperty;
import eme.properties.ExtractionProperties;
import eme.properties.TextProperty;
import jce.codegen.GenModelGenerator;
import jce.codegen.ModelCodeGenerator;
import jce.codegen.WrapperGenerator;
import jce.codegen.XtendLibraryHelper;
import jce.manipulation.EcoreImportManipulator;
import jce.manipulation.FieldEncapsulator;
import jce.manipulation.ImportOrganizer;
import jce.manipulation.InheritanceManipulator;
import jce.manipulation.MemberRemover;
import jce.properties.EcorificationProperties;
import jce.util.MonitorFactory;
import jce.util.ResourceRefresher;

/**
 * Main class for Java code ecorification.
 * @author <NAME>
 */
public class JavaCodeEcorification {
    private static final Logger logger = LogManager.getLogger(JavaCodeEcorification.class.getName());
    private final FieldEncapsulator fieldEncapsulator;
    private final GenModelGenerator genModelGenerator;
    private final InheritanceManipulator inheritanceManipulator;
    private final EcoreMetamodelExtraction metamodelGenerator;
    private final EcorificationProperties properties;
    private final WrapperGenerator wrapperGenerator;
    private final ImportOrganizer importOrganizer;

    /**
     * Basic constructor.
     */
    public JavaCodeEcorification() {
        properties = new EcorificationProperties();
        metamodelGenerator = new EcoreMetamodelExtraction();
        genModelGenerator = new GenModelGenerator();
        wrapperGenerator = new WrapperGenerator(properties);
        fieldEncapsulator = new FieldEncapsulator(properties);
        importOrganizer = new ImportOrganizer(properties);
        inheritanceManipulator = new InheritanceManipulator(properties);
        configureExtraction(metamodelGenerator.getProperties());

    }

    /**
     * Starts the ecorification for a specific Java project. Initializes the different steps of the Ecorification
     * pipeline: The extraction of an Ecore metamodel, the Ecore model code generation, the wrapper generation and the
     * origin code adaption.
     * @param originalProject is the specific Java project as {@link IProject}.
     */
    public void start(IProject originalProject) {
        // 0. initialize:
        check(originalProject);
        logger.info("Starting Ecorification...");
        // 1. generate metamodel, GenModel, model code and make Project copy:
        GeneratedEcoreMetamodel metamodel = metamodelGenerator.extractAndSaveFrom(originalProject);
        GenModel genModel = genModelGenerator.generate(metamodel);
        IProject project = getProject(metamodel.getSavingInformation()); // Retrieve output project
        ModelCodeGenerator.generate(genModel, properties);
        // 2. generate wrappers:
        XtendLibraryHelper.addXtendLibs(project, properties);
        ResourceRefresher.refresh(project);
        wrapperGenerator.buildWrappers(metamodel, project);
        // 3. adapt Ecore code
        new EcoreImportManipulator(metamodel, properties).manipulate(project);
        // 4. adapt origin code:
        fieldEncapsulator.manipulate(project);
        new MemberRemover(metamodel, properties).manipulate(project);
        importOrganizer.manipulate(project);
        inheritanceManipulator.manipulate(project);
        // 5. build project and make changes visible in the Eclipse IDE:
        rebuild(project, properties);
        logger.info("Ecorification complete!");
    }

    /**
     * Checks whether a specific {@link IProject} is valid (neither null nor nonexistent)
     * @param project is the specific {@link IProject}.
     */
    private void check(IProject project) {
        if (project == null) {
            throw new IllegalArgumentException("Project can't be null!");
        } else if (!project.exists()) {
            throw new IllegalArgumentException("Project " + project.toString() + "does not exist!");
        }
    }

    /**
     * Configures the extraction properties. JCE Properties are referenced directly, EME properties are referenced witht
     * the class name.
     */
    private void configureExtraction(ExtractionProperties properties) {
        properties.set(TextProperty.SAVING_STRATEGY, "CopyProject");
        properties.set(TextProperty.PROJECT_SUFFIX, this.properties.get(PROJECT_SUFFIX));
        properties.set(TextProperty.DEFAULT_PACKAGE, this.properties.get(ECORE_PACKAGE));
        properties.set(TextProperty.DATATYPE_PACKAGE, "datatypes");
        properties.set(BinaryProperty.DUMMY_CLASS, false);
    }

    /**
     * Gets {@link IProject} from {@link SavingInformation}.
     */
    private IProject getProject(SavingInformation information) {
        String name = information.getProjectName(); // get project name
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        for (IProject project : root.getProjects()) { // for every project
            if (project.getName().equals(name)) { // compare with name
                ResourceRefresher.refresh(project);
                return project;
            }
        }
        return null;
    }

    /**
     * Tries to build the project.
     */
    private void rebuild(IProject project, EcorificationProperties properties) {
        ResourceRefresher.refresh(project);
        IProgressMonitor monitor = MonitorFactory.createProgressMonitor(logger, properties);
        try {
            project.build(IncrementalProjectBuilder.FULL_BUILD, monitor);
        } catch (CoreException exception) {
            exception.printStackTrace();
        }
    }
}
{'fraction_non_alphanumeric': 0.08519553072625698, 'fraction_numerical': 0.0011638733705772813, 'mean_word_length': 3.6750090678273484, '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': '243527', 'n_tokens_mistral': 3430, 'n_tokens_neox': 3320, 'n_words': 799}