This commit is contained in:
wentch
2015-12-24 15:23:45 +08:00
parent 8a86c5b108
commit 3d0e025516
4 changed files with 59 additions and 31 deletions

View File

@@ -5,7 +5,6 @@
*/ */
package org.redkale.convert; package org.redkale.convert;
import java.beans.*;
import org.redkale.util.Attribute; import org.redkale.util.Attribute;
import java.lang.reflect.*; import java.lang.reflect.*;
import java.util.*; import java.util.*;
@@ -241,7 +240,7 @@ public final class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T
static String[] findConstructorProperties(Creator creator) { static String[] findConstructorProperties(Creator creator) {
try { 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(); return cps == null ? null : cps.value();
} catch (Exception e) { } catch (Exception e) {
return null; return null;

View File

@@ -5,6 +5,7 @@
*/ */
package org.redkale.service; package org.redkale.service;
import java.beans.*;
import java.io.*; import java.io.*;
import java.lang.reflect.*; import java.lang.reflect.*;
import java.nio.channels.*; 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); 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.cacheType = cacheType;
this.expireSeconds = expireSeconds; this.expireSeconds = expireSeconds;
this.lastAccessed = lastAccessed; this.lastAccessed = lastAccessed;
@@ -437,19 +439,11 @@ public class CacheSourceService<K extends Serializable, V extends Object> implem
this.value = value; 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 @Override
public String toString() { public String toString() {
return JsonFactory.root().getConvert().convertTo(this); return JsonFactory.root().getConvert().convertTo(this);
} }
public CacheEntryType getCacheType() {
return cacheType;
}
@Ignore @Ignore
public boolean isListCacheType() { public boolean isListCacheType() {
return cacheType == CacheEntryType.LIST; 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)); return (expireSeconds > 0 && lastAccessed + expireSeconds < (System.currentTimeMillis() / 1000));
} }
public CacheEntryType getCacheType() {
return cacheType;
}
public int getExpireSeconds() { public int getExpireSeconds() {
return expireSeconds; return expireSeconds;
} }

View File

@@ -4,7 +4,10 @@
*/ */
package org.redkale.util; 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.lang.reflect.*;
import java.util.*; import java.util.*;
import jdk.internal.org.objectweb.asm.*; import jdk.internal.org.objectweb.asm.*;
@@ -22,7 +25,7 @@ import static jdk.internal.org.objectweb.asm.Opcodes.*;
* *
* private String name; * private String name;
* *
* public Record(int id, String name) { * Record(int id, String name) {
* this.id = id; * this.id = id;
* this.name = name; * this.name = name;
* } * }
@@ -30,6 +33,7 @@ import static jdk.internal.org.objectweb.asm.Opcodes.*;
* private static Creator createCreator() { * private static Creator createCreator() {
* return new Creator<Record>() { * return new Creator<Record>() {
* @Override * @Override
* @ConstructorParameters({"id", "name"})
* public Record create(Object... params) { * public Record create(Object... params) {
* return new Record((Integer) params[0], (String) params[1]); * return new Record((Integer) params[0], (String) params[1]);
* } * }
@@ -37,12 +41,34 @@ import static jdk.internal.org.objectweb.asm.Opcodes.*;
* } * }
* } * }
* *
* 或者:
* 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 * @see http://www.redkale.org
* @author zhangjx * @author zhangjx
* @param <T> * @param <T>
*/ */
public interface Creator<T> { public interface Creator<T> {
@Documented
@Target({CONSTRUCTOR, TYPE})
@Retention(RUNTIME)
public static @interface ConstructorParameters {
String[] value();
}
public T create(Object... params); public T create(Object... params);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@@ -89,14 +115,15 @@ public interface Creator<T> {
} }
} }
if (constructor == null) { if (constructor == null) {
for (Constructor c : clazz.getConstructors()) { for (Constructor c : clazz.getDeclaredConstructors()) {
if (c.getAnnotation(ConstructorProperties.class) != null) { if (Modifier.isPrivate(c.getModifiers())) continue;
if (c.getAnnotation(ConstructorProperties.class) != null || c.getAnnotation(ConstructorParameters.class) != null) {
constructor = c; constructor = c;
break; 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); ClassWriter cw = new ClassWriter(0);
FieldVisitor fv; FieldVisitor fv;
@@ -106,16 +133,6 @@ public interface Creator<T> {
{//构造方法 {//构造方法
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); 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.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitInsn(RETURN); mv.visitInsn(RETURN);
@@ -124,6 +141,18 @@ public interface Creator<T> {
} }
{//create 方法 {//create 方法
mv = cw.visitMethod(ACC_PUBLIC + ACC_VARARGS, "create", "([Ljava/lang/Object;)L" + interName + ";", null, null); 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.visitTypeInsn(NEW, interName);
mv.visitInsn(DUP); mv.visitInsn(DUP);
//--------------------------------------- //---------------------------------------

View File

@@ -5,6 +5,7 @@
*/ */
package org.redkale.watch; package org.redkale.watch;
import java.beans.*;
import java.util.concurrent.atomic.*; import java.util.concurrent.atomic.*;
/** /**
@@ -20,11 +21,12 @@ public final class WatchNumber extends AtomicLong implements WatchNode {
private final String description; 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.name = name;
this.description = description; this.description = description;
this.interval = interval; this.interval = interval;
this.set(v); this.set(value);
} }
@Override @Override