From 9b83abb06a63b8d1d7c880943541d4e09a44dfc8 Mon Sep 17 00:00:00 2001 From: Redkale <8730487+redkale@users.noreply.github.com> Date: Wed, 18 Dec 2019 22:06:54 +0800 Subject: [PATCH] =?UTF-8?q?Attribute=E5=A2=9E=E5=8A=A0subclass=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E7=94=A8=E4=BA=8E=E8=AF=86=E5=88=AB=E6=B3=9B=E5=9E=8B?= =?UTF-8?q?=E7=9A=84=E5=AD=90=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/org/redkale/util/Attribute.java | 30 +++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/org/redkale/util/Attribute.java b/src/org/redkale/util/Attribute.java index ebc1a9461..440a6f761 100644 --- a/src/org/redkale/util/Attribute.java +++ b/src/org/redkale/util/Attribute.java @@ -37,6 +37,8 @@ import static org.redkale.asm.Opcodes.*; * * private java.lang.reflect.Type _gtype = String.class; * + * private java.lang.Object _attach; + * * @Override * public String field() { * return "name"; @@ -63,6 +65,11 @@ import static org.redkale.asm.Opcodes.*; * } * * @Override + * public Object attach() { + * return _attach; + * } + * + * @Override * public Class declaringClass() { * return Record.class; * } @@ -680,6 +687,7 @@ public interface Attribute { final String fieldname = fieldalias; Class column = fieldtype; java.lang.reflect.Type generictype = fieldtype; + System.out.println("------------generictype:" + generictype + ", tfield: " + tfield + ", tgetter: " + tgetter); if (tfield != null) { // public tfield column = tfield.getType(); generictype = tfield.getGenericType(); @@ -694,19 +702,25 @@ public interface Attribute { } else if (column == null) { throw new RuntimeException("[" + clazz + "]have no field type"); } + boolean checkCast = false; + if (generictype instanceof java.lang.reflect.TypeVariable) { + checkCast = true; + generictype = TypeToken.getGenericType(generictype, subclass); + if (generictype instanceof Class) column = (Class) generictype; + } final Class pcolumn = column; if (column.isPrimitive()) column = java.lang.reflect.Array.get(java.lang.reflect.Array.newInstance(column, 1), 0).getClass(); final String supDynName = Attribute.class.getName().replace('.', '/'); - final String interName = clazz.getName().replace('.', '/'); + final String interName = subclass.getName().replace('.', '/'); final String columnName = column.getName().replace('.', '/'); - final String interDesc = Type.getDescriptor(clazz); + final String interDesc = Type.getDescriptor(subclass); final String columnDesc = Type.getDescriptor(column); ClassLoader loader = Thread.currentThread().getContextClassLoader(); - String newDynName = supDynName + "_Dyn_" + clazz.getSimpleName() + "_" + String newDynName = supDynName + "_Dyn_" + subclass.getSimpleName() + "_" + fieldname.substring(fieldname.indexOf('.') + 1) + "_" + pcolumn.getSimpleName().replace("[]", "Array"); - if (String.class.getClassLoader() != clazz.getClassLoader()) { - loader = clazz.getClassLoader(); + if (String.class.getClassLoader() != subclass.getClassLoader()) { + loader = subclass.getClassLoader(); newDynName = interName + "_Dyn" + Attribute.class.getSimpleName() + "_" + fieldname.substring(fieldname.indexOf('.') + 1) + "_" + pcolumn.getSimpleName().replace("[]", "Array"); } @@ -790,7 +804,7 @@ public interface Attribute { } { //declaringClass 方法 mv = cw.visitMethod(ACC_PUBLIC, "declaringClass", "()Ljava/lang/Class;", null, null); - mv.visitLdcInsn(Type.getType(clazz)); + mv.visitLdcInsn(Type.getType(subclass)); mv.visitInsn(ARETURN); mv.visitMaxs(1, 1); mv.visitEnd(); @@ -807,6 +821,8 @@ public interface Attribute { if (pcolumn != column) { mv.visitMethodInsn(INVOKESTATIC, columnName, "valueOf", "(" + Type.getDescriptor(pcolumn) + ")" + columnDesc, false); m = 2; + } else { + if (checkCast) mv.visitTypeInsn(CHECKCAST, columnName); } } } else { @@ -815,6 +831,8 @@ public interface Attribute { if (pcolumn != column) { mv.visitMethodInsn(INVOKESTATIC, columnName, "valueOf", "(" + Type.getDescriptor(pcolumn) + ")" + columnDesc, false); m = 2; + } else { + if (checkCast) mv.visitTypeInsn(CHECKCAST, columnName); } } mv.visitInsn(ARETURN);