This commit is contained in:
@@ -5,7 +5,6 @@
|
||||
*/
|
||||
package org.redkale.convert;
|
||||
|
||||
import java.beans.*;
|
||||
import org.redkale.util.Attribute;
|
||||
import java.lang.reflect.*;
|
||||
import java.util.*;
|
||||
@@ -241,7 +240,7 @@ public final class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T
|
||||
|
||||
static String[] findConstructorProperties(Creator creator) {
|
||||
try {
|
||||
ConstructorProperties cps = creator.getClass().getConstructor().getAnnotation(ConstructorProperties.class);
|
||||
Creator.ConstructorParameters cps = creator.getClass().getMethod("create", Object[].class).getAnnotation(Creator.ConstructorParameters.class);
|
||||
return cps == null ? null : cps.value();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
package org.redkale.service;
|
||||
|
||||
import java.beans.*;
|
||||
import java.io.*;
|
||||
import java.lang.reflect.*;
|
||||
import java.nio.channels.*;
|
||||
@@ -429,7 +430,8 @@ public class CacheSourceService<K extends Serializable, V extends Object> implem
|
||||
this(cacheType, expireSeconds, (int) (System.currentTimeMillis() / 1000), key, value);
|
||||
}
|
||||
|
||||
private CacheEntry(CacheEntryType cacheType, int expireSeconds, int lastAccessed, K key, T value) {
|
||||
@ConstructorProperties({"cacheType", "expireSeconds", "lastAccessed", "key", "value"})
|
||||
protected CacheEntry(CacheEntryType cacheType, int expireSeconds, int lastAccessed, K key, T value) {
|
||||
this.cacheType = cacheType;
|
||||
this.expireSeconds = expireSeconds;
|
||||
this.lastAccessed = lastAccessed;
|
||||
@@ -437,19 +439,11 @@ public class CacheSourceService<K extends Serializable, V extends Object> implem
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
protected static Creator createCreator() { //供 Creator.create 调用
|
||||
return (Creator<CacheEntry>) (Object... params) -> 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);
|
||||
}
|
||||
|
||||
public CacheEntryType getCacheType() {
|
||||
return cacheType;
|
||||
}
|
||||
|
||||
@Ignore
|
||||
public boolean isListCacheType() {
|
||||
return cacheType == CacheEntryType.LIST;
|
||||
@@ -465,6 +459,10 @@ public class CacheSourceService<K extends Serializable, V extends Object> implem
|
||||
return (expireSeconds > 0 && lastAccessed + expireSeconds < (System.currentTimeMillis() / 1000));
|
||||
}
|
||||
|
||||
public CacheEntryType getCacheType() {
|
||||
return cacheType;
|
||||
}
|
||||
|
||||
public int getExpireSeconds() {
|
||||
return expireSeconds;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,10 @@
|
||||
*/
|
||||
package org.redkale.util;
|
||||
|
||||
import java.beans.ConstructorProperties;
|
||||
import java.beans.*;
|
||||
import java.lang.annotation.*;
|
||||
import static java.lang.annotation.ElementType.*;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
import java.lang.reflect.*;
|
||||
import java.util.*;
|
||||
import jdk.internal.org.objectweb.asm.*;
|
||||
@@ -22,20 +25,35 @@ import static jdk.internal.org.objectweb.asm.Opcodes.*;
|
||||
*
|
||||
* private String name;
|
||||
*
|
||||
* public Record(int id, String name) {
|
||||
* Record(int id, String name) {
|
||||
* this.id = id;
|
||||
* this.name = name;
|
||||
* }
|
||||
*
|
||||
* private static Creator createCreator() {
|
||||
* return new Creator<Record>() {
|
||||
* return new Creator<Record>() {
|
||||
* @Override
|
||||
* @ConstructorParameters({"id", "name"})
|
||||
* public Record create(Object... params) {
|
||||
* return new Record((Integer) params[0], (String) params[1]);
|
||||
* }
|
||||
* };
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* 或者:
|
||||
* public class Record {
|
||||
*
|
||||
* private final int id;
|
||||
*
|
||||
* private String name;
|
||||
*
|
||||
* @ConstructorProperties({"id", "name"})
|
||||
* public Record(int id, String name) {
|
||||
* this.id = id;
|
||||
* this.name = name;
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* @see http://www.redkale.org
|
||||
* @author zhangjx
|
||||
@@ -43,10 +61,18 @@ import static jdk.internal.org.objectweb.asm.Opcodes.*;
|
||||
*/
|
||||
public interface Creator<T> {
|
||||
|
||||
@Documented
|
||||
@Target({CONSTRUCTOR, TYPE})
|
||||
@Retention(RUNTIME)
|
||||
public static @interface ConstructorParameters {
|
||||
|
||||
String[] value();
|
||||
}
|
||||
|
||||
public T create(Object... params);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> Creator<T> create(Class<T> clazz) {
|
||||
public static <T> Creator<T> create(Class<T> clazz) {
|
||||
if (clazz.isAssignableFrom(ArrayList.class)) {
|
||||
clazz = (Class<T>) ArrayList.class;
|
||||
} else if (clazz.isAssignableFrom(HashMap.class)) {
|
||||
@@ -62,7 +88,7 @@ public interface Creator<T> {
|
||||
if (method.getParameterTypes().length != 0) continue;
|
||||
if (method.getReturnType() != Creator.class) continue;
|
||||
try {
|
||||
method.setAccessible(true);
|
||||
method.setAccessible(true);
|
||||
return (Creator<T>) method.invoke(null);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
@@ -89,14 +115,15 @@ public interface Creator<T> {
|
||||
}
|
||||
}
|
||||
if (constructor == null) {
|
||||
for (Constructor c : clazz.getConstructors()) {
|
||||
if (c.getAnnotation(ConstructorProperties.class) != null) {
|
||||
for (Constructor c : clazz.getDeclaredConstructors()) {
|
||||
if (Modifier.isPrivate(c.getModifiers())) continue;
|
||||
if (c.getAnnotation(ConstructorProperties.class) != null || c.getAnnotation(ConstructorParameters.class) != null) {
|
||||
constructor = c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (constructor == null) throw new RuntimeException("[" + clazz + "] have no public or java.beans.ConstructorProperties-Annotation constructor.");
|
||||
if (constructor == null) throw new RuntimeException("[" + clazz + "] have no public or java.beans.ConstructorProperties-Annotation or ConstructorParameters-Annotation constructor.");
|
||||
//-------------------------------------------------------------
|
||||
ClassWriter cw = new ClassWriter(0);
|
||||
FieldVisitor fv;
|
||||
@@ -106,16 +133,6 @@ public interface Creator<T> {
|
||||
|
||||
{//构造方法
|
||||
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
|
||||
ConstructorProperties cps = constructor.getAnnotation(ConstructorProperties.class);
|
||||
if (cps != null) {
|
||||
av0 = mv.visitAnnotation(Type.getDescriptor(ConstructorProperties.class), true);
|
||||
AnnotationVisitor av1 = av0.visitArray("value");
|
||||
for (String n : cps.value()) {
|
||||
av1.visit(null, n);
|
||||
}
|
||||
av1.visitEnd();
|
||||
av0.visitEnd();
|
||||
}
|
||||
mv.visitVarInsn(ALOAD, 0);
|
||||
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
|
||||
mv.visitInsn(RETURN);
|
||||
@@ -124,6 +141,18 @@ public interface Creator<T> {
|
||||
}
|
||||
{//create 方法
|
||||
mv = cw.visitMethod(ACC_PUBLIC + ACC_VARARGS, "create", "([Ljava/lang/Object;)L" + interName + ";", null, null);
|
||||
ConstructorProperties cps = constructor.getAnnotation(ConstructorProperties.class);
|
||||
ConstructorParameters cts = constructor.getAnnotation(ConstructorParameters.class);
|
||||
final String[] cparams = cps == null ? (cts == null ? null : cts.value()) : cps.value();
|
||||
if (cparams != null) {
|
||||
av0 = mv.visitAnnotation(Type.getDescriptor(ConstructorParameters.class), true);
|
||||
AnnotationVisitor av1 = av0.visitArray("value");
|
||||
for (String n : cps.value()) {
|
||||
av1.visit(null, n);
|
||||
}
|
||||
av1.visitEnd();
|
||||
av0.visitEnd();
|
||||
}
|
||||
mv.visitTypeInsn(NEW, interName);
|
||||
mv.visitInsn(DUP);
|
||||
//---------------------------------------
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
package org.redkale.watch;
|
||||
|
||||
import java.beans.*;
|
||||
import java.util.concurrent.atomic.*;
|
||||
|
||||
/**
|
||||
@@ -20,11 +21,12 @@ public final class WatchNumber extends AtomicLong implements WatchNode {
|
||||
|
||||
private final String description;
|
||||
|
||||
WatchNumber(String name, String description, boolean interval, long v) {
|
||||
@ConstructorProperties({"name", "description", "interval", "value"})
|
||||
protected WatchNumber(String name, String description, boolean interval, long value) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.interval = interval;
|
||||
this.set(v);
|
||||
this.set(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user