Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Marking an interface with virtualize() causes compilation failure when returning byte array #752

Open
codeinred opened this issue Apr 5, 2024 · 0 comments

Comments

@codeinred
Copy link

Summary: if an interface has methods that return byte[] in java, the class will fail to compile if the declaration of the interface in the InfoMap is marked with .virtualize().

If .virtualize() is not added, then everything compiles.

This problem seems to occur specifically with arrays, such as byte[]. Returning a BytePointer, for example, does not cause a compile error.

Minimal C++ code

Suppose you have the following interface in C++:

namespace vola {
    using Bytes = std::vector<char>;

    /// Format specifier used to determine how serialization should be done
    enum class FormatIO {
        JSON,    ///< Format as json
        YAML,    ///< Format as YAML
        MSGPACK, ///< Format as MSGPACK
    };


    /// Base class for Serializable types
    class DLL_PUBLIC SerializableI {
       public:
        virtual ~SerializableI() = default;

        /// Convert this object to an array of bytes
        /// @param format format specifier
        /// @return serialized representation of object
        virtual Bytes toBytes(FormatIO format) const = 0;
    };

    using SerializableH = std::shared_ptr<SerializableI>;

    DLL_PUBLIC SerializableH makeTestObject(std::string input);
} // namespace vola

Minimal InfoMapper code

public class volar implements InfoMapper {
    public void map(InfoMap infoMap) {
        infoMap
            .put(new Info("DLL_PUBLIC", "DLL_LOCAL").cppTypes().annotations())
            .put(new Info("std::vector<char>", "Bytes")
                     .annotations("@StdVector")
                     .valueTypes(
                         "@Cast({\"char*\", \"std::vector<char>\"}) byte[]")
                     .pointerTypes("byte[]"))
            .put(new Info("vola::FormatIO").enumerate())
            .put(new Info("vola::SerializableI")
                .pointerTypes("Serializable")
                .virtualize()) // Adding .virtualize() causes the compile error in jnivolar.cpp
            .put(new Info("vola::SerializableH")
                     .annotations(
                         "@Cast(\"const vola::SerializableI*\") @SharedPtr")
                     .pointerTypes("Serializable"));
    }
}

Compile error

/Users/alecto/scratch/javacpp-minimal/target/native/com/voladynamics/volar/macosx-arm64/jnivolar.cpp:1091:21: error: incompatible pointer types assigning to 'jbyteArray' (aka '_jbyteArray *') from 'jobject' (aka '_jobject *')
        rarg = env->CallObjectMethodA(obj, mid, args);
               ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/alecto/scratch/javacpp-minimal/target/native/com/voladynamics/volar/macosx-arm64/jnivolar.cpp:1111:41: error: use of undeclared identifier 'rptr'
    return VectorAdapter< char >((char*)rptr, rsize, rowner);
                                        ^
/Users/alecto/scratch/javacpp-minimal/target/native/com/voladynamics/volar/macosx-arm64/jnivolar.cpp:1111:47: error: use of undeclared identifier 'rsize'
    return VectorAdapter< char >((char*)rptr, rsize, rowner);
                                              ^
/Users/alecto/scratch/javacpp-minimal/target/native/com/voladynamics/volar/macosx-arm64/jnivolar.cpp:1111:54: error: use of undeclared identifier 'rowner'
    return VectorAdapter< char >((char*)rptr, rsize, rowner);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants