/** * Defines the generics bridge method with untyped args to the type specific read method. */ private void defineReadBridgeMethod() { Parameter protocol = arg("protocol", TProtocolReader.class); MethodDefinition method = new MethodDefinition( classDefinition, a(PUBLIC, BRIDGE, SYNTHETIC), "read", type(Object.class), protocol) .addException(Exception.class); method.getBody().append(method.getThis() .invoke("read", structType, ImmutableList.of(protocol)) .ret()); classDefinition.addMethod(method); }
/** * Defines the generics bridge method with untyped args to the type specific write method. */ private void defineWriteBridgeMethod() { Parameter struct = arg("struct", Object.class); Parameter protocol = arg("protocol", TProtocolWriter.class); MethodDefinition method = new MethodDefinition( classDefinition, a(PUBLIC, BRIDGE, SYNTHETIC), "write", null, struct, protocol) .addException(Exception.class); method.getBody().append(method.getThis() .invoke("write", type(void.class), ImmutableList.of(structType, protocol.getType()), ImmutableList.of(struct.cast(structType), protocol)) .ret()); classDefinition.addMethod(method); }
/** * 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()); }
/** * 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()); }