Changes between Version 71 and Version 72 of j2p


Ignore:
Timestamp:
05/16/24 10:40:06 (6 months ago)
Author:
wouter
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • j2p

    v71 v72  
    4444Fields in Java have to be initialized in the __init__ function in the python translation.
    4545
    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.
     46Overloaded 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.
    4747
    4848
     
    124124==== @Nonnull
    125125Java 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}}}.
     126You 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
     129The 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
     138Java 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}}}.
    130139
    131140Also be aware of the subtleties of this notation. For instance
    132 {{{@Nonnnull Set<String>}}} is a set that can not be null but that contain null values.
    133 {{{@Nonnull 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.
    134143
    135144
    136145{{{#!td style="background: #efe"
    137 @Nonnull 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".
    138147}}}
    139148
    140149{{{#!td style="background: #fee"
    141 @Nonnull 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.
    142151}}}
    143152