3. J2Objc: Annotation & 混合代码

目的就是在处理一些中间代码的时候,部分内容依旧在java中管理,例子:cordova插件项目,ios端代码主要来源于andorid端,手工处理了一些公共代码,用接口方式来分别管理。

3. J2Objc: Annotation & 混合代码_第1张图片
接口

特别的是如下代码:

import com.google.j2objc.annotations.Property;
/*-[
  #import "java/lang/NullPointerException.h"
]-*/

public class HelloWorldNative {
  @Property("nonatomic, copy")
  private String tableId;

  /*-HEADER[
    @property (nonatomic, assign) void (^callbackBlockFailed)(FISOperationCallbackModel *failedModel);
  ]-*/
  
  public static native void main(String[] args) /*-[
      NSLog(@"%s", "HelloWorldNative");
      @throw [[JavaLangNullPointerException alloc] init];
  ]-*/;
}

执行步骤:

$ pwd
/Users/wdxxl/J2OBJC_Github/j2objc_wdxxl_demo/0_writing_native_methods
$ j2objc -version
j2objc 2.0.5
$ j2objc HelloWorldNative.java
$ J2objcc -c HelloWorldNative.m
$ j2objcc -o hello HelloWorldNative.o
$ ./hello HelloWorldNative
2018-01-04 11:03:59.696 hello[95951:3963406] HelloWorldNative
Exception in thread "main" java.lang.NullPointerException
    at 0x000000010424a00b java.lang.NullPointerException.() + 14
    at 0x0000000104219801 HelloWorldNative.main() + 81
    at 0x000000010421979d HelloWorldNative.main() + 29
    at 0x00000001044a3794 java.lang.reflect.Method.invoke:object:() + 22
    at 0x00000001044a339e java.lang.reflect.Method.invoke() + 332

你可能感兴趣的:(3. J2Objc: Annotation & 混合代码)