You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.4 KiB
83 lines
2.4 KiB
5 years ago
|
/*
|
||
|
* 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();
|
||
|
}
|
||
|
|
||
5 years ago
|
public void dereferenceFieldIsBAD() {
|
||
|
obj.nonNullableField.toString();
|
||
|
}
|
||
|
|
||
5 years ago
|
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());
|
||
|
}
|
||
|
}
|