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.
137 lines
2.4 KiB
137 lines
2.4 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;
|
||
|
|
||
|
class SubclassExample {
|
||
|
|
||
|
class T {
|
||
6 years ago
|
public void f() {}
|
||
10 years ago
|
}
|
||
|
|
||
|
class A {
|
||
|
|
||
|
public T foo() {
|
||
|
return new T();
|
||
|
}
|
||
|
|
||
6 years ago
|
public @Nullable T bar() {
|
||
10 years ago
|
return null;
|
||
|
}
|
||
|
|
||
|
public void deref(@Nullable T t) {
|
||
|
if (t != null) {
|
||
|
t.f();
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
public void noDeref(T t) {}
|
||
10 years ago
|
}
|
||
|
|
||
|
class B extends A {
|
||
|
|
||
6 years ago
|
public @Nullable T foo() {
|
||
10 years ago
|
return null;
|
||
|
}
|
||
|
|
||
|
public T bar() {
|
||
|
return new T();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
interface I {
|
||
|
public T baz();
|
||
|
}
|
||
|
|
||
|
class C implements I {
|
||
|
|
||
6 years ago
|
public @Nullable T baz() {
|
||
10 years ago
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class D extends A {
|
||
|
|
||
|
public void deref(T t) {
|
||
|
t.f();
|
||
|
}
|
||
|
|
||
|
public void noDeref(@Nullable T t) {
|
||
|
if (t != null) {
|
||
|
t.f();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
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);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
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
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
public class InconsistentSubclassAnnotation implements InconsistentSubclassAnnotationInterface {
|
||
10 years ago
|
|
||
|
public static void callFromSuperclass(SubclassExample.A a) {
|
||
|
SubclassExample.T t = a.foo();
|
||
|
t.f();
|
||
|
}
|
||
|
|
||
|
public static void callWithNullableParam(SubclassExample.A a, @Nullable SubclassExample.T t) {
|
||
|
a.deref(t);
|
||
|
}
|
||
|
|
||
9 years ago
|
public String implementInAnotherFile(String s) {
|
||
|
return "";
|
||
|
}
|
||
6 years ago
|
|
||
|
public @Nullable Object overloadedMethod() {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
6 years ago
|
public Object overloadedMethod(Object object) {
|
||
6 years ago
|
return object;
|
||
|
}
|
||
10 years ago
|
}
|
||
6 years ago
|
|
||
|
class Super {
|
||
|
String overloadingMethodLookupFP(int i) {
|
||
|
return Integer.toString(i);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class Sub extends Super {
|
||
|
@Nullable
|
||
|
String overloadingMethodLookupFP(Object object) {
|
||
|
return null;
|
||
|
}
|
||
|
}
|