The best practice of the J2Objc Annotions framework in the Java class library
The best practice of the J2Objc Annotions framework in the Java class library
### introduce
J2OBJC is an open source tool, which is used to convert the Java code into Objective-C code, so that the Java code can run on the iOS platform.J2OBJC provides an Annotations framework to specify some specific marks in the Java class library to help achieve better code conversion effects and make full use of the characteristics of the iOS platform.
This article will introduce the best practice of the J2Objc Annotations framework in the Java class library and provide the corresponding Java code example.
### @mapping Note
The role of @mapping annotation is to map a name in the source code into another name.This is very useful in the conversion process between the Java library and the Objective-C code, because the naming specifications of the two languages are different.
The following is an example:
public class MyLibrary {
@Mapping("customMethodName")
public static void originalMethodName() {
// Method implementation
}
}
In the above example, the `OriginalMethodName` will be mapped as the` CustomMethodName` when converting to Objective-C code.In this way, J2OBJC will generate the corresponding Objective-C code and ensure that the calling method on the iOS platform can work normally.
### @Weak Note
The role of @Weak's annotation is to bid a certain object in the Java class library as a weak reference to avoid potential cycle reference problems.When converting to Objective-C code, J2OBJC will convert a weak reference to the object marked by @Weak annotation to Objective-C.
The following is an example:
public class MyClass {
@Weak
private MyObject myObject;
}
In the above example, the `MyObject` is marked as a weak reference, so that when converting to Objective-C code, J2Objc will generate the` __weak` modifier in the corresponding attribute statement to avoid the memory leakage caused by circulating reference.
### @mapping attribute
@Mapping Note also provides some attributes to use, to more accurately specify the mapping relationship of the name in the source code and the name in the target code.
-` [] value () `: Used to specify the mapping relationship of one or more names.
-` Boolean iGnore () `: Used to specify whether the mapping relationship is ignored, the default is` false`.
-`` boolean nulled () `: It is used to specify whether the property is allowed to be` null` value in the Objective-C code, and the default is `true`.
The following is an example:
public class MyLibrary {
@Mapping(value = {"firstMethod", "secondMethod"}, ignore = true)
public static void originalMethod() {
// Method implementation
}
}
In the above example, when the `OriginalMethod` is converted to Objective-C code,` firstMethod` and `secondMethod` will be ignored, that is, the corresponding mapping method will not be generated.
### in conclusion
The best practice of the J2Objc Annotations framework in the Java class library is to make full use of @Mapping and @Weak annotations to provide better code conversion effects and handle specific problems.By using these annotations and specifying related attributes according to needs, the switching code can be more accurate and running normally on the iOS platform.
The above is the best practice of the J2Objc Annotations framework in the Java class library. I hope it will be helpful to you.