<commit_before>
package com.ccnode.codegenerator.pojoHelper;

import com.ccnode.codegenerator.enums.FileType;
import com.ccnode.codegenerator.pojo.*;
import com.ccnode.codegenerator.util.*;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiType;
import com.intellij.psi.impl.source.PsiClassImpl;
import com.intellij.psi.impl.source.javadoc.PsiDocCommentImpl;
import com.intellij.psi.impl.source.tree.PsiCommentImpl;
import com.intellij.psi.search.FilenameIndex;
import com.intellij.psi.search.GlobalSearchScope;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;

import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.util.List;

import static org.apache.commons.io.IOUtils.writeLines;

/**
 * What always stop you is what you always believe.
 * <p>
 * Created by zhengjun.du on 2016/05/21 21:50
 */
public class OnePojoInfoHelper {

    private final static Logger LOGGER = LoggerWrapper.getLogger(OnePojoInfoHelper.class);

    @NotNull
    public static Boolean containSplitKey(@NotNull OnePojoInfo onePojoInfo, String splitKey){
        for (PojoFieldInfo pojoFieldInfo : onePojoInfo.getPojoFieldInfos()) {
            if(pojoFieldInfo.getFieldName().equalsIgnoreCase(splitKey)){
                return true;
            }
        }
        return false;
    }

    public static void parseIdeaFieldInfo(@NotNull OnePojoInfo onePojoInfo, GenCodeResponse response){
        String pojoName = onePojoInfo.getPojoName();
        String pojoFileShortName = pojoName + ".java";
        Project project = response.getRequest().getProject();
        PsiFile[] psiFile = FilenameIndex
                .getFilesByName(project, pojoFileShortName, GlobalSearchScope.projectScope(project));
        PsiElement firstChild = psiFile[0].getFirstChild();

        for (PsiFile psiFile: psiFiles){
            VirtualFile vf = psiFile.getVirtualFile();
            if (vf.getPath().equals(onePojoInfo.getFullPojoPath())){
                child = psiFile.getFirstChild();
            }
        }

        List<PsiElement> elements = Lists.newArrayList();

        i// Find Psi of class and package
        do {
            if (child instanceof PsiClassImpl) {
                elements.add(child);
            }
            if (child instanceof PsiPackageStatementImpl){
                onePojoInfo.setPojoPackage(((PsiPackageStatementImpl) child).getPackageName());
            }
            child = child.getNextSibling();
        }
        while (child != null);

        PsiClassImpl psiClass = (PsiClassImpl) elements.get(0);
        PsiElement context = psiClass.getContext();
        if(context == null){
            throw new RuntimeException("parse class error");
        }
        String text = context.getText();
        onePojoInfo.setPojoPackage(parsePackage(text));
        PsiField[] allFields = psiClass.getAllFields();
        List<PojoFieldInfo> fieldList = Lists.newArrayList();

        for (PsiField field : allFields) {
            if(isStaticField(field)){
                continue;
            }
            if(!isSupportType(field.getType().getPresentableText())){
                continue;
            }
            parseComment(field);
            PojoFieldInfo fieldInfo = new PojoFieldInfo();
            fieldInfo.setFieldComment(parseComment(field));
            PsiType type = field.getType();
            fieldInfo.setFieldName(field.getName());
            fieldInfo.setFieldClass(type.getPresentableText());
            fieldInfo.setAnnotations(Lists.newArrayList());
            fieldList.add(fieldInfo);
        }
        onePojoInfo.setPojoFieldInfos(fieldList);
    }

    private static Boolean isSupportType(String fieldType){
        if(StringUtils.isBlank(fieldType)){
            return false;
        }
        List<String> supportList= ImmutableList.of("string","integer","int","short","date","long","bigdecimal","double","float");
        return supportList.contains(fieldType.toLowerCase());
    }

    private static Boolean isStaticField(@NotNull PsiField field){
        field.getText().contains(" static ");
    }

    private static String parsePackage(String context){
        List<String> lines = Splitter.on("\n").trimResults().omitEmptyStrings().splitToList(context);
        for (String line : lines) {
            String match = RegexUtil.getMatch("[\\s]*package[\\s]+.+[\\s]*;", line);
            if(StringUtils.isNotBlank(match)){
                String pojoPackage = Splitter.on("package").trimResults().omitEmptyStrings().splitToList(match).get(0);
                pojoPackage = pojoPackage.replace(";","");
                pojoPackage = pojoPackage.replace(" ","");
                return pojoPackage;
            }
        }
        return StringUtils.EMPTY;
    }
    private static String parseComment(PsiField field) {
        if(field == null){
            return StringUtils.EMPTY;
        }
        PsiElement[] children = field.getChildren();
        for (PsiElement child : children) {
            String text1 = child.getText();
            if(child instanceof PsiDocCommentImpl){
                String text = child.getText();
                text = text.replace("/*","");
                text = text.replace("*/","");
                text = text.replace("//","");
                text = text.replace("\n","");
                text = text.replace("*","");
                text = text.trim();
                return text;
            }
            if(child instanceof  PsiCommentImpl){
                String text = child.getText();
                text = text.replace("/*","");
                text = text.replace("*/","");
                text = text.replace("//","");
                text = text.replace("\n","");
                text = text.replace("*","");
                text = text.trim();
                return text;
            }
        }
        return StringUtils.EMPTY;
    }

    public static void parsePojoFieldInfo(@NotNull OnePojoInfo onePojoInfo){
        @NotNull Class pojoClass = onePojoInfo.getPojoClass();
        Field[] fields = pojoClass.getDeclaredFields();
        if(fields == null || fields.length == 0){
            return;
        }
        List<PojoFieldInfo> fieldInfoList = Lists.newArrayList();
        onePojoInfo.setPojoFieldInfos(fieldInfoList);
        for (Field field : fields) {
            PojoFieldInfo fieldInfo = new PojoFieldInfo();
            fieldInfo.setFieldClass(StringUtils.EMPTY);
            fieldInfo.setFieldName(field.getName());<commit_msg>optimized the function parseComment and added static function formatText for remove duplicating code<commit_after>
package com.ccnode.codegenerator.pojoHelper;

import com.ccnode.codegenerator.enums.FileType;
import com.ccnode.codegenerator.pojo.*;
import com.ccnode.codegenerator.util.*;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiType;
import com.intellij.psi.impl.source.PsiClassImpl;
import com.intellij.psi.impl.source.javadoc.PsiDocCommentImpl;
import com.intellij.psi.impl.source.tree.PsiCommentImpl;
import com.intellij.psi.search.FilenameIndex;
import com.intellij.psi.search.GlobalSearchScope;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;

import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.util.List;

import static org.apache.commons.io.IOUtils.writeLines;

/**
 * What always stop you is what you always believe.
 * <p>
 * Created by zhengjun.du on 2016/05/21 21:50
 */
public class OnePojoInfoHelper {

    private final static Logger LOGGER = LoggerWrapper.getLogger(OnePojoInfoHelper.class);

    @NotNull
    public static Boolean containSplitKey(@NotNull OnePojoInfo onePojoInfo, String splitKey){
        for (PojoFieldInfo pojoFieldInfo : onePojoInfo.getPojoFieldInfos()) {
            if(pojoFieldInfo.getFieldName().equalsIgnoreCase(splitKey)){
                return true;
            }
        }
        return false;
    }

    public static void parseIdeaFieldInfo(@NotNull OnePojoInfo onePojoInfo, GenCodeResponse response){
        String pojoName = onePojoInfo.getPojoName();
        String pojoFileShortName = pojoName + ".java";
        Project project = response.getRequest().getProject();
        PsiFile[] psiFile = FilenameIndex
                .getFilesByName(project, pojoFileShortName, GlobalSearchScope.projectScope(project));
        PsiElement firstChild = psiFile[0].getFirstChild();

        for (PsiFile psiFile: psiFiles){
            VirtualFile vf = psiFile.getVirtualFile();
            if (vf.getPath().equals(onePojoInfo.getFullPojoPath())){
                child = psiFile.getFirstChild();
            }
        }

        List<PsiElement> elements = Lists.newArrayList();

        i// Find Psi of class and package
        do {
            if (child instanceof PsiClassImpl) {
                elements.add(child);
            }
            if (child instanceof PsiPackageStatementImpl){
                onePojoInfo.setPojoPackage(((PsiPackageStatementImpl) child).getPackageName());
            }
            child = child.getNextSibling();
        }
        while (child != null);

        PsiClassImpl psiClass = (PsiClassImpl) elements.get(0);
        PsiElement context = psiClass.getContext();
        if(context == null){
            throw new RuntimeException("parse class error");
        }
        String text = context.getText();
        onePojoInfo.setPojoPackage(parsePackage(text));
        PsiField[] allFields = psiClass.getAllFields();
        List<PojoFieldInfo> fieldList = Lists.newArrayList();

        for (PsiField field : allFields) {
            if(isStaticField(field)){
                continue;
            }
            if(!isSupportType(field.getType().getPresentableText())){
                continue;
            }
            parseComment(field);
            PojoFieldInfo fieldInfo = new PojoFieldInfo();
            fieldInfo.setFieldComment(parseComment(field));
            PsiType type = field.getType();
            fieldInfo.setFieldName(field.getName());
            fieldInfo.setFieldClass(type.getPresentableText());
            fieldInfo.setAnnotations(Lists.newArrayList());
            fieldList.add(fieldInfo);
        }
        onePojoInfo.setPojoFieldInfos(fieldList);
    }

    private static Boolean isSupportType(String fieldType){
        if(StringUtils.isBlank(fieldType)){
            return false;
        }
        List<String> supportList= ImmutableList.of("string","integer","int","short","date","long","bigdecimal","double","float");
        return supportList.contains(fieldType.toLowerCase());
    }

    private static Boolean isStaticField(@NotNull PsiField field){
        field.getText().contains(" static ");
    }

    private static String parsePackage(String context){
        List<String> lines = Splitter.on("\n").trimResults().omitEmptyStrings().splitToList(context);
        for (String line : lines) {
            String match = RegexUtil.getMatch("[\\s]*package[\\s]+.+[\\s]*;", line);
            if(StringUtils.isNotBlank(match)){
                String pojoPackage = Splitter.on("package").trimResults().omitEmptyStrings().splitToList(match).get(0);
                pojoPackage = pojoPackage.replace(";","");
                pojoPackage = pojoPackage.replace(" ","");
                return pojoPackage;
            }
        }
        return StringUtils.EMPTY;
    }
    private static String parseComment(PsiField field) {
        if(field == null){
            return StringUtils.EMPTY;
        }
        PsiElement[] children = field.getChildren();
        for (PsiElement child : children) {
            String text = child.getText();
            if(child instanceof PsiDocCommentImpl || child instanceof PsiCommentImpl){
                return formatText(text);
            }
        }
        return StringUtils.EMPTY;
    }

    private static String formatText(String text){
        text = text.replace("/*","");
        text = text.replace("*/","");
        text = text.replace("//","");
        text = text.replace("\n","");
        text = text.replace("*","");
        text = text.trim();
        return text;
    }

    public static void parsePojoFieldInfo(@NotNull OnePojoInfo onePojoInfo){
        @NotNull Class pojoClass = onePojoInfo.getPojoClass();
        Field[] fields = pojoClass.getDeclaredFields();
        if(fields == null || fields.length == 0){
            return;
        }
        List<PojoFieldInfo> fieldInfoList = Lists.newArrayList();
        onePojoInfo.setPojoFieldInfos(fieldInfoList);
        for (Field field : fields) {
            PojoFieldInfo fieldInfo = new PojoFieldInfo();
            fieldInfo.setFieldClass(StringUtils.EMPTY);
            fieldInfo.setFieldName(field.getName());
{'fraction_non_alphanumeric': 0.11676954732510288, 'fraction_numerical': 0.0028659611992945325, 'mean_word_length': 2.590765171503958, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2406383', 'n_tokens_mistral': 3805, 'n_tokens_neox': 3708, 'n_words': 706}