Search code examples
androidprotocol-buffersj2objc

J2Objc, Protobuf nano and Swift


I have Java classes being translated to Objective C (J2Objc using the cradle plugin). The simple Android app and Unit tests work fine. The same simple Objective C iOS app also works perfectly. However, when I try to write similar code in a Swift project I cannot access instance variables, find methods, etc. This is relatively new to me so I may be missing something basic.

Here's the code in Objective C. Works perfectly:

SHRRefreshAppointments *mTest = [[SHRRefreshAppointments alloc] initWithId:nil withBoolean:true];
[mTest run];
SHREventListResp *mResp = [SHREventListResp parseFromWithByteArray:mTest->mResponseData_];
NSLog(@"%@", mResp.description);

Same code in Swift - I cannot find the instance variable mResponseData which is readily available in Objective C:

let mTest : SHRRefreshAppointments = SHRRefreshAppointments(id: nil, withBoolean: true)
mTest.run()
var mResp : SHREventListResp = SHREventListResp.parseFromWithByteArray(mTest.)

Here are the contents of the bridging header:

#include "JreEmulation.h"
#include "J2ObjC_header.h"

#import "com/gcatconsult/shared/remote/AppConstants.h"
#import "com/gcatconsult/shared/remote/AppUtils.h"
#import "com/gcatconsult/shared/messages/nano/Resp.h"
#import "com/gcatconsult/shared/messages/nano//Req.h"
#import "com/gcatconsult/shared/messages/nano/EventListResp.h"
#import "com/gcatconsult/shared/messages/nano/EventListReq.h"
#import "com/gcatconsult/shared/messages/nano/Event.h"
#import "com/gcatconsult/shared/remote/RefreshAppointments.h"
#import "com/gcatconsult/shared/remote/NetworkBase.h"

SHRRefreshAppointments header:

//
//  Generated by the J2ObjC translator.  DO NOT EDIT!
//  source: /Users/gabrielchoza/AndroidStudioProjects/GcatMobile/shared/src/main/java/com/gcatconsult/shared/remote/RefreshAppointments.java
//

#include "J2ObjC_header.h"

#pragma push_macro("INCLUDE_ALL_ComGcatconsultSharedRemoteRefreshAppointments")
#ifdef RESTRICT_ComGcatconsultSharedRemoteRefreshAppointments
#define INCLUDE_ALL_ComGcatconsultSharedRemoteRefreshAppointments 0
#else
#define INCLUDE_ALL_ComGcatconsultSharedRemoteRefreshAppointments 1
#endif
#undef RESTRICT_ComGcatconsultSharedRemoteRefreshAppointments

#if !defined (SHRRefreshAppointments_) && (INCLUDE_ALL_ComGcatconsultSharedRemoteRefreshAppointments ||     defined(INCLUDE_SHRRefreshAppointments))
#define SHRRefreshAppointments_

#define RESTRICT_ComGcatconsultSharedRemoteNetworkBase 1
#define INCLUDE_SHRNetworkBase 1
#include "com/gcatconsult/shared/remote/NetworkBase.h"

#define RESTRICT_JavaLangRunnable 1
#define INCLUDE_JavaLangRunnable 1
#include "java/lang/Runnable.h"

@interface SHRRefreshAppointments : SHRNetworkBase < JavaLangRunnable >

#pragma mark Public

- (instancetype)initWithId:(id)requestData
           withBoolean:(jboolean)asyncCall;

#pragma mark Protected

- (void)postProcessExcecute;

@end

J2OBJC_EMPTY_STATIC_INIT(SHRRefreshAppointments)

FOUNDATION_EXPORT void SHRRefreshAppointments_initWithId_withBoolean_(SHRRefreshAppointments *self, id requestData, jboolean asyncCall);

FOUNDATION_EXPORT SHRRefreshAppointments *new_SHRRefreshAppointments_initWithId_withBoolean_(id requestData, jboolean asyncCall) NS_RETURNS_RETAINED;

FOUNDATION_EXPORT SHRRefreshAppointments *create_SHRRefreshAppointments_initWithId_withBoolean_(id requestData, jboolean asyncCall);

J2OBJC_TYPE_LITERAL_HEADER(SHRRefreshAppointments)

@compatibility_alias ComGcatconsultSharedRemoteRefreshAppointments SHRRefreshAppointments;

#endif

#pragma pop_macro("INCLUDE_ALL_ComGcatconsultSharedRemoteRefreshAppointments")

The SHRNetworkBase superclass:

//
  //  Generated by the J2ObjC translator.  DO NOT EDIT!
  //  source: /Users/gabrielchoza/AndroidStudioProjects/GcatMobile/shared/src/main/java/com/gcatconsult/shared/remote/NetworkBase.java
  //

  #include "J2ObjC_header.h"

  #pragma push_macro("INCLUDE_ALL_ComGcatconsultSharedRemoteNetworkBase")
  #ifdef RESTRICT_ComGcatconsultSharedRemoteNetworkBase
  #define INCLUDE_ALL_ComGcatconsultSharedRemoteNetworkBase 0
  #else
  #define INCLUDE_ALL_ComGcatconsultSharedRemoteNetworkBase 1
  #endif
  #undef RESTRICT_ComGcatconsultSharedRemoteNetworkBase

  #if !defined (SHRNetworkBase_) && (INCLUDE_ALL_ComGcatconsultSharedRemoteNetworkBase || defined(INCLUDE_SHRNetworkBase))
  #define SHRNetworkBase_

  #define RESTRICT_JavaLangRunnable 1
  #define INCLUDE_JavaLangRunnable 1
  #include "java/lang/Runnable.h"

  @class IOSByteArray;
  @class JavaLangInteger;

  @interface SHRNetworkBase : NSObject < JavaLangRunnable > {
   @public
    NSString *mGetPatientPath_;
    NSString *mStringURL_;
    IOSByteArray *mRequestData_;
    IOSByteArray *mResponseData_;
    jboolean mShouldCompress_;
    jint mCurrentCall_;
    jboolean mAsyncCall_;
  }

  #pragma mark Public

  - (instancetype)initWithNSString:(NSString *)callPath
                       withBoolean:(jboolean)shouldCompress
               withJavaLangInteger:(JavaLangInteger *)currentCall
                       withBoolean:(jboolean)async;

  - (void)run;

  #pragma mark Protected

  - (void)postProcessExcecute;

  - (id)sendAsyncServerRequestWithNSString:(NSString *)stringURL
                                    withId:(id)requestData
                               withBoolean:(jboolean)shouldCompress
                       withJavaLangInteger:(JavaLangInteger *)currentCall;

  @end

  J2OBJC_EMPTY_STATIC_INIT(SHRNetworkBase)

  J2OBJC_FIELD_SETTER(SHRNetworkBase, mGetPatientPath_, NSString *)
  J2OBJC_FIELD_SETTER(SHRNetworkBase, mStringURL_, NSString *)
  J2OBJC_FIELD_SETTER(SHRNetworkBase, mRequestData_, IOSByteArray *)
  J2OBJC_FIELD_SETTER(SHRNetworkBase, mResponseData_, IOSByteArray *)

  FOUNDATION_EXPORT void SHRNetworkBase_initWithNSString_withBoolean_withJavaLangInteger_withBoolean_(SHRNetworkBase *self, NSString *callPath, jboolean shouldCompress, JavaLangInteger *currentCall, jboolean async);

  FOUNDATION_EXPORT SHRNetworkBase *new_SHRNetworkBase_initWithNSString_withBoolean_withJavaLangInteger_withBoolean_(NSString *callPath, jboolean shouldCompress, JavaLangInteger *currentCall, jboolean async) NS_RETURNS_RETAINED;

  FOUNDATION_EXPORT SHRNetworkBase *create_SHRNetworkBase_initWithNSString_withBoolean_withJavaLangInteger_withBoolean_(NSString *callPath, jboolean shouldCompress, JavaLangInteger *currentCall, jboolean async);

  J2OBJC_TYPE_LITERAL_HEADER(SHRNetworkBase)

  @compatibility_alias ComGcatconsultSharedRemoteNetworkBase SHRNetworkBase;

  #endif

  #pragma pop_macro("INCLUDE_ALL_ComGcatconsultSharedRemoteNetworkBase")

Any help will be appreciated.


Solution

  • My limited understanding is that Swift imports Objective C properties as fields, not the fields themselves. You'll therefore need an accessor method for mResponseData.

    The good news is that j2objc has a Property annotation, which you can add to the mResponseDate field so an equivalent Objective C property is generated during translation. That property should be imported later in Swift.

    The Property annotation will also generate default accessors (like @synthesize does in Objective C), so if your Java class already has accessors, specify them using the getter= and setter= @property attributes. The translator's testProperties() test demonstrates a complex property annotation example.