Update dependencies, code cleanup, use moshi

This commit is contained in:
Andrea Cavalli 2021-05-09 22:58:42 +02:00
parent b59e23b03e
commit 42d05e8a17
5 changed files with 67 additions and 105 deletions

31
pom.xml
View File

@ -12,8 +12,7 @@
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<vertx.version>4.0.2</vertx.version>
<junit-jupiter.version>5.7.0</junit-jupiter.version>
<vertx.version>4.0.3</vertx.version>
</properties>
<repositories>
<repository>
@ -85,7 +84,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit-jupiter.version}</version>
<version>5.8.0-M1</version>
<scope>test</scope>
<exclusions>
<exclusion>
@ -97,7 +96,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<version>5.8.0-M1</version>
<scope>test</scope>
</dependency>
<dependency>
@ -128,28 +127,28 @@
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.12.1</version>
<version>2.14.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.12.1</version>
<version>2.14.1</version>
</dependency>
<dependency>
<groupId>org.warp</groupId>
<artifactId>common-utils</artifactId>
<version>1.1.2</version>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-tcnative-boringssl-static</artifactId>
<version>2.0.30.Final</version>
<version>2.0.39.Final</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-java</artifactId>
<version>[4.171.83,)</version>
<version>[4.171.112,)</version>
</dependency>
<dependency>
<groupId>it.cavallium</groupId>
@ -159,18 +158,18 @@
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.2</version>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>it.unimi.dsi</groupId>
<artifactId>fastutil</artifactId>
<version>8.3.1</version>
<version>8.5.4</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi</artifactId>
<version>1.12.0</version>
</dependency>
</dependencies>
<build>
@ -259,12 +258,12 @@
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.2.0</version>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.0</version>
<version>5.8.0-M1</version>
</dependency>
</dependencies>
</plugin>

View File

@ -1,65 +0,0 @@
package it.tdlight.tdlibsession;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import it.tdlight.jni.TdApi;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.ArrayList;
public class TdGson {
private static final TdApiGenericSerializer<?> tdApiGenericSerializerInstance = new TdApiGenericSerializer<>();
private static final ArrayList<Class<?>> abstractClassesSerializers = new ArrayList<>();
static {
for (Class<?> declaredClass : TdApi.class.getDeclaredClasses()) {
var modifiers = declaredClass.getModifiers();
if (Modifier.isAbstract(modifiers) && Modifier.isPublic(modifiers) && Modifier
.isStatic(modifiers)) {
abstractClassesSerializers.add(declaredClass);
}
}
}
public static GsonBuilder registerAdapters(GsonBuilder gsonBuilder) {
for (Class<?> abstractClassesSerializer : abstractClassesSerializers) {
gsonBuilder.registerTypeAdapter(abstractClassesSerializer, tdApiGenericSerializerInstance);
}
return gsonBuilder;
}
public static class TdApiGenericSerializer<T> implements JsonSerializer<T>, JsonDeserializer<T> {
@Override
public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject result = new JsonObject();
result.add("type", new JsonPrimitive(src.getClass().getSimpleName()));
result.add("properties", context.serialize(src, src.getClass()));
return result;
}
@Override
public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
String type = jsonObject.get("type").getAsString().replaceAll("[^a-zA-Z0-9]", "");
JsonElement element = jsonObject.get("properties");
try {
return context
.deserialize(element, Class.forName(TdApi.class.getCanonicalName() + "$" + type));
} catch (ClassNotFoundException cnfe) {
throw new JsonParseException("Unknown element type: " + type, cnfe);
}
}
}
}

View File

@ -73,7 +73,6 @@ public class AsyncTdEasy {
this.logName = logName;
this.logger = LoggerFactory.getLogger("AsyncTdEasy " + logName);
// todo: use Duration.ZERO instead of 10ms interval
this.incomingUpdates = td.receive()
.flatMapSequential(this::preprocessUpdates)
.flatMapSequential(update -> Mono.from(this.getState()).single().map(state -> new AsyncTdUpdateObj(state, update)))
@ -303,13 +302,6 @@ public class AsyncTdEasy {
return td.<T>execute(request, true);
}
/**
* Set if skip updates or not
*/
public Mono<Void> setSkipUpdates(boolean skipUpdates) { //todo: do this
return null;
}
/**
* Closes the client gracefully by sending {@link TdApi.Close}.
*/
@ -349,22 +341,6 @@ public class AsyncTdEasy {
.then();
}
/**
*
* @param timeout Timeout in seconds when reading data
*/
public void setReadTimeout(int timeout) {
//todo: do this
}
/**
*
* @param timeout Timeout in seconds when listening methods or connecting
*/
public void setMethodTimeout(int timeout) {
//todo: do this
}
private Mono<Update> catchErrors(Object obj) {
return Mono.<Update>fromCallable(() -> {
if (obj.getConstructor() == Error.CONSTRUCTOR) {

View File

@ -244,14 +244,17 @@ public class MonoUtils {
}));
}
@NotNull
public static <T> Mono<T> toMono(Single<T> single) {
return Mono.from(single.toFlowable());
}
@NotNull
public static <T> Mono<T> toMono(Maybe<T> single) {
return Mono.from(single.toFlowable());
}
@NotNull
public static <T> Mono<T> toMono(Completable completable) {
return Mono.from(completable.toFlowable());
}

View File

@ -0,0 +1,49 @@
package it.tdlight.utils;
import com.squareup.moshi.JsonAdapter;
import it.tdlight.jni.TdApi;
import it.tdlight.jni.TdApi.Object;
import java.lang.reflect.Modifier;
import java.util.HashSet;
import java.util.Set;
import org.warp.commonutils.moshi.MoshiPolymorphic;
public class TdMoshiPolymorphic extends MoshiPolymorphic<Object> {
private final Set<Class<TdApi.Object>> abstractClasses = new HashSet<>();
private final Set<Class<TdApi.Object>> concreteClasses = new HashSet<>();
public TdMoshiPolymorphic() {
super();
var declaredClasses = TdApi.class.getDeclaredClasses();
for (Class<?> declaredClass : declaredClasses) {
var modifiers = declaredClass.getModifiers();
if (Modifier.isPublic(modifiers) && Modifier
.isStatic(modifiers)) {
if (Modifier.isAbstract(modifiers)) {
//noinspection unchecked
this.abstractClasses.add((Class<TdApi.Object>) declaredClass);
} else {
//noinspection unchecked
this.concreteClasses.add((Class<TdApi.Object>) declaredClass);
}
}
}
}
@Override
public Set<Class<TdApi.Object>> getAbstractClasses() {
return abstractClasses;
}
@Override
public Set<Class<TdApi.Object>> getConcreteClasses() {
return concreteClasses;
}
@Override
protected boolean shouldIgnoreField(String fieldName) {
return fieldName.equals("CONSTRUCTOR");
}
}