Search in sources :

Example 1 with FieldInfo

use of com.alibaba.fastjson.util.FieldInfo in project fastjson by alibaba.

the class ASMDeserializerFactory method _batchSet.

private void _batchSet(Context context, MethodVisitor mw, boolean flag) {
    for (int i = 0, size = context.fieldInfoList.length; i < size; ++i) {
        Label notSet_ = new Label();
        if (flag) {
            _isFlag(mw, context, i, notSet_);
        }
        FieldInfo fieldInfo = context.fieldInfoList[i];
        _loadAndSet(context, mw, fieldInfo);
        if (flag) {
            mw.visitLabel(notSet_);
        }
    }
}
Also used : Label(com.alibaba.fastjson.asm.Label) FieldInfo(com.alibaba.fastjson.util.FieldInfo)

Example 2 with FieldInfo

use of com.alibaba.fastjson.util.FieldInfo in project fastjson by alibaba.

the class ASMDeserializerFactory method _init.

private void _init(ClassWriter cw, Context context) {
    for (int i = 0, size = context.fieldInfoList.length; i < size; ++i) {
        FieldInfo fieldInfo = context.fieldInfoList[i];
        FieldWriter fw = new FieldWriter(cw, ACC_PUBLIC, fieldInfo.name + "_asm_prefix__", "[C");
        fw.visitEnd();
    }
    for (int i = 0, size = context.fieldInfoList.length; i < size; ++i) {
        FieldInfo fieldInfo = context.fieldInfoList[i];
        Class<?> fieldClass = fieldInfo.fieldClass;
        if (fieldClass.isPrimitive()) {
            continue;
        }
        if (Collection.class.isAssignableFrom(fieldClass)) {
            FieldWriter fw = new FieldWriter(cw, ACC_PUBLIC, fieldInfo.name + "_asm_list_item_deser__", desc(ObjectDeserializer.class));
            fw.visitEnd();
        } else {
            FieldWriter fw = new FieldWriter(cw, ACC_PUBLIC, fieldInfo.name + "_asm_deser__", desc(ObjectDeserializer.class));
            fw.visitEnd();
        }
    }
    MethodVisitor mw = new MethodWriter(cw, ACC_PUBLIC, "<init>", "(" + desc(ParserConfig.class) + desc(JavaBeanInfo.class) + ")V", null, null);
    mw.visitVarInsn(ALOAD, 0);
    mw.visitVarInsn(ALOAD, 1);
    mw.visitVarInsn(ALOAD, 2);
    mw.visitMethodInsn(INVOKESPECIAL, type(JavaBeanDeserializer.class), "<init>", "(" + desc(ParserConfig.class) + desc(JavaBeanInfo.class) + ")V");
    // init fieldNamePrefix
    for (int i = 0, size = context.fieldInfoList.length; i < size; ++i) {
        FieldInfo fieldInfo = context.fieldInfoList[i];
        mw.visitVarInsn(ALOAD, 0);
        // public char[] toCharArray()
        mw.visitLdcInsn("\"" + fieldInfo.name + "\":");
        mw.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "toCharArray", "()[C");
        mw.visitFieldInsn(PUTFIELD, context.className, fieldInfo.name + "_asm_prefix__", "[C");
    }
    mw.visitInsn(RETURN);
    mw.visitMaxs(4, 4);
    mw.visitEnd();
}
Also used : JavaBeanInfo(com.alibaba.fastjson.util.JavaBeanInfo) MethodWriter(com.alibaba.fastjson.asm.MethodWriter) FieldInfo(com.alibaba.fastjson.util.FieldInfo) FieldWriter(com.alibaba.fastjson.asm.FieldWriter) MethodVisitor(com.alibaba.fastjson.asm.MethodVisitor)

Example 3 with FieldInfo

use of com.alibaba.fastjson.util.FieldInfo in project fastjson by alibaba.

the class JavaBeanDeserializer method smartMatch.

public FieldDeserializer smartMatch(String key) {
    if (key == null) {
        return null;
    }
    FieldDeserializer fieldDeserializer = getFieldDeserializer(key);
    if (fieldDeserializer == null) {
        boolean startsWithIs = key.startsWith("is");
        for (FieldDeserializer fieldDeser : sortedFieldDeserializers) {
            FieldInfo fieldInfo = fieldDeser.fieldInfo;
            Class<?> fieldClass = fieldInfo.fieldClass;
            String fieldName = fieldInfo.name;
            if (fieldName.equalsIgnoreCase(key)) {
                fieldDeserializer = fieldDeser;
                break;
            }
            if (//
            startsWithIs && //
            (fieldClass == boolean.class || fieldClass == Boolean.class) && fieldName.equalsIgnoreCase(key.substring(2))) {
                fieldDeserializer = fieldDeser;
                break;
            }
        }
    }
    if (fieldDeserializer == null) {
        boolean snakeOrkebab = false;
        String key2 = null;
        for (int i = 0; i < key.length(); ++i) {
            char ch = key.charAt(i);
            if (ch == '_') {
                snakeOrkebab = true;
                key2 = key.replaceAll("_", "");
                break;
            } else if (ch == '-') {
                snakeOrkebab = true;
                key2 = key.replaceAll("-", "");
                break;
            }
        }
        if (snakeOrkebab) {
            fieldDeserializer = getFieldDeserializer(key2);
            if (fieldDeserializer == null) {
                for (FieldDeserializer fieldDeser : sortedFieldDeserializers) {
                    if (fieldDeser.fieldInfo.name.equalsIgnoreCase(key2)) {
                        fieldDeserializer = fieldDeser;
                        break;
                    }
                }
            }
        }
    }
    if (fieldDeserializer == null) {
        for (FieldDeserializer fieldDeser : sortedFieldDeserializers) {
            if (fieldDeser.fieldInfo.alternateName(key)) {
                fieldDeserializer = fieldDeser;
                break;
            }
        }
    }
    return fieldDeserializer;
}
Also used : FieldInfo(com.alibaba.fastjson.util.FieldInfo)

Example 4 with FieldInfo

use of com.alibaba.fastjson.util.FieldInfo in project fastjson by alibaba.

the class JavaBeanDeserializer method createInstance.

public //
Object createInstance(//
Map<String, Object> map, //
ParserConfig config) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    Object object = null;
    if (beanInfo.creatorConstructor == null && beanInfo.factoryMethod == null) {
        object = createInstance(null, clazz);
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            FieldDeserializer fieldDeser = smartMatch(key);
            if (fieldDeser == null) {
                continue;
            }
            final FieldInfo fieldInfo = fieldDeser.fieldInfo;
            Type paramType = fieldInfo.fieldType;
            value = TypeUtils.cast(value, paramType, config);
            fieldDeser.setValue(object, value);
        }
        if (beanInfo.buildMethod != null) {
            Object builtObj;
            try {
                builtObj = beanInfo.buildMethod.invoke(object);
            } catch (Exception e) {
                throw new JSONException("build object error", e);
            }
            return builtObj;
        }
        return object;
    }
    FieldInfo[] fieldInfoList = beanInfo.fields;
    int size = fieldInfoList.length;
    Object[] params = new Object[size];
    for (int i = 0; i < size; ++i) {
        FieldInfo fieldInfo = fieldInfoList[i];
        params[i] = map.get(fieldInfo.name);
    }
    if (beanInfo.creatorConstructor != null) {
        try {
            object = beanInfo.creatorConstructor.newInstance(params);
        } catch (Exception e) {
            throw new JSONException("create instance error, " + beanInfo.creatorConstructor.toGenericString(), e);
        }
    } else if (beanInfo.factoryMethod != null) {
        try {
            object = beanInfo.factoryMethod.invoke(null, params);
        } catch (Exception e) {
            throw new JSONException("create factory method error, " + beanInfo.factoryMethod.toString(), e);
        }
    }
    return object;
}
Also used : JSONException(com.alibaba.fastjson.JSONException) JSONObject(com.alibaba.fastjson.JSONObject) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) FieldInfo(com.alibaba.fastjson.util.FieldInfo) JSONException(com.alibaba.fastjson.JSONException)

Example 5 with FieldInfo

use of com.alibaba.fastjson.util.FieldInfo in project fastjson by alibaba.

the class JavaBeanDeserializer method parseField.

public boolean parseField(DefaultJSONParser parser, String key, Object object, Type objectType, Map<String, Object> fieldValues, int[] setFlags) {
    // xxx
    JSONLexer lexer = parser.lexer;
    final int disableFieldSmartMatchMask = Feature.DisableFieldSmartMatch.mask;
    FieldDeserializer fieldDeserializer;
    if (lexer.isEnabled(disableFieldSmartMatchMask) || (this.beanInfo.parserFeatures & disableFieldSmartMatchMask) != 0) {
        fieldDeserializer = getFieldDeserializer(key);
    } else {
        fieldDeserializer = smartMatch(key);
    }
    final int mask = Feature.SupportNonPublicField.mask;
    if (fieldDeserializer == null && (lexer.isEnabled(mask) || (this.beanInfo.parserFeatures & mask) != 0)) {
        if (this.extraFieldDeserializers == null) {
            ConcurrentHashMap extraFieldDeserializers = new ConcurrentHashMap<String, Object>(1, 0.75f, 1);
            Field[] fields = this.clazz.getDeclaredFields();
            for (Field field : fields) {
                String fieldName = field.getName();
                if (this.getFieldDeserializer(fieldName) != null) {
                    continue;
                }
                int fieldModifiers = field.getModifiers();
                if ((fieldModifiers & Modifier.FINAL) != 0 || (fieldModifiers & Modifier.STATIC) != 0) {
                    continue;
                }
                extraFieldDeserializers.put(fieldName, field);
            }
            this.extraFieldDeserializers = extraFieldDeserializers;
        }
        Object deserOrField = extraFieldDeserializers.get(key);
        if (deserOrField != null) {
            if (deserOrField instanceof FieldDeserializer) {
                fieldDeserializer = ((FieldDeserializer) deserOrField);
            } else {
                Field field = (Field) deserOrField;
                field.setAccessible(true);
                FieldInfo fieldInfo = new FieldInfo(key, field.getDeclaringClass(), field.getType(), field.getGenericType(), field, 0, 0, 0);
                fieldDeserializer = new DefaultFieldDeserializer(parser.getConfig(), clazz, fieldInfo);
                extraFieldDeserializers.put(key, fieldDeserializer);
            }
        }
    }
    if (fieldDeserializer == null) {
        if (!lexer.isEnabled(Feature.IgnoreNotMatch)) {
            throw new JSONException("setter not found, class " + clazz.getName() + ", property " + key);
        }
        parser.parseExtra(object, key);
        return false;
    }
    int fieldIndex = -1;
    for (int i = 0; i < sortedFieldDeserializers.length; ++i) {
        if (sortedFieldDeserializers[i] == fieldDeserializer) {
            fieldIndex = i;
            break;
        }
    }
    if (fieldIndex != -1 && setFlags != null && key.startsWith("_")) {
        int flagIndex = fieldIndex / 32;
        if (flagIndex < setFlags.length) {
            if ((setFlags[flagIndex] & (1 << flagIndex)) != 0) {
                parser.parseExtra(object, key);
                return false;
            }
        }
    }
    lexer.nextTokenWithColon(fieldDeserializer.getFastMatchToken());
    fieldDeserializer.parseField(parser, object, objectType, fieldValues);
    return true;
}
Also used : JSONField(com.alibaba.fastjson.annotation.JSONField) JSONException(com.alibaba.fastjson.JSONException) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) JSONObject(com.alibaba.fastjson.JSONObject) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) FieldInfo(com.alibaba.fastjson.util.FieldInfo)

Aggregations

FieldInfo (com.alibaba.fastjson.util.FieldInfo)20 JSONException (com.alibaba.fastjson.JSONException)7 Label (com.alibaba.fastjson.asm.Label)6 JSONObject (com.alibaba.fastjson.JSONObject)4 JSONField (com.alibaba.fastjson.annotation.JSONField)4 MethodVisitor (com.alibaba.fastjson.asm.MethodVisitor)4 MethodWriter (com.alibaba.fastjson.asm.MethodWriter)4 Type (java.lang.reflect.Type)4 JSON (com.alibaba.fastjson.JSON)3 JSONType (com.alibaba.fastjson.annotation.JSONType)3 ParseContext (com.alibaba.fastjson.parser.ParseContext)3 Field (java.lang.reflect.Field)3 Method (java.lang.reflect.Method)3 ParameterizedType (java.lang.reflect.ParameterizedType)3 Collection (java.util.Collection)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 FieldWriter (com.alibaba.fastjson.asm.FieldWriter)2 JavaBeanInfo (com.alibaba.fastjson.util.JavaBeanInfo)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2