Skip to content

Commit

Permalink
feat(基础模块): 优化
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Jan 3, 2025
1 parent 91cf6a1 commit a167c5f
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 6 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@
<groupId>io.netty</groupId>
<artifactId>netty-codec-mqtt</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-handler</artifactId>
</dependency>
</dependencies>

</project>
19 changes: 15 additions & 4 deletions src/main/java/org/jetlinks/core/enums/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import org.hswebframework.web.exception.ValidationException;
import org.jetlinks.core.exception.DeviceOperationException;

import java.net.SocketTimeoutException;
import java.util.Optional;
import java.util.concurrent.TimeoutException;

/**
* @author bsetfeng
Expand Down Expand Up @@ -51,15 +53,24 @@ public static Optional<ErrorCode> of(String code) {
public static ErrorCode of(Throwable e) {
if (e instanceof DeviceOperationException) {
return ((DeviceOperationException) e).getCode();
} else if (e instanceof IllegalArgumentException
}
// 参数错误
else if (e instanceof IllegalArgumentException
|| e instanceof ValidationException
|| e instanceof javax.validation.ValidationException
|| e instanceof NullPointerException
|| e instanceof ArrayIndexOutOfBoundsException
|| e instanceof StringIndexOutOfBoundsException) {
|| e instanceof IndexOutOfBoundsException) {
return ErrorCode.PARAMETER_ERROR;
} else if (e instanceof UnsupportedOperationException) {
}
// 不支持
else if (e instanceof UnsupportedOperationException) {
return ErrorCode.UNSUPPORTED_MESSAGE;
}
// 超时
else if (e instanceof TimeoutException
|| e instanceof SocketTimeoutException
|| e instanceof io.netty.handler.timeout.TimeoutException) {
return ErrorCode.TIME_OUT;
} else {
return ErrorCode.SYSTEM_ERROR;
}
Expand Down
24 changes: 23 additions & 1 deletion src/main/java/org/jetlinks/core/topic/Topic.java
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,29 @@ public void clean() {
}

@Override
public int compareTo(SeparatedCharSequence o) {
public int compareTo(@Nonnull SeparatedCharSequence obj) {
if (this == obj) {
return 0;
}
if (!(obj instanceof Topic)) {
return this.getTopic().compareTo(obj.toString());
}

Topic<?> left = ((Topic<?>) obj);
Topic<?> right = this;

if (left.depth != right.depth) {
return Integer.compare(left.depth, right.depth);
}

while (left != null && right != null) {
int compare = left.part.compareTo(right.part);
if (compare != 0) {
return compare;
}
left = left.parent;
right = right.parent;
}
return 0;
}

Expand Down
27 changes: 27 additions & 0 deletions src/main/java/org/jetlinks/core/utils/SerializeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public class SerializeUtils {
}
registerSerializer(new TypedMapSerializer());
registerSerializer(new TypedCollectionSerializer());

cache.put(StackTraceElement.class, InternalSerializers.StackTraceElement);
}

private static final Set<Class<?>> safelySerializable = ConcurrentHashMap.newKeySet();
Expand Down Expand Up @@ -929,6 +931,31 @@ void write(Object value, ObjectOutput input) {
input.writeUTF(str.toString());
}
}
},
StackTraceElement(0x42, java.lang.StackTraceElement.class) {
@Override
@SneakyThrows
Object read(ObjectInput input) {
String className = input.readUTF();
String methodName = input.readUTF();

String fileName = SerializeUtils.readNullableUTF(input);

int lineNumber = input.readInt();
return new StackTraceElement(className, methodName, fileName, lineNumber);
}

@Override
@SneakyThrows
void write(Object value, ObjectOutput input) {
StackTraceElement element = ((StackTraceElement) value);
input.writeUTF(element.getClassName());
input.writeUTF(element.getMethodName());

SerializeUtils.writeNullableUTF(element.getFileName(), input);

input.writeInt(element.getLineNumber());
}
};

public final int code;
Expand Down
16 changes: 15 additions & 1 deletion src/test/java/org/jetlinks/core/utils/SerializeUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@
public class SerializeUtilsTest {


@Test
public void testStackTraceElement() {

Object trace = codec(new Exception().getStackTrace());

assertTrue(trace instanceof StackTraceElement[]);
StackTraceElement[] arr = (StackTraceElement[]) trace;

Exception e = new Exception();
e.setStackTrace(arr);


}

@Test
public void testConvertToSafelySerializable() {
assertEquals(1, SerializeUtils.convertToSafelySerializable(1));
Expand Down Expand Up @@ -233,7 +247,7 @@ public void testCrossClassLoader() {
assertNotNull(map);

//deepMap
Object deepMap = codec(Collections.singletonMap("1",Collections.singletonMap("2", obj)));
Object deepMap = codec(Collections.singletonMap("1", Collections.singletonMap("2", obj)));
assertNotNull(deepMap);
System.out.println(deepMap);
}
Expand Down

0 comments on commit a167c5f

Please # to comment.