From 7ff91665cf53ee3009e3325740c1957fc4b3053a Mon Sep 17 00:00:00 2001 From: Roman Donchenko Date: Tue, 15 Oct 2013 15:44:26 +0400 Subject: [PATCH] In Java bindings, wrap version constants into functions to prevent inlining. Java inlines static finals if they're defined with a constant expression. In case of version constants we don't want that to happen, since they obviously change from version to version. If the user substitutes a different OpenCV jar without recompiling, we want user code to still have relevant values for the version constants. This arranges that by turning constant values into function calls, which no longer count as a constant expression. --- modules/java/generator/gen_java.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/modules/java/generator/gen_java.py b/modules/java/generator/gen_java.py index 1b49b6c4fb..226efc0b42 100755 --- a/modules/java/generator/gen_java.py +++ b/modules/java/generator/gen_java.py @@ -785,8 +785,20 @@ public class %(jc)s { version_suffix = ''.join( (epoch, major, minor) ) self.classes[class_name].imports.add("java.lang.String") self.java_code[class_name]["j_code"].write(""" - public static final String VERSION = "%(v)s", NATIVE_LIBRARY_NAME = "opencv_java%(vs)s"; - public static final int VERSION_EPOCH = %(ep)s, VERSION_MAJOR = %(ma)s, VERSION_MINOR = %(mi)s, VERSION_REVISION = %(re)s; + // these constants are wrapped inside functions to prevent inlining + private static String getVersion() { return "%(v)s"; } + private static String getNativeLibraryName() { return "opencv_java%(vs)s"; } + private static int getVersionEpoch() { return %(ep)s; } + private static int getVersionMajor() { return %(ma)s; } + private static int getVersionMinor() { return %(mi)s; } + private static int getVersionRevision() { return %(re)s; } + + public static final String VERSION = getVersion(); + public static final String NATIVE_LIBRARY_NAME = getNativeLibraryName(); + public static final int VERSION_EPOCH = getVersionEpoch(); + public static final int VERSION_MAJOR = getVersionMajor(); + public static final int VERSION_MINOR = getVersionMinor(); + public static final int VERSION_REVISION = getVersionRevision(); """ % { 'v' : version_str, 'vs' : version_suffix, 'ep' : epoch, 'ma' : major, 'mi' : minor, 're' : revision } )