Skip to content

Commit

Permalink
Switch to jspecify
Browse files Browse the repository at this point in the history
  • Loading branch information
octylFractal committed Jun 26, 2024
1 parent 77f07ad commit 8f33292
Show file tree
Hide file tree
Showing 50 changed files with 283 additions and 280 deletions.
4 changes: 2 additions & 2 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ plugins {
}

dependencies {
compileOnlyApi(libs.jetbrains.annotations)
compileOnlyApi(libs.jspecify.annotations)

testImplementation(platform(libs.junit.bom))
testImplementation(libs.junit.jupiter.api)
testRuntimeOnly(libs.junit.jupiter.engine)

testImplementation(libs.truth.asProvider()) {
testImplementation(libs.truth) {
exclude(group = "junit")
}
}
Expand Down
5 changes: 4 additions & 1 deletion common/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import org.jspecify.annotations.NullMarked;

/**
* The common module of lin-bus, shared between the streaming and tree modules.
*/
@NullMarked
module org.enginehub.linbus.common {
exports org.enginehub.linbus.common;
exports org.enginehub.linbus.common.internal to org.enginehub.linbus.stream, org.enginehub.linbus.tree,
org.enginehub.linbus.format.snbt;
requires static org.jetbrains.annotations;
requires static transitive org.jspecify;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package org.enginehub.linbus.common;

import org.jspecify.annotations.Nullable;

import java.io.IOException;
import java.util.function.Function;

Expand All @@ -27,7 +29,7 @@
* @param <T> the input type
* @param <R> the output type
*/
public interface IOFunction<T, R> {
public interface IOFunction<T, R extends @Nullable Object> {
/**
* Applies this function to the given argument.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

package org.enginehub.linbus.common.internal;

import org.jetbrains.annotations.Nullable;
import org.jspecify.annotations.Nullable;

import java.util.Iterator;
import java.util.NoSuchElementException;
Expand All @@ -30,7 +30,7 @@
*
* @param <T> the type of items returned by this iterator
*/
public abstract class AbstractIterator<T> implements Iterator<T> {
public abstract class AbstractIterator<T extends @Nullable Object> implements Iterator<T> {
private boolean needNext = true;
private boolean end;
private T next;
Expand Down
4 changes: 2 additions & 2 deletions format-snbt/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

dependencies {
compileOnlyApi(libs.jetbrains.annotations)
compileOnlyApi(libs.jspecify.annotations)

api(project(":common"))
api(project(":stream"))
Expand All @@ -17,7 +17,7 @@ dependencies {
testImplementation(libs.junit.jupiter.api)
testRuntimeOnly(libs.junit.jupiter.engine)

testImplementation(libs.truth.asProvider()) {
testImplementation(libs.truth) {
exclude(group = "junit")
}
}
Expand Down
5 changes: 4 additions & 1 deletion format-snbt/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import org.jspecify.annotations.NullMarked;

/**
* The tree module of lin-bus. Presents a standard tree structure of NBT. Internally, everything is parsed using the
* streaming module.
*/
@NullMarked
module org.enginehub.linbus.format.snbt {
exports org.enginehub.linbus.format.snbt;
requires static transitive org.jetbrains.annotations;
requires static transitive org.jspecify;
requires transitive org.enginehub.linbus.common;
requires transitive org.enginehub.linbus.stream;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.enginehub.linbus.format.snbt.impl.reader.LinSnbtTokenizer;
import org.enginehub.linbus.stream.LinStream;
import org.enginehub.linbus.stream.LinStreamable;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.Reader;
Expand Down Expand Up @@ -54,7 +53,7 @@ public class LinStringIO {
* @param input the input to read from
* @return the stream of NBT tokens
*/
public static @NotNull LinStream read(@NotNull Reader input) {
public static LinStream read(Reader input) {
return new LinSnbtReader(new LinSnbtTokenizer(input));
}

Expand All @@ -64,7 +63,7 @@ public class LinStringIO {
* @param input the input to read from
* @return the stream of NBT tokens
*/
public static @NotNull LinStream readFromString(@NotNull String input) {
public static LinStream readFromString(String input) {
return read(new StringReader(input));
}

Expand All @@ -86,7 +85,7 @@ public class LinStringIO {
* @return the result
* @throws IOException if an I/O error occurs
*/
public static <R> R readUsing(@NotNull Reader input, @NotNull IOFunction<? super @NotNull LinStream, R> transform) throws IOException {
public static <R> R readUsing(Reader input, IOFunction<? super LinStream, R> transform) throws IOException {
return transform.apply(read(input));
}

Expand All @@ -98,7 +97,7 @@ public static <R> R readUsing(@NotNull Reader input, @NotNull IOFunction<? super
* @param <R> the type of the result
* @return the stream of NBT tokens
*/
public static <R> R readFromStringUsing(@NotNull String input, @NotNull IOFunction<? super @NotNull LinStream, R> transform) {
public static <R> R readFromStringUsing(String input, IOFunction<? super LinStream, R> transform) {
try {
return transform.apply(readFromString(input));
} catch (IOException e) {
Expand All @@ -117,7 +116,7 @@ public static <R> R readFromStringUsing(@NotNull String input, @NotNull IOFuncti
* @param tokens the stream of NBT tokens
* @throws IOException if an I/O error occurs
*/
public static void write(@NotNull Appendable output, @NotNull LinStreamable tokens) throws IOException {
public static void write(Appendable output, LinStreamable tokens) throws IOException {
new LinSnbtWriter().write(output, tokens.linStream());
}

Expand All @@ -131,7 +130,7 @@ public static void write(@NotNull Appendable output, @NotNull LinStreamable toke
* @param tokens the stream of NBT tokens
* @return the string
*/
public static String writeToString(@NotNull LinStreamable tokens) {
public static String writeToString(LinStreamable tokens) {
var builder = new StringBuilder();
try {
write(builder, tokens);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.enginehub.linbus.stream.LinStreamable;
import org.enginehub.linbus.stream.exception.NbtWriteException;
import org.enginehub.linbus.stream.token.LinToken;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -55,7 +54,7 @@ record WritingArray() implements WriteState {
* @param tokens the tokens
* @throws IOException if an I/O error occurs
*/
public void write(@NotNull Appendable output, @NotNull LinStream tokens) throws IOException {
public void write(Appendable output, LinStream tokens) throws IOException {
while (true) {
var state = stateStack.peekLast();
LinToken token = tokens.nextOrNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
import org.enginehub.linbus.stream.LinStream;
import org.enginehub.linbus.stream.exception.NbtParseException;
import org.enginehub.linbus.stream.token.LinToken;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jspecify.annotations.Nullable;

import java.io.DataInput;
import java.nio.Buffer;
Expand Down Expand Up @@ -114,7 +113,7 @@ record ReadValue(boolean mustBeCompound) implements State {

private static final int BUFFER_SIZE = 4096;

private final Iterator<? extends @NotNull SnbtTokenWithMetadata> input;
private final Iterator<? extends SnbtTokenWithMetadata> input;
/**
* The state stack. We're currently on the one that's LAST.
*/
Expand All @@ -126,22 +125,22 @@ record ReadValue(boolean mustBeCompound) implements State {
/**
* The pushback token stack, which is used to push back tokens that we've read.
*/
private final Deque<@NotNull SnbtTokenWithMetadata> readAgainStack;
private final Deque<SnbtTokenWithMetadata> readAgainStack;
private int charIndex;

/**
* Creates a new reader.
*
* @param input the input to read from
*/
public LinSnbtReader(Iterator<? extends @NotNull SnbtTokenWithMetadata> input) {
public LinSnbtReader(Iterator<? extends SnbtTokenWithMetadata> input) {
this.input = input;
this.stateStack = new ArrayDeque<>(List.of(new State.ReadValue(true)));
this.tokenQueue = new ArrayDeque<>();
this.readAgainStack = new ArrayDeque<>();
}

private @NotNull SnbtTokenWithMetadata read() {
private SnbtTokenWithMetadata read() {
var token = readAgainStack.pollFirst();
if (token != null) {
return token;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.enginehub.linbus.format.snbt.impl.Elusion;
import org.enginehub.linbus.stream.exception.NbtParseException;
import org.enginehub.linbus.stream.token.LinToken;
import org.jetbrains.annotations.NotNull;
import org.jspecify.annotations.Nullable;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -32,7 +32,7 @@
/**
* But not like, a tokenizer into {@link LinToken}s. But like our own internal {@link SnbtToken}. Sigh.
*/
public class LinSnbtTokenizer extends AbstractIterator<@NotNull SnbtTokenWithMetadata> {
public class LinSnbtTokenizer extends AbstractIterator<SnbtTokenWithMetadata> {
private final Reader input;
private int charIndex = -1;
private boolean eatAllWhitespaceAfter;
Expand All @@ -42,7 +42,7 @@ public class LinSnbtTokenizer extends AbstractIterator<@NotNull SnbtTokenWithMet
*
* @param input the input to read from
*/
public LinSnbtTokenizer(@NotNull Reader input) {
public LinSnbtTokenizer(Reader input) {
this.input = input.markSupported() ? input : new BufferedReader(input);
}

Expand All @@ -67,7 +67,7 @@ private void skipWhitespace() throws IOException {
}

@Override
protected SnbtTokenWithMetadata computeNext() {
protected @Nullable SnbtTokenWithMetadata computeNext() {
try {
skipWhitespace();
input.mark(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@NullMarked
package org.enginehub.linbus.format.snbt.impl;

import org.jspecify.annotations.NullMarked;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@NullMarked
package org.enginehub.linbus.format.snbt.impl.reader;

import org.jspecify.annotations.NullMarked;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@NullMarked
package org.enginehub.linbus.format.snbt;

import org.jspecify.annotations.NullMarked;
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

package org.enginehub.linbus.format.snbt.util;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
Expand Down Expand Up @@ -55,7 +53,7 @@ private boolean isRelevantStackFrame(StackWalker.StackFrame frame) {
}

@Override
public int read(@NotNull CharBuffer target) throws IOException {
public int read(CharBuffer target) throws IOException {
int result = delegate.read(target);
System.err.println("read(" + target + "): " + result);
printRelevantStack();
Expand All @@ -75,15 +73,15 @@ public int read() throws IOException {
}

@Override
public int read(char @NotNull [] cbuf) throws IOException {
public int read(char[] cbuf) throws IOException {
int result = delegate.read(cbuf);
System.err.println("read(char[].length = " + cbuf.length + "): " + result);
printRelevantStack();
return result;
}

@Override
public int read(char @NotNull [] cbuf, int off, int len) throws IOException {
public int read(char[] cbuf, int off, int len) throws IOException {
int result = delegate.read(cbuf, off, len);
System.err.println("read(char[].length = " + cbuf.length + ", off = " + off + ", len = " + len + "): " + result);
printRelevantStack();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@NullMarked
package org.enginehub.linbus.format.snbt.util;

import org.jspecify.annotations.NullMarked;
10 changes: 2 additions & 8 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
[versions]
truth = "1.4.2"

[libraries]
jetbrains-annotations = "org.jetbrains:annotations:24.1.0"
jspecify-annotations = "org.jspecify:jspecify:0.3.0"
guava = "com.google.guava:guava:31.0.1-jre"
junit-bom = "org.junit:junit-bom:5.10.2"
junit-jupiter-api.module = "org.junit.jupiter:junit-jupiter-api"
junit-jupiter-engine.module = "org.junit.jupiter:junit-jupiter-engine"
truth.module = "com.google.truth:truth"
truth.version.ref = "truth"
truth-extensions-java8.module = "com.google.truth.extensions:truth-java8-extension"
truth-extensions-java8.version.ref = "truth"
truth = "com.google.truth:truth:1.4.2"
7 changes: 2 additions & 5 deletions stream/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@ plugins {
}

dependencies {
compileOnlyApi(libs.jetbrains.annotations)
compileOnlyApi(libs.jspecify.annotations)

api(project(":common"))

testImplementation(platform(libs.junit.bom))
testImplementation(libs.junit.jupiter.api)
testRuntimeOnly(libs.junit.jupiter.engine)

testImplementation(libs.truth.asProvider()) {
exclude(group = "junit")
}
testImplementation(libs.truth.extensions.java8) {
testImplementation(libs.truth) {
exclude(group = "junit")
}
}
Expand Down
5 changes: 4 additions & 1 deletion stream/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import org.jspecify.annotations.NullMarked;

/**
* The streaming module of lin-bus. Features a Jackson-like API for decoding and encoding NBT.
*/
@NullMarked
module org.enginehub.linbus.stream {
exports org.enginehub.linbus.stream;
exports org.enginehub.linbus.stream.exception;
exports org.enginehub.linbus.stream.token;
exports org.enginehub.linbus.stream.internal to org.enginehub.linbus.tree;
requires static transitive org.jetbrains.annotations;
requires static transitive org.jspecify;
requires transitive org.enginehub.linbus.common;
}
Loading

0 comments on commit 8f33292

Please sign in to comment.