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.
209 lines
4.7 KiB
209 lines
4.7 KiB
10 years ago
|
/*
|
||
6 years ago
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||
9 years ago
|
*
|
||
7 years ago
|
* This source code is licensed under the MIT license found in the
|
||
|
* LICENSE file in the root directory of this source tree.
|
||
9 years ago
|
*/
|
||
10 years ago
|
|
||
5 years ago
|
package codetoanalyze.java.nullsafe_default;
|
||
10 years ago
|
|
||
6 years ago
|
import external.library.SomeExternalClass;
|
||
10 years ago
|
import javax.annotation.Nullable;
|
||
|
|
||
5 years ago
|
interface VariousMethods {
|
||
|
String valBoth(String arg);
|
||
10 years ago
|
|
||
5 years ago
|
@Nullable
|
||
|
String nullableReturn(String arg);
|
||
|
|
||
|
String nullableArg(@Nullable String arg);
|
||
|
|
||
|
@Nullable
|
||
|
String nullableBoth(@Nullable String arg);
|
||
|
}
|
||
|
|
||
|
interface Overloads {
|
||
|
String overload(int arg);
|
||
|
|
||
|
String overload(@Nullable String arg);
|
||
|
|
||
|
String overload(String arg1, int arg2);
|
||
|
|
||
|
String overload(String arg1, String arg2);
|
||
5 years ago
|
|
||
|
void notOverload(@Nullable Object arg);
|
||
5 years ago
|
}
|
||
|
|
||
|
// Check return annotations
|
||
|
|
||
|
abstract class ReturnValToNullBAD implements VariousMethods {
|
||
|
@Nullable
|
||
|
public String valBoth(String arg) {
|
||
|
return null;
|
||
10 years ago
|
}
|
||
5 years ago
|
}
|
||
10 years ago
|
|
||
5 years ago
|
abstract class ReturnNullToValOK implements VariousMethods {
|
||
|
public abstract String nullableReturn(String arg);
|
||
|
}
|
||
10 years ago
|
|
||
5 years ago
|
abstract class ReturnValFromValAndNullFromNullOK implements VariousMethods {
|
||
|
@Nullable
|
||
|
public String nullableReturn(String arg) {
|
||
|
return null;
|
||
|
}
|
||
10 years ago
|
|
||
5 years ago
|
public String valBoth(String arg) {
|
||
|
return arg;
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
5 years ago
|
abstract class AbstractReturnValToNullFN implements VariousMethods {
|
||
|
// An abstract override method with inconsistent signature is not reported
|
||
|
@Nullable
|
||
|
public abstract String valBoth(String arg);
|
||
|
}
|
||
|
|
||
|
// Check parameter annotations
|
||
10 years ago
|
|
||
5 years ago
|
abstract class ArgValToNullOK implements VariousMethods {
|
||
|
public String valBoth(@Nullable String arg) {
|
||
|
return "OK";
|
||
10 years ago
|
}
|
||
5 years ago
|
}
|
||
10 years ago
|
|
||
5 years ago
|
abstract class ArgNullToValBAD implements VariousMethods {
|
||
|
public String nullableArg(String arg) {
|
||
|
return arg;
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
5 years ago
|
abstract class ArgNullToValForInterfaceInAnotherFileBAD
|
||
|
implements InconsistentSubclassAnnotationInterface {
|
||
|
public String implementInAnotherFile(String s) {
|
||
|
return "BAD";
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
5 years ago
|
abstract class ArgValToValAndNullToNullOK implements VariousMethods {
|
||
|
public String valBoth(String arg) {
|
||
|
return arg;
|
||
10 years ago
|
}
|
||
|
|
||
5 years ago
|
@Nullable
|
||
|
public String nullableBoth(@Nullable String arg) {
|
||
|
return arg;
|
||
10 years ago
|
}
|
||
5 years ago
|
}
|
||
10 years ago
|
|
||
5 years ago
|
// Check overrides + overloads
|
||
10 years ago
|
|
||
5 years ago
|
// These are 'good' cases with real overrides
|
||
5 years ago
|
abstract class OverrideExistingCorrectlyOK implements Overloads {
|
||
|
public String overload(int arg) {
|
||
|
return "OK";
|
||
10 years ago
|
}
|
||
|
|
||
5 years ago
|
public String overload(@Nullable String arg) {
|
||
5 years ago
|
return "OK";
|
||
5 years ago
|
}
|
||
10 years ago
|
|
||
5 years ago
|
public String overload(String arg1, int arg2) {
|
||
|
return arg1;
|
||
|
}
|
||
10 years ago
|
|
||
5 years ago
|
public String overload(String arg1, String arg2) {
|
||
|
return arg1;
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
abstract class NoOverrideSinceDifferentTypesOK implements Overloads {
|
||
5 years ago
|
@Nullable
|
||
|
public String overload(Object arg) {
|
||
5 years ago
|
return null;
|
||
5 years ago
|
}
|
||
|
|
||
|
public String overload(Double arg) {
|
||
|
return arg.toString();
|
||
10 years ago
|
}
|
||
5 years ago
|
|
||
|
// Although, String is a subtype of Object, this method is not an override
|
||
|
public void notOverload(String arg) {
|
||
|
return;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
5 years ago
|
// This is just a smoke test to check that incorrect overrides of overloaded methods get reported
|
||
|
abstract class OverloadExistingIncorrectBAD implements Overloads {
|
||
|
@Nullable
|
||
|
public String overload(String arg1, String arg2) {
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Check constructors
|
||
|
|
||
10 years ago
|
class ConstructorsAreExcluded {
|
||
|
class Base {
|
||
6 years ago
|
Base(@Nullable String s) {}
|
||
10 years ago
|
}
|
||
|
|
||
|
class Derived extends Base {
|
||
6 years ago
|
Derived(String s) { // OK: there's no sub-typing between constructors
|
||
10 years ago
|
super(s);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// Check interop with external libraries
|
||
|
|
||
6 years ago
|
class ExtendsExternalLibrary extends SomeExternalClass {
|
||
|
|
||
|
@Override
|
||
|
public @Nullable Object externalMethod1() {
|
||
|
// subtyping error on the return type not reported as we cannot
|
||
|
// rely on the external libraries to be correctly annotated
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void externalMethod2(Object object) {
|
||
|
// subtyping error on the parameter type are reported
|
||
|
}
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
// Check that 1) we have a special error message for lack of annotation in this method and 2) treat
|
||
|
// `x` as implicitly nullable
|
||
5 years ago
|
class JavaLangEquals {
|
||
|
@Override
|
||
|
public boolean equals(Object x) {
|
||
5 years ago
|
// BAD: x can not be directly dereferenced without null comparison:
|
||
|
// it is implicitly nullable because Java requires `x.equals(null)` to work correctly.
|
||
|
// It is a common enough case to make the nullsafe support this specifically.
|
||
|
return x.toString() == "JavaLangEquals";
|
||
5 years ago
|
}
|
||
|
}
|
||
5 years ago
|
|
||
|
// Check multiple interfaces in the inheritance chain
|
||
|
interface NullableGetter {
|
||
|
@Nullable
|
||
|
String get();
|
||
|
}
|
||
|
|
||
|
interface NonNullableInterfaceGetterOK extends NullableGetter {
|
||
|
String get();
|
||
|
}
|
||
|
|
||
|
class NonNullableConcreteGetterOK implements NonNullableInterfaceGetterOK {
|
||
|
public String get() {
|
||
|
return "OK";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class NullableConcreteGetterBAD implements NonNullableInterfaceGetterOK {
|
||
|
@Nullable
|
||
|
public String get() {
|
||
|
return null;
|
||
|
}
|
||
|
}
|