diff --git a/SWIG/Doc/Manual/Java.html b/SWIG/Doc/Manual/Java.html index a1c41811f..a7e706292 100644 --- a/SWIG/Doc/Manual/Java.html +++ b/SWIG/Doc/Manual/Java.html @@ -702,7 +702,8 @@ public interface exampleConstants { } -These are runtime constants. They are not compiler constants that can, for example, be used +Note that SWIG has inferred the C type and used an appropriate Java type that will fit the range of all possible values for the C type. +By default SWIG generates runtime constants. They are not compiler constants that can, for example, be used in a switch statement. This can be changed by using the %javaconst(flag) directive. It works like all the other %feature directives. The default is %javaconst(0). It is possible to initialize all wrapped constants from pure Java code by placing a %javaconst(1) before SWIG parses the constants. @@ -712,22 +713,62 @@ Here is an example:
 %javaconst(1);
 %javaconst(0) BIG;
-#define BIG 1000LL
+%javaconst(0) LARGE;
+
 #define EXPRESSION (0x100+5)
+#define BIG 1000LL
+#define LARGE 2000ULL
 
generates:
 public interface exampleConstants {
-  public final static long BIG = exampleJNI.get_BIG();
   public final static int EXPRESSION = (0x100+5);
+  public final static long BIG = exampleJNI.get_BIG();
+  public final static java.math.BigInteger LARGE = exampleJNI.get_LARGE();
 }
 
-Be careful using the %javaconst(1) directive as not all C code will compile as Java code. For example the -1000LL value for the BIG constant above would not generate valid Java code. -The example demonstrates how you can target particular constants (BIG) with %javaconst. +Note that SWIG has inferred the C long long type from BIG and used an appropriate Java type (long) as +a Java long is the smallest sized Java type that will take all possible values for a C long long. +Similarly for LARGE. + +

+ +Be careful using the %javaconst(1) directive as not all C code will compile as Java code. For example neither the +1000LL value for BIG nor 2000ULL for LARGE above would generate valid Java code. +The example demonstrates how you can target particular constants (BIG and LARGE) with %javaconst. +SWIG doesn't use %javaconst(1) as the default as it tries to generate code that will always compile. +However, using a %javaconst(1) at the top of your interface file is strongly recommended as the preferred compile time constants +will be generated and most C constants will compile as Java code and in anycase the odd constant that doesn't can be fixed using %javaconst(0). +

+ +There is an alternative directive which can be used for these rare constant values that won't compile as Java code. +This is the %javaconstvalue(value) directive, where value is a Java code replacement for the C constant and can be either a string or a number. +This is useful if you do not want to use either the parsed C value nor a JNI call, +such as when the C parsed value will not compile as Java code and a compile time constant is required. +The same example demonstrates this: + +

+%javaconst(1);
+%javaconstvalue("new java.math.BigInteger(\"2000\")") LARGE;
+%javaconstvalue(1000) BIG;
+
+#define EXPRESSION (0x100+5)
+#define BIG 1000LL
+#define LARGE 2000ULL
+
+ +Note the string quotes for "2000" are escaped. The following is then generated: + +
+public interface exampleConstants {
+  public final static int EXPRESSION = (0x100+5);
+  public final static long BIG = 1000;
+  public final static java.math.BigInteger LARGE = new java.math.BigInteger("2000");
+}
+

Note: declarations declared as const are wrapped as read-only variables and @@ -783,11 +824,11 @@ public interface exampleConstants { } -The %javaconst(flag) directive introduced in the previous section on constants can also be used with enums, -thereby allowing the enum values to be used in Java switch statements. +The %javaconst(flag) and %javaconst(value) directive introduced in the previous section on constants can also be used with enums. As is the case for constants, the default is %javaconst(0) as not all C values will compile as Java code. -However, it is recommended to add in a %javaconst(1) directive at the top of your +However, it is strongly recommended to add in a %javaconst(1) directive at the top of your interface file as it is only on very rare occasions that this will produce code that won't compile under Java. +Using %javaconst(1) will ensure compile time constants are generated, thereby allowing the enum values to be used in Java switch statements. Example usage:

@@ -843,7 +884,7 @@ public final class Beverage {
-See Typesafe enum classes to see the ommitted support methods. +See Typesafe enum classes to see the omitted support methods. Note that the enum item with an initializer (LAGER) is initialized with the enum value obtained via a JNI call. However, as with anonymous enums and constants, use of the %javaconst directive is strongly recommended to change this behaviour: @@ -874,6 +915,7 @@ The generated code is easier to read and more efficient as a true constant is us As is the case for constants, the default is %javaconst(0) as not all C values will compile as Java code. However, it is recommended to add in a %javaconst(1) directive at the top of your interface file as it is only on very rare occasions that this will produce code that won't compile under Java. +The %javaconstvalue(value) directive can also be used for typesafe enums. Note that global enums are generated into a Java class within whatever package you are using. C++ enums defined within a C++ class are generated into a static final inner Java class within the Java proxy class. @@ -931,10 +973,11 @@ public enum Beverage { -See Proper Java enum classes to see the ommitted support methods. +See Proper Java enum classes to see the omitted support methods. The generated Java enum has numerous additional methods to support enums with initializers, such as LAGER above. Note that as with the typesafe enum pattern, enum items with initializers are by default initialized with the enum value obtained via a JNI call. However, this is not the case above as we have used the recommended %javaconst(1) to avoid the JNI call. +The %javaconstvalue(value) directive covered in the Constants section can also be used for proper Java enums. The additional support methods need not be generated if none of the enum items have initializers and this is covered later in the Simpler Java enums for enums without initializers section. @@ -973,6 +1016,7 @@ public final class Beverage { As is the case previously, the default is %javaconst(0) as not all C/C++ values will compile as Java code. However, again it is recommended to add in a %javaconst(1) directive. +and the %javaconstvalue(value) directive covered in the Constants section can also be used for type unsafe enums. Note that global enums are generated into a Java class within whatever package you are using. C++ enums defined within a C++ class are generated into a static final inner Java class within the Java proxy class.