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

The following examples show how to use com.baidu.bjf.remoting.protobuf.FieldType#OBJECT . 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
/**
 * Compute list size.
 *
 * @param order the order
 * @param list the list
 * @param type the type
 * @param debug the debug
 * @param path the path
 * @param packed the packed
 * @param sizeOnly the size only if true will not include order size and tag size
 * @return the int
 */
public static int computeListSize(int order, Collection list, FieldType type, boolean debug, File path, boolean packed,
        boolean sizeOnly) {
    int size = 0;
    if (list == null || list.isEmpty()) {
        return size;
    }

    int dataSize = 0;
    for (Object object : list) {
        dataSize += computeSize(order, object, type, debug, path);
    }
    size += dataSize;
    if (type != FieldType.OBJECT) {
        if (packed) {
            if (!sizeOnly) {
                size += com.google.protobuf.CodedOutputStream.computeInt32SizeNoTag(dataSize);
                int tag = CodedConstant.makeTag(order,
                        WireFormat.WIRETYPE_LENGTH_DELIMITED);
                size += com.google.protobuf.CodedOutputStream.computeUInt32SizeNoTag(tag);
            }
        } else {
            size += list.size() * CodedOutputStream.computeTagSize(order);
        }
    }
    return size;
}
 
Example 4
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 5
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;
}
 
Example 6
Source File: FieldInfo.java    From jprotobuf with Apache License 2.0 2 votes vote down vote up
/**
 * Checks if is object type.
 *
 * @return true, if is object type
 */
public boolean isObjectType() {
    return fieldType == FieldType.OBJECT;
}