Search in sources :

Example 1 with ClassEntity

use of org.gsonformat.intellij.entity.ClassEntity 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 2 with ClassEntity

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

the class ConvertBridge method createInnerClass.

/**
     * @param className
     * @param json
     * @param parentClass
     * @return
     */
private ClassEntity createInnerClass(String className, JSONObject json, ClassEntity parentClass) {
    if (Config.getInstant().isSplitGenerate()) {
        String qualifiedName = packageName == null ? className : packageName + "." + className;
        if (CheckUtil.getInstant().containsDeclareClassName(qualifiedName)) {
            //存在同名。
            PsiClass psiClass = PsiClassUtil.exist(file, qualifiedName);
            if (psiClass != null) {
                ClassEntity classEntity = collectClassAttribute(psiClass, false);
                classEntity.setLock(true);
                if (classEntity.isSame(json)) {
                    //                        if (Config.getInstant().isReuseEntity()) {
                    declareClass.put(classEntity.getQualifiedName(), classEntity);
                    //                        }
                    return classEntity;
                }
            }
        }
    }
    ClassEntity subClassEntity = new ClassEntity();
    Set<String> set = json.keySet();
    List<String> list = new ArrayList<String>(set);
    List<FieldEntity> fields = createFields(json, list, subClassEntity);
    subClassEntity.addAllFields(fields);
    if (Config.getInstant().isSplitGenerate()) {
        subClassEntity.setPackName(packageName);
    } else {
        subClassEntity.setPackName(parentClass.getQualifiedName());
    }
    subClassEntity.setClassName(className);
    if (handleDeclareClassName(subClassEntity, "")) {
        CheckUtil.getInstant().addDeclareClassName(subClassEntity.getQualifiedName());
    }
    if (Config.getInstant().isReuseEntity()) {
        declareClass.put(subClassEntity.getQualifiedName(), subClassEntity);
    }
    parentClass.addInnerClass(subClassEntity);
    return subClassEntity;
}
Also used : ClassEntity(org.gsonformat.intellij.entity.ClassEntity) FieldEntity(org.gsonformat.intellij.entity.FieldEntity) IterableFieldEntity(org.gsonformat.intellij.entity.IterableFieldEntity)

Example 3 with ClassEntity

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

the class ConvertBridge method collectClassAttribute.

private ClassEntity collectClassAttribute(PsiClass psiClass, boolean collectInnerClass) {
    if (psiClass == null) {
        return null;
    }
    ClassEntity innerClass = new ClassEntity();
    innerClass.setLock(true);
    declareClass.put(psiClass.getQualifiedName(), innerClass);
    CheckUtil.getInstant().addDeclareClassName(psiClass.getQualifiedName());
    innerClass.setClassName(psiClass.getName());
    innerClass.addAllFields(collectDeclareFields(psiClass));
    innerClass.setPsiClass(psiClass);
    innerClass.setPackName(getPackName(psiClass));
    if (collectInnerClass) {
        recursionInnerClass(innerClass);
    }
    return innerClass;
}
Also used : ClassEntity(org.gsonformat.intellij.entity.ClassEntity)

Example 4 with ClassEntity

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

the class ConvertBridge method existDeclareClass.

private ClassEntity existDeclareClass(JSONObject jsonObject) {
    for (ClassEntity classEntity : declareClass.values()) {
        Iterator<String> keys = jsonObject.keys();
        boolean had = false;
        while (keys.hasNext()) {
            String key = keys.next();
            Object value = jsonObject.get(key);
            had = false;
            for (FieldEntity fieldEntity : classEntity.getFields()) {
                if (fieldEntity.getKey().equals(key) && DataType.isSameDataType(DataType.typeOfString(fieldEntity.getType()), DataType.typeOfObject(value))) {
                    had = true;
                    break;
                }
            }
            if (!had) {
                break;
            }
        }
        if (had) {
            return classEntity;
        }
    }
    return null;
}
Also used : ClassEntity(org.gsonformat.intellij.entity.ClassEntity) FieldEntity(org.gsonformat.intellij.entity.FieldEntity) IterableFieldEntity(org.gsonformat.intellij.entity.IterableFieldEntity) JSONObject(org.json.JSONObject)

Example 5 with ClassEntity

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

the class DataWriter method run.

@Override
protected void run() {
    if (targetClass == null) {
        return;
    }
    generateClassList.clear();
    new ClassProcessor(factory, cls).generate(targetClass, new IProcessor() {

        @Override
        public void onStarProcess(ClassEntity classEntity, PsiElementFactory factory, PsiClass cls) {
            generateClassList.add(cls.getQualifiedName());
        }

        @Override
        public void onEndProcess(ClassEntity classEntity, PsiElementFactory factory, PsiClass cls) {
        }

        @Override
        public void onStartGenerateClass(PsiElementFactory factory, ClassEntity classEntity, PsiClass parentClass) {
        }

        @Override
        public void onEndGenerateClass(PsiElementFactory factory, ClassEntity classEntity, PsiClass parentClass, PsiClass generateClass) {
            generateClassList.add(generateClass.getQualifiedName());
        }
    });
}
Also used : ClassEntity(org.gsonformat.intellij.entity.ClassEntity) ClassProcessor(org.gsonformat.intellij.process.ClassProcessor) PsiElementFactory(com.intellij.psi.PsiElementFactory) PsiClass(com.intellij.psi.PsiClass) IProcessor(org.gsonformat.intellij.process.IProcessor)

Aggregations

ClassEntity (org.gsonformat.intellij.entity.ClassEntity)13 FieldEntity (org.gsonformat.intellij.entity.FieldEntity)9 IterableFieldEntity (org.gsonformat.intellij.entity.IterableFieldEntity)5 JSONObject (org.json.JSONObject)4 DefaultMutableTreeTableNode (org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode)2 JSONArray (org.json.JSONArray)2 PsiClass (com.intellij.psi.PsiClass)1 PsiElementFactory (com.intellij.psi.PsiElementFactory)1 BorderLayout (java.awt.BorderLayout)1 JTree (javax.swing.JTree)1 TreePath (javax.swing.tree.TreePath)1 ClassProcessor (org.gsonformat.intellij.process.ClassProcessor)1 IProcessor (org.gsonformat.intellij.process.IProcessor)1