com.baidu.bjf.remoting.protobuf.annotation.Protobuf Java Examples

The following examples show how to use com.baidu.bjf.remoting.protobuf.annotation.Protobuf. 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: ProtobufIDLProxy.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
/**
 * to generate @Protobuf defined code for target field.
 * 
 * @param code
 * @param field
 */
private static void generateProtobufDefinedForField(StringBuilder code, Field field, Set<String> enumNames) {
    code.append("@").append(Protobuf.class.getSimpleName()).append("(");

    String fieldType = fieldTypeMapping.get(field.getType());
    if (fieldType == null) {
        if (enumNames.contains(field.getType())) {
            fieldType = "FieldType.ENUM";
        } else {
            fieldType = "FieldType.OBJECT";
        }
    }

    code.append("fieldType=").append(fieldType);
    code.append(", order=").append(field.getTag());
    if (Label.OPTIONAL == field.getLabel()) {
        code.append(", required=false");
    } else if (Label.REQUIRED == field.getLabel()) {
        code.append(", required=true");
    }
    code.append(")\n");

}
 
Example #2
Source File: ReflectiveCodec.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
public ReflectiveCodec(Class<T> cls) {
	this.cls = cls;

	List<Field> fields = FieldUtils.findMatchedFields(cls, Protobuf.class);
	if (fields.isEmpty()) {
		throw new IllegalArgumentException("Invalid class [" + cls.getName() + "] no field use annotation @"
				+ Protobuf.class.getName() + " at class " + cls.getName());
	}

	fieldInfos = ProtobufProxyUtils.processDefaultValue(fields);

	orderFieldsMapping = new HashMap<Integer, FieldInfo>();
	for (FieldInfo fieldInfo : fieldInfos) {
		int tag = CodedConstant.makeTag(fieldInfo.getOrder(),
				fieldInfo.getFieldType().getInternalFieldType().getWireType());
		orderFieldsMapping.put(tag, fieldInfo);
	}
}
 
Example #3
Source File: ProtobufUtils.java    From brpc-java with Apache License 2.0 5 votes vote down vote up
public static MessageType getMessageType(Method method) {
    Class<?>[] types = method.getParameterTypes();
    Class returnType = method.getReturnType();
    if (types.length < 0) {
        throw new IllegalArgumentException("invalid rpc method params");
    }

    if (types.length != 1) {
        return MessageType.POJO;
    }

    Class<?> inputType = types[0];
    if (Message.class.isAssignableFrom(inputType)
            && Message.class.isAssignableFrom(returnType)) {
        return MessageType.PROTOBUF;
    }

    ProtobufClass protobufClass = inputType.getAnnotation(ProtobufClass.class);
    if (protobufClass != null) {
        return MessageType.JPROTOBUF;
    }

    Field[] fields = inputType.getDeclaredFields();
    for (Field field : fields) {
        Protobuf protobuf = field.getAnnotation(Protobuf.class);
        if (protobuf != null) {
            return MessageType.JPROTOBUF;
        }
    }

    return MessageType.POJO;
}
 
Example #4
Source File: ProtobufIDLProxy.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
/**
 * to generate @Protobuf defined code for target field.
 *
 * @param code the code
 * @param field the field
 * @param enumNames the enum names
 */
private static void generateProtobufDefinedForField(StringBuilder code, FieldElement field, Set<String> enumNames) {
    code.append("@").append(Protobuf.class.getSimpleName()).append("(");

    String fieldType = fieldTypeMapping.get(getTypeName(field));
    if (fieldType == null) {
        if (enumNames.contains(getTypeName(field))) {
            fieldType = "FieldType.ENUM";
        } else {
            if (field.type().kind() == DataType.Kind.MAP) {
                fieldType = "FieldType.MAP";
            } else {
                fieldType = "FieldType.OBJECT";
            }

        }
    }

    code.append("fieldType=").append(fieldType);
    code.append(", order=").append(field.tag());
    if (FieldElement.Label.OPTIONAL == field.label()) {
        code.append(", required=false");
    } else if (Label.REQUIRED == field.label()) {
        code.append(", required=true");
    }
    code.append(")\n");

}
 
Example #5
Source File: ProtobufField.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
public ProtobufField(Field field) {
    annotation = field.getAnnotation(Protobuf.class);
    name = field.getName();
    type = field.getType();
    declaredClass = field.getDeclaringClass();
    genericType = field.getGenericType();
}
 
Example #6
Source File: ProtobufProxyUtils.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch field infos.
 *
 * @return the list
 */
public static List<FieldInfo> fetchFieldInfos(Class cls, boolean ignoreNoAnnotation) {
    // if set ProtobufClass annotation
    Annotation annotation = cls.getAnnotation(ProtobufClass.class);

    Annotation zipZap = cls.getAnnotation(EnableZigZap.class);
    boolean isZipZap = false;
    if (zipZap != null) {
        isZipZap = true;
    }

    boolean typeDefined = false;
    List<Field> fields = null;
    if (annotation == null) {
        fields = FieldUtils.findMatchedFields(cls, Protobuf.class);
        if (fields.isEmpty() && ignoreNoAnnotation) {
            throw new IllegalArgumentException("Invalid class [" + cls.getName() + "] no field use annotation @"
                    + Protobuf.class.getName() + " at class " + cls.getName());
        }
    } else {
        typeDefined = true;

        fields = FieldUtils.findMatchedFields(cls, null);
    }

    List<FieldInfo> fieldInfos = ProtobufProxyUtils.processDefaultValue(fields, typeDefined, isZipZap);
    return fieldInfos;
}
 
Example #7
Source File: CodeGenerator.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch field infos.
 *
 * @return the list
 */
protected List<FieldInfo> fetchFieldInfos() {
    // if set ProtobufClass annotation
    Annotation annotation = cls.getAnnotation(ProtobufClass.class);
    
    Annotation zipZap = cls.getAnnotation(EnableZigZap.class);
    boolean isZipZap = false;
    if (zipZap != null) {
        isZipZap = true;
    }
    
    boolean typeDefined = false;
    List<Field> fields = null;
    if (annotation == null) {
        fields = FieldUtils.findMatchedFields(cls, Protobuf.class);
        if (fields.isEmpty()) {
            throw new IllegalArgumentException("Invalid class [" + cls.getName() + "] no field use annotation @"
                    + Protobuf.class.getName() + " at class " + cls.getName());
        }
    } else {
        typeDefined = true;
        
        fields = FieldUtils.findMatchedFields(cls, null);
    }
    
    List<FieldInfo> fieldInfos = ProtobufProxyUtils.processDefaultValue(fields, typeDefined, isZipZap);
    return fieldInfos;
}
 
Example #8
Source File: ProtobufField.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
public ProtobufField(Field field) {
    annotation = field.getAnnotation(Protobuf.class);
    name = field.getName();
    type = field.getType();
    declaredClass = field.getDeclaringClass();
    genericType = field.getGenericType();
}
 
Example #9
Source File: ProtobufField.java    From jprotobuf with Apache License 2.0 3 votes vote down vote up
/**
 * get the annotation
 * @return the annotation
 */
public Protobuf getAnnotation() {
    return annotation;
}
 
Example #10
Source File: ProtobufField.java    From jprotobuf with Apache License 2.0 2 votes vote down vote up
/**
 * get the annotation
 * @return the annotation
 */
public Protobuf getAnnotation() {
    return annotation;
}