Giter VIP home page Giter VIP logo

notnullgsonformat's Introduction

NotNullGsonFormat

代码拷贝字GsonFormat插件,增加了get的时候如果为null的生成

title: AS的GsonFormat插件变种 date: 2018-01-16 14:00:58 tags: code

Bean的生成会用到一些插件来完成,比如GsonFormat,但是后台会有情况比如生成的一个类A,但是后台给这个类A的值不是一个空对象,而是null。这样导致我们在调用类A的cc属性的时候,因为A已经是null了还调用null的一个属性而导致空指针异常,所以这个插件为了就是解决这个问题,自动补充null的时候的值,所有代码都是改别人的库,变种而已。更改的代码如下:

     String method = "public "
                            .concat(typeStr).concat(
                                    "   get").concat(
                                    captureName(field)).concat(
                                    "() { " +getNotNull(typeStr, field1.getGenerateFieldName())+"  return ").concat(
                                    field1.getGenerateFieldName()).concat(" ;} ");
                    mClass.add(mFactory.createMethodFromText(method, mClass));

在这个原来生成get的方法代码中插入一个getNotNull字符,方法内容如下:

private String getNotNull(String typeStr,String fieldName) {
        String result="";
        if (typeStr.equals("String")){//
            result="if("+fieldName+"==null){\n"+fieldName+"=\"\";}";
            return result;
        }
        if (typeStr.equals("int")
                |typeStr.equals("double")
                |typeStr.equals("long")
                |typeStr.equals("float")
                |typeStr.equals("byte")
                |typeStr.equals("short")
                |typeStr.equals("char")){
            return result;
        }else{
            String prx=typeStr;
            if (typeStr.startsWith("java.util.List")){
                prx=prx.replace("java.util.List","java.util.ArrayList");
            }
            result="if("+fieldName+"==null){\n"+fieldName+"= new "+prx+"();}";
        }
        return result;
    }

如果不是基本数据类型,而且是null值,就new一个出来,如果还有报错,可以以后再修正。就是一个静态代码的生成罢了 test.jar就是插件安装包

代码地址https://github.com/yeqinfu/NotNullGsonFormat 我的博客:www.yeqinfu.com

notnullgsonformat's People

Contributors

yeqinfu avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.