打造升级GsonFormat驼峰命名规则

前景

        今天高高高兴兴的敲代码,龙飞凤舞,张牙舞爪,帅的一B,创建presenter,modle,view一起合成,复制返回数据,解析json数据,打开GsonFomat->粘贴->OK,帅的一B, 一气合成我都佩服自己,来看看结果

public class ContrastListModel2 {

    /**
     * car_id : 12345
     * rrc_id : bj-53215
     * first_img : http://static.renrenche.com/o_1cnldi8vb597881539632895472032546.jpg
     * is_promise_sell : 保卖
     * status : 已检测
     * car_model : 大众-高尔夫 2015款 1.6L自动时尚型
     * receive_time : 2018-08-15 16:20
     * review_time : 2018-08-28 10:20
     * redline_diff_count : 2
     * other_diff_count : 5
     */

    private int car_id;
    private String rrc_id;
    private String first_img;
    private String is_promise_sell;
    private String status;
    private String car_model;
    private String receive_time;
    private String review_time;
    private int redline_diff_count;
    private int other_diff_count;

    public static ContrastListModel2 objectFromData(String str) {

        return new Gson().fromJson(str, ContrastListModel2.class);
    }

    public int getCar_id() {
        return car_id;
    }

    public void setCar_id(int car_id) {
        this.car_id = car_id;
    }

    public String getRrc_id() {
        return rrc_id;
    }

    public void setRrc_id(String rrc_id) {
        this.rrc_id = rrc_id;
    }

    public String getFirst_img() {
        return first_img;
    }

    public void setFirst_img(String first_img) {
        this.first_img = first_img;
    }

    public String getIs_promise_sell() {
        return is_promise_sell;
    }

    public void setIs_promise_sell(String is_promise_sell) {
        this.is_promise_sell = is_promise_sell;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getCar_model() {
        return car_model;
    }

    public void setCar_model(String car_model) {
        this.car_model = car_model;
    }

    public String getReceive_time() {
        return receive_time;
    }

    public void setReceive_time(String receive_time) {
        this.receive_time = receive_time;
    }

    public String getReview_time() {
        return review_time;
    }

    public void setReview_time(String review_time) {
        this.review_time = review_time;
    }

    public int getRedline_diff_count() {
        return redline_diff_count;
    }

    public void setRedline_diff_count(int redline_diff_count) {
        this.redline_diff_count = redline_diff_count;
    }

    public int getOther_diff_count() {
        return other_diff_count;
    }

    public void setOther_diff_count(int other_diff_count) {
        this.other_diff_count = other_diff_count;
    }
}

这是个啥玩意,怎么命名的这么操蛋,驼峰呢??一个一个加@SerializedName改名字吗?我去看了下GsonFormat,好像没有什么能对驼峰命名的定义,好郁闷啊!搜索了下GsonFormat还好是开源的,想了下要不要自己尝试改一改,我感觉还是有这个必要的因为后端哪里命名规则是下划线为标准的,以后改这个地方挺多的,还是改下吧!

1:开始动工-学习下简单的插件开发

资料1    资料2

2:下载GsonFormat源码

https://github.com/DimRed/GsonFormat

3:导入项目

花了好几个失效,下载该下载的东西,又是又是等待,我的亲娘啊折腾死了,当我准备调试运行的时候,我郁闷了,怎么开源的是导入的结构和上门的资料上不一样啊,开源的源码怎么好像是用Gradle构建的呀,脑壳疼!折腾了半天,由于我是小白,折腾了半天,也没啥资料,最后没办法,我想到了自己创建一个项目

1:按照资料1提供的教程,创建一个plugin项目

2:复制GsonFormat源码,粘贴到src目录

打造升级GsonFormat驼峰命名规则_第1张图片

3:复制GsonFormat的resources目录,替换自己创建的项目

OK运行一下

打造升级GsonFormat驼峰命名规则_第2张图片

发现打开了另外一个Intellij创建,我想了下这个窗口就应该能用GsonFormat了,测试发现也可以

2:代码好了,接下来修改代码

通过上面的学习人,我们知道插件肯定有Action的一个入口,都知道GsonFormat都是在Generate中所以找了下这个Action

打造升级GsonFormat驼峰命名规则_第3张图片

可以看到当点击的时候会,打开一个JsonDialog的diaog,这也就是第一个ui,定位了下位置

打造升级GsonFormat驼峰命名规则_第4张图片

想了下我是不是应该,在设置界面加入一个,智能驼峰命名的选项呀,SettingDialog.form,添加了个智能驼峰命名

打造升级GsonFormat驼峰命名规则_第5张图片

修改SettingDialog代码,注释上门都有

package org.gsonformat.intellij.ui;

import com.intellij.openapi.project.Project;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.codeStyle.VariableKind;
import org.apache.http.util.TextUtils;
import org.gsonformat.intellij.config.Config;
import org.gsonformat.intellij.config.Constant;

import javax.swing.*;
import java.awt.event.*;

public class SettingDialog extends JFrame {

    private JPanel contentPane;
    private JRadioButton fieldPublicRadioButton;
    private JRadioButton fieldPrivateRadioButton;
    private JCheckBox useSerializedNameCheckBox;
    private JButton objectButton;
    private JButton object1Button;
    private JButton arrayButton;
    private JButton array1Button;
    private JTextField suffixEdit;
    private JCheckBox objectFromDataCB;
    private JCheckBox objectFromData1CB;
    private JCheckBox arrayFromDataCB;
    private JCheckBox arrayFromData1CB;
    private JCheckBox reuseEntityCB;
    /**
     * 是否驼峰命名的checkBox控件
     */
    private JCheckBox isAutoName;
    private JButton cancelButton;
    private JButton okButton;
    private JTextField filedPrefixTF;
    private JCheckBox filedPrefixCB;
    private JRadioButton gsonJRB;
    private JRadioButton jackRB;
    private JRadioButton fastJsonRB;
    private JRadioButton otherRB;
    private JTextField annotationFT;
    private JCheckBox virgoModelCB;
    private JCheckBox generateCommentsCT;
    private JRadioButton loganSquareCB;
    private JRadioButton autoValueRadioButton;
    private JCheckBox splitGenerateMode;
    private JRadioButton lombokRB;
    private String annotaionStr;
    private JCheckBox useWrapperClassCB;

    public SettingDialog(Project project) {
        setContentPane(contentPane);
//        setModal(true);
        getRootPane().setDefaultButton(okButton);
        this.setAlwaysOnTop(true);
        setTitle("Setting");
        okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        if (Config.getInstant().isFieldPrivateMode()) {
            fieldPrivateRadioButton.setSelected(true);
        } else {
            fieldPublicRadioButton.setSelected(true);
        }

        virgoModelCB.setSelected(Config.getInstant().isVirgoMode());
        generateCommentsCT.setSelected(Config.getInstant().isGenerateComments());
        filedPrefixCB.setSelected(Config.getInstant().isUseFieldNamePrefix());
        filedPrefixTF.setEnabled(Config.getInstant().isUseFieldNamePrefix());
        useSerializedNameCheckBox.setSelected(Config.getInstant().isUseSerializedName());
        objectFromDataCB.setSelected(Config.getInstant().isObjectFromData());
        objectFromData1CB.setSelected(Config.getInstant().isObjectFromData1());
        arrayFromDataCB.setSelected(Config.getInstant().isArrayFromData());
        arrayFromData1CB.setSelected(Config.getInstant().isArrayFromData1());
        reuseEntityCB.setSelected(Config.getInstant().isReuseEntity());
        /**
         * 初始化界面的值
         */
        isAutoName.setSelected(Config.getInstant().isAutoName());
        objectButton.setEnabled(objectFromDataCB.isSelected());
        object1Button.setEnabled(objectFromData1CB.isSelected());
        arrayButton.setEnabled(arrayFromDataCB.isSelected());
        array1Button.setEnabled(arrayFromData1CB.isSelected());
        suffixEdit.setText(Config.getInstant().getSuffixStr());
        splitGenerateMode.setSelected(Config.getInstant().isSplitGenerate());
        useWrapperClassCB.setSelected(Config.getInstant().isUseWrapperClass());
        objectFromDataCB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                objectButton.setEnabled(objectFromDataCB.isSelected());
            }
        });
        objectFromData1CB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                object1Button.setEnabled(objectFromData1CB.isSelected());
            }
        });
        arrayFromDataCB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                arrayButton.setEnabled(arrayFromDataCB.isSelected());
            }
        });
        arrayFromData1CB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                array1Button.setEnabled(arrayFromData1CB.isSelected());
            }
        });
        filedPrefixCB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                filedPrefixTF.setEnabled(filedPrefixCB.isSelected());
            }
        });
        otherRB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                annotationFT.setText("@{filed}");
                annotationFT.setEnabled(otherRB.isSelected());
                objectFromDataCB.setEnabled(false);
                objectFromData1CB.setEnabled(false);
                arrayFromDataCB.setEnabled(false);
                arrayFromData1CB.setEnabled(false);
                objectFromDataCB.setSelected(false);
                objectFromData1CB.setSelected(false);
                arrayFromDataCB.setSelected(false);
                arrayFromData1CB.setSelected(false);
                objectButton.setEnabled(false);
                object1Button.setEnabled(false);
                arrayButton.setEnabled(false);
                array1Button.setEnabled(false);

            }
        });
        loganSquareCB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                if (loganSquareCB.isSelected()) {
                    annotationFT.setText(Constant.loganSquareAnnotation);
                }
                annotationFT.setEnabled(otherRB.isSelected());
                objectFromDataCB.setEnabled(false);
                objectFromData1CB.setEnabled(false);
                arrayFromDataCB.setEnabled(false);
                arrayFromData1CB.setEnabled(false);
                objectFromDataCB.setSelected(false);
                objectFromData1CB.setSelected(false);
                arrayFromDataCB.setSelected(false);
                arrayFromData1CB.setSelected(false);
                objectButton.setEnabled(false);
                object1Button.setEnabled(false);
                arrayButton.setEnabled(false);
                array1Button.setEnabled(false);
            }
        });
        lombokRB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                if (lombokRB.isSelected()) {
                    annotationFT.setText(Constant.lombokAnnotation);
                }
                annotationFT.setEnabled(otherRB.isSelected());
                objectFromDataCB.setEnabled(false);
                objectFromData1CB.setEnabled(false);
                arrayFromDataCB.setEnabled(false);
                arrayFromData1CB.setEnabled(false);
                objectFromDataCB.setSelected(false);
                objectFromData1CB.setSelected(false);
                arrayFromDataCB.setSelected(false);
                arrayFromData1CB.setSelected(false);
                objectButton.setEnabled(false);
                object1Button.setEnabled(false);
                arrayButton.setEnabled(false);
                array1Button.setEnabled(false);
            }
        });
        String filedPrefix = null;
        filedPrefix = Config.getInstant().getFiledNamePreFixStr();
        if (TextUtils.isEmpty(filedPrefix)) {
            JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(project);
            filedPrefix = styleManager.getPrefixByVariableKind(VariableKind.FIELD
            );
        }
        filedPrefixTF.setText(filedPrefix);
        gsonJRB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                if (gsonJRB.isSelected()) {
                    annotationFT.setText(Constant.gsonAnnotation);
                }
                objectFromDataCB.setEnabled(true);
                objectFromData1CB.setEnabled(true);
                arrayFromDataCB.setEnabled(true);
                arrayFromData1CB.setEnabled(true);
                annotationFT.setEnabled(false);
            }
        });
        fastJsonRB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {

                if (fastJsonRB.isSelected()) {
                    annotationFT.setText(Constant.fastAnnotation);
                }
                objectFromDataCB.setEnabled(false);
                objectFromData1CB.setEnabled(false);
                arrayFromDataCB.setEnabled(false);
                arrayFromData1CB.setEnabled(false);
                annotationFT.setEnabled(false);
                objectFromDataCB.setSelected(false);
                objectFromData1CB.setSelected(false);
                arrayFromDataCB.setSelected(false);
                arrayFromData1CB.setSelected(false);
                objectButton.setEnabled(false);
                object1Button.setEnabled(false);
                arrayButton.setEnabled(false);
                array1Button.setEnabled(false);
            }
        });
        jackRB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                if (jackRB.isSelected()) {
                    annotationFT.setText(Constant.jackAnnotation);
                }
                annotationFT.setEnabled(false);
                objectFromDataCB.setEnabled(false);
                objectFromData1CB.setEnabled(false);
                arrayFromDataCB.setEnabled(false);
                arrayFromData1CB.setEnabled(false);
                annotationFT.setEnabled(false);
                objectFromDataCB.setSelected(false);
                objectFromData1CB.setSelected(false);
                arrayFromDataCB.setSelected(false);
                arrayFromData1CB.setSelected(false);
                objectButton.setEnabled(false);
                object1Button.setEnabled(false);
                arrayButton.setEnabled(false);
                array1Button.setEnabled(false);
            }
        });
        autoValueRadioButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (autoValueRadioButton.isSelected()) {
                    annotationFT.setText(Constant.autoValueAnnotation);
                }
            }
        });


        annotaionStr = Config.getInstant().getAnnotationStr();
        if (annotaionStr.equals(Constant.gsonAnnotation)) {
            gsonJRB.setSelected(true);
            annotationFT.setEnabled(false);
        } else if (annotaionStr.equals(Constant.fastAnnotation)) {
            fastJsonRB.setSelected(true);
            annotationFT.setEnabled(false);
        } else if (annotaionStr.equals(Constant.jackAnnotation)) {
            jackRB.setSelected(true);
            annotationFT.setEnabled(false);
        } else if (annotaionStr.equals(Constant.loganSquareAnnotation)) {
            loganSquareCB.setSelected(true);
            annotationFT.setEnabled(false);
        } else if (annotaionStr.equals(Constant.autoValueAnnotation)) {
            autoValueRadioButton.setSelected(true);
            annotationFT.setEnabled(false);
        } else {
            otherRB.setSelected(true);
            annotationFT.setEnabled(true);
        }
        annotationFT.setText(annotaionStr);
        objectButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                EditDialog editDialog = new EditDialog(EditDialog.Type.OBJECT_FROM_DATA);
                editDialog.setSize(600, 360);
                editDialog.setLocationRelativeTo(null);
                editDialog.setResizable(false);
                editDialog.setVisible(true);
            }
        });
        object1Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                EditDialog editDialog = new EditDialog(EditDialog.Type.OBJECT_FROM_DATA1);
                editDialog.setSize(600, 360);
                editDialog.setLocationRelativeTo(null);
                editDialog.setResizable(false);
                editDialog.setVisible(true);
            }
        });
        arrayButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                EditDialog editDialog = new EditDialog(EditDialog.Type.ARRAY_FROM_DATA);
                editDialog.setSize(600, 600);
                editDialog.setLocationRelativeTo(null);
                editDialog.setResizable(false);
                editDialog.setVisible(true);
            }
        });
        array1Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                EditDialog editDialog = new EditDialog(EditDialog.Type.ARRAY_FROM_DATA1);
                editDialog.setSize(600, 600);
                editDialog.setLocationRelativeTo(null);
                editDialog.setResizable(false);
                editDialog.setVisible(true);
            }
        });
    }

    /**
     * 点击OK保存参数
     */
    private void onOK() {

        Config.getInstant().setFieldPrivateMode(fieldPrivateRadioButton.isSelected());
        Config.getInstant().setUseSerializedName(useSerializedNameCheckBox.isSelected());
        Config.getInstant().setArrayFromData(arrayFromDataCB.isSelected());
        Config.getInstant().setArrayFromData1(arrayFromData1CB.isSelected());
        Config.getInstant().setObjectFromData(objectFromDataCB.isSelected());
        Config.getInstant().setObjectFromData1(objectFromData1CB.isSelected());
        Config.getInstant().setReuseEntity(reuseEntityCB.isSelected());
        /**
         * 驼峰命名参数配置保存
         */
        Config.getInstant().setIsAutoName(isAutoName.isSelected());
        Config.getInstant().setSuffixStr(suffixEdit.getText());
        Config.getInstant().setVirgoMode(virgoModelCB.isSelected());
        Config.getInstant().setGenerateComments(generateCommentsCT.isSelected());
        Config.getInstant().setFiledNamePreFixStr(filedPrefixTF.getText());
        Config.getInstant().setAnnotationStr(annotationFT.getText());
        Config.getInstant().setUseFieldNamePrefix(filedPrefixCB.isSelected());
        Config.getInstant().setSplitGenerate(splitGenerateMode.isSelected());
        Config.getInstant().setUseWrapperClass(useWrapperClassCB.isSelected());
        Config.getInstant().save();

        dispose();

    }

    private void createUIComponents() {
    }


    private void onCancel() {
        dispose();
    }


}

修改配置org.gsonformat.intellij.config.Config对配置参数进行保存

public class Config {

    private static Config config;

    private boolean fieldPrivateMode = true;
    private boolean generateComments = true;
    private boolean useSerializedName = false;
    private boolean objectFromData = false;
    private boolean objectFromData1 = false;
    private boolean arrayFromData = false;
    private boolean arrayFromData1 = false;
    private boolean reuseEntity = false;
    // 是否自动命名
    private boolean isAutoName;
    private boolean virgoMode = true; //处女座模式
    private boolean useFieldNamePrefix = false;
    private boolean splitGenerate = false;


    private String objectFromDataStr;
    private String objectFromDataStr1;
    private String arrayFromDataStr;
    private String arrayFromData1Str;
    private String annotationStr; //注解语句
    private String filedNamePreFixStr; //字段前缀
    private String entityPackName;//创建实体类的包名.
    private String suffixStr;

    /**
     * 错误次数,前两次提醒哪里查看错误日志.
     */
    private int errorCount;

    /**
     * 是否使用包装类来替代基本类型
     */
    private boolean useWrapperClass;



    private Config() {

    }

    public void save() {

        PropertiesComponent.getInstance().setValue("fieldPrivateMode", "" + isFieldPrivateMode());
        PropertiesComponent.getInstance().setValue("useSerializedName", isUseSerializedName() + "");
        PropertiesComponent.getInstance().setValue("objectFromData", objectFromData + "");
        PropertiesComponent.getInstance().setValue("objectFromData1", objectFromData1 + "");
        PropertiesComponent.getInstance().setValue("arrayFromData", arrayFromData + "");
        PropertiesComponent.getInstance().setValue("arrayFromData1", arrayFromData1 + "");
        PropertiesComponent.getInstance().setValue("objectFromDataStr", objectFromDataStr + "");
        PropertiesComponent.getInstance().setValue("objectFromDataStr1", objectFromDataStr1 + "");
        PropertiesComponent.getInstance().setValue("arrayFromData1Str", arrayFromData1Str + "");
        PropertiesComponent.getInstance().setValue("suffixStr", suffixStr + "");
        PropertiesComponent.getInstance().setValue("reuseEntity", reuseEntity + "");
        PropertiesComponent.getInstance().setValue("IsAutoName", isAutoName + "");
        PropertiesComponent.getInstance().setValue("virgoMode", virgoMode + "");
        PropertiesComponent.getInstance().setValue("filedNamePreFixStr", filedNamePreFixStr + "");
        PropertiesComponent.getInstance().setValue("annotationStr", annotationStr + "");
        PropertiesComponent.getInstance().setValue("errorCount", errorCount + "");
        PropertiesComponent.getInstance().setValue("entityPackName", entityPackName + "");
        PropertiesComponent.getInstance().setValue("useFieldNamePrefix", useFieldNamePrefix + "");
        PropertiesComponent.getInstance().setValue("generateComments", generateComments + "");
        PropertiesComponent.getInstance().setValue("splitGenerate", splitGenerate + "");
        PropertiesComponent.getInstance().setValue("useWrapperClass", useWrapperClass + "");

    }

    public static Config getInstant() {

        if (config == null) {
            config = new Config();
            config.setFieldPrivateMode(PropertiesComponent.getInstance().getBoolean("fieldPrivateMode", true));
            config.setUseSerializedName(PropertiesComponent.getInstance().getBoolean("useSerializedName", false));
            config.setObjectFromData(PropertiesComponent.getInstance().getBoolean("objectFromData", false));
            config.setObjectFromData1(PropertiesComponent.getInstance().getBoolean("objectFromData1", false));
            config.setArrayFromData(PropertiesComponent.getInstance().getBoolean("arrayFromData", false));
            config.setArrayFromData1(PropertiesComponent.getInstance().getBoolean("arrayFromData1", false));
            config.setSuffixStr(PropertiesComponent.getInstance().getValue("suffixStr", "Bean"));
            config.setReuseEntity(PropertiesComponent.getInstance().getBoolean("reuseEntity", false));
            // 读取是否自动命名
            config.setIsAutoName(PropertiesComponent.getInstance().getBoolean("IsAutoName", false));
            config.setObjectFromDataStr(PropertiesComponent.getInstance().getValue("objectFromDataStr", Constant.objectFromObject));
            config.setObjectFromDataStr1(PropertiesComponent.getInstance().getValue("objectFromDataStr1", Constant.objectFromObject1));
            config.setArrayFromDataStr(PropertiesComponent.getInstance().getValue("arrayFromDataStr", Constant.arrayFromData));
            config.setArrayFromData1Str(PropertiesComponent.getInstance().getValue("arrayFromData1Str", Constant.arrayFromData1));
            config.setAnnotationStr(PropertiesComponent.getInstance().getValue("annotationStr", Constant.gsonAnnotation));
            config.setEntityPackName(PropertiesComponent.getInstance().getValue("entityPackName"));
            config.setFiledNamePreFixStr(PropertiesComponent.getInstance().getValue("filedNamePreFixStr"));
            config.setErrorCount(PropertiesComponent.getInstance().getOrInitInt("errorCount", 0));
            config.setVirgoMode(PropertiesComponent.getInstance().getBoolean("virgoMode", true));
            config.setUseFieldNamePrefix(PropertiesComponent.getInstance().getBoolean("useFieldNamePrefix", false));
            config.setGenerateComments(PropertiesComponent.getInstance().getBoolean("generateComments", true));
            config.setSplitGenerate(PropertiesComponent.getInstance().getBoolean("splitGenerate", false));
            config.setUseWrapperClass(PropertiesComponent.getInstance().getBoolean("useWrapperClass", false));

        }
        return config;
    }

    public boolean isUseFieldNamePrefix() {
        return useFieldNamePrefix;
    }

    public void setUseFieldNamePrefix(boolean useFieldNamePrefix) {
        this.useFieldNamePrefix = useFieldNamePrefix;
    }

    public boolean isObjectFromData() {
        return objectFromData;
    }

    public int getErrorCount() {
        return errorCount;
    }

    public void setErrorCount(int errorCount) {
        this.errorCount = errorCount;
    }

    public String getEntityPackName() {
        return entityPackName;
    }

    public String geFullNameAnnotation() {

        if (annotationStr.equals(Constant.gsonAnnotation)) {
            return Constant.gsonFullNameAnnotation;
        }
        if (annotationStr.equals(Constant.jackAnnotation)) {
            return Constant.jackFullNameAnnotation;
        }
        if (annotationStr.equals(Constant.fastAnnotation)) {
            return Constant.fastFullNameAnnotation;
        }
        if (annotationStr.equals(Constant.loganSquareAnnotation)) {
            return Constant.loganSquareFullNameAnnotation;
        }
        return annotationStr.replaceAll("\\(", "(").replaceAll("\\)", ")").replaceAll("\\s\\*", "");
    }


    public boolean isGenerateComments() {
        return generateComments;
    }

    public void setGenerateComments(boolean generateComments) {
        this.generateComments = generateComments;
    }

    public void setEntityPackName(String entityPackName) {
        this.entityPackName = entityPackName;
    }

    public boolean isVirgoMode() {
        return virgoMode;
    }

    public void setVirgoMode(boolean virgoMode) {
        this.virgoMode = virgoMode;
    }

    public String getFiledNamePreFixStr() {
        return filedNamePreFixStr;
    }

    public void setFiledNamePreFixStr(String filedNamePreFixStr) {
        this.filedNamePreFixStr = filedNamePreFixStr;
    }

    public String getAnnotationStr() {
        return annotationStr;
    }

    public void setAnnotationStr(String annotationStr) {
        this.annotationStr = annotationStr;
    }

    public void setObjectFromData(boolean objectFromData) {
        this.objectFromData = objectFromData;
    }

    public boolean isObjectFromData1() {
        return objectFromData1;
    }

    public void setObjectFromData1(boolean objectFromData2) {
        this.objectFromData1 = objectFromData2;
    }

    public boolean isArrayFromData() {
        return arrayFromData;
    }

    public void setArrayFromData(boolean arrayFromData) {
        this.arrayFromData = arrayFromData;
    }

    public boolean isArrayFromData1() {
        return arrayFromData1;
    }

    public void setArrayFromData1(boolean arrayFromData1) {
        this.arrayFromData1 = arrayFromData1;
    }


    public void setObjectFromDataStr(String objectFromDataStr) {
        this.objectFromDataStr = objectFromDataStr;
    }

    public void setObjectFromDataStr1(String objectFromDataStr1) {
        this.objectFromDataStr1 = objectFromDataStr1;
    }

    public void setArrayFromDataStr(String arrayFromDataStr) {
        this.arrayFromDataStr = arrayFromDataStr;
    }

    public void setArrayFromData1Str(String arrayFromData1Str) {
        this.arrayFromData1Str = arrayFromData1Str;
    }

    public String getObjectFromDataStr() {
        return objectFromDataStr;
    }

    public String getObjectFromDataStr1() {
        return objectFromDataStr1;
    }

    public String getArrayFromDataStr() {
        return arrayFromDataStr;
    }

    public String getArrayFromData1Str() {
        return arrayFromData1Str;
    }

    public String getSuffixStr() {
        return suffixStr;
    }

    public void setSuffixStr(String suffixStr) {
        this.suffixStr = suffixStr;
    }

    public boolean isReuseEntity() {
        return reuseEntity;
    }


    public void setReuseEntity(boolean reuseEntity) {
        this.reuseEntity = reuseEntity;
    }

    public void setIsAutoName(boolean isAutoName) {
        this.isAutoName = isAutoName;
    }

    public boolean isUseSerializedName() {
        return useSerializedName;
    }

    public void setUseSerializedName(boolean useSerializedName) {
        this.useSerializedName = useSerializedName;
    }

    public boolean isFieldPrivateMode() {
        return fieldPrivateMode;
    }

    public void setFieldPrivateMode(boolean fieldPrivateMode) {
        this.fieldPrivateMode = fieldPrivateMode;
    }


    public void saveObjectFromDataStr(String objectFromDataStr) {
        this.objectFromDataStr = objectFromDataStr;
        PropertiesComponent.getInstance().setValue("objectFromDataStr", objectFromDataStr + "");
    }

    public void saveObjectFromDataStr1(String objectFromDataStr1) {
        this.objectFromDataStr1 = objectFromDataStr1;
        PropertiesComponent.getInstance().setValue("objectFromDataStr1", objectFromDataStr1 + "");
    }

    public void saveArrayFromDataStr(String arrayFromDataStr) {
        this.arrayFromDataStr = arrayFromDataStr;
        PropertiesComponent.getInstance().setValue("arrayFromDataStr", arrayFromDataStr + "");
    }

    public void saveArrayFromData1Str(String arrayFromData1Str) {
        this.arrayFromData1Str = arrayFromData1Str;
        PropertiesComponent.getInstance().setValue("arrayFromData1Str", arrayFromData1Str + "");
    }

    public boolean isToastError() {
        if (Config.getInstant().getErrorCount() < 3) {
            Config.getInstant().setErrorCount(Config.getInstant().getErrorCount() + 1);
            Config.getInstant().save();
            return true;
        }
        return false;
    }

    public boolean isSplitGenerate() {
        return splitGenerate;
    }

    public void setSplitGenerate(boolean splitGenerate) {
        this.splitGenerate = splitGenerate;
    }


    public void saveCurrentPackPath(String entityPackName) {
        if (entityPackName == null) {
            return;
        }
        setEntityPackName(entityPackName+".");
        save();
    }

    public boolean isUseWrapperClass() {
        return useWrapperClass;
    }

    public boolean isAutoName() {
        return isAutoName;
    }

    public void setUseWrapperClass(boolean useWrapperClass) {
        this.useWrapperClass = useWrapperClass;
    }
}

OK了,看看修改的效果

打造升级GsonFormat驼峰命名规则_第6张图片

配置加上了,接下来,需要对生成的参数,变成驼峰命名,追踪代码,主要还是从FieldsDialog入手,进入到这个界面的时候,我们只需要修改Field name的值即可

打造升级GsonFormat驼峰命名规则_第7张图片

阅读代码这里的createData方法应该是关键

private void initListener(ClassEntity classEntity, String generateClassStr) {
        .....
        JXTreeTable treetable = new JXTreeTable(new FiledTreeTableModel(createData(classEntity)));
        .....
    }

接续跟进

    private DefaultMutableTreeTableNode createData(ClassEntity classEntity) {
        DefaultMutableTreeTableNode root = new DefaultMutableTreeTableNode(classEntity);
        createDataNode(root, classEntity);
        return root;
    }

    private void createDataNode(DefaultMutableTreeTableNode root, ClassEntity innerClassEntity) {
        for (FieldEntity field : innerClassEntity.getFields()) {
            DefaultMutableTreeTableNode node = new DefaultMutableTreeTableNode(field);
            root.add(node);
            defaultMutableTreeTableNodeList.add(node);
        }
        for (ClassEntity classEntity : innerClassEntity.getInnerClasss()) {
            DefaultMutableTreeTableNode node = new DefaultMutableTreeTableNode(classEntity);
            root.add(node);
            createDataNode(node, classEntity);
        }

    }

发现了FieldEntity对象,这应该就是生成字段封装的对象了吧,发现里面的确有一个fieldName字段,这样只需要修改这个值即可,我当时预备看哪里调用了setFieldName方法,再次此方法前进行修改,但是发现有好几处,好吧,直接在setFieldName方法内部修改

    public void setFieldName(String fieldName) {
        if (TextUtils.isEmpty(fieldName)) {
            return;
        }
        // 判断是否需要自动命名
        if (Config.getInstant().isAutoName()) {
            // 需要从新命名,将字段名转换成驼峰
            this.fieldName = underlineToHump(fieldName);
        } else {
            this.fieldName = fieldName;
        }
    }


    public String underlineToHump(String para) {
        StringBuilder result = new StringBuilder();
        String a[] = para.split("_");
        for (String s : a) {
            if (result.length() == 0) {
                result.append(s.toLowerCase());
            } else {
                result.append(s.substring(0, 1).toUpperCase());
                result.append(s.substring(1).toLowerCase());
            }
        }
        return result.toString();
    }

好了大功告成,修改下版本,测试下!

好啦打包一个安装的jar包,自行安装andorid studio吧

插件安装包下载地址

代码

你可能感兴趣的:(技术学习)