[nullsafe] Mark @Nullsafe with @TypeQualifierDefault&Co for Kotlin interop

Summary:
Kotlin has an experimental support for [JSR-305 custom nullability
qualifiers](https://github.com/Kotlin/KEEP/blob/master/proposals/jsr-305-custom-nullability-qualifiers.md).

Annotating Nullsafe in a special way makes kotlinc recognize it as
such custom nullability qualifier and therefore treat types coming
from Nullsafe Java classes **not** as platform types, but rather
proper nonnull/nullable, which affects:
1. Generated bytecode (more thorough null-checks).
2. Type inference in the IDE.

NOTE re: p.1: one might expect that with properly annotated Java code
Kotlin would avoid inserting runtime checks. This is not how
Kotlin-Java interop works - in reality Kotlin does even more runtime
checking for Java code annotated as Nonull, which IMO is a good
thing, since you can't trust Java anyway.

Reviewed By: mityal

Differential Revision: D21278440

fbshipit-source-id: d0598738a
master
Artem Pianykh 5 years ago committed by Facebook GitHub Bot
parent c2ec55fe37
commit 064b4786f1

@ -8,6 +8,7 @@ include $(ROOT_DIR)/Makefile.config
CWD = $(shell pwd)
JSR_JAR = $(DEPENDENCIES_DIR)/java/jsr-305/jsr305.jar
KOTLIN_ANNOT_JAR = $(DEPENDENCIES_DIR)/java/kotlin-annotations/kotlin-annotations-jvm-1.3.72.jar
SOURCES_DIR = src/main/java
ANNOT_SOURCES = $(shell find $(SOURCES_DIR)/com/facebook/infer/annotation -name "*.java")
ANNOT_CLASSES = 'annot_classes'
@ -19,7 +20,7 @@ all: $(ANNOTATIONS_JAR) $(SOURCES_JAR)
$(ANNOTATIONS_JAR): $(ANNOT_SOURCES)
$(MKDIR_P) $(ANNOT_CLASSES)
$(JAVAC) -source 7 -target 7 -cp $(JSR_JAR) $(ANNOT_SOURCES) -d $(ANNOT_CLASSES)
$(JAVAC) -source 7 -target 7 -cp $(JSR_JAR):$(KOTLIN_ANNOT_JAR) $(ANNOT_SOURCES) -d $(ANNOT_CLASSES)
cd $(ANNOT_CLASSES) && jar cvf $(ANNOTATIONS_JAR) com
$(SOURCES_JAR): $(ANNOT_SOURCES)

@ -65,6 +65,12 @@
<artifactId>jsr305</artifactId>
<version>3.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-annotations-jvm -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-annotations-jvm</artifactId>
<version>1.3.72</version>
</dependency>
</dependencies>
<build>

@ -11,9 +11,22 @@ import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.annotation.Nonnull;
import javax.annotation.meta.TypeQualifierDefault;
import kotlin.annotations.jvm.MigrationStatus;
import kotlin.annotations.jvm.UnderMigration;
@Retention(RetentionPolicy.CLASS)
@Target({ElementType.TYPE})
// These 2 annotations are needed for better interop of @Nullsafe with Kotlin,
// essentially telling it that both params and return values are non-null by
// default.
@Nonnull
@TypeQualifierDefault({ElementType.METHOD, ElementType.PARAMETER})
// This annotation is needed for kotlinc to recognize {@code
// TypeQualifierDefault} without explicitly passing -Xjsr305=strict flag (which
// may be problematic in large codebases).
@UnderMigration(status = MigrationStatus.STRICT)
/**
* Configures nullability checking mode of annotated classes; a more general version of {@link
* NullsafeStrict}.

Loading…
Cancel
Save