Skip to content

Commit

Permalink
refactor(plc4j/codegen): remove IO classes
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Jan 26, 2022
1 parent 63c6301 commit be88a5e
Show file tree
Hide file tree
Showing 38 changed files with 145 additions and 566 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ protected List<Template> getSpecTemplates(Configuration freemarkerConfiguration)

@Override
protected List<Template> getComplexTypeTemplates(Configuration freemarkerConfiguration) throws IOException {
return Arrays.asList(
freemarkerConfiguration.getTemplate("templates/java/model-template.java.ftlh"),
freemarkerConfiguration.getTemplate("templates/java/io-template.java.ftlh"));
return List.of(freemarkerConfiguration.getTemplate("templates/java/model-template.java.ftlh"));
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ import java.math.BigInteger;

<#-- TODO: the code below implies that parserArguments will be null if not present... not pretty -->
<#if type.parserArguments.isPresent()><#assign parserArguments=type.allParserArguments.orElseThrow()></#if>
public<#if type.isDiscriminatedParentTypeDefinition()> abstract</#if> class ${type.name}<#if type.isDiscriminatedParentTypeDefinition()><T extends ${type.name}></#if><#if type.parentType??> extends ${type.parentType.name}<${type.name}></#if> implements Message, MessageInput<<#if type.isDiscriminatedParentTypeDefinition()>T<#else>${type.name}</#if>> {
public<#if type.isDiscriminatedParentTypeDefinition()> abstract</#if> class ${type.name}<#if type.isDiscriminatedParentTypeDefinition()><T extends ${type.name}></#if><#if type.parentType??> extends ${type.parentType.name}<${type.name}></#if> implements Message {

<#--
If this is a discriminated child type, we need to generate methods for accessing it's discriminator
Expand Down Expand Up @@ -481,32 +481,26 @@ public<#if type.isDiscriminatedParentTypeDefinition()> abstract</#if> class ${ty

<#-- The parse and serialize methods here are just proxies for forwardning the requests to static counterparts -->
<#if !type.isDiscriminatedChildTypeDefinition()>
@Override
public <#if type.isDiscriminatedParentTypeDefinition()>T<#else>${type.name}</#if> parse(ReadBuffer readBuffer, Object... args) throws ParseException {
public static ${type.name} staticParse(ReadBuffer readBuffer, Object... args) throws ParseException {
<#if parserArguments?has_content>
if((args == null) || (args.length != ${parserArguments?size})) {
throw new PlcRuntimeException("Wrong number of arguments, expected ${parserArguments?size}, but got " + args.length);
}
<#list parserArguments as parserArgument>
${helper.getLanguageTypeNameForTypeReference(parserArgument.type, false)} ${parserArgument.name};
if(args[${parserArgument?index}] instanceof ${helper.getLanguageTypeNameForTypeReference(parserArgument.type, false)}) {
${parserArgument.name} = (${helper.getLanguageTypeNameForTypeReference(parserArgument.type, false)}) args[${parserArgument?index}];
<#assign languageName=helper.getLanguageTypeNameForTypeReference(parserArgument.type, false)>
${languageName} ${parserArgument.name};
if(args[${parserArgument?index}] instanceof ${languageName}) {
${parserArgument.name} = (${languageName}) args[${parserArgument?index}];
<#if helper.isSimpleTypeReference(parserArgument.type)>
} else if (args[${parserArgument?index}] instanceof String) {
${parserArgument.name} = ${helper.getLanguageTypeNameForTypeReference(parserArgument.type, false)}.valueOf((String) args[${parserArgument?index}]);
${parserArgument.name} = ${languageName}.valueOf((String) args[${parserArgument?index}]);
</#if>
} else {
throw new PlcRuntimeException("Argument ${parserArgument?index} expected to be of type ${helper.getLanguageTypeNameForTypeReference(parserArgument.type, false)} or a string which is parseable but was " + args[${parserArgument?index}].getClass().getName());
throw new PlcRuntimeException("Argument ${parserArgument?index} expected to be of type ${languageName} or a string which is parseable but was " + args[${parserArgument?index}].getClass().getName());
}
</#list>
</#if>
return <#if type.isDiscriminatedParentTypeDefinition()>(T)</#if>${type.name}.staticParse(readBuffer<#if parserArguments?has_content>, <#list parserArguments as parserArgument>${parserArgument.name}<#sep>, </#sep></#list></#if>);
}

<#else>
@Override
public ${type.name} parse(ReadBuffer readBuffer, Object... args) throws ParseException {
return (${type.name}) super.parse(readBuffer, args);
return staticParse(readBuffer<#if parserArguments?has_content>, <#list parserArguments as parserArgument>${parserArgument.name}<#sep>, </#sep></#list></#if>);
}

</#if>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.apache.plc4x.java.abeth.field.AbEthFieldHandler;
import org.apache.plc4x.java.abeth.protocol.AbEthProtocolLogic;
import org.apache.plc4x.java.abeth.readwrite.CIPEncapsulationPacket;
import org.apache.plc4x.java.abeth.readwrite.io.CIPEncapsulationPacketIO;
import org.apache.plc4x.java.api.model.PlcField;
import org.apache.plc4x.java.spi.values.IEC61131ValueHandler;
import org.apache.plc4x.java.api.value.PlcValueHandler;
Expand Down Expand Up @@ -81,7 +80,7 @@ protected boolean awaitDisconnectComplete() {

@Override
protected ProtocolStackConfigurer<CIPEncapsulationPacket> getStackConfigurer() {
return SingleProtocolStackConfigurer.builder(CIPEncapsulationPacket.class, CIPEncapsulationPacketIO.class)
return SingleProtocolStackConfigurer.builder(CIPEncapsulationPacket.class, CIPEncapsulationPacket::staticParse)
.withProtocol(AbEthProtocolLogic.class)
.withPacketSizeEstimator(ByteLengthEstimator.class)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.apache.plc4x.java.ads.field.AdsFieldHandler;
import org.apache.plc4x.java.ads.protocol.AdsProtocolLogic;
import org.apache.plc4x.java.ads.readwrite.AmsTCPPacket;
import org.apache.plc4x.java.ads.readwrite.io.AmsTCPPacketIO;
import org.apache.plc4x.java.spi.values.IEC61131ValueHandler;
import org.apache.plc4x.java.api.value.PlcValueHandler;
import org.apache.plc4x.java.spi.configuration.Configuration;
Expand Down Expand Up @@ -99,7 +98,7 @@ protected boolean awaitDisconnectComplete() {

@Override
protected ProtocolStackConfigurer<AmsTCPPacket> getStackConfigurer() {
return SingleProtocolStackConfigurer.builder(AmsTCPPacket.class, AmsTCPPacketIO.class)
return SingleProtocolStackConfigurer.builder(AmsTCPPacket.class, AmsTCPPacket::staticParse)
.withPacketSizeEstimator(ByteLengthEstimator.class)
.withProtocol(AdsProtocolLogic.class)
.littleEndian()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import org.apache.commons.codec.binary.Hex;
import org.apache.plc4x.java.ads.readwrite.AmsTCPPacket;
import org.apache.plc4x.java.ads.readwrite.io.AmsTCPPacketIO;
import org.apache.plc4x.java.spi.generation.ByteOrder;
import org.apache.plc4x.java.spi.generation.ReadBuffer;
import org.apache.plc4x.java.spi.generation.ReadBufferByteBased;
Expand All @@ -31,7 +30,7 @@ public static void main(String[] args) throws Exception {
byte[] data = Hex.decodeHex("0000f1000000c0a817c80101feffc0a817140101530309000500d1000000000000002f00000000000000c90000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012a00a07d0f7e1c8e31489f8cf1006b3604b888defc88f64eb6fbb217d11fc5cf5f148b0abf05407c941a93b8263301db0f4940d66875727a0000000000d2040000ab6459032bbf7e03b888defcb8a5b249044de82ee03a2ab8a577006f006c0066000000488bf0488bfa33c9");
//byte[] data = Hex.decodeHex("0000f1000000c0a817c80101feffc0a817140101530309000500d1000000000000002f00000000000000c90000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012a00a07d0f7e1c8e31489f8cf1006b3604b888defc");
ReadBuffer readBuffer = new ReadBufferByteBased(data, ByteOrder.LITTLE_ENDIAN);
final AmsTCPPacket amsTCPPacket = AmsTCPPacketIO.staticParse(readBuffer);
final AmsTCPPacket amsTCPPacket = AmsTCPPacket.staticParse(readBuffer);
System.out.println(amsTCPPacket);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.apache.plc4x.java.bacnetip.field.BacNetIpFieldHandler;
import org.apache.plc4x.java.bacnetip.protocol.BacNetIpProtocolLogic;
import org.apache.plc4x.java.bacnetip.readwrite.BVLC;
import org.apache.plc4x.java.bacnetip.readwrite.io.BVLCIO;
import org.apache.plc4x.java.spi.configuration.Configuration;
import org.apache.plc4x.java.spi.connection.GeneratedDriverBase;
import org.apache.plc4x.java.spi.connection.ProtocolStackConfigurer;
Expand Down Expand Up @@ -85,7 +84,7 @@ protected PlcValueHandler getValueHandler() {

@Override
protected ProtocolStackConfigurer<BVLC> getStackConfigurer() {
return SingleProtocolStackConfigurer.builder(BVLC.class, BVLCIO.class)
return SingleProtocolStackConfigurer.builder(BVLC.class, BVLC::staticParse)
.withProtocol(BacNetIpProtocolLogic.class)
.withPacketSizeEstimator(ByteLengthEstimator.class)
.withCorruptPacketRemover(CorruptPackageCleaner.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import org.apache.commons.codec.binary.Hex;
import org.apache.plc4x.java.bacnetip.readwrite.BVLC;
import org.apache.plc4x.java.bacnetip.readwrite.io.BVLCIO;
import org.apache.plc4x.java.spi.generation.ReadBuffer;
import org.apache.plc4x.java.spi.generation.ReadBufferByteBased;

Expand All @@ -29,7 +28,7 @@ public class ManualBacNetDecoder {
public static void main(String[] args) throws Exception {
final byte[] bytes = Hex.decodeHex("810a002b01040205790109011c020000142c000002f93a06b24e09552e44434a00002f096f2e8204002f4f");
ReadBuffer readBuffer = new ReadBufferByteBased(bytes);
final BVLC packet = BVLCIO.staticParse(readBuffer);
final BVLC packet = BVLC.staticParse(readBuffer);
System.out.println(packet);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.apache.plc4x.java.bacnetip.readwrite.*;
import org.apache.plc4x.java.bacnetip.readwrite.io.BVLCIO;
import org.apache.plc4x.java.spi.generation.ParseException;
import org.apache.plc4x.java.spi.generation.ReadBufferByteBased;
import org.apache.plc4x.java.spi.generation.SerializationException;
Expand Down Expand Up @@ -5165,7 +5164,7 @@ public BVLC nextBVLC(Integer ensurePackageNumber) throws NotOpenException, Parse
byte[] rawData = udpPacket.getPayload().getRawData();
LOGGER.info("Reading BVLC from:\n{}", Hex.dump(rawData));
try {
return BVLCIO.staticParse(new ReadBufferByteBased(rawData));
return BVLC.staticParse(new ReadBufferByteBased(rawData));
} catch (ParseException e) {
throw new ParseException(String.format("Caught at current package number: %d. Packages read so far %d", currentPackageNumber, readPackages), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.apache.plc4x.java.canopen.readwrite.CANOpenFrame;
import org.apache.plc4x.java.canopen.readwrite.CANOpenPayload;
import org.apache.plc4x.java.canopen.readwrite.CANOpenService;
import org.apache.plc4x.java.canopen.readwrite.io.CANOpenPayloadIO;
import org.apache.plc4x.java.spi.generation.ByteOrder;
import org.apache.plc4x.java.spi.generation.Message;
import org.apache.plc4x.java.spi.generation.ParseException;
import org.apache.plc4x.java.spi.generation.SerializationException;
import org.apache.plc4x.java.spi.generation.WriteBufferByteBased;
import org.apache.plc4x.java.transport.can.CANFrameBuilder;
Expand All @@ -50,7 +48,7 @@ public CANOpenFrameDataHandler(Supplier<CANFrameBuilder<Message>> builder) {
public CANOpenFrame fromCAN(FrameData frame) {
CANOpenService service = StaticHelper.serviceId((short) frame.getNodeId());
int nodeId = Math.abs(service.getMin() - frame.getNodeId());
return new CANOpenFrame((short) nodeId, service, frame.read(new CANOpenPayloadIO(), service));
return new CANOpenFrame((short) nodeId, service, frame.read(CANOpenPayload::staticParse, service));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class CANOpenPayloadTest extends ParserSerializerTestsuiteRunner {

public CANOpenPayloadTest() {
super("/protocols/canopen/CANOpenPayloadTestSuite.xml", true);
super("/protocols/canopen/CANOpenPayloadTestSuite.xml");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@
import java.util.function.ToIntFunction;

import org.apache.plc4x.java.canopen.readwrite.CANOpenFrame;
import org.apache.plc4x.java.canopen.readwrite.io.CANOpenFrameIO;
import org.apache.plc4x.java.canopen.transport.CANOpenFrameDataAdapter;
import org.apache.plc4x.java.canopen.transport.IdentityCANOpenFrameBuilder;
import org.apache.plc4x.java.spi.configuration.Configuration;
import org.apache.plc4x.java.spi.generation.MessageIO;
import org.apache.plc4x.java.spi.generation.MessageInput;
import org.apache.plc4x.java.transport.can.CANFrameBuilder;
import org.apache.plc4x.java.transport.can.CANTransport;
Expand All @@ -49,7 +47,7 @@ public Class<CANOpenFrame> getMessageType() {

@Override
public MessageInput<CANOpenFrame> getMessageInput(Configuration configuration) {
return new CANOpenFrameIO();
return CANOpenFrame::staticParse;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ Licensed to the Apache Software Foundation (ASF) under one
package org.apache.plc4x.java.canopen.transport;

import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
import org.apache.plc4x.java.canopen.readwrite.CANOpenPayload;
import org.apache.plc4x.java.canopen.readwrite.utils.StaticHelper;
import org.apache.plc4x.java.canopen.readwrite.CANOpenFrame;
import org.apache.plc4x.java.canopen.readwrite.CANOpenService;
import org.apache.plc4x.java.canopen.readwrite.io.CANOpenPayloadIO;
import org.apache.plc4x.java.spi.generation.ByteOrder;
import org.apache.plc4x.java.spi.generation.ParseException;
import org.apache.plc4x.java.spi.generation.ReadBufferByteBased;
Expand Down Expand Up @@ -56,7 +56,7 @@ public CANFrameBuilder<CANOpenFrame> withData(byte[] data) {
public CANOpenFrame create() {
try {
return new CANOpenFrame(
nodeId, service, CANOpenPayloadIO.staticParse(new ReadBufferByteBased(data, ByteOrder.LITTLE_ENDIAN), service)
nodeId, service, CANOpenPayload.staticParse(new ReadBufferByteBased(data, ByteOrder.LITTLE_ENDIAN), service)
);
} catch (ParseException e) {
throw new PlcRuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.apache.plc4x.java.eip.readwrite.configuration.EIPConfiguration;
import org.apache.plc4x.java.eip.readwrite.field.EipField;
import org.apache.plc4x.java.eip.readwrite.field.EipFieldHandler;
import org.apache.plc4x.java.eip.readwrite.io.EipPacketIO;
import org.apache.plc4x.java.eip.readwrite.protocol.EipProtocolLogic;
import org.apache.plc4x.java.spi.values.IEC61131ValueHandler;
import org.apache.plc4x.java.api.value.PlcValueHandler;
Expand Down Expand Up @@ -88,7 +87,7 @@ protected boolean canWrite() {

@Override
protected ProtocolStackConfigurer<EipPacket> getStackConfigurer() {
return SingleProtocolStackConfigurer.builder(EipPacket.class, EipPacketIO.class)
return SingleProtocolStackConfigurer.builder(EipPacket.class, EipPacket::staticParse)
.withProtocol(EipProtocolLogic.class)
.withPacketSizeEstimator(ByteLengthEstimator.class)
.littleEndian()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.apache.plc4x.java.eip.readwrite.*;
import org.apache.plc4x.java.eip.readwrite.configuration.EIPConfiguration;
import org.apache.plc4x.java.eip.readwrite.field.EipField;
import org.apache.plc4x.java.eip.readwrite.io.CipServiceIO;
import org.apache.plc4x.java.spi.ConversationContext;
import org.apache.plc4x.java.spi.Plc4xProtocolBase;
import org.apache.plc4x.java.spi.configuration.HasConfiguration;
Expand Down Expand Up @@ -292,7 +291,7 @@ else if (p instanceof MultipleServiceResponse) {
ReadBuffer serviceBuf = new ReadBufferByteBased(read.getBytes(offset, offset + length), org.apache.plc4x.java.spi.generation.ByteOrder.LITTLE_ENDIAN);
CipService service = null;
try {
service = CipServiceIO.staticParse(read, length);
service = CipService.staticParse(read, length);
arr.add(service);
} catch (ParseException e) {
throw new PlcRuntimeException(e);
Expand Down Expand Up @@ -503,7 +502,7 @@ private PlcResponse decodeWriteResponse(CipService p, PlcWriteRequest writeReque
ReadBuffer serviceBuf = new ReadBufferByteBased(read.getBytes(offset, length), org.apache.plc4x.java.spi.generation.ByteOrder.LITTLE_ENDIAN);
CipService service = null;
try {
service = CipServiceIO.staticParse(read, length);
service = CipService.staticParse(read, length);
arr.add(service);
} catch (ParseException e) {
throw new PlcRuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.apache.plc4x.java.firmata.readwrite.context.FirmataDriverContext;
import org.apache.plc4x.java.firmata.readwrite.field.FirmataField;
import org.apache.plc4x.java.firmata.readwrite.field.FirmataFieldHandler;
import org.apache.plc4x.java.firmata.readwrite.io.FirmataMessageIO;
import org.apache.plc4x.java.firmata.readwrite.protocol.FirmataProtocolLogic;
import org.apache.plc4x.java.spi.values.IEC61131ValueHandler;
import org.apache.plc4x.java.api.value.PlcValueHandler;
Expand Down Expand Up @@ -90,7 +89,7 @@ protected boolean awaitDisconnectComplete() {

@Override
protected ProtocolStackConfigurer<FirmataMessage> getStackConfigurer() {
return SingleProtocolStackConfigurer.builder(FirmataMessage.class, FirmataMessageIO.class)
return SingleProtocolStackConfigurer.builder(FirmataMessage.class, FirmataMessage::staticParse)
.withProtocol(FirmataProtocolLogic.class)
.withDriverContext(FirmataDriverContext.class)
.withPacketSizeEstimator(ByteLengthEstimator.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.apache.plc4x.java.knxnetip.context.KnxNetIpDriverContext;
import org.apache.plc4x.java.knxnetip.field.KnxNetIpField;
import org.apache.plc4x.java.knxnetip.readwrite.KnxNetIpMessage;
import org.apache.plc4x.java.knxnetip.readwrite.io.KnxNetIpMessageIO;
import org.apache.plc4x.java.spi.configuration.Configuration;
import org.apache.plc4x.java.knxnetip.field.KnxNetIpFieldHandler;
import org.apache.plc4x.java.knxnetip.protocol.KnxNetIpProtocolLogic;
Expand Down Expand Up @@ -97,7 +96,7 @@ protected PlcValueHandler getValueHandler() {

@Override
protected ProtocolStackConfigurer<KnxNetIpMessage> getStackConfigurer() {
return SingleProtocolStackConfigurer.builder(KnxNetIpMessage.class, KnxNetIpMessageIO.class)
return SingleProtocolStackConfigurer.builder(KnxNetIpMessage.class, KnxNetIpMessage::staticParse)
.withProtocol(KnxNetIpProtocolLogic.class)
.withDriverContext(KnxNetIpDriverContext.class)
.withPacketSizeEstimator(PacketSizeEstimator.class)
Expand Down
Loading

0 comments on commit be88a5e

Please sign in to comment.