Skip to content

Commit

Permalink
[Java] Clean up after merge of PR #904.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjpt777 committed Sep 27, 2022
1 parent 742cd8a commit a52aef5
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 93 deletions.
6 changes: 3 additions & 3 deletions sbe-tool/src/main/java/uk/co/real_logic/sbe/SbeTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
* <li><b>sbe.keyword.append.token</b>: Token to be appended to keywords.</li>
* <li><b>sbe.decode.unknown.enum.values</b>: Support unknown decoded enum values. Defaults to false.</li>
* <li><b>sbe.xinclude.aware</b>: Is XInclude supported for the schema. Defaults to false.</li>
* <li><b>sbe.type.package.override</b>: Is a package attribute for types element supported (only for JAVA). Defaults to
* <li><b>sbe.type.package.override</b>: Is package attribute for types element supported (only for JAVA). Defaults to
* false.</li>
* </ul>
*/
Expand Down Expand Up @@ -121,9 +121,9 @@ public class SbeTool

/**
* Boolean system property to control the support of package names in {@code <types>} elements.
* Part of SBE v2-rc2. Defaults to false.
* Part of SBE v2-rc3. Defaults to false.
*/
public static final String TYPE_PACKAGE_OVERRIDE = "sbe.type.package.override";
public static final String TYPES_PACKAGE_OVERRIDE = "sbe.types.package.override";

/**
* Target language for generated code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public CodeGenerator newInstance(final Ir ir, final String outputDir)
"true".equals(System.getProperty(JAVA_GROUP_ORDER_ANNOTATION)),
"true".equals(System.getProperty(JAVA_GENERATE_INTERFACES)),
"true".equals(System.getProperty(DECODE_UNKNOWN_ENUM_VALUES)),
"true".equals(System.getProperty(TYPE_PACKAGE_OVERRIDE)),
"true".equals(System.getProperty(TYPES_PACKAGE_OVERRIDE)),
new JavaOutputManager(outputDir, ir.applicableNamespace()));
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ enum CodecType
private final boolean shouldGenerateGroupOrderAnnotation;
private final boolean shouldGenerateInterfaces;
private final boolean shouldDecodeUnknownEnumValues;
private final boolean shouldSupportTypePackages;
private final Set<String> typePackages = new HashSet<>();
private final boolean shouldSupportTypesPackageNames;
private final Set<String> packageNameByTypes = new HashSet<>();

/**
* Create a new Java language {@link CodeGenerator}. Generator support for types in their own package is disabled.
Expand Down Expand Up @@ -106,7 +106,7 @@ public JavaGenerator(
* @param shouldGenerateGroupOrderAnnotation in the codecs.
* @param shouldGenerateInterfaces for common methods.
* @param shouldDecodeUnknownEnumValues generate support for unknown enum values when decoding.
* @param shouldSupportTypePackages generator support for types in their own package
* @param shouldSupportTypesPackageNames generator support for types in their own package.
* @param outputManager for generating the codecs to.
*/
public JavaGenerator(
Expand All @@ -116,14 +116,14 @@ public JavaGenerator(
final boolean shouldGenerateGroupOrderAnnotation,
final boolean shouldGenerateInterfaces,
final boolean shouldDecodeUnknownEnumValues,
final boolean shouldSupportTypePackages,
final boolean shouldSupportTypesPackageNames,
final DynamicPackageOutputManager outputManager)
{
Verify.notNull(ir, "ir");
Verify.notNull(outputManager, "outputManager");

this.ir = ir;
this.shouldSupportTypePackages = shouldSupportTypePackages;
this.shouldSupportTypesPackageNames = shouldSupportTypesPackageNames;
this.outputManager = outputManager;

this.mutableBuffer = validateBufferImplementation(mutableBuffer, MutableDirectBuffer.class);
Expand Down Expand Up @@ -176,18 +176,19 @@ public void generateTypeStubs() throws IOException
}

/**
* Register the the type's explicit package - if it's set and should be supported.
* Register the types explicit package - if it's set and should be supported.
*
* @param token the 0-th token of the type
* @param ir the intermediate representation
* @return the overriden package name of the type if set and supported, or {@link Ir#applicableNamespace() }
* @param token the 0-th token of the type.
* @param ir the intermediate representation.
* @return the overridden package name of the type if set and supported, or {@link Ir#applicableNamespace()}.
*/
private String registerTypePackage(final Token token, final Ir ir)
private String registerTypesPackageName(final Token token, final Ir ir)
{
if (shouldSupportTypePackages && token.packageName() != null)
if (shouldSupportTypesPackageNames && token.packageName() != null)
{
typePackages.add(token.packageName());
packageNameByTypes.add(token.packageName());
outputManager.setPackageName(token.packageName());

return token.packageName();
}
return ir.applicableNamespace();
Expand All @@ -198,7 +199,7 @@ private String registerTypePackage(final Token token, final Ir ir)
*/
public void generate() throws IOException
{
typePackages.clear();
packageNameByTypes.clear();
generatePackageInfo();
generateTypeStubs();
generateMessageHeaderStub();
Expand Down Expand Up @@ -1238,7 +1239,7 @@ private void generateBitSet(final List<Token> tokens) throws IOException
final List<Token> choiceList = tokens.subList(1, tokens.size() - 1);
final String implementsString = implementsInterface(Flyweight.class.getSimpleName());

registerTypePackage(token, ir);
registerTypesPackageName(token, ir);
try (Writer out = outputManager.createOutput(decoderName))
{
final Encoding encoding = token.encoding();
Expand All @@ -1259,7 +1260,7 @@ private void generateBitSet(final List<Token> tokens) throws IOException
out.append("}\n");
}

registerTypePackage(token, ir);
registerTypesPackageName(token, ir);
try (Writer out = outputManager.createOutput(encoderName))
{
generateFixedFlyweightHeader(out, token, encoderName, implementsString, mutableBuffer, fqMutableBuffer);
Expand All @@ -1277,7 +1278,7 @@ private void generateFixedFlyweightHeader(
final String buffer,
final String fqBuffer) throws IOException
{
final String packageName = registerTypePackage(token, ir);
final String packageName = registerTypesPackageName(token, ir);
out.append(generateFileHeader(packageName, fqBuffer));
out.append(generateDeclaration(typeName, implementsString, token));
out.append(generateFixedFlyweightCode(typeName, token.encodedLength(), buffer));
Expand All @@ -1291,7 +1292,7 @@ private void generateCompositeFlyweightHeader(
final String fqBuffer,
final String implementsString) throws IOException
{
final String packageName = registerTypePackage(token, ir);
final String packageName = registerTypesPackageName(token, ir);
out.append(generateFileHeader(packageName, fqBuffer));
out.append(generateDeclaration(typeName, implementsString, token));
out.append(generateFixedFlyweightCode(typeName, token.encodedLength(), buffer));
Expand All @@ -1304,7 +1305,7 @@ private void generateEnum(final List<Token> tokens) throws IOException
final Encoding encoding = enumToken.encoding();
final String nullVal = encoding.applicableNullValue().toString();

final String packageName = registerTypePackage(enumToken, ir);
final String packageName = registerTypesPackageName(enumToken, ir);
try (Writer out = outputManager.createOutput(enumName))
{
out.append(generateEnumFileHeader(packageName));
Expand All @@ -1327,7 +1328,7 @@ private void generateComposite(final List<Token> tokens) throws IOException
final String decoderName = decoderName(compositeName);
final String encoderName = encoderName(compositeName);

registerTypePackage(token, ir);
registerTypesPackageName(token, ir);
try (Writer out = outputManager.createOutput(decoderName))
{
final String implementsString = implementsInterface(CompositeDecoderFlyweight.class.getSimpleName());
Expand Down Expand Up @@ -1376,7 +1377,7 @@ private void generateComposite(final List<Token> tokens) throws IOException
out.append("}\n");
}

registerTypePackage(token, ir);
registerTypesPackageName(token, ir);
try (Writer out = outputManager.createOutput(encoderName))
{
final String implementsString = implementsInterface(CompositeEncoderFlyweight.class.getSimpleName());
Expand Down Expand Up @@ -1629,8 +1630,8 @@ private CharSequence generateFileHeader(final String packageName, final String f
private CharSequence generateMainHeader(
final String packageName, final CodecType codecType, final boolean hasVarData)
{
final StringBuffer packageImports = new StringBuffer();
for (final String typePackage : typePackages)
final StringBuilder packageImports = new StringBuilder();
for (final String typePackage : packageNameByTypes)
{
packageImports.append("import ");
packageImports.append(typePackage);
Expand All @@ -1644,7 +1645,7 @@ private CharSequence generateMainHeader(
"package " + packageName + ";\n\n" +
"import " + fqMutableBuffer + ";\n" +
interfaceImportLine() +
packageImports.toString();
packageImports;
}
else
{
Expand All @@ -1657,7 +1658,7 @@ private CharSequence generateMainHeader(
(hasMutableBuffer ? "import " + fqMutableBuffer + ";\n" : "") +
(hasReadOnlyBuffer ? "import " + fqReadOnlyBuffer + ";\n" : "") +
interfaceImportLine() +
packageImports.toString();
packageImports;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,36 @@
*/
package uk.co.real_logic.sbe.generation.java;

import java.io.FilterWriter;
import java.io.IOException;
import java.io.Writer;
import org.agrona.collections.Object2NullableObjectHashMap;
import org.agrona.collections.Object2ObjectHashMap;
import org.agrona.generation.DynamicPackageOutputManager;
import org.agrona.generation.PackageOutputManager;

import java.io.FilterWriter;
import java.io.IOException;
import java.io.Writer;

/**
* Implementation of {@link DynamicPackageOutputManager} for Java.
*/
public class JavaOutputManager implements DynamicPackageOutputManager
{
private final String baseDirName;
private final PackageOutputManager basePackageOutputManager;
private final PackageOutputManager initialPackageOutputManager;
private PackageOutputManager actingPackageOutputManager;
private final Object2ObjectHashMap<String, PackageOutputManager> outputManagerCache
private final Object2ObjectHashMap<String, PackageOutputManager> outputManagerByPackageName
= new Object2NullableObjectHashMap<>();

/**
* Constructor.
*
* @param baseDirName the target directory
* @param packageName the initial package name
*/
public JavaOutputManager(final String baseDirName, final String packageName)
{
basePackageOutputManager = new PackageOutputManager(baseDirName, packageName);
actingPackageOutputManager = basePackageOutputManager;
initialPackageOutputManager = new PackageOutputManager(baseDirName, packageName);
actingPackageOutputManager = initialPackageOutputManager;
this.baseDirName = baseDirName;
}

Expand All @@ -51,17 +53,17 @@ public JavaOutputManager(final String baseDirName, final String packageName)
*/
public void setPackageName(final String packageName)
{
actingPackageOutputManager = outputManagerCache.get(packageName);
actingPackageOutputManager = outputManagerByPackageName.get(packageName);
if (actingPackageOutputManager == null)
{
actingPackageOutputManager = new PackageOutputManager(baseDirName, packageName);
outputManagerCache.put(packageName, actingPackageOutputManager);
outputManagerByPackageName.put(packageName, actingPackageOutputManager);
}
}

private void resetPackage()
{
actingPackageOutputManager = basePackageOutputManager;
actingPackageOutputManager = initialPackageOutputManager;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions sbe-tool/src/main/java/uk/co/real_logic/sbe/ir/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ public static class Builder
{
private Signal signal;
private String name;
private String packageName = null;
private String packageName;
private String referencedName;
private String description;
private int id = INVALID_ID;
Expand Down Expand Up @@ -413,7 +413,7 @@ public Builder name(final String name)
}

/**
* Package name for the Token. Default is null. Use for BEGIN_MESSAGE tokens for types that require an explicit
* Package name for the Token, default is null. Use for BEGIN_MESSAGE tokens for types that require an explicit
* package.
*
* @param packageName for the Token.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public EncodedDataType(final Node node)
/**
* Construct a new encodedDataType from XML Schema.
*
* @param node from the XML Schema Parsing
* @param node from the XML Schema Parsing.
* @param givenName for this node.
* @param referencedName of the type when created from a ref in a composite.
*/
Expand Down Expand Up @@ -201,13 +201,13 @@ private PrimitiveValue lookupValueRef(final Node node)
/**
* Construct a new EncodedDataType with direct values. Does not handle constant values.
*
* @param name of the type
* @param presence of the type
* @param description of the type or null
* @param semanticType of the type or null
* @param primitiveType of the EncodedDataType
* @param length of the EncodedDataType
* @param varLen of the EncodedDataType
* @param name of the type.
* @param presence of the type.
* @param description of the type or null.
* @param semanticType of the type or null.
* @param primitiveType of the EncodedDataType.
* @param length of the EncodedDataType.
* @param varLen of the EncodedDataType.
*/
public EncodedDataType(
final String name,
Expand All @@ -222,16 +222,16 @@ public EncodedDataType(
}

/**
* Construct a new EncodedDataType with direct values.Does not handle constant values.
* Construct a new EncodedDataType with direct values. Does not handle constant values.
*
* @param name of the type
* @param packageName of the type
* @param presence of the type
* @param description of the type or null
* @param semanticType of the type or null
* @param primitiveType of the EncodedDataType
* @param length of the EncodedDataType
* @param varLen of the EncodedDataType
* @param name of the type.
* @param packageName of the type.
* @param presence of the type.
* @param description of the type or null.
* @param semanticType of the type or null.
* @param primitiveType of the EncodedDataType.
* @param length of the EncodedDataType.
* @param varLen of the EncodedDataType.
*/
public EncodedDataType(
final String name,
Expand Down
Loading

0 comments on commit a52aef5

Please sign in to comment.