<commit_before>/*
 * Copyright 2016 <NAME> <<EMAIL>>
 * and other copyright owners as documented in the project's IP log.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.basinmc.sink.plugin.java;

import com.google.common.collect.ImmutableMap;

import org.basinmc.faucet.plugin.ClassLoaderPluginContext;
import org.basinmc.faucet.plugin.Plugin;
import org.basinmc.faucet.plugin.PluginContext;
import org.basinmc.faucet.plugin.PluginMetadata;
import org.basinmc.faucet.plugin.PluginVersion;
import org.basinmc.faucet.plugin.VersionRange;
import org.basinmc.sink.SinkServer;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.TypePath;

import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import javax.annotation.Nonnull;

import jdk.internal.org.objectweb.asm.Type;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public abstract class AbstractJavaPluginContext implements ClassLoaderPluginContext {
    private final Path source;
    private final Set<PluginContext> wiredPluginDependencies = new HashSet<>();
    private State state = State.LOADED;
    private State targetState = State.RUNNING;

    public AbstractJavaPluginContext(@Nonnull Path source) throws IOException {
        this.source = source;
    }

    /**
     * {@inheritDoc}
     */
    @Nonnull
    @Override
    public Path getSource() {
        return this.source;
    }

    /**
     * {@inheritDoc}
     */
    @Nonnull
    @Override
    public State getState() {
        return this.state;
    }

    /**
     * {@inheritDoc}
     */
    @Nonnull
    @Override
    public State getTargetState() {
        return this.targetState;
    }

    /**
     * {@inheritDoc}
     */
    @Nonnull
    @Override
    public Set<PluginContext> getWiredDependencies() {
        return Collections.unmodifiableSet(this.wiredPluginDependencies);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void wire(@Nonnull PluginContext context) {
        this.wiredPluginDependencies.add(context);
    }

    /**
     * Provides a class visitor capable of locating a plugin main class as well as processing its
     * metadata.
     */
    protected static class LocatorClassVisitor extends ClassVisitor {
        private String className = null;
        private PluginAnnotationVisitor annotationVisitor = null;

        public LocatorClassVisitor() {
            super(Opcodes.ASM5);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public AnnotationVisitor visitTypeAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) {<commit_msg>Added a setter in order to communicate with context implementations when switching target states.<commit_after>/*
 * Copyright 2016 <NAME> <<EMAIL>>
 * and other copyright owners as documented in the project's IP log.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.basinmc.sink.plugin.java;

import com.google.common.collect.ImmutableMap;

import org.basinmc.faucet.plugin.ClassLoaderPluginContext;
import org.basinmc.faucet.plugin.Plugin;
import org.basinmc.faucet.plugin.PluginContext;
import org.basinmc.faucet.plugin.PluginMetadata;
import org.basinmc.faucet.plugin.PluginVersion;
import org.basinmc.faucet.plugin.VersionRange;
import org.basinmc.sink.SinkServer;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.TypePath;

import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import javax.annotation.Nonnull;

import jdk.internal.org.objectweb.asm.Type;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public abstract class AbstractJavaPluginContext implements ClassLoaderPluginContext {
    private final Path source;
    private final Set<PluginContext> wiredPluginDependencies = new HashSet<>();
    private State state = State.LOADED;
    private State targetState = State.RUNNING;

    public AbstractJavaPluginContext(@Nonnull Path source) throws IOException {
        this.source = source;
    }

    /**
     * {@inheritDoc}
     */
    @Nonnull
    @Override
    public Path getSource() {
        return this.source;
    }

    /**
     * {@inheritDoc}
     */
    @Nonnull
    @Override
    public State getState() {
        return this.state;
    }

    /**
     * {@inheritDoc}
     */
    @Nonnull
    @Override
    public State getTargetState() {
        return this.targetState;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setTargetState(@Nonnull State state) throws IllegalArgumentException {
        if (state != State.LOADED && state != State.RUNNING) {
            throw new IllegalArgumentException("Invalid target state: " + state);
        }

        this.targetState = state;
    }

    /**
     * {@inheritDoc}
     */
    @Nonnull
    @Override
    public Set<PluginContext> getWiredDependencies() {
        return Collections.unmodifiableSet(this.wiredPluginDependencies);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void wire(@Nonnull PluginContext context) {
        this.wiredPluginDependencies.add(context);
    }

    /**
     * Provides a class visitor capable of locating a plugin main class as well as processing its
     * metadata.
     */
    protected static class LocatorClassVisitor extends ClassVisitor {
        private String className = null;
        private PluginAnnotationVisitor annotationVisitor = null;

        public LocatorClassVisitor() {
            super(Opcodes.ASM5);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public AnnotationVisitor visitTypeAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) {
{'fraction_non_alphanumeric': 0.09585021657118904, 'fraction_numerical': 0.0025150202598854267, 'mean_word_length': 3.154381892048752, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '3635454', 'n_tokens_mistral': 2028, 'n_tokens_neox': 1976, 'n_words': 561}