fix[zfoo]: maven compile warning

This commit is contained in:
godotg
2023-09-09 12:20:05 +08:00
parent 94e7e39e9b
commit 5bd66f4254
20 changed files with 94 additions and 115 deletions
@@ -61,7 +61,7 @@ public class EventRegisterProcessor implements BeanPostProcessor {
if (!IEvent.class.isAssignableFrom(paramClazzs[0])) {
throw new IllegalArgumentException(StringUtils.format("[class:{}] [method:{}] must have one [IEvent] type parameter!", bean.getClass().getName(), method.getName()));
}
@SuppressWarnings("unchecked")
var eventClazz = (Class<? extends IEvent>) paramClazzs[0];
var eventName = eventClazz.getCanonicalName();
var methodName = method.getName();
-8
View File
@@ -72,14 +72,6 @@
<version>${zfoo.version}</version>
</dependency>
<!--fix-->
<!-- Warning:java: 未知的枚举常量 javax.annotation.meta.When.MAYBE 原因: 找不到-->
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
</dependency>
<!-- 依赖的通信类库 -->
<!-- <dependency>-->
<!-- <groupId>io.netty</groupId>-->
@@ -40,6 +40,7 @@ public class MongodbAccessor implements IAccessor {
@Override
public <E extends IEntity<?>> boolean insert(E entity) {
@SuppressWarnings("unchecked")
var entityClazz = (Class<E>) entity.getClass();
var collection = OrmContext.getOrmManager().getCollection(entityClazz);
var result = collection.insertOne(entity);
@@ -51,6 +52,7 @@ public class MongodbAccessor implements IAccessor {
if (CollectionUtils.isEmpty(entities)) {
return;
}
@SuppressWarnings("unchecked")
var entityClazz = (Class<E>) entities.get(0).getClass();
var collection = OrmContext.getOrmManager().getCollection(entityClazz);
collection.insertMany(entities);
@@ -59,6 +61,7 @@ public class MongodbAccessor implements IAccessor {
@Override
public <E extends IEntity<?>> boolean update(E entity) {
try {
@SuppressWarnings("unchecked")
var entityClazz = (Class<E>) entity.getClass();
var collection = OrmContext.getOrmManager().getCollection(entityClazz);
@@ -83,6 +86,7 @@ public class MongodbAccessor implements IAccessor {
}
try {
@SuppressWarnings("unchecked")
var entityClazz = (Class<E>) entities.get(0).getClass();
var collection = OrmContext.getOrmManager().getCollection(entityClazz);
@@ -102,6 +106,7 @@ public class MongodbAccessor implements IAccessor {
@Override
public <E extends IEntity<?>> boolean delete(E entity) {
@SuppressWarnings("unchecked")
var entityClazz = (Class<E>) entity.getClass();
var collection = OrmContext.getOrmManager().getCollection(entityClazz);
var result = collection.deleteOne(eq("_id", entity.id()));
@@ -120,6 +125,7 @@ public class MongodbAccessor implements IAccessor {
if (CollectionUtils.isEmpty(entities)) {
return;
}
@SuppressWarnings("unchecked")
var entityClazz = (Class<E>) entities.get(0).getClass();
var collection = OrmContext.getOrmManager().getCollection(entityClazz);
var ids = entities.stream().map(it -> (it).id()).collect(Collectors.toList());
+14 -4
View File
@@ -71,7 +71,9 @@ public class EntityCache<PK extends Comparable<PK>, E extends IEntity<PK>> imple
// 缓存失效之前,将数据写入数据库
var entity = pnode.getEntity();
var collection = OrmContext.getOrmManager().getCollection((Class<E>) entityDef.getClazz());
@SuppressWarnings("unchecked")
var entityClass = (Class<E>) entityDef.getClazz();
var collection = OrmContext.getOrmManager().getCollection(entityClass);
var version = entity.gvs();
entity.svs(version + 1);
@@ -89,11 +91,14 @@ public class EntityCache<PK extends Comparable<PK>, E extends IEntity<PK>> imple
.build(new CacheLoader<PK, PNode<E>>() {
@Override
public @Nullable PNode<E> load(@NonNull PK pk) {
@SuppressWarnings("unchecked")
var entity = (E) OrmContext.getAccessor().load(pk, (Class<IEntity<?>>) entityDef.getClazz());
// 如果数据库中不存在则给一个默认值
if (entity == null) {
entity = (E) entityDef.newEntity(pk);
@SuppressWarnings("unchecked")
var newEntity = (E) entityDef.newEntity(pk);
return new PNode<E>(newEntity);
}
return new PNode<E>(entity);
@@ -126,6 +131,7 @@ public class EntityCache<PK extends Comparable<PK>, E extends IEntity<PK>> imple
}
logger.warn("数据库[{}]无法加载缓存[pk:{}],返回默认值", entityDef.getClazz().getSimpleName(), pk);
@SuppressWarnings("unchecked")
var entity = (E) entityDef.newEntity(pk);
var pnode = new PNode<E>(entity);
cache.put(pk, pnode);
@@ -209,7 +215,9 @@ public class EntityCache<PK extends Comparable<PK>, E extends IEntity<PK>> imple
page.setPage(currentPage);
var currentUpdateList = page.currentPageList(updateList);
try {
var collection = OrmContext.getOrmManager().getCollection((Class<E>) entityDef.getClazz()).withWriteConcern(WriteConcern.ACKNOWLEDGED);
@SuppressWarnings("unchecked")
var entityClass = (Class<E>) entityDef.getClazz();
var collection = OrmContext.getOrmManager().getCollection(entityClass).withWriteConcern(WriteConcern.ACKNOWLEDGED);
var batchList = currentUpdateList.stream()
.map(it -> {
@@ -256,7 +264,9 @@ public class EntityCache<PK extends Comparable<PK>, E extends IEntity<PK>> imple
var ids = updateList.stream().map(it -> it.id()).collect(Collectors.toList());
try {
var dbList = OrmContext.getQuery((Class<E>) entityDef.getClazz()).in("_id", ids).queryAll();
@SuppressWarnings("unchecked")
var entityClass = (Class<E>) entityDef.getClazz();
var dbList = OrmContext.getQuery(entityClass).in("_id", ids).queryAll();
var dbMap = dbList.stream().collect(Collectors.toMap(key -> key.id(), value -> value));
for (var entity : updateList) {
var dbEntity = dbMap.get(entity.id());
@@ -89,6 +89,7 @@ public class OrmManager implements IOrmManager {
var entityDefMap = entityClass();
for (var entityDef : entityDefMap.values()) {
@SuppressWarnings("rawtypes")
var entityCaches = new EntityCache(entityDef);
entityCachesMap.put(entityDef.getClazz(), entityCaches);
allEntityCachesUsableMap.put(entityDef.getClazz(), false);
@@ -198,7 +199,8 @@ public class OrmManager implements IOrmManager {
}
Type[] types = ((ParameterizedType) type).getActualTypeArguments();
Class<? extends IEntity<?>> entityClazz = (Class<? extends IEntity<?>>) types[1];
@SuppressWarnings("unchecked")
var entityClazz = (Class<? extends IEntity<?>>) types[1];
IEntityCache<?, ?> entityCaches = entityCachesMap.get(entityClazz);
if (entityCaches == null) {
@@ -234,7 +236,9 @@ public class OrmManager implements IOrmManager {
if (!usable) {
throw new RunException("Orm没有使用[]的EntityCaches,为了节省内存提前释放了它;只有使用EntityCachesInjection注解的Entity才能被动态获取", clazz.getCanonicalName());
}
return (IEntityCache<?, E>) entityCachesMap.get(clazz);
@SuppressWarnings("unchecked")
var entityCache = (IEntityCache<?, E>) entityCachesMap.get(clazz);
return entityCache;
}
@Override
@@ -276,8 +280,10 @@ public class OrmManager implements IOrmManager {
var cacheDefMap = new HashMap<Class<? extends IEntity<?>>, EntityDef>();
for (var clazz : classSet) {
var cacheDef = parserEntityDef((Class<? extends IEntity<?>>) clazz);
cacheDefMap.putIfAbsent((Class<? extends IEntity<?>>) clazz, cacheDef);
@SuppressWarnings("unchecked")
var entityClass = (Class<? extends IEntity<?>>) clazz;
var cacheDef = parserEntityDef(entityClass);
cacheDefMap.putIfAbsent(entityClass, cacheDef);
}
return cacheDefMap;
}
@@ -109,7 +109,9 @@ public class MongoQueryBuilder<E extends IEntity<?>> implements IQueryBuilder<E>
result.forEach(new Consumer<IEntity<?>>() {
@Override
public void accept(IEntity<?> entity) {
list.add((E) entity);
@SuppressWarnings("unchecked")
var e = (E) entity;
list.add(e);
}
});
return list;
@@ -128,7 +130,9 @@ public class MongoQueryBuilder<E extends IEntity<?>> implements IQueryBuilder<E>
.forEach(new Consumer<IEntity<?>>() {
@Override
public void accept(IEntity<?> entity) {
list.add((E) entity);
@SuppressWarnings("unchecked")
var e = (E) entity;
list.add(e);
}
});
@@ -36,6 +36,7 @@ public class EntityCachesTest {
var context = new ClassPathXmlApplicationContext("application.xml");
// 动态去拿到UserEntity的EntityCaches
@SuppressWarnings("unchecked")
var userEntityCaches = (IEntityCache<Long, UserEntity>) OrmContext.getOrmManager().getEntityCaches(UserEntity.class);
for (var i = 1; i <= 10; i++) {
@@ -148,22 +148,6 @@ public class SingleMongoTest {
collection.find().forEach(doc -> System.out.println(doc.toJson()));
}
@Test
public void mapReduceTest() {
String mapFunction = "" +
"function() { " +
"emit(this.name, { count: 1 });" +
"}";
String reduceFunction = "" +
"function(key, values) {" +
"var reduced = { name:key, count:0 };" +
"values.forEach(function(value){ reduced.count += value.count; }); " +
"return reduced;" +
"}";
MongoCollection<Document> collection = mongodb.getCollection("student");
var iterable = collection.mapReduce(mapFunction, reduceFunction);
iterable.forEach((Consumer<? super Document>) document -> System.out.println(document));
}
// ******************************************************************************************************
+8
View File
@@ -115,6 +115,14 @@
</exclusions>
</dependency>
<!--fix protobuf warning-->
<!-- Warning:java: 未知的枚举常量 javax.annotation.meta.When.MAYBE 原因: 找不到-->
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
@@ -368,7 +368,9 @@ public abstract class ArrayUtils {
var length = list.size();
var objectArray = Array.newInstance(clazz, length);
return (T[]) copy(list.toArray(), objectArray, length);
@SuppressWarnings("unchecked")
var copy = (T[]) copy(list.toArray(), objectArray, length);
return copy;
}
@@ -452,7 +454,9 @@ public abstract class ArrayUtils {
AssertionUtils.notNull(clazz);
var length = source.length;
var target = Array.newInstance(clazz, length);
return (T[]) copy(source, target, length);
@SuppressWarnings("unchecked")
var copy = (T[]) copy(source, target, length);
return copy;
}
private static Object copy(Object source, Object target, int length) {
@@ -234,52 +234,6 @@ public abstract class CollectionUtils {
}
/**
* list合并
*
* @param exclusive 元素是否是独占的也就是说是否可以重复
* @param pairs 需要被合并的pairs集合第一个参数是步数第二个参数是集合
* @return 返回合并后的list
*/
public static <T> List<T> listJoinList(boolean exclusive, Pair<Integer, List<T>>... pairs) {
return listJoinList(exclusive, List.of(pairs));
}
public static <T> List<T> listJoinList(boolean exclusive, List<Pair<Integer, List<T>>> pairs) {
var iteratorList = new ArrayList<List<T>>();
var iteratorMap = new HashMap<List<T>, Iterator<T>>();
var stepMap = new HashMap<List<T>, Integer>();
for (var pair : pairs) {
var step = pair.getKey();
var list = pair.getValue();
AssertionUtils.ge1(step);
if (isNotEmpty(list)) {
var iterator = list.iterator();
iteratorList.add(list);
iteratorMap.put(list, iterator);
stepMap.put(list, step);
}
}
var result = new ArrayList<T>();
while (iteratorMap.values().stream().anyMatch(it -> it.hasNext())) {
for (var list : iteratorList) {
var iterator = iteratorMap.get(list);
var step = stepMap.get(list);
for (var i = 0; i < step && iterator.hasNext(); i++) {
var element = iterator.next();
if (exclusive && result.contains(element)) {
i--;
continue;
}
result.add(element);
}
}
}
return result;
}
/**
* 获取集合的最后几个元素
@@ -49,6 +49,7 @@ public class ConcurrentArrayList<E> implements List<E> {
public List<E> clearAndReturn() {
lock.lock();
try {
@SuppressWarnings("unchecked")
var newList = (ArrayList<E>) list.clone();
list.clear();
return newList;
@@ -71,6 +72,7 @@ public class ConcurrentArrayList<E> implements List<E> {
public Iterator<E> iterator() {
lock.lock();
try {
@SuppressWarnings("unchecked")
var newList = (ArrayList<E>) list.clone();
return newList.iterator();
} finally {
@@ -237,6 +239,7 @@ public class ConcurrentArrayList<E> implements List<E> {
public ListIterator<E> listIterator() {
lock.lock();
try {
@SuppressWarnings("unchecked")
var newList = (ArrayList<E>) list.clone();
return newList.listIterator();
} finally {
@@ -248,6 +251,7 @@ public class ConcurrentArrayList<E> implements List<E> {
public ListIterator<E> listIterator(int index) {
lock.lock();
try {
@SuppressWarnings("unchecked")
var newList = (ArrayList<E>) list.clone();
return newList.listIterator(index);
} finally {
@@ -123,7 +123,9 @@ public class FileChannelMap<V> implements LpMap<V>, Closeable {
dbBuffer.writeBytes(dbFileChannel, packetPosition, (int) packetSize);
var packet = protocolRegistration.read(dbBuffer);
return (V) packet;
@SuppressWarnings("unchecked")
var p = (V) packet;
return p;
} catch (Exception e) {
return null;
}
@@ -110,6 +110,7 @@ public class FileHeapMap<V> implements LpMap<V> {
var size = ByteBufUtils.readLong(buffer);
for (var i = 0; i < size; i++) {
var key = ByteBufUtils.readLong(buffer);
@SuppressWarnings("unchecked")
var value = (V) protocolRegistration.read(buffer);
put(key, value);
}
@@ -13,7 +13,6 @@
package com.zfoo.protocol.util;
import com.zfoo.protocol.collection.CollectionUtils;
import com.zfoo.protocol.model.Pair;
import org.junit.Assert;
import org.junit.Test;
@@ -49,19 +48,6 @@ public class CollectionUtilsTest {
Assert.assertArrayEquals(c.toArray(), a.toArray());
}
@Test
public void listJoinListTest() {
var a = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
var b = List.of(11, 22, 33, 44, 55);
var c = List.of(1, 2, 3, 4, 5, 6);
var d = CollectionUtils.listJoinList(true, new Pair<>(2, a), new Pair<>(2, b), new Pair<>(2, c));
Assert.assertArrayEquals(d.toArray(), List.of(1, 2, 11, 22, 3, 4, 5, 6, 33, 44, 7, 8, 55, 9).toArray());
var e = CollectionUtils.listJoinList(false, new Pair<>(2, a), new Pair<>(2, b), new Pair<>(2, c));
Assert.assertArrayEquals(e.toArray(), List.of(1, 2, 11, 22, 1, 2, 3, 4, 33, 44, 3, 4, 5, 6, 55, 5, 6, 7, 8, 9).toArray());
}
@Test
public void subListLastTest() {
var list = List.of(1, 2, 3, 4, 5);
@@ -29,7 +29,9 @@ public class StorageInt<K, V> extends StorageObject<K, V> {
public StorageInt(StorageObject<K, V> storage) {
this.dataMap = new IntObjectHashMap<V>(storage.size());
this.dataMap.putAll((Map<? extends Integer, ? extends V>) storage.getData());
@SuppressWarnings("unchecked")
var map = (Map<? extends Integer, ? extends V>) storage.getData();
this.dataMap.putAll(map);
super.indexMap = storage.indexMap;
super.uniqueIndexMap = storage.uniqueIndexMap;
super.clazz = storage.clazz;
@@ -89,7 +91,9 @@ public class StorageInt<K, V> extends StorageObject<K, V> {
@Override
public Map<K, V> getData() {
return (Map<K, V>) Collections.unmodifiableMap(dataMap);
@SuppressWarnings("unchecked")
var map = (Map<K, V>) Collections.unmodifiableMap(dataMap);
return map;
}
}
@@ -29,7 +29,9 @@ public class StorageLong<K, V> extends StorageObject<K, V> {
public StorageLong(StorageObject<K, V> storage) {
this.dataMap = new LongObjectHashMap<V>(storage.size());
this.dataMap.putAll((Map<? extends Long, ? extends V>) storage.getData());
@SuppressWarnings("unchecked")
var map = (Map<? extends Long, ? extends V>) storage.getData();
this.dataMap.putAll(map);
super.indexMap = storage.indexMap;
super.uniqueIndexMap = storage.uniqueIndexMap;
super.clazz = storage.clazz;
@@ -89,7 +91,9 @@ public class StorageLong<K, V> extends StorageObject<K, V> {
@Override
public Map<K, V> getData() {
return (Map<K, V>) Collections.unmodifiableMap(dataMap);
@SuppressWarnings("unchecked")
var map = (Map<K, V>) Collections.unmodifiableMap(dataMap);
return map;
}
}
@@ -78,12 +78,16 @@ public class StorageObject<K, V> implements IStorage<K, V> {
@Override
public boolean contain(int key) {
return contain((K) Integer.valueOf(key));
@SuppressWarnings("unchecked")
var k = (K) Integer.valueOf(key);
return contain(k);
}
@Override
public boolean contain(long key) {
return contain((K) Long.valueOf(key));
@SuppressWarnings("unchecked")
var k = (K) Long.valueOf(key);
return contain(k);
}
@Override
@@ -95,12 +99,16 @@ public class StorageObject<K, V> implements IStorage<K, V> {
@Override
public V get(int id) {
return get((K) Integer.valueOf(id));
@SuppressWarnings("unchecked")
var key = (K) Integer.valueOf(id);
return get(key);
}
@Override
public V get(long id) {
return get((K) Long.valueOf(id));
@SuppressWarnings("unchecked")
var key = (K) Long.valueOf(id);
return get(key);
}
@Override
@@ -164,6 +172,7 @@ public class StorageObject<K, V> implements IStorage<K, V> {
}
public V put(Object value) {
@SuppressWarnings("unchecked")
var key = (K) ReflectionUtils.getField(idDef.getField(), value);
if (key == null) {
@@ -175,22 +184,24 @@ public class StorageObject<K, V> implements IStorage<K, V> {
}
// 添加资源
var result = dataMap.put(key, (V) value);
@SuppressWarnings("unchecked")
var v = (V) value;
var result = dataMap.put(key, v);
// 添加索引
for (var def : indexDefMap.values()) {
// 使用field的名称作为索引的名称
var indexKey = def.getField().getName();
var indexValue = ReflectionUtils.getField(def.getField(), value);
var indexValue = ReflectionUtils.getField(def.getField(), v);
if (def.isUnique()) {// 唯一索引
var index = uniqueIndexMap.computeIfAbsent(indexKey, k -> new HashMap<>());
if (index.put(indexValue, (V) value) != null) {
if (index.put(indexValue, v) != null) {
throw new RuntimeException(StringUtils.format("Duplicate unique index [index:{}][value:{}] of static resource [class:{}]", indexKey, indexValue, clazz.getName()));
}
} else {// 不是唯一索引
var index = indexMap.computeIfAbsent(indexKey, k -> new HashMap<>());
var list = index.computeIfAbsent(indexValue, k -> new ArrayList<V>());
list.add((V) value);
list.add(v);
}
}
return result;
@@ -63,11 +63,9 @@ public class ConversionTest {
ConversionService conversionService = csfb.getObject();
//Json to Map
String str = "{\"1\":1,\"2\":2,\"3\":3}";
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
// 注意第3个参数不能写成TypeDescriptor.valueOf(map.getClass()) 而是要明确指定Map的key和value的类型
map = (Map<Integer, Integer>) conversionService.convert(str, TypeDescriptor.valueOf(String.class), TypeDescriptor.map(map.getClass(), TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class)));
@SuppressWarnings("unchecked")
var map = (Map<Integer, Integer>) conversionService.convert(str, TypeDescriptor.valueOf(String.class), TypeDescriptor.map(HashMap.class, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class)));
Assert.assertEquals(3, map.size());
}
@@ -103,7 +103,7 @@ public class ExportBinaryTesting {
ProtocolManager.write(buffer, resourceData);
var bytes = ByteBufUtils.readAllBytes(buffer);
FileUtils.writeInputStreamToFile(new File("D:/github/godot-bird/binary_data.cfg"), new ByteArrayInputStream(bytes));
@SuppressWarnings("unchecked")
var storage = (StorageObject<Integer, StudentResource>) storageManager.getStorage(StudentResource.class);
for (StudentResource resource : storage.getAll()) {
System.out.println(JsonUtils.object2String(resource));