<commit_before>/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2010, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.modules;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
* A module is a unit of classes and other resources, along with the specification of what is imported and exported
* by this module from and to other modules.  Modules are created by {@link ModuleLoader}s which build modules from
* various configuration information and resource roots.
*
* @author <a href="mailto:<EMAIL>"><NAME></a>
* @author <a href="mailto:<EMAIL>"><NAME></a>
*/
public final class Module {
    static {
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
            public Void run() {
                try {
                    URL.setURLStreamHandlerFactory(new ModularURLStreamHandlerFactory());
                } catch (Throwable t) {
                    // todo log a warning or something
                }
                return null;
            }
        });
    }

    // static properties

    /**
     * The system-wide module logger, which may be changed via {@link #setModuleLogger(ModuleLogger)}.
     */
    static volatile ModuleLogger log = NoopModuleLogger.getInstance();

    /**
     * The selector to choose the module loader to use for locating modules.
     */
    private static volatile ModuleLoaderSelector moduleLoaderSelector = ModuleLoaderSelector.DEFAULT;

    // immutable properties

    /**
     * The identifier of this module.
     */
    private final ModuleIdentifier identifier;
    /**
     * The name of the main class, if any (may be {@code null}).
     */
    private final String mainClassName;
    /**
     * The module class loader for this module.
     */
    private final ModuleClassLoader moduleClassLoader;
    /**
     * The module loader which created this module.
     */
    private final ModuleLoader moduleLoader;
    /**
     * This module's dependencies.
     */
    private final Dependency[] dependencies;
    /**
     * This reference exists solely to prevent the {@code FutureModule} from getting GC'd prematurely.
     */
    @SuppressWarnings({ "UnusedDeclaration" })
    private final Object myKey;

    // mutable properties

    /**
     * The complete collection of all paths.  Initially, the paths are uninitialized.
     */
    private volatile Paths paths = Paths.NONE;

    /**
     * Construct an unresolved instance from a module spec.
     *
     * @param moduleLoader the creating module loader
     * @param myKey
     */
    Module(final ModuleIdentifier identifier, final String mainClassName, final ModuleLoader moduleLoader, final AssertionSetting assertionSetting, final Collection<ResourceLoader> resourceLoaders, final Dependency[] dependencies, final Object myKey) {
        this.moduleLoader = moduleLoader;
        this.identifier = identifier;
        this.mainClassName = mainClassName;
        this.myKey = myKey;
        // should be safe, so...
        //noinspection ThisEscapedInObjectConstruction
        moduleClassLoader = new ModuleClassLoader(this, assertionSetting, resourceLoaders);
        this.dependencies = dependencies;
    }


    Module(final ModuleSpec spec, final ModuleLoader moduleLoader, final Object myKey) {
        this.moduleLoader = moduleLoader;
        this.myKey = myKey;

        // Initialize state from the spec.
        identifier = spec.getModuleIdentifier();
        mainClassName = spec.getMainClass();
        //noinspection ThisEscapedInObjectConstruction
        moduleClassLoader = new ModuleClassLoader(this, spec.getAssertionSetting(), Arrays.asList(spec.getResourceLoaders()));
        final List<Dependency> dependencies = new ArrayList<Dependency>();
        for (ModuleSpec.SpecifiedDependency specifiedDependency : spec.getDependencies()) {
            //noinspection ThisEscapedInObjectConstruction
            dependencies.add(specifiedDependency.getDependency(this));
        }
        this.dependencies = dependencies.toArray(new Dependency[dependencies.size()]);
    }

    Dependency[] getDependencies() {
        return dependencies;
    }

    enum LoadState {

        /**
         * This module's content and dependency information have successfully been loaded.
         */
        LOADED,
        /**
         * This module's linkage information is complete, with a populated path->loader mapping.
         */
        RESOLVED,
        /**
         * All of this module's dependencies (and their transitives) are linked and in the READY state.
         */
        LINKED,
    }

    private volatile LoadState loadState = LoadState.LOADED;

    private static final AtomicReferenceFieldUpdater<Module, LoadState> loadStateUpdater = AtomicReferenceFieldUpdater.newUpdater(Module.class, LoadState.class, "loadState");<commit_msg>Add the possibility of adding new dependencies<commit_after>/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2010, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.modules;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
* A module is a unit of classes and other resources, along with the specification of what is imported and exported
* by this module from and to other modules.  Modules are created by {@link ModuleLoader}s which build modules from
* various configuration information and resource roots.
*
* @author <a href="mailto:<EMAIL>"><NAME></a>
* @author <a href="mailto:<EMAIL>"><NAME></a>
*/
public final class Module {
    static {
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
            public Void run() {
                try {
                    URL.setURLStreamHandlerFactory(new ModularURLStreamHandlerFactory());
                } catch (Throwable t) {
                    // todo log a warning or something
                }
                return null;
            }
        });
    }

    // static properties

    /**
     * The system-wide module logger, which may be changed via {@link #setModuleLogger(ModuleLogger)}.
     */
    static volatile ModuleLogger log = NoopModuleLogger.getInstance();

    /**
     * The selector to choose the module loader to use for locating modules.
     */
    private static volatile ModuleLoaderSelector moduleLoaderSelector = ModuleLoaderSelector.DEFAULT;

    // immutable properties

    /**
     * The identifier of this module.
     */
    private final ModuleIdentifier identifier;
    /**
     * The name of the main class, if any (may be {@code null}).
     */
    private final String mainClassName;
    /**
     * The module class loader for this module.
     */
    private final ModuleClassLoader moduleClassLoader;
    /**
     * The module loader which created this module.
     */
    private final ModuleLoader moduleLoader;
    /**
     * This module's dependencies.
     */
    private volatile Dependency[] dependencies;
    /**
     * This reference exists solely to prevent the {@code FutureModule} from getting GC'd prematurely.
     */
    @SuppressWarnings({ "UnusedDeclaration" })
    private final Object myKey;

    private static final AtomicReferenceFieldUpdater<Module, Dependency[]> dependenciesUpdater = AtomicReferenceFieldUpdater.newUpdater(Module.class, Dependency[].class, "dependencies");

    // mutable properties

    /**
     * The complete collection of all paths.  Initially, the paths are uninitialized.
     */
    private volatile Paths paths = Paths.NONE;

    /**
     * Construct an unresolved instance from a module spec.
     *
     * @param moduleLoader the creating module loader
     * @param myKey
     */
    Module(final ModuleIdentifier identifier, final String mainClassName, final ModuleLoader moduleLoader, final AssertionSetting assertionSetting, final Collection<ResourceLoader> resourceLoaders, final Dependency[] dependencies, final Object myKey) {
        this.moduleLoader = moduleLoader;
        this.identifier = identifier;
        this.mainClassName = mainClassName;
        this.myKey = myKey;
        // should be safe, so...
        //noinspection ThisEscapedInObjectConstruction
        moduleClassLoader = new ModuleClassLoader(this, assertionSetting, resourceLoaders);
        this.dependencies = dependencies;
    }


    Module(final ModuleSpec spec, final ModuleLoader moduleLoader, final Object myKey) {
        this.moduleLoader = moduleLoader;
        this.myKey = myKey;

        // Initialize state from the spec.
        identifier = spec.getModuleIdentifier();
        mainClassName = spec.getMainClass();
        //noinspection ThisEscapedInObjectConstruction
        moduleClassLoader = new ModuleClassLoader(this, spec.getAssertionSetting(), Arrays.asList(spec.getResourceLoaders()));
        final List<Dependency> dependencies = new ArrayList<Dependency>();
        for (ModuleSpec.SpecifiedDependency specifiedDependency : spec.getDependencies()) {
            //noinspection ThisEscapedInObjectConstruction
            dependencies.add(specifiedDependency.getDependency(this));
        }
        this.dependencies = dependencies.toArray(new Dependency[dependencies.size()]);
    }

    Dependency[] getDependencies() {
        return dependencies;
    }

    void addDependency(Dependency dependency) {
        Dependency[] oldDeps, newDeps;
        int len;
        do {
            oldDeps = dependencies;
            len = oldDeps.length;
            newDeps = Arrays.copyOf(oldDeps, len + 1);
            newDeps[len] = dependency;
        } while (! dependenciesUpdater.compareAndSet(this, oldDeps, newDeps));
    }

    enum LoadState {

        /**
         * This module's content and dependency information have successfully been loaded.
         */
        LOADED,
        /**
         * This module's linkage information is complete, with a populated path->loader mapping.
         */
        RESOLVED,
        /**
         * All of this module's dependencies (and their transitives) are linked and in the READY state.
         */
        LINKED,
    }

    private volatile LoadState loadState = LoadState.LOADED;

    private static final AtomicReferenceFieldUpdater<Module, LoadState> loadStateUpdater = AtomicReferenceFieldUpdater.newUpdater(Module.class, LoadState.class, "loadState");
{'fraction_non_alphanumeric': 0.08252390633235449, 'fraction_numerical': 0.00263534372411716, 'mean_word_length': 3.032179720704311, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4729401', 'n_tokens_mistral': 3393, 'n_tokens_neox': 3261, 'n_words': 1165}