Search in sources :

Example 1 with IterableFieldEntity

use of org.gsonformat.intellij.entity.IterableFieldEntity in project GsonFormat by zzz40500.

the class ConvertBridge method evalFieldEntity.

private FieldEntity evalFieldEntity(FieldEntity fieldEntity, PsiType type) {
    if (type instanceof PsiPrimitiveType) {
        if (fieldEntity == null) {
            fieldEntity = new FieldEntity();
        }
        fieldEntity.setType(type.getPresentableText());
        return fieldEntity;
    } else if (type instanceof PsiArrayType) {
        if (fieldEntity == null) {
            fieldEntity = new IterableFieldEntity();
        }
        IterableFieldEntity iterableFieldEntity = (IterableFieldEntity) fieldEntity;
        iterableFieldEntity.setDeep(iterableFieldEntity.getDeep() + 1);
        return evalFieldEntity(fieldEntity, ((PsiArrayType) type).getComponentType());
    } else if (type instanceof PsiClassReferenceType) {
        PsiClass psi = ((PsiClassReferenceType) type).resolveGenerics().getElement();
        if (isCollection(psi)) {
            if (fieldEntity == null) {
                fieldEntity = new IterableFieldEntity();
            }
            IterableFieldEntity iterableFieldEntity = (IterableFieldEntity) fieldEntity;
            iterableFieldEntity.setDeep(iterableFieldEntity.getDeep() + 1);
            PsiType[] parameters = ((PsiClassReferenceType) type).getParameters();
            if (parameters.length > 0) {
                PsiType parameter = parameters[0];
                if (parameter instanceof PsiWildcardType) {
                    if (((PsiWildcardType) parameter).isExtends()) {
                        final PsiType extendsBound = ((PsiWildcardType) parameter).getExtendsBound();
                        evalFieldEntity(fieldEntity, extendsBound);
                    }
                    if (((PsiWildcardType) parameter).isSuper()) {
                        final PsiType superBound = ((PsiWildcardType) parameter).getSuperBound();
                        evalFieldEntity(fieldEntity, superBound);
                    }
                } else if (parameter instanceof PsiClassReferenceType) {
                    PsiClass element = ((PsiClassReferenceType) parameter).resolveGenerics().getElement();
                    handleClassReferenceType(fieldEntity, element);
                }
            }
            return fieldEntity;
        } else {
            if (fieldEntity == null) {
                fieldEntity = new FieldEntity();
            }
            handleClassReferenceType(fieldEntity, psi);
            return fieldEntity;
        }
    }
    if (fieldEntity == null) {
        fieldEntity = new IterableFieldEntity();
    }
    return fieldEntity;
}
Also used : IterableFieldEntity(org.gsonformat.intellij.entity.IterableFieldEntity) FieldEntity(org.gsonformat.intellij.entity.FieldEntity) IterableFieldEntity(org.gsonformat.intellij.entity.IterableFieldEntity) PsiClassReferenceType(com.intellij.psi.impl.source.PsiClassReferenceType)

Example 2 with IterableFieldEntity

use of org.gsonformat.intellij.entity.IterableFieldEntity in project GsonFormat by zzz40500.

the class ConvertBridge method handleJSONArray.

private FieldEntity handleJSONArray(ClassEntity parentClass, JSONArray jsonArray, String key, int deep) {
    FieldEntity fieldEntity;
    if (jsonArray.length() > 0) {
        Object item = jsonArray.get(0);
        if (item instanceof JSONObject) {
            item = getJsonObject(jsonArray);
        }
        fieldEntity = listTypeByValue(parentClass, key, item, deep);
    } else {
        fieldEntity = new IterableFieldEntity();
        fieldEntity.setKey(key);
        fieldEntity.setType("?");
        ((IterableFieldEntity) fieldEntity).setDeep(deep);
    }
    return fieldEntity;
}
Also used : IterableFieldEntity(org.gsonformat.intellij.entity.IterableFieldEntity) JSONObject(org.json.JSONObject) FieldEntity(org.gsonformat.intellij.entity.FieldEntity) IterableFieldEntity(org.gsonformat.intellij.entity.IterableFieldEntity) JSONObject(org.json.JSONObject)

Example 3 with IterableFieldEntity

use of org.gsonformat.intellij.entity.IterableFieldEntity in project GsonFormat by zzz40500.

the class ConvertBridge method listTypeByValue.

private FieldEntity listTypeByValue(ClassEntity parentClass, String key, Object type, int deep) {
    FieldEntity item = null;
    if (type instanceof JSONObject) {
        ClassEntity classEntity = existDeclareClass((JSONObject) type);
        if (classEntity == null) {
            IterableFieldEntity iterableFieldEntity = new IterableFieldEntity();
            ClassEntity innerClassEntity = createInnerClass(createSubClassName(key, type), (JSONObject) type, parentClass);
            iterableFieldEntity.setKey(key);
            iterableFieldEntity.setDeep(deep);
            iterableFieldEntity.setTargetClass(innerClassEntity);
            item = iterableFieldEntity;
        } else {
            IterableFieldEntity fieldEntity = new IterableFieldEntity();
            fieldEntity.setKey(key);
            fieldEntity.setTargetClass(classEntity);
            fieldEntity.setType(classEntity.getQualifiedName());
            fieldEntity.setDeep(deep);
            item = fieldEntity;
        }
    } else if (type instanceof JSONArray) {
        FieldEntity fieldEntity = handleJSONArray(parentClass, (JSONArray) type, key, ++deep);
        fieldEntity.setKey(key);
        item = fieldEntity;
    } else {
        IterableFieldEntity fieldEntity = new IterableFieldEntity();
        fieldEntity.setKey(key);
        fieldEntity.setType(type.getClass().getSimpleName());
        fieldEntity.setDeep(deep);
        item = fieldEntity;
    }
    return item;
}
Also used : ClassEntity(org.gsonformat.intellij.entity.ClassEntity) IterableFieldEntity(org.gsonformat.intellij.entity.IterableFieldEntity) JSONObject(org.json.JSONObject) FieldEntity(org.gsonformat.intellij.entity.FieldEntity) IterableFieldEntity(org.gsonformat.intellij.entity.IterableFieldEntity) JSONArray(org.json.JSONArray)

Aggregations

FieldEntity (org.gsonformat.intellij.entity.FieldEntity)3 IterableFieldEntity (org.gsonformat.intellij.entity.IterableFieldEntity)3 JSONObject (org.json.JSONObject)2 PsiClassReferenceType (com.intellij.psi.impl.source.PsiClassReferenceType)1 ClassEntity (org.gsonformat.intellij.entity.ClassEntity)1 JSONArray (org.json.JSONArray)1