lcm-proj / lcm

Lightweight Communications and Marshalling
GNU Lesser General Public License v2.1
944 stars 385 forks source link

lcm-java generates invalid code when messages with fields named "interface" are used #367

Open poolfeast-420 opened 2 years ago

poolfeast-420 commented 2 years ago

The following LCM definition will create an invalid .java file. Because it uses the reserved "interface" keyword as a variable name.

struct Demo {
    byte interface;
}

Creates the following Java code:

/* LCM type definition class file
 * This file was automatically generated by lcm-gen
 * DO NOT MODIFY BY HAND!!!!
 */

package lcm_packets;

import java.io.*;
import java.util.*;
import lcm.lcm.*;

public final class Demo implements lcm.lcm.LCMEncodable
{
    public byte interface; // <<< First error on this line

    public Demo()
    {
    }

    public static final long LCM_FINGERPRINT;
    public static final long LCM_FINGERPRINT_BASE = 0x7637e14b73433ea2L;

    static {
        LCM_FINGERPRINT = _hashRecursive(new ArrayList<Class<?>>());
    }

    public static long _hashRecursive(ArrayList<Class<?>> classes)
    {
        if (classes.contains(lcm_packets.Demo.class))
            return 0L;

        classes.add(lcm_packets.Demo.class);
        long hash = LCM_FINGERPRINT_BASE
            ;
        classes.remove(classes.size() - 1);
        return (hash<<1) + ((hash>>63)&1);
    }

    public void encode(DataOutput outs) throws IOException
    {
        outs.writeLong(LCM_FINGERPRINT);
        _encodeRecursive(outs);
    }

    public void _encodeRecursive(DataOutput outs) throws IOException
    {
        outs.writeByte(this.interface); 

    }

    public Demo(byte[] data) throws IOException
    {
        this(new LCMDataInputStream(data));
    }

    public Demo(DataInput ins) throws IOException
    {
        if (ins.readLong() != LCM_FINGERPRINT)
            throw new IOException("LCM Decode error: bad fingerprint");

        _decodeRecursive(ins);
    }

    public static lcm_packets.Demo _decodeRecursiveFactory(DataInput ins) throws IOException
    {
        lcm_packets.Demo o = new lcm_packets.Demo();
        o._decodeRecursive(ins);
        return o;
    }

    public void _decodeRecursive(DataInput ins) throws IOException
    {
        this.interface = ins.readByte();

    }

    public lcm_packets.Demo copy()
    {
        lcm_packets.Demo outobj = new lcm_packets.Demo();
        outobj.interface = this.interface;

        return outobj;
    }

}