Search in sources :

Example 6 with FieldEntity

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

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

the class ConvertBridge method createFields.

private List<FieldEntity> createFields(JSONObject json, List<String> fieldList, ClassEntity parentClass) {
    List<FieldEntity> fieldEntityList = new ArrayList<FieldEntity>();
    List<String> listEntityList = new ArrayList<String>();
    boolean writeExtra = Config.getInstant().isGenerateComments();
    for (int i = 0; i < fieldList.size(); i++) {
        String key = fieldList.get(i);
        Object value = json.get(key);
        if (value instanceof JSONArray) {
            listEntityList.add(key);
            continue;
        }
        FieldEntity fieldEntity = createField(parentClass, key, value);
        fieldEntityList.add(fieldEntity);
        if (writeExtra) {
            writeExtra = false;
            parentClass.setExtra(Utils.createCommentString(json, fieldList));
        }
    }
    for (int i = 0; i < listEntityList.size(); i++) {
        String key = listEntityList.get(i);
        Object type = json.get(key);
        FieldEntity fieldEntity = createField(parentClass, key, type);
        fieldEntityList.add(fieldEntity);
    }
    return fieldEntityList;
}
Also used : FieldEntity(org.gsonformat.intellij.entity.FieldEntity) IterableFieldEntity(org.gsonformat.intellij.entity.IterableFieldEntity) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject)

Example 8 with FieldEntity

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

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

the class Processor method process.

public void process(ClassEntity classEntity, PsiElementFactory factory, PsiClass cls, IProcessor visitor) {
    mainPackage = PsiClassUtil.getPackage(cls);
    onStarProcess(classEntity, factory, cls, visitor);
    for (FieldEntity fieldEntity : classEntity.getFields()) {
        generateField(factory, fieldEntity, cls, classEntity);
    }
    for (ClassEntity innerClass : classEntity.getInnerClasss()) {
        generateClass(factory, innerClass, cls, visitor);
    }
    generateGetterAndSetter(factory, cls, classEntity);
    generateConvertMethod(factory, cls, classEntity);
    onEndProcess(classEntity, factory, cls, visitor);
}
Also used : ClassEntity(org.gsonformat.intellij.entity.ClassEntity) FieldEntity(org.gsonformat.intellij.entity.FieldEntity)

Example 10 with FieldEntity

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

the class Processor method generateClass.

protected void generateClass(PsiElementFactory factory, ClassEntity classEntity, PsiClass parentClass, IProcessor visitor) {
    onStartGenerateClass(factory, classEntity, parentClass, visitor);
    PsiClass generateClass = null;
    if (classEntity.isGenerate()) {
        //// TODO: 16/11/9  待重构 
        if (Config.getInstant().isSplitGenerate()) {
            try {
                generateClass = PsiClassUtil.getPsiClass(parentClass.getContainingFile(), parentClass.getProject(), classEntity.getQualifiedName());
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        } else {
            String classContent = "public static class " + classEntity.getClassName() + "{}";
            generateClass = factory.createClassFromText(classContent, null).getInnerClasses()[0];
        }
        if (generateClass != null) {
            for (ClassEntity innerClass : classEntity.getInnerClasss()) {
                generateClass(factory, innerClass, generateClass, visitor);
            }
            if (!Config.getInstant().isSplitGenerate()) {
                generateClass = (PsiClass) parentClass.add(generateClass);
            }
            for (FieldEntity fieldEntity : classEntity.getFields()) {
                generateField(factory, fieldEntity, generateClass, classEntity);
            }
            generateGetterAndSetter(factory, generateClass, classEntity);
            generateConvertMethod(factory, generateClass, classEntity);
        }
    }
    onEndGenerateClass(factory, classEntity, parentClass, generateClass, visitor);
    if (Config.getInstant().isSplitGenerate()) {
        formatJavCode(generateClass);
    }
}
Also used : ClassEntity(org.gsonformat.intellij.entity.ClassEntity) FieldEntity(org.gsonformat.intellij.entity.FieldEntity)

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