Java Code Examples for com.baidu.bjf.remoting.protobuf.FieldType#STRING

The following examples show how to use com.baidu.bjf.remoting.protobuf.FieldType#STRING . 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: 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 2
Source File: CodedConstant.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the mapped type size.
 *
 * @param field field
 * @param order field order
 * @param type field type
 * @param isList is field type is a {@link List}
 * @param isMap the is map
 * @param debug debug mode if true enable debug.
 * @param path the path
 * @return full java expression
 */
public static String getMappedTypeSize(FieldInfo field, int order, FieldType type, boolean isList, boolean isMap,
        boolean debug, File path) {
    String fieldName = getFieldName(order);

    String spath = "null";
    if (path != null) {
        spath = "new java.io.File(\"" + path.getAbsolutePath().replace('\\', '/') + "\")";
    }

    String typeString = type.getType().toUpperCase();
    if (isList) {
        return "CodedConstant.computeListSize(" + order + ", " + fieldName + ", FieldType." + typeString + ", "
                + Boolean.valueOf(debug) + ", " + spath + "," + Boolean.valueOf(field.isPacked()) + ")"
                + CodeGenerator.JAVA_LINE_BREAK;
    } else if (isMap) {

        String joinedSentence = getMapFieldGenericParameterString(field);
        return "CodedConstant.computeMapSize(" + order + ", " + fieldName + ", " + joinedSentence + ")"
                + CodeGenerator.JAVA_LINE_BREAK;
    }

    if (type == FieldType.OBJECT) {
        return "CodedConstant.computeSize(" + order + "," + fieldName + ", FieldType." + typeString + ","
                + Boolean.valueOf(debug) + "," + spath + ")" + CodeGenerator.JAVA_LINE_BREAK;
    }

    String t = type.getType();
    if (type == FieldType.STRING) {
        t = "String";
    }
    
    if (type == FieldType.BYTES) {
        t = "ByteArray";
    }
    t = capitalize(t);

    boolean enumSpecial = false;
    if (type == FieldType.ENUM) {
        if (EnumReadable.class.isAssignableFrom(field.getField().getType())) {
            String clsName = ClassHelper.getInternalName(field.getField().getType().getCanonicalName());
            fieldName = "((" + clsName + ") " + fieldName + ").value()";
            enumSpecial = true;
        }
    }
    if (!enumSpecial) {
        fieldName = fieldName + type.getToPrimitiveType();
    }

    return "com.google.protobuf.CodedOutputStream.compute" + t + "Size(" + order + "," + fieldName + ")"
            + CodeGenerator.JAVA_LINE_BREAK;
}
 
Example 3
Source File: CodedConstant.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
/**
 * get object size by {@link FieldType}.
 *
 * @param order the order
 * @param o the o
 * @param type the type
 * @param list the list
 * @param debug the debug
 * @param path the path
 * @return the int
 */
public static int computeSize(int order, Object o, FieldType type, boolean list, boolean debug, File path) {
    int size = 0;
    if (o == null) {
        return size;
    }

    if (type == FieldType.OBJECT) {
        Class cls = o.getClass();
        Codec target = ProtobufProxy.create(cls, debug, path);
        try {
            size = target.size(o);
            size = size + CodedOutputStream.computeRawVarint32Size(size);
            return size + CodedOutputStream.computeTagSize(order);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    if (type == FieldType.STRING) {
        size = CodedOutputStream.computeStringSizeNoTag(String.valueOf(o));
    } else if (type == FieldType.BOOL) {
        size = CodedOutputStream.computeBoolSizeNoTag(Boolean.valueOf(String.valueOf(o)));
    } else if (type == FieldType.BYTES) {
        byte[] bb = (byte[]) o;
        size = CodedOutputStream.computeBytesSizeNoTag(ByteString.copyFrom(bb));
    } else if (type == FieldType.DOUBLE) {
        size = CodedOutputStream.computeDoubleSizeNoTag(Double.valueOf(o.toString()));
    } else if (type == FieldType.FIXED32 || type == FieldType.INT32 || type == FieldType.SFIXED32
            || type == FieldType.SINT32 || type == FieldType.UINT32) {
        size = CodedOutputStream.computeInt32SizeNoTag(Integer.valueOf(o.toString()));
    } else if (type == FieldType.FIXED64 || type == FieldType.INT64 || type == FieldType.SFIXED64
            || type == FieldType.SINT64 || type == FieldType.UINT64) {
        size = CodedOutputStream.computeInt64SizeNoTag(Long.valueOf(o.toString()));
    } else if (type == FieldType.FLOAT) {
        size = CodedOutputStream.computeFloatSizeNoTag(Float.valueOf(o.toString()));
    } else if (type == FieldType.ENUM) {
        if (o instanceof EnumReadable) {
            size = CodedOutputStream.computeInt32SizeNoTag(((EnumReadable) o).value());
        } else if (o instanceof Enum) {
            size = CodedOutputStream.computeInt32SizeNoTag(((Enum) o).ordinal());
        }
    }

    return size;
}