public JavaBeanDeserializer(ParserConfig config, Class<?> clazz, Type type) { this.feildDeserializerMap = new IdentityHashMap(); this.fieldDeserializers = new ArrayList(); this.sortedFieldDeserializers = new ArrayList(); this.clazz = clazz; this.beanInfo = DeserializeBeanInfo.computeSetters(clazz, type); for (FieldInfo fieldInfo : this.beanInfo.getFieldList()) { addFieldDeserializer(config, clazz, fieldInfo); } for (FieldInfo fieldInfo2 : this.beanInfo.getSortedFieldList()) { this.sortedFieldDeserializers.add((FieldDeserializer) this.feildDeserializerMap.get(fieldInfo2.getName().intern())); } }
public Context(String className, ParserConfig config, DeserializeBeanInfo beanInfo, int initVariantIndex) { this.className = className; this.clazz = beanInfo.getClazz(); this.variantIndex = initVariantIndex; this.beanInfo = beanInfo; this.fieldInfoList = new ArrayList(beanInfo.getFieldList()); }
public Context(String className, ParserConfig config, DeserializeBeanInfo beanInfo, int initVariantIndex){ this.className = className; this.clazz = beanInfo.getClazz(); this.variantIndex = initVariantIndex; this.beanInfo = beanInfo; fieldInfoList = new ArrayList<FieldInfo>(beanInfo.getFieldList()); }
@Override public void gen() throws IOException { beanInfo = DeserializeBeanInfo.computeSetters(clazz, type); genClassName = clazz.getSimpleName() + "GenDecoder"; print("package "); print(clazz.getPackage().getName()); println(";"); println(); println("import java.lang.reflect.Type;"); println(); println("import com.alibaba.fastjson.parser.DefaultJSONParser;"); println("import com.alibaba.fastjson.parser.DefaultJSONParser.ResolveTask;"); println("import com.alibaba.fastjson.parser.ParserConfig;"); println("import com.alibaba.fastjson.parser.Feature;"); println("import com.alibaba.fastjson.parser.JSONLexerBase;"); println("import com.alibaba.fastjson.parser.JSONToken;"); println("import com.alibaba.fastjson.parser.ParseContext;"); println("import com.alibaba.fastjson.parser.deserializer.ASMJavaBeanDeserializer;"); println("import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;"); println(); print("public class "); print(genClassName); print(" extends ASMJavaBeanDeserializer implements ObjectDeserializer {"); incrementIndent(); println(); genConstructor(); genCreateInstance(); genDeserialze(); endClass(); }
public ObjectDeserializer createJavaBeanDeserializer(ParserConfig config, Class<?> clazz, Type type) throws Exception { if (clazz.isPrimitive()) { throw new IllegalArgumentException("not support type :" + clazz.getName()); } String className = getGenClassName(clazz); ClassWriter cw = new ClassWriter(); cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, className, getType(ASMJavaBeanDeserializer.class), null); DeserializeBeanInfo beanInfo = DeserializeBeanInfo.computeSetters(clazz, type); _init(cw, new Context(className, config, beanInfo, 3)); _createInstance(cw, new Context(className, config, beanInfo, 3)); _deserialze(cw, new Context(className, config, beanInfo, 4)); _deserialzeArrayMapping(cw, new Context(className, config, beanInfo, 4)); byte[] code = cw.toByteArray(); // org.apache.commons.io.IOUtils.write(code, new java.io.FileOutputStream( // "/usr/alibaba/workspace-3.7/fastjson-asm/target/classes/" // + className + ".class")); Class<?> exampleClass = classLoader.defineClassPublic(className, code, 0, code.length); Constructor<?> constructor = exampleClass.getConstructor(ParserConfig.class, Class.class); Object instance = constructor.newInstance(config, clazz); return (ObjectDeserializer) instance; }
public JavaBeanDeserializer(ParserConfig config, Class<?> clazz, Type type){ this.clazz = clazz; this.type = type; beanInfo = DeserializeBeanInfo.computeSetters(clazz, type); for (FieldInfo fieldInfo : beanInfo.getFieldList()) { addFieldDeserializer(config, clazz, fieldInfo); } }
public DeserializeBeanInfo getBeanInfo() { return this.beanInfo; }
public ObjectDeserializer createJavaBeanDeserializer(ParserConfig config, Class<?> clazz, Type type) throws Exception { Exception ex; Throwable th; if (clazz.isPrimitive()) { throw new IllegalArgumentException("not support type :" + clazz.getName()); } String className = getGenClassName(clazz); ClassWriter cw = new ClassWriter(); cw.visit(49, 33, className, "com/alibaba/fastjson/parser/deserializer/ASMJavaBeanDeserializer", null); DeserializeBeanInfo beanInfo = DeserializeBeanInfo.computeSetters(clazz, type); _init(cw, new Context(className, config, beanInfo, 3)); _createInstance(cw, new Context(className, config, beanInfo, 3)); _deserialze(cw, new Context(className, config, beanInfo, 4)); _deserialzeArrayMapping(cw, new Context(className, config, beanInfo, 4)); byte[] code = cw.toByteArray(); if (JSON.DUMP_CLASS != null) { FileOutputStream fos = null; try { FileOutputStream fos2 = new FileOutputStream(JSON.DUMP_CLASS + File.separator + className + ".class"); try { fos2.write(code); if (fos2 != null) { fos2.close(); } } catch (Exception e) { ex = e; fos = fos2; try { System.err.println("FASTJSON dump class:" + className + "失败:" + ex.getMessage()); if (fos != null) { fos.close(); } return (ObjectDeserializer) this.classLoader.defineClassPublic(className, code, 0, code.length).getConstructor(new Class[]{ParserConfig.class, Class.class}).newInstance(new Object[]{config, clazz}); } catch (Throwable th2) { th = th2; if (fos != null) { fos.close(); } throw th; } } catch (Throwable th3) { th = th3; fos = fos2; if (fos != null) { fos.close(); } throw th; } } catch (Exception e2) { ex = e2; System.err.println("FASTJSON dump class:" + className + "失败:" + ex.getMessage()); if (fos != null) { fos.close(); } return (ObjectDeserializer) this.classLoader.defineClassPublic(className, code, 0, code.length).getConstructor(new Class[]{ParserConfig.class, Class.class}).newInstance(new Object[]{config, clazz}); } } return (ObjectDeserializer) this.classLoader.defineClassPublic(className, code, 0, code.length).getConstructor(new Class[]{ParserConfig.class, Class.class}).newInstance(new Object[]{config, clazz}); }
public ObjectDeserializer createJavaBeanDeserializer(Class<?> clazz, Type type) { boolean asmEnable = this.asmEnable; if (asmEnable) { Class<?> superClass = clazz; while (Modifier.isPublic(superClass.getModifiers())) { superClass = superClass.getSuperclass(); if (superClass != Object.class) { if (superClass == null) { break; } } break; } asmEnable = false; } if (clazz.getTypeParameters().length != 0) { asmEnable = false; } if (asmEnable && this.asmFactory != null && this.asmFactory.isExternalClass(clazz)) { asmEnable = false; } if (asmEnable) { asmEnable = ASMUtils.checkName(clazz.getName()); } if (asmEnable) { if (clazz.isInterface()) { asmEnable = false; } DeserializeBeanInfo beanInfo = DeserializeBeanInfo.computeSetters(clazz, type); if (beanInfo.getFieldList().size() > 200) { asmEnable = false; } if (beanInfo.getDefaultConstructor() == null && !clazz.isInterface()) { asmEnable = false; } for (FieldInfo fieldInfo : beanInfo.getFieldList()) { if (fieldInfo.isGetOnly()) { asmEnable = false; break; } Class<?> fieldClass = fieldInfo.getFieldClass(); if (!Modifier.isPublic(fieldClass.getModifiers())) { asmEnable = false; break; } if (fieldClass.isMemberClass() && !Modifier.isStatic(fieldClass.getModifiers())) { asmEnable = false; } if (!ASMUtils.checkName(fieldInfo.getMember().getName())) { asmEnable = false; } } } if (asmEnable && clazz.isMemberClass() && !Modifier.isStatic(clazz.getModifiers())) { asmEnable = false; } if (!asmEnable) { return new JavaBeanDeserializer(this, clazz, type); } try { return this.asmFactory.createJavaBeanDeserializer(this, clazz, type); } catch (NoSuchMethodException e) { return new JavaBeanDeserializer(this, clazz, type); } catch (ASMException e2) { return new JavaBeanDeserializer(this, clazz, type); } catch (Exception e3) { throw new JSONException("create asm deserializer error, " + clazz.getName(), e3); } }
public DeserializeBeanInfo getBeanInfo() { return beanInfo; }
public ObjectDeserializer createJavaBeanDeserializer(Class<?> clazz, Type type) { boolean asmEnable = this.asmEnable; if (asmEnable) { Class<?> superClass = clazz; for (;;) { if (!Modifier.isPublic(superClass.getModifiers())) { asmEnable = false; break; } superClass = superClass.getSuperclass(); if (superClass == Object.class || superClass == null) { break; } } } if (clazz.getTypeParameters().length != 0) { asmEnable = false; } if (asmFactory.isExternalClass(clazz)) { asmEnable = false; } if (asmEnable) { if (clazz.isInterface()) { asmEnable = false; } DeserializeBeanInfo beanInfo = DeserializeBeanInfo.computeSetters(clazz, type); if (beanInfo.getFieldList().size() > 200) { asmEnable = false; } Constructor<?> defaultConstructor = beanInfo.getDefaultConstructor(); if (defaultConstructor == null && !clazz.isInterface()) { asmEnable = false; } for (FieldInfo fieldInfo : beanInfo.getFieldList()) { if (fieldInfo.isGetOnly()) { asmEnable = false; break; } Class<?> fieldClass = fieldInfo.getFieldClass(); if (!Modifier.isPublic(fieldClass.getModifiers())) { asmEnable = false; break; } if (fieldClass.isMemberClass() && !Modifier.isStatic(fieldClass.getModifiers())) { asmEnable = false; } } } if (asmEnable) { if (clazz.isMemberClass() && !Modifier.isStatic(clazz.getModifiers())) { asmEnable = false; } } if (!asmEnable) { return new JavaBeanDeserializer(this, clazz, type); } try { return asmFactory.createJavaBeanDeserializer(this, clazz, type); // } catch (VerifyError e) { // e.printStackTrace(); // return new JavaBeanDeserializer(this, clazz, type); } catch (NoSuchMethodException ex) { return new JavaBeanDeserializer(this, clazz, type); } catch (ASMException asmError) { return new JavaBeanDeserializer(this, clazz, type); } catch (Exception e) { throw new JSONException("create asm deserializer error, " + clazz.getName(), e); } }
public JavaBeanDeserializer(DeserializeBeanInfo beanInfo){ this.beanInfo = beanInfo; this.clazz = beanInfo.getClass(); this.type = beanInfo.getType(); }