From d7bc10d7ff0e3a30d74c4e4c439230f3983bcfb4 Mon Sep 17 00:00:00 2001 From: Mathieu Féry Date: Thu, 15 Jun 2023 18:03:13 +0200 Subject: feat(jni): Add directive for dedicated JNI Code with CustomJNICode --- src/java/com/jogamp/gluegen/JavaConfiguration.java | 38 ++++++++++++++++++++++ src/java/com/jogamp/gluegen/JavaEmitter.java | 20 +++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) (limited to 'src/java/com') diff --git a/src/java/com/jogamp/gluegen/JavaConfiguration.java b/src/java/com/jogamp/gluegen/JavaConfiguration.java index f392525..3241d83 100644 --- a/src/java/com/jogamp/gluegen/JavaConfiguration.java +++ b/src/java/com/jogamp/gluegen/JavaConfiguration.java @@ -165,6 +165,7 @@ public class JavaConfiguration { private final Set manuallyImplement = new HashSet(); private final Map delegatedImplementation = new HashMap(); private final Map> customJavaCode = new HashMap>(); + private final Map> customJNICode = new HashMap>(); private final Map> classJavadoc = new HashMap>(); private final Map> methodJavadoc = new HashMap>(); private final Map structPackages = new HashMap(); @@ -601,6 +602,19 @@ public class JavaConfiguration { return res; } + /** Returns a list of Strings containing user-implemented JNI code for + the given Java type name (not fully-qualified, only the class + name); returns either null or an empty list if there is no + custom code for the class. */ + public List customJNICodeForClass(final String className) { + List res = customJNICode.get(className); + if (res == null) { + res = new ArrayList(); + customJNICode.put(className, res); + } + return res; + } + public List javadocForMethod(final String methodName) { List res = methodJavadoc.get(methodName); if (res == null) { @@ -1330,6 +1344,10 @@ public class JavaConfiguration { } else if (cmd.equalsIgnoreCase("CustomCCode")) { readCustomCCode(tok, filename, lineNo); // Warning: make sure delimiters are reset at the top of this loop + // because readCustomJNICode changes them. + } else if (cmd.equalsIgnoreCase("CustomJNICode")) { + readCustomJNICode(tok, filename, lineNo); + // Warning: make sure delimiters are reset at the top of this loop // because readCustomCCode changes them. } else if (cmd.equalsIgnoreCase("MethodJavadoc")) { readMethodJavadoc(tok, filename, lineNo); @@ -1697,6 +1715,26 @@ public class JavaConfiguration { } } + protected void readCustomJNICode(final StringTokenizer tok, final String filename, final int lineNo) { + try { + final String tokenClassName = tok.nextToken(); + try { + final String restOfLine = tok.nextToken("\n\r\f"); + addCustomJNICode(tokenClassName, restOfLine); + } catch (final NoSuchElementException e) { + addCustomJNICode(tokenClassName, ""); + } + } catch (final NoSuchElementException e) { + throw new RuntimeException("Error parsing \"CustomJNICode\" command at line " + lineNo + + " in file \"" + filename + "\"", e); + } + } + + protected void addCustomJNICode(final String className, final String code) { + final List codeList = customJNICodeForClass(className); + codeList.add(code); + } + protected void readMethodJavadoc(final StringTokenizer tok, final String filename, final int lineNo) { try { final String tokenClassName = tok.nextToken(); diff --git a/src/java/com/jogamp/gluegen/JavaEmitter.java b/src/java/com/jogamp/gluegen/JavaEmitter.java index ada1ce9..733f12a 100644 --- a/src/java/com/jogamp/gluegen/JavaEmitter.java +++ b/src/java/com/jogamp/gluegen/JavaEmitter.java @@ -830,7 +830,7 @@ public class JavaEmitter implements GlueEmitter { // Note that machDescJava MachineDataInfo is always 64bit unix, // which complies w/ Java types. - boolean needsNativeCode = false; + boolean needsNativeCode = !cfg.customJNICodeForClass(containingJTypeName).isEmpty(); // Native code for calls through function pointers gets emitted // into the abstract base class; Java code which accesses fields @@ -1162,6 +1162,7 @@ public class JavaEmitter implements GlueEmitter { javaUnit.emitln("}"); javaUnit.close(); if (needsNativeCode) { + emitCustomJNICode(jniUnit, containingJTypeName); jniUnit.close(); } if( GlueGen.debug() ) { @@ -2524,6 +2525,23 @@ public class JavaEmitter implements GlueEmitter { unit.emitln(" // ---- End CustomJavaCode .cfg declarations"); } + /** + * Emit all the strings specified in the "CustomJNICode" parameters of + * the configuration file. + */ + protected void emitCustomJNICode(final CodeUnit unit, final String className) throws Exception { + final List code = cfg.customJNICodeForClass(className); + if (code.isEmpty()) + return; + + unit.emitln(); + unit.emitln(" // --- Begin CustomJNICode .cfg declarations"); + for (final String line : code) { + unit.emitln(line); + } + unit.emitln(" // ---- End CustomJNICode .cfg declarations"); + } + public String[] getClassAccessModifiers(final String classFQName) { String[] accessModifiers; final MethodAccess acc = cfg.accessControl(classFQName); -- cgit v1.2.3