Skip to content

Commit

Permalink
[nullsafe] Mark @nullsafe with @TypeQualifierDefault&Co for Kotlin in…
Browse files Browse the repository at this point in the history
…terop

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
  • Loading branch information
artempyanykh authored and facebook-github-bot committed Apr 30, 2020
1 parent c2ec55f commit 064b478
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
Binary file not shown.
3 changes: 2 additions & 1 deletion infer/annotations/Makefile
Expand Up @@ -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'
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions infer/annotations/pom.xml
Expand Up @@ -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>
Expand Down
Expand Up @@ -11,9 +11,22 @@
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}.
Expand Down

0 comments on commit 064b478

Please sign in to comment.