Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave committed Oct 19, 2021
1 parent 357a3bf commit 11052ca
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea/
*.iml
target/

29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
# avaje-lang
# avaje-lang

Provides `@NonNullAPI` and `@Nullable` for APIs

## Step 1. Add dependency

```xml
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-lang</artifactId>
<version>1.0</version>
</dependency>
```

## Step 2. Put `@NonNullAPI` on either package-info or a type

This defines the return type and parameters to be `@Nonnull` by default

## Step 3. Put `@Nullable` on anything that can be nullable


# How it works

These annotations are meta annotations using `jsr-305` annotations (`javax.annotation`)
and are then honored by most tools - IntelliJ etc.

Note that these annotations are RetentionType CLASS and expected to be used at compile time
only via static analysis and can not be read or used at runtime.
55 changes: 55 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.avaje</groupId>
<artifactId>avaje-lang</artifactId>
<version>1.0</version>

<parent>
<groupId>org.avaje</groupId>
<artifactId>java8-oss</artifactId>
<version>3.2</version>
</parent>

<scm>
<developerConnection>scm:git:git@github.com:avaje/avaje-lang.git</developerConnection>
<tag>HEAD</tag>
</scm>

<dependencies>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jsr305</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.moditect</groupId>
<artifactId>moditect-maven-plugin</artifactId>
<version>1.0.0.RC1</version>
<executions>
<execution>
<id>add-module-infos</id>
<phase>package</phase>
<goals>
<goal>add-module-info</goal>
</goals>
<configuration>
<jvmVersion>9</jvmVersion>
<module>
<moduleInfoFile>src/main/java9/module-info.java</moduleInfoFile>
</module>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
29 changes: 29 additions & 0 deletions src/main/java/io/avaje/lang/NonNullApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.avaje.lang;

import java.lang.annotation.Documented;
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;

/**
* A common annotation to declare that parameters and return values
* are to be considered as non-nullable by default for a given package.
*
* <p>Leverages JSR-305 meta-annotations to indicate nullability in Java to common
* tools with JSR-305 support and used by Kotlin to infer nullability of API.
*
* <p>Should be used in association with Nullable annotations at parameter
* and return value level.
*/
@Target({ElementType.PACKAGE, ElementType.TYPE})
@Retention(RetentionPolicy.CLASS)
@Documented
@Nonnull
@TypeQualifierDefault({ElementType.METHOD, ElementType.PARAMETER})
public @interface NonNullApi {
}

31 changes: 31 additions & 0 deletions src/main/java/io/avaje/lang/Nullable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.avaje.lang;

import java.lang.annotation.Documented;
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.TypeQualifierNickname;
import javax.annotation.meta.When;

/**
* A common annotation to declare that annotated elements can be {@code null}.
*
* <p>Leverages JSR-305 meta-annotations to indicate nullability in Java to common
* tools with JSR-305 support and used by Kotlin to infer nullability.
*
* <p>Should be used at parameter, return value, and field level. Methods override should
* repeat parent {@code @Nullable} annotations unless they behave differently.
*
* <p>Can be used in association with {@code @NonNullApi} to override the default
* non-nullable semantic to nullable.
*/
@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.CLASS)
@Documented
@Nonnull(when = When.MAYBE)
@TypeQualifierNickname
public @interface Nullable {
}
3 changes: 3 additions & 0 deletions src/main/java9/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module io.avaje.lang {
exports io.avaje.lang;
}

0 comments on commit 11052ca

Please sign in to comment.