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;
}
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;
}
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;
}
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);
}
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);
}
}
Aggregations