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