Search in sources :

Example 1 with FieldEntity

use of org.gsonformat.intellij.entity.FieldEntity 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 FieldEntity

use of org.gsonformat.intellij.entity.FieldEntity 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 FieldEntity

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

the class ConvertBridge method createField.

private FieldEntity createField(ClassEntity parentClass, String key, Object type) {
    //过滤 不符合规则的key
    String fieldName = CheckUtil.getInstant().handleArg(key);
    if (Config.getInstant().isUseSerializedName()) {
        fieldName = StringUtils.captureStringLeaveUnderscore(convertSerializedName(fieldName));
    }
    fieldName = handleDeclareFieldName(fieldName, "");
    FieldEntity fieldEntity = typeByValue(parentClass, key, type);
    fieldEntity.setFieldName(fieldName);
    return fieldEntity;
}
Also used : FieldEntity(org.gsonformat.intellij.entity.FieldEntity) IterableFieldEntity(org.gsonformat.intellij.entity.IterableFieldEntity)

Example 4 with FieldEntity

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

the class ConvertBridge method run.

public void run() {
    JSONObject json = null;
    operator.cleanErrorInfo();
    try {
        json = parseJSONObject(jsonStr);
    } catch (Exception e) {
        String jsonTS = removeComment(jsonStr);
        jsonTS = jsonTS.replaceAll("^.*?\\{", "{");
        try {
            json = parseJSONObject(jsonTS);
        } catch (Exception e2) {
            handleDataError(e2);
        }
    }
    if (json != null) {
        try {
            ClassEntity classEntity = collectClassAttribute(targetClass, Config.getInstant().isReuseEntity());
            if (classEntity != null) {
                for (FieldEntity item : classEntity.getFields()) {
                    declareFields.put(item.getKey(), item);
                    CheckUtil.getInstant().addDeclareFieldName(item.getKey());
                }
            }
            if (Config.getInstant().isSplitGenerate()) {
                collectPackAllClassName();
            }
            operator.setVisible(false);
            parseJson(json);
        } catch (Exception e2) {
            handleDataError(e2);
            operator.setVisible(true);
        }
    }
    declareFields = null;
    declareClass = null;
}
Also used : ClassEntity(org.gsonformat.intellij.entity.ClassEntity) JSONObject(org.json.JSONObject) FieldEntity(org.gsonformat.intellij.entity.FieldEntity) IterableFieldEntity(org.gsonformat.intellij.entity.IterableFieldEntity)

Example 5 with FieldEntity

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

the class ConvertBridge method collectDeclareFields.

private List<FieldEntity> collectDeclareFields(PsiClass mClass) {
    ArrayList<FieldEntity> filterFieldList = new ArrayList<>();
    if (mClass != null) {
        PsiField[] psiFields = mClass.getAllFields();
        for (PsiField psiField : psiFields) {
            String fileName = null;
            String psiFieldText = removeComment(psiField.getText());
            if (filterRegex != null && psiFieldText.contains(filterRegex)) {
                boolean isSerializedName = false;
                psiFieldText = psiFieldText.trim();
                Pattern pattern = Pattern.compile(fullFilterRegex.toString());
                Matcher matcher = pattern.matcher(psiFieldText);
                if (matcher.find()) {
                    fileName = matcher.group(1);
                    isSerializedName = true;
                }
                pattern = Pattern.compile(briefFilterRegex.toString());
                matcher = pattern.matcher(psiFieldText);
                if (matcher.find()) {
                    fileName = matcher.group(1);
                    isSerializedName = true;
                }
                if (!isSerializedName) {
                    fileName = psiField.getName();
                }
            } else {
                fileName = psiField.getName();
            }
            FieldEntity fieldEntity = evalFieldEntity(null, psiField.getType());
            fieldEntity.setKey(fileName);
            fieldEntity.setFieldName(fileName);
            filterFieldList.add(fieldEntity);
        }
    }
    return filterFieldList;
}
Also used : Pattern(java.util.regex.Pattern) FieldEntity(org.gsonformat.intellij.entity.FieldEntity) IterableFieldEntity(org.gsonformat.intellij.entity.IterableFieldEntity) Matcher(java.util.regex.Matcher)

Aggregations

FieldEntity (org.gsonformat.intellij.entity.FieldEntity)14 IterableFieldEntity (org.gsonformat.intellij.entity.IterableFieldEntity)10 ClassEntity (org.gsonformat.intellij.entity.ClassEntity)9 JSONObject (org.json.JSONObject)6 JSONArray (org.json.JSONArray)3 DefaultMutableTreeTableNode (org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode)2 PsiClassReferenceType (com.intellij.psi.impl.source.PsiClassReferenceType)1 BorderLayout (java.awt.BorderLayout)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 JTree (javax.swing.JTree)1 TreePath (javax.swing.tree.TreePath)1