Changes between Version 71 and Version 72 of j2p
- Timestamp:
- 05/16/24 10:40:06 (6 months ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
j2p
v71 v72 44 44 Fields in Java have to be initialized in the __init__ function in the python translation. 45 45 46 Overloaded methods can not be handled by default python and need to be handled using an external library {{{plum-dispatch==2.2.2}}} and result in additional {{{@dispatch}}} annotations in the translated code. If you use overloading, you need to have plum installed to run your code. Check also the @Non null section.46 Overloaded methods can not be handled by default python and need to be handled using an external library {{{plum-dispatch==2.2.2}}} and result in additional {{{@dispatch}}} annotations in the translated code. If you use overloading, you need to have plum installed to run your code. Check also the @NonNull section. 47 47 48 48 … … 124 124 ==== @Nonnull 125 125 Java variables like {{{String val}}} can contain a null value, and functions like {{{String f() { ... } }}} can return null. Therefore they are translated to {{{val:Optional[str]}}} and {{{def f(self)->Optional[str]}}}. 126 You can annotate the java code with @Nonnull (from {{{javax.annotation.Nonnull}}}) to indicate the value/return value will not be null, like this 127 {{{@Nonnull String val}}} or {{{@Nonnull String f() { ... } }}}. Java primitive types like {{{boolean}}} and {{{int}}} can never be null and do not need @Nonnull 128 129 Java code {{{X instanceof C}}} is translated as in Java, so null/None is not an instance of C - C is not "Optional" and you do not need to write {{{X instanceof @Nonnull C}}}. 126 You can annotate the java code with @NonNull (from {{{org.eclipse.jdt.annotation.NonNull}}}) to indicate the value/return value will not be null, like this 127 {{{@NonNull String val}}} or {{{@NonNull String f() { ... } }}}. Java primitive types like {{{boolean}}} and {{{int}}} can never be null and do not need @NonNull 128 129 The dependency needed for this is (unfortunately @Nonnull is not built in anymore in the JRE) 130 {{{ 131 <dependency> 132 <groupId>org.eclipse.jdt</groupId> 133 <artifactId>org.eclipse.jdt.annotation</artifactId> 134 <version>2.3.0</version> 135 </dependency> 136 }}} 137 138 Java code {{{X instanceof C}}} is translated as in Java, so null/None is not an instance of C - C is not "Optional" and you do not need to write {{{X instanceof @NonNull C}}}. 130 139 131 140 Also be aware of the subtleties of this notation. For instance 132 {{{@Non nnull Set<String>}}} is a set that can not be null but that contain null values.133 {{{@Non null Set<@Nonnull String>}}} is a set that can not be null and also can not contain null values.141 {{{@NonNull Set<String>}}} is a set that can not be null but that contain null values. 142 {{{@NonNull Set<@Nonnull String>}}} is a set that can not be null and also can not contain null values. 134 143 135 144 136 145 {{{#!td style="background: #efe" 137 @Non null can be used by the java compiler to do extra checks on your code. You may need to enable this. For instance in Eclipse check Preferences/Java/Compiler/Errors/Warnings and scroll down to "Null Analysis". Note that Eclipse uses its own annotations, you have to add @Nonnull.146 @NonNull can be used by the java compiler to do extra checks on your code. Eclipse uses this annotation by default, but you may need to enable or customize it in Preferences/Java/Compiler/Errors/Warnings and scroll down to "Null Analysis". 138 147 }}} 139 148 140 149 {{{#!td style="background: #fee" 141 @Non null is NOT inherited by subclasses (was decided so by Java engineers). Therefore @Nonnull has to be repeated in the derived classes.150 @NonNull is NOT inherited by subclasses. Therefore @NonNull has to be repeated in the derived classes. 142 151 }}} 143 152