/* * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ package codetoanalyze.java.nullsafe_default; import com.facebook.infer.annotation.NullsafeStrict; import javax.annotation.Nullable; import some.test.pckg.ThirdPartyTestClass; /** * In this test, we test how Strict mode works for calls of 3rd party libraries, and how detection * differs based on if the function is whitelisted or not in 3rd party signatures repository. */ @NullsafeStrict public class StrictModeForThirdParty { ThirdPartyTestClass obj; StrictModeForThirdParty() { obj = new ThirdPartyTestClass(); } public @Nullable String getNullable() { return null; } public String getNonnull() { return ""; } // Return values. // In strict mode, return values should be pessimistically treated as nullable // if the function is unspecified, and treated according to their return annotation if // the function is whitelisted in the 3rd party repo. public void dereferenceUnspecifiedIsBAD() { obj.returnUnspecified().toString(); } public void dereferenceSpecifiedAsNullableIsBAD() { obj.returnSpecifiedAsNullable().toString(); } public void dereferenceSpecifiedAsNonnullIsOK() { obj.returnSpecifiedAsNonnull().toString(); } // Params. // In strict mode, params should be pessimistically treated as non-nullable if the function is // unspecified, // and treated based on their annotation if the function is whitelisted in the 3rd party repo. public void passingNullableParamToUnspecifiedIsBAD() { obj.paramUnspecified(getNullable()); } public void passingNonnullParamToUnspecifiedIsOK() { obj.paramUnspecified(getNonnull()); } public void passingNullableToParamSpecifiedAsNonnullIsBAD() { obj.secondParamSpecifiedAsNonnull(getNonnull(), getNullable()); } public void passingNullableToParamSpecifiedAsNullableIsOK() { // first param is explicitly whitelisted as specified as nullable, so everything is OK obj.secondParamSpecifiedAsNonnull(getNullable(), getNonnull()); } public void passingNonnullToParamIsOK() { // Independently of param signature, it is safe to pass non-nullables obj.secondParamSpecifiedAsNonnull(getNonnull(), getNonnull()); } }