去掉java.beans.ConstructorProperties,使用org.redkale.util.ConstructorParameters 代替

This commit is contained in:
Redkale
2018-01-11 11:05:39 +08:00
parent 7d3e4529af
commit 26fc039aa5
15 changed files with 140 additions and 125 deletions

View File

@@ -6,7 +6,12 @@
* *
module org.redkale { module org.redkale {
requires java.se; requires java.base;
requires java.logging;
requires java.xml;
requires java.sql;
requires java.sql.rowset;
requires jdk.unsupported; //sun.misc.Unsafe requires jdk.unsupported; //sun.misc.Unsafe
exports javax.annotation; exports javax.annotation;

View File

@@ -119,7 +119,7 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
this.register(AnyValue.class, Creator.create(AnyValue.DefaultAnyValue.class)); this.register(AnyValue.class, Creator.create(AnyValue.DefaultAnyValue.class));
this.register(HttpCookie.class, new Creator<HttpCookie>() { this.register(HttpCookie.class, new Creator<HttpCookie>() {
@Override @Override
@Creator.ConstructorParameters({"name", "value"}) @ConstructorParameters({"name", "value"})
public HttpCookie create(Object... params) { public HttpCookie create(Object... params) {
return new HttpCookie((String) params[0], (String) params[1]); return new HttpCookie((String) params[0], (String) params[1]);
} }

View File

@@ -207,7 +207,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 {
Creator.ConstructorParameters cps = creator.getClass().getMethod("create", Object[].class).getAnnotation(Creator.ConstructorParameters.class); ConstructorParameters cps = creator.getClass().getMethod("create", Object[].class).getAnnotation(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

@@ -326,7 +326,7 @@ public final class Transport {
this.enable = true; this.enable = true;
} }
@java.beans.ConstructorProperties({"address", "enable"}) @ConstructorParameters({"address", "enable"})
public TransportAddress(InetSocketAddress address, boolean enable) { public TransportAddress(InetSocketAddress address, boolean enable) {
this.address = address; this.address = address;
this.enable = enable; this.enable = enable;

View File

@@ -88,7 +88,7 @@ public class HttpContext extends Context {
mv = new AsmMethodVisitor(cw.visitMethod(ACC_PUBLIC, "<init>", "(" + handlerDesc + ")V", null, null)); mv = new AsmMethodVisitor(cw.visitMethod(ACC_PUBLIC, "<init>", "(" + handlerDesc + ")V", null, null));
//mv.setDebug(true); //mv.setDebug(true);
{ {
av0 = mv.visitAnnotation("Ljava/beans/ConstructorProperties;", true); av0 = mv.visitAnnotation("Lorg/redkale/util/ConstructorParameters;", true);
{ {
AnnotationVisitor av1 = av0.visitArray("value"); AnnotationVisitor av1 = av0.visitArray("value");
av1.visit(null, "handler"); av1.visit(null, "handler");

View File

@@ -53,7 +53,7 @@ public interface SncpAsyncHandler<V, A> extends CompletionHandler<V, A> {
* *
* private CompletableFuture sncpfuture; * private CompletableFuture sncpfuture;
* *
* &#64;java.beans.ConstructorProperties({"sncphandler"}) * &#64;ConstructorParameters({"sncphandler"})
* public XXXAsyncHandler_DyncSncpAsyncHandler_4323(SncpAsyncHandler sncphandler) { * public XXXAsyncHandler_DyncSncpAsyncHandler_4323(SncpAsyncHandler sncphandler) {
* super(); * super();
* this.sncphandler = sncphandler; * this.sncphandler = sncphandler;
@@ -123,7 +123,7 @@ public interface SncpAsyncHandler<V, A> extends CompletionHandler<V, A> {
mv = new AsmMethodVisitor(cw.visitMethod(ACC_PUBLIC, "<init>", "(" + sncpHandlerDesc + ")V", null, null)); mv = new AsmMethodVisitor(cw.visitMethod(ACC_PUBLIC, "<init>", "(" + sncpHandlerDesc + ")V", null, null));
//mv.setDebug(true); //mv.setDebug(true);
{ {
av0 = mv.visitAnnotation("Ljava/beans/ConstructorProperties;", true); av0 = mv.visitAnnotation("org/redkale/util/ConstructorParameters;", true);
{ {
AnnotationVisitor av1 = av0.visitArray("value"); AnnotationVisitor av1 = av0.visitArray("value");
av1.visit(null, "sncphandler"); av1.visit(null, "sncphandler");

View File

@@ -5,12 +5,12 @@
*/ */
package org.redkale.source; package org.redkale.source;
import java.beans.ConstructorProperties;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.*; import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
import org.redkale.convert.ConvertColumn; import org.redkale.convert.ConvertColumn;
import org.redkale.convert.json.JsonFactory; import org.redkale.convert.json.JsonFactory;
import org.redkale.util.ConstructorParameters;
/** /**
* *
@@ -248,7 +248,7 @@ public interface CacheSource<V extends Object> {
this(cacheType, expireSeconds, (int) (System.currentTimeMillis() / 1000), key, objectValue, csetValue, listValue); this(cacheType, expireSeconds, (int) (System.currentTimeMillis() / 1000), key, objectValue, csetValue, listValue);
} }
@ConstructorProperties({"cacheType", "expireSeconds", "lastAccessed", "key", "objectValue", "csetValue", "listValue"}) @ConstructorParameters({"cacheType", "expireSeconds", "lastAccessed", "key", "objectValue", "csetValue", "listValue"})
public CacheEntry(CacheEntryType cacheType, int expireSeconds, int lastAccessed, String key, T objectValue, CopyOnWriteArraySet<T> csetValue, ConcurrentLinkedQueue<T> listValue) { public CacheEntry(CacheEntryType cacheType, int expireSeconds, int lastAccessed, String key, T objectValue, CopyOnWriteArraySet<T> csetValue, ConcurrentLinkedQueue<T> listValue) {
this.cacheType = cacheType; this.cacheType = cacheType;
this.expireSeconds = expireSeconds; this.expireSeconds = expireSeconds;

View File

@@ -211,9 +211,9 @@ public final class EntityInfo<T> {
this.tableStrategy = dts; this.tableStrategy = dts;
this.creator = Creator.create(type); this.creator = Creator.create(type);
Creator.ConstructorParameters cp = null; ConstructorParameters cp = null;
try { try {
cp = this.creator.getClass().getMethod("create", Object[].class).getAnnotation(Creator.ConstructorParameters.class); cp = this.creator.getClass().getMethod("create", Object[].class).getAnnotation(ConstructorParameters.class);
} catch (Exception e) { } catch (Exception e) {
logger.log(Level.SEVERE, type + " cannot find ConstructorParameters Creator", e); logger.log(Level.SEVERE, type + " cannot find ConstructorParameters Creator", e);
} }

View File

@@ -6,6 +6,7 @@
package org.redkale.source; package org.redkale.source;
import java.util.Objects; import java.util.Objects;
import org.redkale.util.ConstructorParameters;
/** /**
* FilterKey主要用于自身字段间的表达式, 如: a.recordid = a.parentid , a.parentid就需要FilterKey来表示 new FilterKey("parentid") * FilterKey主要用于自身字段间的表达式, 如: a.recordid = a.parentid , a.parentid就需要FilterKey来表示 new FilterKey("parentid")
@@ -21,7 +22,7 @@ public class FilterKey implements java.io.Serializable {
private final String column; private final String column;
@java.beans.ConstructorProperties({"column"}) @ConstructorParameters({"column"})
public FilterKey(String column) { public FilterKey(String column) {
this.column = Objects.requireNonNull(column); this.column = Objects.requireNonNull(column);
} }

View File

@@ -372,7 +372,7 @@ public abstract class AnyValue {
T value; T value;
@java.beans.ConstructorProperties({"name", "value"}) @ConstructorParameters({"name", "value"})
public Entry(String name0, T value0) { public Entry(String name0, T value0) {
this.name = name0; this.name = name0;
this.value = value0; this.value = value0;

View File

@@ -0,0 +1,26 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.redkale.util;
import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* 类似java.beans.ConstructorProperties, 必须配合Creator使用
*
* <p>
* 详情见: https://redkale.org
*
* @author zhangjx
*/
@Documented
@Target({METHOD, CONSTRUCTOR})
@Retention(RUNTIME)
public @interface ConstructorParameters {
String[] value();
}

View File

@@ -4,11 +4,7 @@
*/ */
package org.redkale.util; package org.redkale.util;
import java.beans.*;
import java.io.*; import java.io.*;
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.net.*; import java.net.*;
import java.util.*; import java.util.*;
@@ -56,7 +52,7 @@ import static org.redkale.asm.Opcodes.*;
* *
* private String name; * private String name;
* *
* &#64;java.beans.ConstructorProperties({"id", "name"}) * &#64;ConstructorParameters({"id", "name"})
* public Record(int id, String name) { * public Record(int id, String name) {
* this.id = id; * this.id = id;
* this.name = name; * this.name = name;
@@ -72,17 +68,6 @@ import static org.redkale.asm.Opcodes.*;
*/ */
public interface Creator<T> { public interface Creator<T> {
/**
* 该注解只用于Creator.create方法上 与 java.beans.ConstructorProperties 类似。
*
*/
@Documented
@Target({METHOD})
@Retention(RUNTIME)
public static @interface ConstructorParameters {
String[] value();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
static class CreatorInner { static class CreatorInner {
@@ -180,7 +165,6 @@ public interface Creator<T> {
return se; return se;
} }
} }
}
/** /**
* 创建对象 * 创建对象
@@ -252,9 +236,9 @@ public interface Creator<T> {
} }
if (constructor0 == null) { // 2、查找public带ConstructorProperties注解的构造函数 if (constructor0 == null) { // 2、查找public带ConstructorProperties注解的构造函数
for (Constructor c : clazz.getConstructors()) { for (Constructor c : clazz.getConstructors()) {
ConstructorProperties cp = (ConstructorProperties) c.getAnnotation(ConstructorProperties.class); ConstructorParameters cp = (ConstructorParameters) c.getAnnotation(ConstructorParameters.class);
if (cp == null) continue; if (cp == null) continue;
SimpleEntry<String, Class>[] fields = ConstructorParameters.CreatorInner.getConstructorField(clazz, c.getParameterCount(), cp.value()); SimpleEntry<String, Class>[] fields = CreatorInner.getConstructorField(clazz, c.getParameterCount(), cp.value());
if (fields != null) { if (fields != null) {
constructor0 = c; constructor0 = c;
constructorParameters0 = fields; constructorParameters0 = fields;
@@ -262,17 +246,17 @@ public interface Creator<T> {
} }
} }
} }
if (constructor0 == null) { // 3、查找public且不带ConstructorProperties注解的构造函数 if (constructor0 == null) { // 3、查找public且不带ConstructorParameters注解的构造函数
List<Constructor> cs = new ArrayList<>(); List<Constructor> cs = new ArrayList<>();
for (Constructor c : clazz.getConstructors()) { for (Constructor c : clazz.getConstructors()) {
if (c.getAnnotation(ConstructorProperties.class) != null) continue; if (c.getAnnotation(ConstructorParameters.class) != null) continue;
if (c.getParameterCount() < 1) continue; if (c.getParameterCount() < 1) continue;
cs.add(c); cs.add(c);
} }
//优先参数最多的构造函数 //优先参数最多的构造函数
cs.sort((o1, o2) -> o2.getParameterCount() - o1.getParameterCount()); cs.sort((o1, o2) -> o2.getParameterCount() - o1.getParameterCount());
for (Constructor c : cs) { for (Constructor c : cs) {
SimpleEntry<String, Class>[] fields = ConstructorParameters.CreatorInner.getConstructorField(clazz, c.getParameterCount(), Type.getConstructorDescriptor(c)); SimpleEntry<String, Class>[] fields = CreatorInner.getConstructorField(clazz, c.getParameterCount(), Type.getConstructorDescriptor(c));
if (fields != null) { if (fields != null) {
constructor0 = c; constructor0 = c;
constructorParameters0 = fields; constructorParameters0 = fields;
@@ -283,9 +267,9 @@ public interface Creator<T> {
if (constructor0 == null) { // 4、查找非private带ConstructorProperties的构造函数 if (constructor0 == null) { // 4、查找非private带ConstructorProperties的构造函数
for (Constructor c : clazz.getDeclaredConstructors()) { for (Constructor c : clazz.getDeclaredConstructors()) {
if (Modifier.isPublic(c.getModifiers()) || Modifier.isPrivate(c.getModifiers())) continue; if (Modifier.isPublic(c.getModifiers()) || Modifier.isPrivate(c.getModifiers())) continue;
ConstructorProperties cp = (ConstructorProperties) c.getAnnotation(ConstructorProperties.class); ConstructorParameters cp = (ConstructorParameters) c.getAnnotation(ConstructorParameters.class);
if (cp == null) continue; if (cp == null) continue;
SimpleEntry<String, Class>[] fields = ConstructorParameters.CreatorInner.getConstructorField(clazz, c.getParameterCount(), cp.value()); SimpleEntry<String, Class>[] fields = CreatorInner.getConstructorField(clazz, c.getParameterCount(), cp.value());
if (fields != null) { if (fields != null) {
constructor0 = c; constructor0 = c;
constructorParameters0 = fields; constructorParameters0 = fields;
@@ -297,14 +281,14 @@ public interface Creator<T> {
List<Constructor> cs = new ArrayList<>(); List<Constructor> cs = new ArrayList<>();
for (Constructor c : clazz.getDeclaredConstructors()) { for (Constructor c : clazz.getDeclaredConstructors()) {
if (Modifier.isPublic(c.getModifiers()) || Modifier.isPrivate(c.getModifiers())) continue; if (Modifier.isPublic(c.getModifiers()) || Modifier.isPrivate(c.getModifiers())) continue;
if (c.getAnnotation(ConstructorProperties.class) != null) continue; if (c.getAnnotation(ConstructorParameters.class) != null) continue;
if (c.getParameterCount() < 1) continue; if (c.getParameterCount() < 1) continue;
cs.add(c); cs.add(c);
} }
//优先参数最多的构造函数 //优先参数最多的构造函数
cs.sort((o1, o2) -> o2.getParameterCount() - o1.getParameterCount()); cs.sort((o1, o2) -> o2.getParameterCount() - o1.getParameterCount());
for (Constructor c : cs) { for (Constructor c : cs) {
SimpleEntry<String, Class>[] fields = ConstructorParameters.CreatorInner.getConstructorField(clazz, c.getParameterCount(), Type.getConstructorDescriptor(c)); SimpleEntry<String, Class>[] fields = CreatorInner.getConstructorField(clazz, c.getParameterCount(), Type.getConstructorDescriptor(c));
if (fields != null) { if (fields != null) {
constructor0 = c; constructor0 = c;
constructorParameters0 = fields; constructorParameters0 = fields;
@@ -315,7 +299,7 @@ public interface Creator<T> {
final Constructor<T> constructor = constructor0; final Constructor<T> constructor = constructor0;
final SimpleEntry<String, Class>[] constructorParameters = constructorParameters0; final SimpleEntry<String, Class>[] constructorParameters = constructorParameters0;
if (constructor == null || constructorParameters == null) { if (constructor == null || constructorParameters == null) {
throw new RuntimeException("[" + clazz + "] have no public or java.beans.ConstructorProperties-Annotation constructor."); throw new RuntimeException("[" + clazz + "] have no public or ConstructorParameters-Annotation constructor.");
} }
//------------------------------------------------------------- //-------------------------------------------------------------
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
@@ -475,7 +459,7 @@ public interface Creator<T> {
t.printStackTrace(); t.printStackTrace();
} }
} }
if (!ispub && resultClazz == null) throw new RuntimeException("[" + clazz + "] have no public or java.beans.ConstructorProperties-Annotation constructor."); if (!ispub && resultClazz == null) throw new RuntimeException("[" + clazz + "] have no public or ConstructorParameters-Annotation constructor.");
try { try {
if (resultClazz == null) resultClazz = new ClassLoader(loader) { if (resultClazz == null) resultClazz = new ClassLoader(loader) {
public final Class<?> loadClass(String name, byte[] b) { public final Class<?> loadClass(String name, byte[] b) {

View File

@@ -5,9 +5,9 @@
*/ */
package org.redkale.test.convert; package org.redkale.test.convert;
import java.beans.*;
import org.redkale.convert.bson.*; import org.redkale.convert.bson.*;
import org.redkale.convert.json.*; import org.redkale.convert.json.*;
import org.redkale.util.ConstructorParameters;
/** /**
* 测试不存在无参数的构造函数的bean类解析 * 测试不存在无参数的构造函数的bean类解析
@@ -22,7 +22,7 @@ public class ConstructorArgsEntity {
private long createtime; private long createtime;
@ConstructorProperties({"userid", "name"}) @ConstructorParameters({"userid", "name"})
public ConstructorArgsEntity(int userid, String name) { public ConstructorArgsEntity(int userid, String name) {
this.userid = userid; this.userid = userid;
this.name = name; this.name = name;

View File

@@ -5,7 +5,6 @@
*/ */
package org.redkale.test.util; package org.redkale.test.util;
import java.beans.*;
import org.redkale.convert.json.*; import org.redkale.convert.json.*;
import org.redkale.util.*; import org.redkale.util.*;
@@ -33,7 +32,7 @@ public class CreatorRecord {
private double dval; private double dval;
@ConstructorProperties({"id", "name", "lval", "tval", "bval", "sval", "cval", "fval", "dval"}) @ConstructorParameters({"id", "name", "lval", "tval", "bval", "sval", "cval", "fval", "dval"})
CreatorRecord(int id, String name, long lval, boolean tval, byte bval, short sval, char cval, float fval, double dval) { CreatorRecord(int id, String name, long lval, boolean tval, byte bval, short sval, char cval, float fval, double dval) {
this.id = id; this.id = id;
this.name = name; this.name = name;

View File

@@ -62,7 +62,7 @@ class BService {
System.out.println("@Resource = " + name + " 资源变更: newVal = " + newVal + ", oldVal = " + oldVal); System.out.println("@Resource = " + name + " 资源变更: newVal = " + newVal + ", oldVal = " + oldVal);
} }
@java.beans.ConstructorProperties({"name"}) @ConstructorParameters({"name"})
public BService(String name) { public BService(String name) {
this.name = name; this.name = name;
} }