1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| public void generate(String json) throws IOException { Gson gson = new Gson(); Entity entity = gson.fromJson(json, Entity.class); String entityClassName = WordUtils.capitalize(entity.getName()); String entityObjectLiteral = WordUtils.uncapitalize(entityClassName); ClassName className = ClassName.get(entity.getPackageName(), entityClassName);
TypeSpec.Builder entityTypeBuilder = TypeSpec.classBuilder(entityClassName) .addModifiers(Modifier.PUBLIC); MethodSpec entityConstructor = MethodSpec.constructorBuilder() .addModifiers(Modifier.PRIVATE).build(); entityTypeBuilder.addMethod(entityConstructor);
TypeSpec.Builder builderTypeBuilder = TypeSpec.classBuilder("Builder") .addModifiers(Modifier.PUBLIC, Modifier.STATIC) .addField(className, entityObjectLiteral, Modifier.PRIVATE); MethodSpec builderConstructor = MethodSpec.constructorBuilder() .addParameter(className, entityObjectLiteral) .addStatement("this.$N = $N", entityObjectLiteral, entityObjectLiteral) .addModifiers(Modifier.PRIVATE).build(); builderTypeBuilder.addMethod(builderConstructor);
entity.getFields().forEach(it -> { FieldTypeFormatAndValue fieldTypeFormatAndValue = fieldNameWithInitializeValue.get(it.getType()); FieldSpec fieldSpec = FieldSpec.builder(fieldTypeNameWithTypeName.get(it.getType()), it.getName(), Modifier.PRIVATE) .initializer(fieldTypeFormatAndValue.getFormat(), fieldTypeFormatAndValue.getValue()).build(); entityTypeBuilder.addField(fieldSpec); MethodSpec setMethod = MethodSpec.methodBuilder(String.format("set%s", WordUtils.capitalize(it.getName()))) .addModifiers(Modifier.PUBLIC) .addParameter(fieldTypeNameWithTypeName.get(it.getType()), it.getName()) .returns(ClassName.get("", "Builder")) .addStatement("$N.$N = $N", entityObjectLiteral, it.getName(), it.getName()) .addStatement("return this").build();
builderTypeBuilder.addMethod(setMethod); MethodSpec clearMethod = MethodSpec.methodBuilder(String.format("clear%s", WordUtils.capitalize(it.getName()))) .addModifiers(Modifier.PUBLIC) .returns(ClassName.get("", "Builder")) .addStatement(String.format("$N.$N = %s", fieldTypeFormatAndValue.getFormat()), entityObjectLiteral, it.getName(), fieldTypeFormatAndValue.getValue()) .addStatement("return this").build(); builderTypeBuilder.addMethod(clearMethod);
MethodSpec getMethod = MethodSpec.methodBuilder(String.format("get%s", WordUtils.capitalize(it.getName()))) .addModifiers(Modifier.PUBLIC) .returns(fieldTypeNameWithTypeName.get(it.getType())) .addStatement("return $N", it.getName()).build(); entityTypeBuilder.addMethod(getMethod); }); MethodSpec buildMethod = MethodSpec.methodBuilder("build") .addModifiers(Modifier.PUBLIC) .returns(ClassName.get("", entityClassName)) .addStatement("return $N", entityObjectLiteral).build(); builderTypeBuilder.addMethod(buildMethod);
MethodSpec newBuilderMethod = MethodSpec.methodBuilder("newBuilder") .addModifiers(Modifier.PUBLIC, Modifier.STATIC) .returns(ClassName.get("", "Builder")) .addStatement("return new $N(new $N())", "Builder", entityClassName).build(); entityTypeBuilder.addMethod(newBuilderMethod);
MethodSpec toBuilderMethod = MethodSpec.methodBuilder("toBuilder") .addModifiers(Modifier.PUBLIC) .returns(ClassName.get("", "Builder")) .addStatement("return new $N(this)", "Builder").build(); entityTypeBuilder.addMethod(toBuilderMethod);
entityTypeBuilder.addType(builderTypeBuilder.build()); JavaFile javaFile = JavaFile.builder(entity.getPackageName(), entityTypeBuilder.build()) .build();
javaFile.writeToFile(new File(String.format("src/main/java"))); }
|