Java Code Examples for com.baidu.bjf.remoting.protobuf.FieldType#getJavaType()

The following examples show how to use com.baidu.bjf.remoting.protobuf.FieldType#getJavaType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractCodeGenerator.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
/**
 * Check {@link FieldType} is validate to class type of {@link Field}.
 *
 * @param type the type
 * @param field the field
 */
protected void checkType(FieldType type, Field field) {
    Class<?> cls = field.getType();

    if (type == FieldType.OBJECT || type == FieldType.ENUM) {
        return;
    }

    String javaType = type.getJavaType();
    if (Integer.class.getSimpleName().equals(javaType)) {
        if (cls.getSimpleName().equals("int") || Integer.class.getSimpleName().equals(cls.getSimpleName())) {
            return;
        }
        throw new IllegalArgumentException(getMismatchTypeErroMessage(type, field));
    }
    if (!javaType.equalsIgnoreCase(cls.getSimpleName())) {
        throw new IllegalArgumentException(getMismatchTypeErroMessage(type, field));
    }
}
 
Example 2
Source File: CodeGenerator.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
/**
 * Check {@link FieldType} is validate to class type of {@link Field}
 * 
 * @param type
 * @param field
 */
private void checkType(FieldType type, Field field) {
    Class<?> cls = field.getType();

    if (type == FieldType.OBJECT || type == FieldType.ENUM) {
        return;
    }

    String javaType = type.getJavaType();
    if (Integer.class.getSimpleName().equals(javaType)) {
        if (cls.getSimpleName().equals("int") || Integer.class.getSimpleName().equals(cls.getSimpleName())) {
            return;
        }
        throw new IllegalArgumentException(getMismatchTypeErroMessage(type, field));
    }
    if (!javaType.equalsIgnoreCase(cls.getSimpleName())) {
        throw new IllegalArgumentException(getMismatchTypeErroMessage(type, field));
    }
}
 
Example 3
Source File: CodedConstant.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
/**
 * get mapped type defined java expression.
 * 
 * @param order field order
 * @param type field type
 * @param express java expression
 * @param isList is field type is a {@link List}
 * @param isMap is field type is a {@link Map}
 * @return full java expression
 */
public static String getMappedTypeDefined(int order, FieldType type, String express, boolean isList,
        boolean isMap) {
    StringBuilder code = new StringBuilder();
    String fieldName = getFieldName(order);
    if ((type == FieldType.STRING || type == FieldType.BYTES) && !isList) {
        // add null check
        code.append("com.google.protobuf.ByteString ").append(fieldName).append(" = null")
                .append(CodeGenerator.JAVA_LINE_BREAK);
        code.append("if (!CodedConstant.isNull(").append(express).append(")) {").append(CodeGenerator.LINE_BREAK);

        String method = "copyFromUtf8";
        if (type == FieldType.BYTES) {
            method = "copyFrom";
        }
        code.append(fieldName).append(" = com.google.protobuf.ByteString.").append(method).append("(")
                .append(express).append(")").append(CodeGenerator.JAVA_LINE_BREAK);
        code.append("}").append(CodeGenerator.LINE_BREAK);
        return code.toString();
    }
    // add null check
    String defineType = type.getJavaType();
    if (isList) {
        defineType = "List";
    } else if (isMap) {
        defineType = "Map";
    }
    code.setLength(0);
    code.append(defineType).append(" ").append(fieldName).append(" = null").append(CodeGenerator.JAVA_LINE_BREAK);
    code.append("if (!CodedConstant.isNull(").append(express).append(")) {").append(CodeGenerator.LINE_BREAK);
    code.append(fieldName).append(" = ").append(express).append(CodeGenerator.JAVA_LINE_BREAK);
    code.append("}").append(CodeGenerator.LINE_BREAK);
    return code.toString();
}
 
Example 4
Source File: CodedConstant.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the filed type.
 *
 * @param type the type
 * @param isList the is list
 * @return the filed type
 */
public static String getFiledType(FieldType type, boolean isList) {
    // add null check
    String defineType = type.getJavaType();
    if (isList) {
        defineType = "Collection";
    }
    
    return defineType;
    
}
 
Example 5
Source File: AbstractCodeGenerator.java    From jprotobuf with Apache License 2.0 2 votes vote down vote up
/**
 * get error message info by type not matched.
 *
 * @param type the type
 * @param field the field
 * @return error message for mismatch type
 */
private String getMismatchTypeErroMessage(FieldType type, Field field) {
    return "Type mismatch. @Protobuf required type '" + type.getJavaType() + "' but field type is '"
            + field.getType().getSimpleName() + "' of field name '" + field.getName() + "' on class "
            + field.getDeclaringClass().getCanonicalName();
}
 
Example 6
Source File: CodeGenerator.java    From jprotobuf with Apache License 2.0 2 votes vote down vote up
/**
 * get error message info by type not matched
 * 
 * @param type
 * @param field
 * @return error message for mismatch type
 */
private String getMismatchTypeErroMessage(FieldType type, Field field) {
    return "Type mismatch. @Protobuf required type '" + type.getJavaType() + "' but field type is '"
            + field.getType().getSimpleName() + "' of field name '" + field.getName() + "' on class "
            + field.getDeclaringClass().getCanonicalName();
}