1616package uk .co .real_logic .sbe .generation .java ;
1717
1818import uk .co .real_logic .sbe .PrimitiveType ;
19+ import uk .co .real_logic .sbe .codec .java .DirectBuffer ;
1920import uk .co .real_logic .sbe .generation .CodeGenerator ;
2021import uk .co .real_logic .sbe .generation .OutputManager ;
2122import uk .co .real_logic .sbe .ir .*;
@@ -37,15 +38,39 @@ public class JavaGenerator implements CodeGenerator
3738
3839 private final Ir ir ;
3940 private final OutputManager outputManager ;
41+ private final String fullyQualifiedBufferImplementation ;
42+ private final String bufferImplementation ;
4043
41- public JavaGenerator (final Ir ir , final String bufferImplementation , final OutputManager outputManager )
44+ public JavaGenerator (
45+ final Ir ir , final String fullyQualifiedBufferImplementation , final OutputManager outputManager )
4246 throws IOException
4347 {
4448 Verify .notNull (ir , "ir" );
4549 Verify .notNull (outputManager , "outputManager" );
4650
4751 this .ir = ir ;
4852 this .outputManager = outputManager ;
53+ this .bufferImplementation = validateBufferImplementation (fullyQualifiedBufferImplementation );
54+ this .fullyQualifiedBufferImplementation = fullyQualifiedBufferImplementation ;
55+ }
56+
57+ private String validateBufferImplementation (final String fullyQualifiedBufferImplementation )
58+ {
59+ try
60+ {
61+ final Class <?> cls = Class .forName (fullyQualifiedBufferImplementation );
62+ if (!cls .isAssignableFrom (DirectBuffer .class ))
63+ {
64+ throw new IllegalArgumentException (
65+ fullyQualifiedBufferImplementation + " doesn't implement " + DirectBuffer .class .getName ());
66+ }
67+ return cls .getSimpleName ();
68+ }
69+ catch (ClassNotFoundException e )
70+ {
71+ throw new IllegalArgumentException (
72+ "Unable to validate " + fullyQualifiedBufferImplementation + " because it can't be found" , e );
73+ }
4974 }
5075
5176 public void generateMessageHeaderStub () throws IOException
@@ -206,28 +231,11 @@ private void generateGroupClassHeader(
206231 final String dimensionsClassName = formatClassName (tokens .get (index + 1 ).name ());
207232 final Integer dimensionHeaderSize = Integer .valueOf (tokens .get (index + 1 ).size ());
208233
209- sb .append (String .format (
210- "\n " +
211- indent + "public static class %1$s implements Iterable<%1$s>, java.util.Iterator<%1$s>\n " +
212- indent + "{\n " +
213- indent + " private static final int HEADER_SIZE = %2$d;\n " +
214- indent + " private final %3$s dimensions = new %3$s();\n " +
215- indent + " private %4$s parentMessage;\n " +
216- indent + " private DirectBuffer buffer;\n " +
217- indent + " private int blockLength;\n " +
218- indent + " private int actingVersion;\n " +
219- indent + " private int count;\n " +
220- indent + " private int index;\n " +
221- indent + " private int offset;\n \n " ,
222- formatClassName (groupName ),
223- dimensionHeaderSize ,
224- dimensionsClassName ,
225- parentMessageClassName
226- ));
234+ generateClassDeclaration (sb , groupName , parentMessageClassName , indent , dimensionsClassName , dimensionHeaderSize );
227235
228236 sb .append (String .format (
229237 indent + " public void wrapForDecode(\n " +
230- indent + " final %s parentMessage, final DirectBuffer buffer, final int actingVersion)\n " +
238+ indent + " final %s parentMessage, final %s buffer, final int actingVersion)\n " +
231239 indent + " {\n " +
232240 indent + " this.parentMessage = parentMessage;\n " +
233241 indent + " this.buffer = buffer;\n " +
@@ -238,15 +246,16 @@ private void generateGroupClassHeader(
238246 indent + " index = -1;\n " +
239247 indent + " parentMessage.limit(parentMessage.limit() + HEADER_SIZE);\n " +
240248 indent + " }\n \n " ,
241- parentMessageClassName
249+ parentMessageClassName ,
250+ bufferImplementation
242251 ));
243252
244253 final Integer blockLength = Integer .valueOf (tokens .get (index ).size ());
245254 final String javaTypeForBlockLength = javaTypeName (tokens .get (index + 2 ).encoding ().primitiveType ());
246255 final String javaTypeForNumInGroup = javaTypeName (tokens .get (index + 3 ).encoding ().primitiveType ());
247256
248257 sb .append (String .format (
249- indent + " public void wrapForEncode(final %1$s parentMessage, final DirectBuffer buffer, final int count)\n " +
258+ indent + " public void wrapForEncode(final %1$s parentMessage, final %5$s buffer, final int count)\n " +
250259 indent + " {\n " +
251260 indent + " this.parentMessage = parentMessage;\n " +
252261 indent + " this.buffer = buffer;\n " +
@@ -262,7 +271,8 @@ private void generateGroupClassHeader(
262271 parentMessageClassName ,
263272 javaTypeForBlockLength ,
264273 blockLength ,
265- javaTypeForNumInGroup
274+ javaTypeForNumInGroup ,
275+ bufferImplementation
266276 ));
267277
268278 sb .append (String .format (
@@ -323,6 +333,32 @@ private void generateGroupClassHeader(
323333 ));
324334 }
325335
336+ private void generateClassDeclaration (
337+ final StringBuilder sb , final String groupName , final String parentMessageClassName , final String indent ,
338+ final String dimensionsClassName , final Integer dimensionHeaderSize )
339+ {
340+
341+ sb .append (String .format (
342+ "\n " +
343+ indent + "public static class %1$s implements Iterable<%1$s>, java.util.Iterator<%1$s>\n " +
344+ indent + "{\n " +
345+ indent + " private static final int HEADER_SIZE = %2$d;\n " +
346+ indent + " private final %3$s dimensions = new %3$s();\n " +
347+ indent + " private %4$s parentMessage;\n " +
348+ indent + " private %5$s buffer;\n " +
349+ indent + " private int blockLength;\n " +
350+ indent + " private int actingVersion;\n " +
351+ indent + " private int count;\n " +
352+ indent + " private int index;\n " +
353+ indent + " private int offset;\n \n " ,
354+ formatClassName (groupName ),
355+ dimensionHeaderSize ,
356+ dimensionsClassName ,
357+ parentMessageClassName ,
358+ bufferImplementation
359+ ));
360+ }
361+
326362 private CharSequence generateGroupProperty (final String groupName , final Token token , final String indent )
327363 {
328364 final StringBuilder sb = new StringBuilder ();
@@ -651,8 +687,10 @@ private CharSequence generateFileHeader(final String packageName)
651687 return String .format (
652688 "/* Generated SBE (Simple Binary Encoding) message codec */\n " +
653689 "package %s;\n \n " +
654- "import uk.co.real_logic.sbe.codec.java.*;\n \n " ,
655- packageName
690+ "import uk.co.real_logic.sbe.codec.java.*;\n " +
691+ "import %s;\n \n " ,
692+ packageName ,
693+ fullyQualifiedBufferImplementation
656694 );
657695 }
658696
@@ -1135,10 +1173,10 @@ private CharSequence generateByteLiteralList(final byte[] bytes)
11351173 private CharSequence generateFixedFlyweightCode (final String className , final int size )
11361174 {
11371175 return String .format (
1138- " private DirectBuffer buffer;\n " +
1176+ " private %3$s buffer;\n " +
11391177 " private int offset;\n " +
11401178 " private int actingVersion;\n \n " +
1141- " public %s wrap(final DirectBuffer buffer, final int offset, final int actingVersion)\n " +
1179+ " public %1$ s wrap(final %3$s buffer, final int offset, final int actingVersion)\n " +
11421180 " {\n " +
11431181 " this.buffer = buffer;\n " +
11441182 " this.offset = offset;\n " +
@@ -1147,10 +1185,11 @@ private CharSequence generateFixedFlyweightCode(final String className, final in
11471185 " }\n \n " +
11481186 " public int size()\n " +
11491187 " {\n " +
1150- " return %d;\n " +
1188+ " return %2$ d;\n " +
11511189 " }\n " ,
11521190 className ,
1153- Integer .valueOf (size )
1191+ Integer .valueOf (size ),
1192+ bufferImplementation
11541193 );
11551194 }
11561195
@@ -1168,7 +1207,7 @@ private CharSequence generateMessageFlyweightCode(final String className, final
11681207 " public static final %5$s SCHEMA_ID = %6$s;\n " +
11691208 " public static final %7$s SCHEMA_VERSION = %8$s;\n \n " +
11701209 " private final %9$s parentMessage = this;\n " +
1171- " private DirectBuffer buffer;\n " +
1210+ " private %11$s buffer;\n " +
11721211 " private int offset;\n " +
11731212 " private int limit;\n " +
11741213 " private int actingBlockLength;\n " +
@@ -1198,7 +1237,7 @@ private CharSequence generateMessageFlyweightCode(final String className, final
11981237 " {\n " +
11991238 " return offset;\n " +
12001239 " }\n \n " +
1201- " public %9$s wrapForEncode(final DirectBuffer buffer, final int offset)\n " +
1240+ " public %9$s wrapForEncode(final %11$s buffer, final int offset)\n " +
12021241 " {\n " +
12031242 " this.buffer = buffer;\n " +
12041243 " this.offset = offset;\n " +
@@ -1208,7 +1247,7 @@ private CharSequence generateMessageFlyweightCode(final String className, final
12081247 " return this;\n " +
12091248 " }\n \n " +
12101249 " public %9$s wrapForDecode(\n " +
1211- " final DirectBuffer buffer, final int offset, final int actingBlockLength, final int actingVersion)\n " +
1250+ " final %11$s buffer, final int offset, final int actingBlockLength, final int actingVersion)\n " +
12121251 " {\n " +
12131252 " this.buffer = buffer;\n " +
12141253 " this.offset = offset;\n " +
@@ -1239,7 +1278,8 @@ private CharSequence generateMessageFlyweightCode(final String className, final
12391278 schemaVersionType ,
12401279 generateLiteral (ir .headerStructure ().schemaVersionType (), Integer .toString (token .version ())),
12411280 className ,
1242- semanticType
1281+ semanticType ,
1282+ bufferImplementation
12431283 );
12441284 }
12451285
0 commit comments