Skip to content

Commit 98217aa

Browse files
committed
Annotated serialization interfaces with @Contract
Added `@Contract` annotations to methods in serialization-related interfaces to enhance code contracts, improve purity clarity, and document immutability expectations.
1 parent 13ded9d commit 98217aa

File tree

6 files changed

+20
-0
lines changed

6 files changed

+20
-0
lines changed

nbt/src/main/java/core/nbt/serialization/NBT.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public sealed interface NBT extends TagSerializationContext, TagDeserializationC
1717
*
1818
* @return a new Builder instance for constructing NBT objects
1919
*/
20+
@Contract(value = " -> new", pure = true)
2021
static Builder builder() {
2122
return new SimpleNBT.Builder();
2223
}
@@ -30,6 +31,7 @@ static Builder builder() {
3031
* @return the deserialized object of the specified type
3132
* @deprecated use {@link #deserialize(Tag, Class)} instead
3233
*/
34+
@Contract(pure = true)
3335
@Deprecated(forRemoval = true, since = "2.4.0")
3436
default <T> T fromTag(Tag tag, Class<T> type) {
3537
return deserialize(tag, type);
@@ -44,6 +46,7 @@ default <T> T fromTag(Tag tag, Class<T> type) {
4446
* @return the deserialized object of the specified type
4547
* @deprecated use {@link #deserialize(Tag, Type)} instead
4648
*/
49+
@Contract(pure = true)
4750
@Deprecated(forRemoval = true, since = "2.4.0")
4851
default <T> T fromTag(Tag tag, Type type) {
4952
return deserialize(tag, type);
@@ -56,6 +59,7 @@ default <T> T fromTag(Tag tag, Type type) {
5659
* @return the Tag representation of the provided object
5760
* @deprecated use {@link #serialize(Object)} instead
5861
*/
62+
@Contract(value = "_ -> new", pure = true)
5963
@Deprecated(forRemoval = true, since = "2.4.0")
6064
default Tag toTag(Object object) {
6165
return serialize(object);
@@ -69,6 +73,7 @@ default Tag toTag(Object object) {
6973
* @return the serialized tag representation of the object
7074
* @deprecated use {@link #serialize(Object, Class)} instead
7175
*/
76+
@Contract(value = "_, _ -> new", pure = true)
7277
@Deprecated(forRemoval = true, since = "2.4.0")
7378
default Tag toTag(Object object, Class<?> type) {
7479
return serialize(object, type);
@@ -82,6 +87,7 @@ default Tag toTag(Object object, Class<?> type) {
8287
* @return the serialized tag representation of the object
8388
* @deprecated use {@link #serialize(Object, Type)} instead
8489
*/
90+
@Contract(value = "_, _ -> new", pure = true)
8591
@Deprecated(forRemoval = true, since = "2.4.0")
8692
default Tag toTag(Object object, Type type) {
8793
return serialize(object, type);

nbt/src/main/java/core/nbt/serialization/TagDeserializationContext.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package core.nbt.serialization;
22

33
import core.nbt.tag.Tag;
4+
import org.jetbrains.annotations.Contract;
45
import org.jspecify.annotations.NullMarked;
56

67
import java.lang.reflect.Type;
@@ -19,6 +20,7 @@ public interface TagDeserializationContext {
1920
* @return an object of the specified type deserialized from the tag
2021
* @throws ParserException if an error occurs during deserialization
2122
*/
23+
@Contract(pure = true)
2224
<T> T deserialize(Tag tag, Class<T> type) throws ParserException;
2325

2426
/**
@@ -30,5 +32,6 @@ public interface TagDeserializationContext {
3032
* @return an object of the specified type deserialized from the tag
3133
* @throws ParserException if an error occurs during deserialization
3234
*/
35+
@Contract(pure = true)
3336
<T> T deserialize(Tag tag, Type type) throws ParserException;
3437
}

nbt/src/main/java/core/nbt/serialization/TagDeserializer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package core.nbt.serialization;
22

33
import core.nbt.tag.Tag;
4+
import org.jetbrains.annotations.Contract;
45
import org.jspecify.annotations.NullMarked;
56

67
/**
@@ -18,5 +19,6 @@ public interface TagDeserializer<T> {
1819
* @return the deserialized object of type T
1920
* @throws ParserException if an error occurs during deserialization
2021
*/
22+
@Contract(pure = true)
2123
T deserialize(Tag tag, TagDeserializationContext context) throws ParserException;
2224
}

nbt/src/main/java/core/nbt/serialization/TagSerializable.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package core.nbt.serialization;
22

33
import core.nbt.tag.Tag;
4+
import org.jetbrains.annotations.Contract;
45
import org.jspecify.annotations.NullMarked;
56

67
/**
@@ -15,6 +16,7 @@ public interface TagSerializable {
1516
* @return the serialized Tag representation of the current object
1617
* @throws ParserException if an error occurs during serialization
1718
*/
19+
@Contract(value = " -> new", pure = true)
1820
Tag serialize() throws ParserException;
1921

2022
/**
@@ -23,5 +25,6 @@ public interface TagSerializable {
2325
* @param tag the Tag object to be deserialized
2426
* @throws ParserException if an error occurs during deserialization
2527
*/
28+
@Contract(mutates = "this")
2629
void deserialize(Tag tag) throws ParserException;
2730
}

nbt/src/main/java/core/nbt/serialization/TagSerializationContext.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package core.nbt.serialization;
22

33
import core.nbt.tag.Tag;
4+
import org.jetbrains.annotations.Contract;
45
import org.jspecify.annotations.NullMarked;
56

67
import java.lang.reflect.Type;
@@ -17,6 +18,7 @@ public interface TagSerializationContext {
1718
* @return the serialized Tag representation of the object
1819
* @throws ParserException if an error occurs during serialization
1920
*/
21+
@Contract(value = "_ -> new", pure = true)
2022
Tag serialize(Object object) throws ParserException;
2123

2224
/**
@@ -27,6 +29,7 @@ public interface TagSerializationContext {
2729
* @return the serialized Tag representation of the object
2830
* @throws ParserException if an error occurs during serialization
2931
*/
32+
@Contract(value = "_, _ -> new", pure = true)
3033
Tag serialize(Object object, Class<?> type) throws ParserException;
3134

3235
/**
@@ -37,5 +40,6 @@ public interface TagSerializationContext {
3740
* @return the serialized Tag representation of the object
3841
* @throws ParserException if an error occurs during serialization
3942
*/
43+
@Contract(value = "_, _ -> new", pure = true)
4044
Tag serialize(Object object, Type type) throws ParserException;
4145
}

nbt/src/main/java/core/nbt/serialization/TagSerializer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package core.nbt.serialization;
22

33
import core.nbt.tag.Tag;
4+
import org.jetbrains.annotations.Contract;
45
import org.jspecify.annotations.NullMarked;
56

67
/**
@@ -18,5 +19,6 @@ public interface TagSerializer<T> {
1819
* @return the Tag representation of the provided vector
1920
* @throws ParserException if an error occurs during serialization
2021
*/
22+
@Contract(value = "_, _ -> new", pure = true)
2123
Tag serialize(T object, TagSerializationContext context) throws ParserException;
2224
}

0 commit comments

Comments
 (0)