Java 类com.parse.ParseClassName 实例源码

项目:Android-ParseModel    文件:ElementCodeGenerator.java   
public static TypeSpec.Builder generate(AnnotatedClass annotated) {
    String classNameUC = UPPER_CAMEL.to(UPPER_CAMEL, annotated.mShortClassName); //to be sure

    ClassName parseQuery = ClassName.get("com.parse", "ParseQuery");
    ClassName thisElement = ClassName.get("", classNameUC);
    TypeName parseQueryOfThisElement = ParameterizedTypeName.get(parseQuery, thisElement);

    TypeSpec.Builder builder = TypeSpec.classBuilder(classNameUC)
            .superclass(ClassName.get("", PARSE_BASE_OBJECT))
            .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
            .addMethod(MethodSpec.constructorBuilder()
                    .addModifiers(Modifier.PUBLIC)
                    .build())
            .addAnnotation(AnnotationSpec.builder(ParseClassName.class)
                    .addMember("value", "$S", classNameUC)
                    .build())
            .addStaticBlock(CodeBlock.builder()
                    .addStatement(STATIC_BLOCK_STATEMENT, classNameUC)
                    .build())
            .addMethod(MethodSpec.methodBuilder(STATIC_GET_QUERY_METHOD_NAME)
                    .addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
                    .returns(parseQueryOfThisElement)
                    .addStatement(STATIC_GET_QUERY_METHOD_RETURN_STATEMENT, classNameUC)
                    .build());

    String fieldName;
    String fieldNameLC;
    String fieldNameUC;
    String fieldNameUU;

    for (int i = 0; i < annotated.mFieldNames.size(); ++i) {
        fieldName = annotated.mFieldNames.get(i);
        fieldNameLC = LOWER_CAMEL.to(LOWER_CAMEL, fieldName);
        fieldNameUC = LOWER_CAMEL.to(UPPER_CAMEL, fieldName);
        fieldNameUU = STATIC_KEY_FIELD + LOWER_CAMEL.to(UPPER_UNDERSCORE, fieldName);

        builder.addField(FieldSpec
                .builder(String.class, fieldNameUU, Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
                .initializer("$S", fieldName)
                .build());

        FieldType type = annotated.mFieldTypes.get(i);
        TypeName setterParameter = type.getTypeName();

        builder.addMethod(MethodSpec.methodBuilder(SETTER_METHOD_NAME + fieldNameUC)
                .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
                .addParameter(setterParameter, fieldNameLC)
                .addStatement(SETTER_METHOD_STATEMENT, fieldNameUU, fieldNameLC)
                .build());

        MethodSpec.Builder getterBuilder = MethodSpec.methodBuilder(GETTER_METHOD_NAME + fieldNameUC)
                .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
                .returns(setterParameter);
        type.addGetterStatements(getterBuilder, fieldNameUU);

        builder.addMethod(getterBuilder.build());
    }

    return builder;
}