diff --git a/src/org/redkale/convert/Factory.java b/src/org/redkale/convert/Factory.java index af41d54f9..686335d0a 100644 --- a/src/org/redkale/convert/Factory.java +++ b/src/org/redkale/convert/Factory.java @@ -336,8 +336,24 @@ public abstract class Factory { od = new ObjectDecoder(type); decoder = od; } else if (!clazz.getName().startsWith("java.")) { - od = new ObjectDecoder(type); - decoder = od; + SimpledCoder simpleCoder = null; + for (final Method method : clazz.getDeclaredMethods()) { + if (!Modifier.isStatic(method.getModifiers())) continue; + if (method.getParameterTypes().length != 0) continue; + if (method.getReturnType() != SimpledCoder.class) continue; + try { + method.setAccessible(true); + simpleCoder = (SimpledCoder) method.invoke(null); + break; + } catch (Exception e) { + } + } + if (simpleCoder == null) { + od = new ObjectDecoder(type); + decoder = od; + } else { + decoder = simpleCoder; + } } if (decoder == null) throw new ConvertException("not support the type (" + type + ")"); register(type, decoder); @@ -398,8 +414,24 @@ public abstract class Factory { } else if (clazz == Object.class) { return (Encodeable) this.anyEncoder; } else if (!clazz.getName().startsWith("java.")) { - oe = new ObjectEncoder(type); - encoder = oe; + SimpledCoder simpleCoder = null; + for (final Method method : clazz.getDeclaredMethods()) { + if (!Modifier.isStatic(method.getModifiers())) continue; + if (method.getParameterTypes().length != 0) continue; + if (method.getReturnType() != SimpledCoder.class) continue; + try { + method.setAccessible(true); + simpleCoder = (SimpledCoder) method.invoke(null); + break; + } catch (Exception e) { + } + } + if (simpleCoder == null) { + oe = new ObjectEncoder(type); + encoder = oe; + } else { + encoder = simpleCoder; + } } if (encoder == null) throw new ConvertException("not support the type (" + type + ")"); register(type, encoder); diff --git a/src/org/redkale/service/CacheSourceService.java b/src/org/redkale/service/CacheSourceService.java index 723b8e45c..e05f95ad6 100644 --- a/src/org/redkale/service/CacheSourceService.java +++ b/src/org/redkale/service/CacheSourceService.java @@ -53,7 +53,6 @@ public class CacheSourceService implem protected final ConcurrentHashMap> container = new ConcurrentHashMap<>(); public CacheSourceService() { - CacheEntry.initCreator(); } public final CacheSourceService setStoreType(Class keyType, Class valueType) { @@ -122,7 +121,6 @@ public class CacheSourceService implem // TODO if (!this.needStore) return; try { - CacheEntry.initCreator(); File store = new File(home, "cache/" + name()); if (!store.isFile() || !store.canRead()) return; LineNumberReader reader = new LineNumberReader(new FileReader(store)); @@ -160,7 +158,6 @@ public class CacheSourceService implem if (scheduler != null) scheduler.shutdownNow(); if (!this.needStore || Sncp.isRemote(this) || container.isEmpty()) return; try { - CacheEntry.initCreator(); File store = new File(home, "cache/" + name()); store.getParentFile().mkdirs(); PrintStream stream = new PrintStream(store, "UTF-8"); @@ -413,27 +410,6 @@ public class CacheSourceService implem public static final String JSON_LIST_KEY = "{\"cacheType\":\"" + CacheEntryType.LIST + "\""; - public static class CacheEntryCreator implements Creator { - - public static final CacheEntryCreator CREATOR = new CacheEntryCreator(); - - @java.beans.ConstructorProperties({"cacheType", "expireSeconds", "lastAccessed", "key", "value"}) - public CacheEntryCreator() { - } - - @Override - public CacheEntry create(Object... params) { - return new CacheEntry((CacheEntryType) params[0], (Integer) params[1], (Integer) params[2], (Serializable) params[3], params[4]); - } - - } - - static void initCreator() { - if (JsonFactory.root().findCreator(CacheEntry.class) == null) { - JsonFactory.root().register(CacheEntry.class, CacheEntryCreator.CREATOR); - } - } - private final CacheEntryType cacheType; private final K key; @@ -453,7 +429,6 @@ public class CacheSourceService implem this(cacheType, expireSeconds, (int) (System.currentTimeMillis() / 1000), key, value); } - @java.beans.ConstructorProperties({"cacheType", "expireSeconds", "lastAccessed", "key", "value"}) private CacheEntry(CacheEntryType cacheType, int expireSeconds, int lastAccessed, K key, T value) { this.cacheType = cacheType; this.expireSeconds = expireSeconds; @@ -462,6 +437,15 @@ public class CacheSourceService implem this.value = value; } + private static Creator createCreator() { + return new Creator() { + @Override + public CacheEntry create(Object... params) { + return new CacheEntry((CacheEntryType) params[0], (Integer) params[1], (Integer) params[2], (Serializable) params[3], params[4]); + } + }; + } + @Override public String toString() { return JsonFactory.root().getConvert().convertTo(this);