<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License").
  ~ You may not use this file except in compliance with the License.
  ~ A copy of the License is located at
  ~
  ~  http://aws.amazon.com/apache2.0
  ~
  ~ or in the "license" file accompanying this file. This file 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.
  -->

<FindBugsFilter>

    <!-- Ignore Java "Serializable" problems in the code generator. -->
    <Match>
        <Package name="~software\.amazon\.awssdk\.codegen.*"/>
        <Bug pattern="SE_BAD_FIELD,SE_NO_SERIALVERSIONID,SE_BAD_FIELD_STORE"/>
    </Match>


    <!-- Ignore "confusing name" problems that result of needing to match the inconsistent naming in
    the service model. -->
    <Match>
        <Package name="~software\.amazon\.awssdk\.codegen\.model.*"/>
        <Bug pattern="NM_CONFUSING"/>
    </Match>

    <Match>
        <!-- PZLA_PREFER_ZERO_LENGTH_ARRAYS: This rule causes many false positives, because sometimes we want to return
             null for arrays. -->
        <!-- UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR: This rule causes many false positives, because we may be null
             checking using a library function, like Validate.notNull. -->
        <Bug pattern="PZLA_PREFER_ZERO_LENGTH_ARRAYS,UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR"/>
    </Match>

    <Match>
        <Or>
            <!-- Container classes do not copy arrays for performance reasons at this time. -->
            <Class name="software.amazon.awssdk.protocols.json.JsonContent" />
        </Or>
        <Bug pattern="EI_EXPOSE_REP,EI_EXPOSE_REP2" />
    </Match>

    <Match>
        <!-- Explicitly an unsafe option that the customer must opt into. -->
        <Class name="software.amazon.awssdk.core.BytesWrapper" />
        <Method name="asByteArrayUnsafe" />
        <Bug pattern="EI_EXPOSE_REP" />
    </Match>

    <!-- Delegate closes input stream. -->
    <Match>
        <Class name="software.amazon.awssdk.protocols.ion.internal.IonFactory" />
        <Method name="createParser" />
        <Bug pattern="OBL_UNSATISFIED_OBLIGATION" />
    </Match>

    <!-- We do not have control over these objects being serializable, but we do not intend for them to fully function
         after serialization. -->
    <Match>
        <Or>
            <Class name="software.amazon.awssdk.services.dynamodb.datamodeling.DynamoDBMapper$BatchGetItemException"/>
            <Class name="software.amazon.awssdk.protocols.ion.internal.IonFactory"/>
        </Or>
        <Bug pattern="SE_TRANSIENT_FIELD_NOT_RESTORED" />
    </Match>

    <!-- Protocol test client suppressions. -->
    <Match>
        <Or>
            <Package name="~software\.amazon\.awssdk\.services\.protocol.*"/>
        </Or>
        <!-- URF_UNREAD_FIELD, DLS_DEAD_LOCAL_STORE: Sometimes we have unread variables and fields because they're only
        conditionally used. It's cleaner to just always generate them, even if we may not actually be using them. -->
        <!-- REC_CATCH_EXCEPTION: Sometimes we want to convert runtime exceptions into sdk exceptions, so we catch it and
        wrap it in an sdk-specific exception. -->
        <Bug pattern="URF_UNREAD_FIELD,DLS_DEAD_LOCAL_STORE,REC_CATCH_EXCEPTION" />
    </Match>

    <!-- False positive -->
    <Match>
        <Class name="software.amazon.awssdk.core.ResponseBytes" />
        <Bug pattern="EI_EXPOSE_REP2" />
    </Match>

    <!-- False positive Unconfirmed cast-->
    <Match>
        <Or>
            <Class name="software.amazon.awssdk.core.client.handler.BaseAsyncClientHandler$InterceptorCallingHttpResponseHandler" />
            <Class name="software.amazon.awssdk.core.client.handler.BaseAsyncClientHandler$UnmarshallingSdkHttpResponseHandler" />
            <Class name="software.amazon.awssdk.core.internal.http.async.SyncResponseHandlerAdapter" />
        </Or>
        <Bug pattern="BC_UNCONFIRMED_CAST" />
    </Match>

    <!-- Currently they look the same and we will revisit this when we add APIG -->
    <Match>
        <Or>
            <Class name="software.amazon.awssdk.core.client.handler.SdkAsyncClientHandler" />
            <Class name="software.amazon.awssdk.core.client.handler.SdkSyncClientHandler" />
            <Class name="software.amazon.awssdk.awscore.client.handler.AwsSyncClientHandler" />
            <Class name="software.amazon.awssdk.awscore.client.handler.AwsAsyncClientHandler" />
        </Or>
        <Bug pattern="RI_REDUNDANT_INTERFACES" />
    </Match>

    <Match>
        <Or>
            <Package name="io.netty.handler.codec.http2"/>
            <Class name="software.amazon.awssdk.http.nio.netty.internal.utils.BetterFixedChannelPool" />
        </Or>
        <Bug pattern="BC_UNCONFIRMED_CAST,SIC_INNER_SHOULD_BE_STATIC_ANON,DLS_DEAD_LOCAL_STORE,DM_CONVERT_CASE,NM_CLASS_NOT_EXCEPTION,NP_NULL_ON_SOME_PATH"/>
    </Match>

    <Match>
        <Class name="software.amazon.awssdk.benchmark.marshaller.dynamodb.V1DynamoDbAttributeValue$TestItemUnmarshalling" />
        <Method name="utf8" />
        <Bug pattern="EI_EXPOSE_REP" />
    </Match>

    <Match>
        <Class name="software.amazon.awssdk.benchmark.marshaller.dynamodb.V2DynamoDbAttributeValue$TestItemUnmarshalling" />
        <Method name="utf8" />
        <Bug pattern="EI_EXPOSE_REP" />
    </Match>

    <Match>
        <Class name="software.amazon.awssdk.authcrt.signer.internal.SdkSigningResult" />
        <Method name="getSignature" />
        <Bug pattern="EI_EXPOSE_REP" />
    </Match>

    <Match>
        <Class name="software.amazon.awssdk.http.auth.aws.crt.internal.signer.V4aRequestSigningResult" />
        <Method name="getSignature" />
        <Bug pattern="EI_EXPOSE_REP" />
    </Match>

    <Match>
        <Class name="software.amazon.awssdk.http.auth.aws.crt.internal.signer.RollingSigner" />
        <Bug pattern="EI_EXPOSE_REP" />
    </Match>

    <Match>
        <Class name="software.amazon.awssdk.protocols.json.internal.unmarshall.JsonProtocolUnmarshaller" />
        <Method name="unmarshallStructured" />
        <Bug pattern="BC_UNCONFIRMED_CAST" />
    </Match>

    <!-- We want the content to be restored to the default vaue of null -->
    <Match>
        <Class name="software.amazon.awssdk.http.DefaultSdkHttpFullResponse" />
        <Field name="content" />
        <Bug pattern="SE_TRANSIENT_FIELD_NOT_RESTORED" />
    </Match>

    <!-- Don't understand why these are bugs -->
    <Match>
        <Class name="software.amazon.awssdk.services.protocolec2.DefaultProtocolEc2AsyncClient" />
        <Method name="init" />
        <Bug pattern="BC_UNCONFIRMED_CAST_OF_RETURN_VALUE" />
    </Match>
    <Match>
        <Class name="software.amazon.awssdk.services.protocolec2.DefaultProtocolEc2Client" />
        <Method name="init" />
        <Bug pattern="BC_UNCONFIRMED_CAST_OF_RETURN_VALUE" />
    </Match>

    <!-- https://github.com/spotbugs/spotbugs/issues/600 -->
    <Match>
        <Or>
            <Class name="software.amazon.awssdk.core.interceptor.ClasspathInterceptorChainFactory"/>
            <Class name="software.amazon.awssdk.protocols.json.internal.dom.JsonDomParser"/>
            <Class name="software.amazon.awssdk.testutils.service.AwsIntegrationTestBase"/>
            <Class name="software.amazon.awssdk.protocol.asserts.marshalling.XmlAsserts" />
            <Class name="software.amazon.awssdk.testutils.FileUtils"/>
        </Or>
        <Bug pattern="RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE"/>
    </Match>

    <!-- See SpotBugs bug: https://github.com/spotbugs/spotbugs/issues/600, https://github.com/spotbugs/spotbugs/issues/756 -->
    <Match>
        <Class name="software.amazon.awssdk.internal.http.AbstractFileStoreTlsKeyManagersProvider"/>
        <Method name="createKeyStore"/>
        <Bug pattern="RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE"/>
    </Match>

    <!-- See SpotBugs bug: https://github.com/spotbugs/spotbugs/issues/600, https://github.com/spotbugs/spotbugs/issues/756 -->
    <Match>
        <Class name="software.amazon.awssdk.codegen.emitters.CodeWriter"/>
        <Method name="flush"/>
        <Bug pattern="RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE"/>
    </Match>

    <!-- See SpotBugs bug: https://github.com/spotbugs/spotbugs/issues/600, https://github.com/spotbugs/spotbugs/issues/756 -->
    <Match>
        <Class name="software.amazon.awssdk.core.SdkServiceClientConfiguration$Builder"/>
        <Method name="overrideConfiguration"/>
        <Bug pattern="RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE"/>
    </Match>

    <!-- If we're not sure about the RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT, skip the warning. -->
    <Match>
        <Package name="~software.amazon.awssdk.http.nio.netty.*"/>
        <Bug pattern="RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT"/>
        <Not>
            <Confidence value="1"/>
        </Not>
    </Match>

    <Match>
        <Class name="software.amazon.awssdk.core.internal.waiters.ResponseOrException"/>
        <Bug pattern="NM_CLASS_NOT_EXCEPTION"/>
    </Match>

    <Match>
        <Package name="~software.amazon.awssdk.http.*"/>
        <Bug pattern="URF_UNREAD_FIELD, RV_RETURN_VALUE_IGNORED"/>
    </Match>

    <Match>
        <Class name="software.amazon.awssdk.utils.async.StoringSubscriber"/>
        <Method name="drop"/>
        <Bug pattern="RV_RETURN_VALUE_IGNORED"/>
    </Match>


    <Match>
        <Class name="software.amazon.awssdk.http.crt.internal.response.CrtResponseBodyPublisher" />
        <Method name="subscribe"/>
        <Bug pattern="SIC_INNER_SHOULD_BE_STATIC_ANON"/>
    </Match>

    <!-- on some java versions, the try-with-resources byte code is getting flagged by this -->
    <Match>
        <Class name="software.amazon.awssdk.http.crt.AwsCrtAsyncHttpClient" />
        <Method name="execute"/>
        <Bug pattern="RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE"/>
    </Match>
    <Match>
        <Class name="software.amazon.awssdk.http.crt.AwsCrtAsyncHttpClient"/>
        <Method name="&lt;init&gt;"/>
        <Bug pattern="RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE"/>
    </Match>


    <!-- For forward-compatibility with this having members (i.e. the checksum type) -->
    <Match>
        <Class name="software.amazon.awssdk.core.interceptor.trait.HttpChecksumRequired"/>
        <Method name="create"/>
        <Bug pattern="ISC_INSTANTIATE_STATIC_CLASS"/>
    </Match>

    <!-- This is very buggy https://github.com/spotbugs/spotbugs/issues/1539 -->
    <Match>
        <Bug pattern="DMI_RANDOM_USED_ONLY_ONCE" />
    </Match>

    <!-- Uses these methods legitimately. -->
    <Match>
        <Or>
            <Class name="software.amazon.awssdk.http.SdkHttpHeaders"/>
            <Class name="software.amazon.awssdk.http.SdkHttpRequest"/>
        </Or>
        <Bug pattern="SDK_BAD_METHOD_CALL"/>
    </Match>

    <!-- False positive on JDK 11+ -->
    <Match>
        <Class name="software.amazon.awssdk.transfer.s3.model.ResumableFileDownload"/>
        <Method name="fromFile"/>
        <Bug pattern="RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE"/>
    </Match>

    <!-- False positive on JDK 11+ -->
    <Match>
        <Class name="software.amazon.awssdk.transfer.s3.model.ResumableFileUpload"/>
        <Method name="fromFile"/>
        <Bug pattern="RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE"/>
    </Match>

    <!-- Intentional for backwards-compatibility -->
    <Match>
        <Or>
            <Class name="software.amazon.awssdk.auth.signer.AwsSignerExecutionAttribute"/>
            <Class name="software.amazon.awssdk.auth.signer.S3SignerExecutionAttribute"/>
        </Or>
        <Bug pattern="NP_BOOLEAN_RETURN_NULL"/>
    </Match>

    <!-- New flags as of spotbogs 4.7.3.5 -->
    <!-- TODO: Fix or explicitly exclude each occurrence -->
     <Match>
        <Bug pattern="EI_EXPOSE_REP2,EI_EXPOSE_REP,MS_EXPOSE_REP,MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR,SA_FIELD_SELF_ASSIGNMENT,DCN_NULLPOINTER_EXCEPTION"/>
    </Match>
</FindBugsFilter>
