<commit_before>/*
 * Copyright 2019, TeamDev. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package io.spine.server.model;

import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Immutable;
import com.google.errorprone.annotations.OverridingMethodsMustInvokeSuper;
import com.google.protobuf.Message;
import io.spine.core.MessageEnvelope;
import io.spine.server.model.declare.ParameterSpec;
import io.spine.type.MessageClass;
import org.checkerframework.checker.nullness.qual.Nullable;

import javax.annotation.PostConstruct;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.lang.String.format;

/**
 * An abstract base for wrappers over methods handling messages.
 *
 * <p>Two message handlers are equivalent when they refer to the same method on the
 * same object (not class).
 *
 * @param <T> the type of the target object
 * @param <M> the type of the message handled by this method
 * @param <C> the type of the message class
 * @param <E> the type of message envelopes, in which the messages to handle are wrapped
 * @param <R> the type of the method invocation result
 */
@Immutable
public abstract class AbstractHandlerMethod<T,
                                            M extends Message,
                                            C extends MessageClass<M>,
                                            E extends MessageEnvelope<?, ?, ?>,
                                            P extends MessageClass<?>,
                                            R extends MethodResult<?>>
        implements HandlerMethod<T, C, E, P, R> {

    /** The method to be called. */
    @SuppressWarnings("Immutable")
    private final Method method;

    /** The class of the first parameter. */
    private final Class<M> messageClass;

    /**
     * The set of the metadata attributes set via method annotations.
     *
     * @implNote Even though that {@code MethodAttribute} is parameterized with {@code Object},
     * which is mutable, the {@code @Immutable} annotation of {@code MethodAttribute} class
     * ensures that we don't have mutable types passed as generic parameters.
     *
     * <p>This field is initialized in {@link #discoverAttributes()} to allow derived classes to
     * {@linkplain #attributeSuppliers() customize} the set of supported method attributes.
     */
    @SuppressWarnings("Immutable")
    private ImmutableSet<MethodAttribute<?>> attributes;

    /**
     * The specification of parameters for this method.
     *
     * @implNote It serves to extract the argument values from the {@linkplain E envelope} used
     * as a source for the method call.
     */
    private final ParameterSpec<E> parameterSpec;

    private final HandlerReturnType<P> returnType;

    /**
     * Creates a new instance to wrap {@code method} on {@code target}.
     * @param method
     *         subscriber method
     * @param parameterSpec
     *         the specification of method parameters
     */
    protected AbstractHandlerMethod(Method method, ParameterSpec<E> parameterSpec) {
        this.method = checkNotNull(method);
        this.messageClass = getFirstParamType(method);
        this.attributes = discoverAttributes(method);
        this.parameterSpec = parameterSpec;
        this.returnType = HandlerReturnType.of(method);

        method.setAccessible(true);
    }
<commit_msg>Fix formatting typo<commit_after>/*
 * Copyright 2019, TeamDev. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package io.spine.server.model;

import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Immutable;
import com.google.errorprone.annotations.OverridingMethodsMustInvokeSuper;
import com.google.protobuf.Message;
import io.spine.core.MessageEnvelope;
import io.spine.server.model.declare.ParameterSpec;
import io.spine.type.MessageClass;
import org.checkerframework.checker.nullness.qual.Nullable;

import javax.annotation.PostConstruct;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.lang.String.format;

/**
 * An abstract base for wrappers over methods handling messages.
 *
 * <p>Two message handlers are equivalent when they refer to the same method on the
 * same object (not class).
 *
 * @param <T> the type of the target object
 * @param <M> the type of the message handled by this method
 * @param <C> the type of the message class
 * @param <E> the type of message envelopes, in which the messages to handle are wrapped
 * @param <R> the type of the method invocation result
 */
@Immutable
public abstract class AbstractHandlerMethod<T,
                                            M extends Message,
                                            C extends MessageClass<M>,
                                            E extends MessageEnvelope<?, ?, ?>,
                                            P extends MessageClass<?>,
                                            R extends MethodResult<?>>
        implements HandlerMethod<T, C, E, P, R> {

    /** The method to be called. */
    @SuppressWarnings("Immutable")
    private final Method method;

    /** The class of the first parameter. */
    private final Class<M> messageClass;

    /**
     * The set of the metadata attributes set via method annotations.
     *
     * @implNote Even though that {@code MethodAttribute} is parameterized with {@code Object},
     * which is mutable, the {@code @Immutable} annotation of {@code MethodAttribute} class
     * ensures that we don't have mutable types passed as generic parameters.
     *
     * <p>This field is initialized in {@link #discoverAttributes()} to allow derived classes to
     * {@linkplain #attributeSuppliers() customize} the set of supported method attributes.
     */
    @SuppressWarnings("Immutable")
    private ImmutableSet<MethodAttribute<?>> attributes;

    /**
     * The specification of parameters for this method.
     *
     * @implNote It serves to extract the argument values from the {@linkplain E envelope} used
     * as a source for the method call.
     */
    private final ParameterSpec<E> parameterSpec;

    private final HandlerReturnType<P> returnType;

    /**
     * Creates a new instance to wrap {@code method} on {@code target}.
     *
     * @param method
     *         subscriber method
     * @param parameterSpec
     *         the specification of method parameters
     */
    protected AbstractHandlerMethod(Method method, ParameterSpec<E> parameterSpec) {
        this.method = checkNotNull(method);
        this.messageClass = getFirstParamType(method);
        this.attributes = discoverAttributes(method);
        this.parameterSpec = parameterSpec;
        this.returnType = HandlerReturnType.of(method);

        method.setAccessible(true);
    }

{'fraction_non_alphanumeric': 0.07862969004893965, 'fraction_numerical': 0.0008700380641653072, 'mean_word_length': 3.1367521367521367, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6648433', 'n_tokens_mistral': 2362, 'n_tokens_neox': 2230, 'n_words': 904}