Skip to content

Commit

Permalink
extract constant
Browse files Browse the repository at this point in the history
  • Loading branch information
xiemalin authored and xiemalin committed Mar 6, 2019
1 parent 5649795 commit 368107b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>com.baidu</groupId>
<artifactId>jprotobuf</artifactId>
<version>1.11.7</version>
<version>1.11.8-SNAPSHOT</version>

<name>Jprotobuf</name>
<description>A very useful utility library for java programmer using google protobuf.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,17 @@
*/
public class CodedConstant {

/** The Constant FIELD_PREFIX. */
private static final String FIELD_PREFIX = "f_";

/**
* get field name.
*
* @param order field order
* @return field name
*/
public static String getFieldName(int order) {
String fieldName = "f_" + order;
String fieldName = FIELD_PREFIX + order;
return fieldName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package com.baidu.bjf.remoting.protobuf.enumeration;

import java.util.ArrayList;
import java.util.List;

import com.baidu.bjf.remoting.protobuf.EnumReadable;

/**
Expand Down Expand Up @@ -60,7 +63,16 @@ public int value() {
}

public static void main(String[] args) {
EnumAttrPOJO valueOf = Enum.valueOf(EnumAttrPOJO.class, EnumAttrPOJO.values()[0].name());
System.out.println(valueOf);

List<String> list = new ArrayList();
list.add("1");
list.add("2");
list.add("3");
list.add("1");

for (String string : list) {
list.remove(string);
System.out.println(list);
}
}
}
2 changes: 1 addition & 1 deletion v3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
<plugin>
<groupId>com.baidu</groupId>
<artifactId>jprotobuf-precompile-plugin</artifactId>
<version>2.0.6</version>
<version>2.0.8</version>
<configuration>
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
<filterClassPackage>com.baidu</filterClassPackage>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
*/
public class CodedConstant {

private static final String FIELD_PREFIX = "f_";

/** The Constant MAP_ENTRY_SUFFIX. */
private static final String MAP_ENTRY_SUFFIX = "Entry";

Expand All @@ -100,7 +102,7 @@ public class CodedConstant {
* @return field name
*/
public static String getFieldName(int order) {
String fieldName = "f_" + order;
String fieldName = FIELD_PREFIX + order;
return fieldName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public String getCode() {
if (!StringUtils.isEmpty(pkg)) {
pkg = "package " + pkg + ";";
}

// set package
templator.setVariable("package", pkg);

Expand Down Expand Up @@ -268,7 +268,7 @@ protected void initDecodeMethodTemplateVariable() {
} else if (field.isMap()) {

String getMapCommand = getMapCommand(field);

if (field.isEnumValueType()) {
String enumClassName = field.getGenericeValueType().getCanonicalName();
code.append("EnumHandler<").append(enumClassName).append("> handler");
Expand All @@ -277,13 +277,14 @@ protected void initDecodeMethodTemplateVariable() {
code.append(ClassCode.LINE_BREAK);
code.append("public ").append(enumClassName).append(" handle(int value) {");
code.append(ClassCode.LINE_BREAK);
code.append("String enumName = CodedConstant.getEnumName(").append(enumClassName).append(".values(), value)");
code.append("String enumName = CodedConstant.getEnumName(").append(enumClassName)
.append(".values(), value)");
code.append(ClassCode.JAVA_LINE_BREAK);
code.append("return ").append(enumClassName).append(".valueOf(enumName)");
code.append(ClassCode.JAVA_LINE_BREAK);
code.append("}}");
code.append(ClassCode.JAVA_LINE_BREAK);

}
objectDecodeExpress = code.toString();
code.setLength(0);
Expand Down Expand Up @@ -343,27 +344,26 @@ protected void initDecodeMethodTemplateVariable() {
} else {
templator.setVariable("deocdeCheckNull", "");
}

String objectPackedDecodeExpress = "";
// read packed type
if (isList) {
FieldType fieldType = field.getFieldType();
if (fieldType.isPrimitive() || fieldType.isEnum()) {
code.append("if (tag == ").append(CodedConstant.makeTag(field.getOrder(),
WireFormat.WIRETYPE_LENGTH_DELIMITED));
code.append("if (tag == ")
.append(CodedConstant.makeTag(field.getOrder(), WireFormat.WIRETYPE_LENGTH_DELIMITED));
code.append(") {").append(ClassCode.LINE_BREAK);



code.append("int length = input.readRawVarint32()").append(ClassCode.JAVA_LINE_BREAK);
code.append("int limit = input.pushLimit(length)").append(ClassCode.JAVA_LINE_BREAK);

code.append(getSetToField("ret", field.getField(), cls, express, isList, field.isMap(), true));

code.append("input.popLimit(limit)").append(ClassCode.JAVA_LINE_BREAK);

code.append("continue").append(ClassCode.JAVA_LINE_BREAK);
code.append("}").append(ClassCode.LINE_BREAK);

objectPackedDecodeExpress = code.toString();
}
}
Expand All @@ -374,7 +374,7 @@ protected void initDecodeMethodTemplateVariable() {
templator.addBlock("decodeFields");
}
}

/**
* @param field
* @return
Expand Down

0 comments on commit 368107b

Please sign in to comment.