Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java] Using sinceVersion from field instead of type declaration #548

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1054,12 +1054,13 @@ private void generateComposite(final List<Token> tokens) throws IOException
final StringBuilder sb = new StringBuilder();
generateEncodingOffsetMethod(sb, propertyName, encodingToken.offset(), BASE_INDENT);
generateEncodingLengthMethod(sb, propertyName, encodingToken.encodedLength(), BASE_INDENT);
generateFieldSinceVersionMethod(sb, encodingToken, BASE_INDENT);

switch (encodingToken.signal())
{
case ENCODING:
out.append(sb).append(generatePrimitiveDecoder(
true, encodingToken.name(), encodingToken, BASE_INDENT));
true, encodingToken.name(), encodingToken, encodingToken, BASE_INDENT));
break;

case BEGIN_ENUM:
Expand Down Expand Up @@ -1477,19 +1478,24 @@ private static CharSequence generateEnumDeclaration(final String name, final Tok
}

private CharSequence generatePrimitiveDecoder(
final boolean inComposite, final String propertyName, final Token token, final String indent)
final boolean inComposite,
final String propertyName,
final Token propertyToken,
final Token encodingToken,
final String indent)
{
final StringBuilder sb = new StringBuilder();

sb.append(generatePrimitiveFieldMetaData(propertyName, token, indent));
sb.append(generatePrimitiveFieldMetaData(propertyName, encodingToken, indent));

if (token.isConstantEncoding())
if (encodingToken.isConstantEncoding())
{
sb.append(generateConstPropertyMethods(propertyName, token, indent));
sb.append(generateConstPropertyMethods(propertyName, encodingToken, indent));
}
else
{
sb.append(generatePrimitivePropertyDecodeMethods(inComposite, propertyName, token, indent));
sb.append(generatePrimitivePropertyDecodeMethods(
inComposite, propertyName, propertyToken, encodingToken, indent));
}

return sb;
Expand All @@ -1515,11 +1521,16 @@ private CharSequence generatePrimitiveEncoder(
}

private CharSequence generatePrimitivePropertyDecodeMethods(
final boolean inComposite, final String propertyName, final Token token, final String indent)
final boolean inComposite,
final String propertyName,
final Token propertyToken,
final Token encodingToken,
final String indent)
{
return token.matchOnLength(
() -> generatePrimitivePropertyDecode(inComposite, propertyName, token, indent),
() -> generatePrimitiveArrayPropertyDecode(inComposite, propertyName, token, indent));
return encodingToken.matchOnLength(
() -> generatePrimitivePropertyDecode(inComposite, propertyName, propertyToken, encodingToken, indent),
() -> generatePrimitiveArrayPropertyDecode(
inComposite, propertyName, propertyToken, encodingToken, indent));
}

private CharSequence generatePrimitivePropertyEncodeMethods(
Expand Down Expand Up @@ -1570,12 +1581,16 @@ private CharSequence generatePrimitiveFieldMetaData(
}

private CharSequence generatePrimitivePropertyDecode(
final boolean inComposite, final String propertyName, final Token token, final String indent)
final boolean inComposite,
final String propertyName,
final Token propertyToken,
final Token encodingToken,
final String indent)
{
final Encoding encoding = token.encoding();
final Encoding encoding = encodingToken.encoding();
final String javaTypeName = javaTypeName(encoding.primitiveType());

final int offset = token.offset();
final int offset = encodingToken.offset();
final String byteOrderStr = byteOrderString(encoding);

return String.format(
Expand All @@ -1587,7 +1602,7 @@ private CharSequence generatePrimitivePropertyDecode(
indent + " }\n\n",
javaTypeName,
propertyName,
generateFieldNotPresentCondition(inComposite, token.version(), encoding, indent),
generateFieldNotPresentCondition(inComposite, propertyToken.version(), encoding, indent),
generateGet(encoding.primitiveType(), "offset + " + offset, byteOrderStr));
}

Expand Down Expand Up @@ -1676,13 +1691,17 @@ private static CharSequence generatePropertyNotPresentCondition(
}

private CharSequence generatePrimitiveArrayPropertyDecode(
final boolean inComposite, final String propertyName, final Token token, final String indent)
final boolean inComposite,
final String propertyName,
final Token propertyToken,
final Token encodingToken,
final String indent)
{
final Encoding encoding = token.encoding();
final Encoding encoding = encodingToken.encoding();
final String javaTypeName = javaTypeName(encoding.primitiveType());
final int offset = token.offset();
final int offset = encodingToken.offset();
final String byteOrderStr = byteOrderString(encoding);
final int fieldLength = token.arrayLength();
final int fieldLength = encodingToken.arrayLength();
final int typeSize = sizeOfPrimitive(encoding);

final StringBuilder sb = new StringBuilder();
Expand All @@ -1703,7 +1722,7 @@ private CharSequence generatePrimitiveArrayPropertyDecode(
javaTypeName,
propertyName,
fieldLength,
generateFieldNotPresentCondition(inComposite, token.version(), encoding, indent),
generateFieldNotPresentCondition(inComposite, propertyToken.version(), encoding, indent),
offset,
typeSize,
generateGet(encoding.primitiveType(), "pos", byteOrderStr)));
Expand All @@ -1727,7 +1746,7 @@ private CharSequence generatePrimitiveArrayPropertyDecode(
indent + " }\n",
Generators.toUpperFirstChar(propertyName),
fieldLength,
generateArrayFieldNotPresentCondition(token.version(), indent),
generateArrayFieldNotPresentCondition(propertyToken.version(), indent),
offset));

sb.append(String.format("\n" +
Expand All @@ -1741,7 +1760,7 @@ private CharSequence generatePrimitiveArrayPropertyDecode(
indent + " return new String(dst, 0, end, %s);\n" +
indent + " }\n\n",
formatPropertyName(propertyName),
generateStringNotPresentCondition(token.version(), indent),
generateStringNotPresentCondition(propertyToken.version(), indent),
fieldLength, offset,
fieldLength, fieldLength,
charset(encoding.characterEncoding())));
Expand Down Expand Up @@ -2259,7 +2278,7 @@ private CharSequence generateDecoderFields(final List<Token> tokens, final Strin
switch (typeToken.signal())
{
case ENCODING:
sb.append(generatePrimitiveDecoder(false, propertyName, typeToken, indent));
sb.append(generatePrimitiveDecoder(false, propertyName, fieldToken, typeToken, indent));
break;

case BEGIN_ENUM:
Expand Down Expand Up @@ -2485,7 +2504,7 @@ private CharSequence generateCompositeProperty(
generateFlyweightPropertyJavadoc(indent + INDENT, propertyToken, compositeName),
compositeName,
propertyName,
generatePropertyNotPresentCondition(inComposite, codecType, compositeToken.version(), indent),
generatePropertyNotPresentCondition(inComposite, codecType, propertyToken.version(), indent),
propertyName,
compositeToken.offset(),
propertyName));
Expand Down
Loading