Skip to content

Commit

Permalink
Update to Java 21, remove and replace gson dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
TBlueF committed Jun 22, 2024
1 parent b18f1ae commit 1747dbb
Show file tree
Hide file tree
Showing 26 changed files with 750 additions and 1,740 deletions.
12 changes: 7 additions & 5 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: recursive
fetch-depth: 0 # needed for versioning
- name: Set up Java 11
uses: actions/setup-java@v1
- name: Set up Java
uses: actions/setup-java@v4
with:
java-version: 11
distribution: 'temurin'
java-version: 21
cache: 'gradle'
- name: Build with Gradle
run: ./gradlew clean build test
- uses: actions/upload-artifact@v2-preview
- uses: actions/upload-artifact@v4
with:
name: artifact
path: build/libs/*
10 changes: 6 additions & 4 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: recursive
fetch-depth: 0 # needed for versioning
- name: Set up Java 11
uses: actions/setup-java@v1
- name: Set up Java
uses: actions/setup-java@v4
with:
java-version: 11
distribution: 'temurin'
java-version: 21
cache: 'gradle'
- name: Build with Gradle
run: ./gradlew clean publish
env:
Expand Down
13 changes: 1 addition & 12 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@ version = lastVersion +

println("Version: $version")

val javaTarget = 11
java {
sourceCompatibility = JavaVersion.toVersion(javaTarget)
targetCompatibility = JavaVersion.toVersion(javaTarget)

toolchain.languageVersion.set(JavaLanguageVersion.of(21))
withSourcesJar()
withJavadocJar()
}
Expand All @@ -59,14 +56,6 @@ repositories {
dependencies {
compileOnly ("org.jetbrains:annotations:24.0.1")
compileOnly ("org.projectlombok:lombok:1.18.32")

api ("com.google.code.gson:gson") {
version {
strictly("[2.8.0,)")
prefer("2.8.9")
}
}

annotationProcessor ("org.projectlombok:lombok:1.18.32")

testImplementation ("com.github.Querz:NBT:4.0")
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
79 changes: 62 additions & 17 deletions src/main/java/de/bluecolored/bluenbt/BlueNBT.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
*/
package de.bluecolored.bluenbt;

import com.google.gson.InstanceCreator;
import com.google.gson.reflect.TypeToken;
import de.bluecolored.bluenbt.adapter.*;
import de.bluecolored.bluenbt.internal.ConstructorConstructor;
import lombok.Getter;
import lombok.Setter;

Expand All @@ -41,11 +38,10 @@ public class BlueNBT {

private final List<TypeSerializerFactory> serializerFactories = new ArrayList<>();
private final List<TypeDeserializerFactory> deserializerFactories = new ArrayList<>();
private final List<InstanceCreatorFactory> instanceCreatorFactories = new ArrayList<>();
private final Map<TypeToken<?>, TypeSerializer<?>> typeSerializerMap = new HashMap<>();
private final Map<TypeToken<?>, TypeDeserializer<?>> typeDeserializerMap = new HashMap<>();
private final Map<Type, InstanceCreator<?>> instanceCreators = new HashMap<>();

private final ConstructorConstructor constructorConstructor = new ConstructorConstructor(instanceCreators);
private final Map<TypeToken<?>, InstanceCreator<?>> instanceCreatorMap = new HashMap<>();

@Getter @Setter
private FieldNameTransformer fieldNameTransformer = s -> {
Expand Down Expand Up @@ -78,6 +74,10 @@ public void register(TypeDeserializerFactory typeDeserializerFactory) {
deserializerFactories.add(typeDeserializerFactory);
}

public void register(InstanceCreatorFactory instanceCreatorFactory) {
instanceCreatorFactories.add(instanceCreatorFactory);
}

public <T> void register(TypeToken<T> type, TypeAdapter<T> typeAdapter) {
register(new TypeAdapterFactory() {
@Override
Expand Down Expand Up @@ -115,7 +115,15 @@ public <U> Optional<TypeDeserializer<U>> create(TypeToken<U> createType, BlueNBT
}

public <T> void register(Type type, InstanceCreator<T> instanceCreator) {
this.instanceCreators.put(type, instanceCreator);
register(new InstanceCreatorFactory() {
@Override
@SuppressWarnings("unchecked")
public <U> Optional<? extends InstanceCreator<U>> create(TypeToken<U> createType, BlueNBT blueNBT) {
if (createType.equals(type))
return Optional.of((InstanceCreator<U>) instanceCreator);
return Optional.empty();
}
});
}

public <T> TypeSerializer<T> getTypeSerializer(TypeToken<T> type) {
Expand Down Expand Up @@ -166,6 +174,30 @@ public <T> TypeDeserializer<T> getTypeDeserializer(TypeToken<T> type) {
return deserializer;
}

public <T> InstanceCreator<T> getInstanceCreator(TypeToken<T> type) {
@SuppressWarnings("unchecked")
InstanceCreator<T> instanceCreator = (InstanceCreator<T>) instanceCreatorMap.get(type);

if (instanceCreator == null) {
FutureInstanceCreator<T> future = new FutureInstanceCreator<>();
instanceCreatorMap.put(type, future); // set future before creation of new deserializers to avoid recursive creation

for (int i = instanceCreatorFactories.size() - 1; i >= 0; i--) {
InstanceCreatorFactory factory = instanceCreatorFactories.get(i);
instanceCreator = factory.create(type, this).orElse(null);
if (instanceCreator != null) break;
}

if (instanceCreator == null)
instanceCreator = DefaultInstanceCreatorFactory.INSTANCE.createFor(type, this);

future.complete(instanceCreator);
instanceCreatorMap.put(type, instanceCreator);
}

return instanceCreator;
}

public <T> void write(T object, OutputStream out, TypeToken<T> type) throws IOException {
write(object, new NBTWriter(out), type);
}
Expand All @@ -175,11 +207,11 @@ public <T> void write(T object, NBTWriter out, TypeToken<T> type) throws IOExcep
}

public <T> void write(T object, OutputStream out, Class<T> type) throws IOException {
write(object, out, TypeToken.get(type));
write(object, out, TypeToken.of(type));
}

public <T> void write(T object, NBTWriter out, Class<T> type) throws IOException {
write(object, out, TypeToken.get(type));
write(object, out, TypeToken.of(type));
}

@SuppressWarnings({"unchecked", "rawtypes"})
Expand All @@ -201,23 +233,19 @@ public <T> T read(NBTReader in, TypeToken<T> type) throws IOException {
}

public <T> T read(InputStream in, Class<T> type) throws IOException {
return read(in, TypeToken.get(type));
return read(in, TypeToken.of(type));
}

public <T> T read(NBTReader in, Class<T> type) throws IOException {
return read(in, TypeToken.get(type));
return read(in, TypeToken.of(type));
}

public Object read(InputStream in, Type type) throws IOException {
return read(in, TypeToken.get(type));
return read(in, TypeToken.of(type));
}

public Object read(NBTReader in, Type type) throws IOException {
return read(in, TypeToken.get(type));
}

public <T> ObjectConstructor<T> createObjectConstructor(TypeToken<T> type) {
return constructorConstructor.get(type);
return read(in, TypeToken.of(type));
}

private static class FutureTypeSerializer<T> implements TypeSerializer<T> {
Expand Down Expand Up @@ -260,4 +288,21 @@ public T read(NBTReader reader) throws IOException {

}

private static class FutureInstanceCreator<T> implements InstanceCreator<T> {

private InstanceCreator<T> value;

public void complete(InstanceCreator<T> value) {
if (this.value != null) throw new IllegalStateException("FutureObjectConstructor already completed!");
this.value = Objects.requireNonNull(value);
}

@Override
public T create() {
if (this.value == null) throw new IllegalStateException("FutureObjectConstructor is not ready!");
return this.value.create();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/
package de.bluecolored.bluenbt;

public interface ObjectConstructor<T> {
T construct();
public interface InstanceCreator<T> {

T create();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package de.bluecolored.bluenbt;

import java.util.Optional;

public interface InstanceCreatorFactory {

<T> Optional<? extends InstanceCreator<T>> create(TypeToken<T> type, BlueNBT blueNBT);

}
2 changes: 0 additions & 2 deletions src/main/java/de/bluecolored/bluenbt/TypeAdapterFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
*/
package de.bluecolored.bluenbt;

import com.google.gson.reflect.TypeToken;

import java.util.Optional;

public interface TypeAdapterFactory extends TypeSerializerFactory, TypeDeserializerFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
*/
package de.bluecolored.bluenbt;

import com.google.gson.reflect.TypeToken;

import java.util.Optional;

@FunctionalInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
*/
package de.bluecolored.bluenbt;

import com.google.gson.reflect.TypeToken;

import java.util.Optional;

@FunctionalInterface
Expand Down
Loading

0 comments on commit 1747dbb

Please sign in to comment.