@Test public void testCreateClass() throws Exception { Class<?> c = DynamicClassFactory.INSTANCE.fetchOrCreatePairClass(new ClassPair(String.class, Date.class)); KVPair pair = (KVPair) c.newInstance(); assertEquals(1, c.getInterfaces().length); assertEquals(KVPair.class, c.getInterfaces()[0]); Annotation[] as = c.getAnnotations(); assertEquals(1, as.length); assertEquals(JsonPropertyOrder.class, as[0].annotationType()); assertEquals(String.class, c.getMethod("getKey").getReturnType()); assertEquals(Date.class, c.getMethod("getValue").getReturnType()); assertEquals(String.class, c.getMethod("setKey", String.class).getParameterTypes()[0]); assertEquals(Date.class, c.getMethod("setValue", Date.class).getParameterTypes()[0]); Date date = new Date(); pair.key("key"); pair.value(date); assertEquals("key", pair.key()); assertEquals(date, pair.value()); }
private Class<?> createClass(ClassPair classPair) { String className = generateClassName(classPair); String classStr = className.replaceAll("\\.", "/"); String keyClassType = Type.getDescriptor(classPair.keyClass); String valueClassType = Type.getDescriptor(classPair.valueClass); ClassWriter cw = new ClassWriter(0); cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, className, null, "java/lang/Object", new String[]{interfaceName}); AnnotationVisitor anno = cw.visitAnnotation(Type.getDescriptor(JsonPropertyOrder.class), true); AnnotationVisitor aa = anno.visitArray("value"); aa.visit("", "key"); aa.visit("", "value"); aa.visitEnd(); anno.visitEnd(); FieldVisitor keyField = cw.visitField(ACC_PRIVATE, "key", keyClassType, null, null); keyField.visitEnd(); FieldVisitor valueField = cw.visitField(ACC_PRIVATE, "value", valueClassType, null, null); valueField.visitEnd(); MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitMaxs(2, 1); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(Object.class), "<init>", "()V", false); mv.visitInsn(RETURN); mv.visitEnd(); addGetterSetter(classPair, className, classStr, keyClassType, valueClassType, cw); addKVPairMethods(classPair, className, classStr, keyClassType, valueClassType, cw); cw.visitEnd(); return defineClass(className, cw.toByteArray()); }
DynamicClassFactory { private Class<?> createClass(ClassPair classPair) { String className = generateClassName(classPair); String classStr = className.replaceAll("\\.", "/"); String keyClassType = Type.getDescriptor(classPair.keyClass); String valueClassType = Type.getDescriptor(classPair.valueClass); ClassWriter cw = new ClassWriter(0); cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, className, null, "java/lang/Object", new String[]{interfaceName}); AnnotationVisitor anno = cw.visitAnnotation(Type.getDescriptor(JsonPropertyOrder.class), true); AnnotationVisitor aa = anno.visitArray("value"); aa.visit("", "key"); aa.visit("", "value"); aa.visitEnd(); anno.visitEnd(); FieldVisitor keyField = cw.visitField(ACC_PRIVATE, "key", keyClassType, null, null); keyField.visitEnd(); FieldVisitor valueField = cw.visitField(ACC_PRIVATE, "value", valueClassType, null, null); valueField.visitEnd(); MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitMaxs(2, 1); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(Object.class), "<init>", "()V", false); mv.visitInsn(RETURN); mv.visitEnd(); addGetterSetter(classPair, className, classStr, keyClassType, valueClassType, cw); addKVPairMethods(classPair, className, classStr, keyClassType, valueClassType, cw); cw.visitEnd(); return defineClass(className, cw.toByteArray()); } }
DynamicClassFactory { private Class<?> createClass(ClassPair classPair) { String className = generateClassName(classPair); String classStr = className.replaceAll("\\.", "/"); String keyClassType = Type.getDescriptor(classPair.keyClass); String valueClassType = Type.getDescriptor(classPair.valueClass); ClassWriter cw = new ClassWriter(0); cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, className, null, "java/lang/Object", new String[]{interfaceName}); AnnotationVisitor anno = cw.visitAnnotation(Type.getDescriptor(JsonPropertyOrder.class), true); AnnotationVisitor aa = anno.visitArray("value"); aa.visit("", "key"); aa.visit("", "value"); aa.visitEnd(); anno.visitEnd(); FieldVisitor keyField = cw.visitField(ACC_PRIVATE, "key", keyClassType, null, null); keyField.visitEnd(); FieldVisitor valueField = cw.visitField(ACC_PRIVATE, "value", valueClassType, null, null); valueField.visitEnd(); MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitMaxs(2, 1); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(Object.class), "<init>", "()V", false); mv.visitInsn(RETURN); mv.visitEnd(); addGetterSetter(classPair, className, classStr, keyClassType, valueClassType, cw); addKVPairMethods(classPair, className, classStr, keyClassType, valueClassType, cw); cw.visitEnd(); return defineClass(className, cw.toByteArray()); } private  DynamicClassFactory(); }
DynamicClassFactory { private Class<?> createClass(ClassPair classPair) { String className = generateClassName(classPair); String classStr = className.replaceAll("\\.", "/"); String keyClassType = Type.getDescriptor(classPair.keyClass); String valueClassType = Type.getDescriptor(classPair.valueClass); ClassWriter cw = new ClassWriter(0); cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, className, null, "java/lang/Object", new String[]{interfaceName}); AnnotationVisitor anno = cw.visitAnnotation(Type.getDescriptor(JsonPropertyOrder.class), true); AnnotationVisitor aa = anno.visitArray("value"); aa.visit("", "key"); aa.visit("", "value"); aa.visitEnd(); anno.visitEnd(); FieldVisitor keyField = cw.visitField(ACC_PRIVATE, "key", keyClassType, null, null); keyField.visitEnd(); FieldVisitor valueField = cw.visitField(ACC_PRIVATE, "value", valueClassType, null, null); valueField.visitEnd(); MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitMaxs(2, 1); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(Object.class), "<init>", "()V", false); mv.visitInsn(RETURN); mv.visitEnd(); addGetterSetter(classPair, className, classStr, keyClassType, valueClassType, cw); addKVPairMethods(classPair, className, classStr, keyClassType, valueClassType, cw); cw.visitEnd(); return defineClass(className, cw.toByteArray()); } private  DynamicClassFactory(); synchronized Class<?> fetchOrCreatePairClass(ClassPair classPair); synchronized Class<?> fetchOrCreatePairClass(MapType mapType); void setKeyValue(Object obj, Object key, Object value); @SuppressWarnings({ "rawtypes", "unchecked" }) KeyValuePair getKeyValue(Object obj); }
DynamicClassFactory { private Class<?> createClass(ClassPair classPair) { String className = generateClassName(classPair); String classStr = className.replaceAll("\\.", "/"); String keyClassType = Type.getDescriptor(classPair.keyClass); String valueClassType = Type.getDescriptor(classPair.valueClass); ClassWriter cw = new ClassWriter(0); cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, className, null, "java/lang/Object", new String[]{interfaceName}); AnnotationVisitor anno = cw.visitAnnotation(Type.getDescriptor(JsonPropertyOrder.class), true); AnnotationVisitor aa = anno.visitArray("value"); aa.visit("", "key"); aa.visit("", "value"); aa.visitEnd(); anno.visitEnd(); FieldVisitor keyField = cw.visitField(ACC_PRIVATE, "key", keyClassType, null, null); keyField.visitEnd(); FieldVisitor valueField = cw.visitField(ACC_PRIVATE, "value", valueClassType, null, null); valueField.visitEnd(); MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitMaxs(2, 1); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(Object.class), "<init>", "()V", false); mv.visitInsn(RETURN); mv.visitEnd(); addGetterSetter(classPair, className, classStr, keyClassType, valueClassType, cw); addKVPairMethods(classPair, className, classStr, keyClassType, valueClassType, cw); cw.visitEnd(); return defineClass(className, cw.toByteArray()); } private  DynamicClassFactory(); synchronized Class<?> fetchOrCreatePairClass(ClassPair classPair); synchronized Class<?> fetchOrCreatePairClass(MapType mapType); void setKeyValue(Object obj, Object key, Object value); @SuppressWarnings({ "rawtypes", "unchecked" }) KeyValuePair getKeyValue(Object obj); static DynamicClassFactory INSTANCE; }