body.append(value.ret());
.condition(wasNullVariable) .ifTrue(constantFalse().ret()) .ifFalse(result.ret()));
private static void generateGetEstimatedSize(ClassDefinition definition, List<FieldDefinition> stateFields) { MethodDefinition method = definition.declareMethod(a(PUBLIC), "getEstimatedSize", type(long.class)); Variable estimatedSize = method.getScope().declareVariable(long.class, "estimatedSize"); method.getBody().append(estimatedSize.set(constantLong(0L))); for (FieldDefinition stateField : stateFields) { method.getBody() .append(estimatedSize.set( BytecodeExpressions.add( estimatedSize, method.getThis().getField(stateField).invoke("getEstimatedSize", long.class)))); } method.getBody().append(estimatedSize.ret()); }
body.append(size.ret());
body.append(value.ret());
body.append(value.ret());
@Test public void testGetField() throws Exception { Function<Scope, BytecodeNode> nodeGenerator = scope -> { Variable point = scope.declareVariable(Point.class, "point"); BytecodeExpression setPoint = point.set(newInstance(Point.class, constantInt(3), constantInt(7))); assertEquals(setPoint.toString(), "point = new Point(3, 7);"); return new BytecodeBlock() .append(setPoint) .append(point.ret()); }; assertBytecodeNode(nodeGenerator, type(Point.class), new Point(3, 7)); } }
public static void assertSetPoint(Function<BytecodeExpression, BytecodeExpression> setX) throws Exception { Function<Scope, BytecodeNode> nodeGenerator = scope -> { Variable point = scope.declareVariable(Point.class, "point"); BytecodeExpression setExpression = setX.apply(point); assertEquals(setExpression.toString(), "point.x = 42;"); return new BytecodeBlock() .append(point.set(newInstance(Point.class, constantInt(3), constantInt(7)))) .append(setExpression) .append(point.ret()); }; assertBytecodeNode(nodeGenerator, type(Point.class), new Point(42, 7)); }
.condition(wasNullVariable) .ifTrue(constantFalse().ret()) .ifFalse(result.ret()));
.condition(wasNullVariable) .ifTrue(constantFalse().ret()) .ifFalse(result.ret()));
/** * Defines the read method for a struct. */ private void defineReadStructMethod() { Parameter protocol = arg("protocol", TProtocolReader.class); MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), "read", structType, protocol) .addException(Exception.class); // ProtocolReader reader = new ProtocolReader(protocol); Variable reader = method.getScope().declareVariable(ProtocolReader.class, "reader"); method.getBody().append(reader.set(newInstance(ProtocolReader.class, protocol))); // read all of the data in to local variables Map<Short, Variable> structData = readFieldValues(method, reader); // build the struct Variable result = buildStruct(method, structData); // return the instance method.getBody().append(result.ret()); }
private static void generateGetEstimatedSize(ClassDefinition definition, List<FieldDefinition> stateFields) { MethodDefinition method = definition.declareMethod(a(PUBLIC), "getEstimatedSize", type(long.class)); Variable estimatedSize = method.getScope().declareVariable(long.class, "estimatedSize"); method.getBody().append(estimatedSize.set(constantLong(0L))); for (FieldDefinition stateField : stateFields) { method.getBody() .append(estimatedSize.set( BytecodeExpressions.add( estimatedSize, method.getThis().getField(stateField).invoke("getEstimatedSize", long.class)))); } method.getBody().append(estimatedSize.ret()); }
private static void generateGetEstimatedSize(ClassDefinition definition, List<FieldDefinition> stateFields) { MethodDefinition method = definition.declareMethod(a(PUBLIC), "getEstimatedSize", type(long.class)); Variable estimatedSize = method.getScope().declareVariable(long.class, "estimatedSize"); method.getBody().append(estimatedSize.set(constantLong(0L))); for (FieldDefinition stateField : stateFields) { method.getBody() .append(estimatedSize.set( BytecodeExpressions.add( estimatedSize, method.getThis().getField(stateField).invoke("getEstimatedSize", long.class)))); } method.getBody().append(estimatedSize.ret()); }
body.append(size.ret());
body.append(size.ret());
/** * Defines the read method for an union. */ private void defineReadUnionMethod() { Parameter protocol = arg("protocol", TProtocolReader.class); MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), "read", structType, protocol) .addException(Exception.class); // ProtocolReader reader = new ProtocolReader(protocol); Variable reader = method.getScope().declareVariable(type(ProtocolReader.class), "reader"); method.getBody().append(reader.set(newInstance(ProtocolReader.class, protocol))); // field id field. Variable fieldId = method.getScope().declareVariable(short.class, "fieldId"); method.getBody().append(fieldId.set(defaultValue(fieldId.getType()))); // read all of the data in to local variables Map<Short, Variable> unionData = readSingleFieldValue(method, reader, fieldId); // build the struct Variable result = buildUnion(method, fieldId, unionData); // return the instance method.getBody().append(result.ret()); }