bytedeco/javacpp

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

开放

#752 创建于 2024年4月5日

 (0 条评论) (0 个反应) (0 位负责人)Java (620 个派生)batch import
bughelp wanted

仓库指标

星标
 (4,279 个星标)
PR 合并指标
 (30 天内没有已合并 PR)

描述

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);

贡献者指南