diff options
author | Sven Gothel <[email protected]> | 2023-06-26 04:09:19 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2023-06-26 04:09:19 +0200 |
commit | 68911f778039bbf08b5afbffd536b4cdacd33032 (patch) | |
tree | d201a9bc0762eea594d555ae777ab55bcf749a60 /src | |
parent | 93c63bd880b6bc8a85a0057112b5065b312af1db (diff) |
GlueGen JavaCallback: Expose getJNIMethodDescriptor() for JNI mangled method names, used in CMethodBindingEmitter; Use JavaCallback's function-pointer-type capital-name as simple-class-name and its FQN for JNI resolution.
Diffstat (limited to 'src')
-rw-r--r-- | src/java/com/jogamp/gluegen/CMethodBindingEmitter.java | 4 | ||||
-rw-r--r-- | src/java/com/jogamp/gluegen/JavaEmitter.java | 18 | ||||
-rw-r--r-- | src/java/com/jogamp/gluegen/JavaType.java | 41 |
3 files changed, 49 insertions, 14 deletions
diff --git a/src/java/com/jogamp/gluegen/CMethodBindingEmitter.java b/src/java/com/jogamp/gluegen/CMethodBindingEmitter.java index 334df1b..66d4814 100644 --- a/src/java/com/jogamp/gluegen/CMethodBindingEmitter.java +++ b/src/java/com/jogamp/gluegen/CMethodBindingEmitter.java @@ -1213,9 +1213,7 @@ public class CMethodBindingEmitter extends FunctionEmitter { getJNIMangledArg(Integer.TYPE, buf, false); } } else if (type.isNamedClass()) { - buf.append("L"); - buf.append(type.getName().replace('.', '_')); - buf.append("_2"); + buf.append(type.getJNIMethodDesciptor()); } else if (type.isCompoundTypeWrapper()) { // Mangle wrappers for C structs as ByteBuffer getJNIMangledArg(java.nio.ByteBuffer.class, buf, true); diff --git a/src/java/com/jogamp/gluegen/JavaEmitter.java b/src/java/com/jogamp/gluegen/JavaEmitter.java index 4953807..139e823 100644 --- a/src/java/com/jogamp/gluegen/JavaEmitter.java +++ b/src/java/com/jogamp/gluegen/JavaEmitter.java @@ -1455,13 +1455,17 @@ public class JavaEmitter implements GlueEmitter { static class JavaCallback { final String funcName; + final String simpleClazzName; + final String fqClazzName; final FunctionType func; final int userParamIdx; final Type userParamType; final String userParamName; - JavaCallback(final String funcName, final FunctionType func, final int userParamIdx) { + JavaCallback(final JavaConfiguration cfg, final String funcName, final FunctionType func, final int userParamIdx) { this.funcName = funcName; + this.simpleClazzName = capitalizeString(funcName); + this.fqClazzName = cfg.packageName()+"."+cfg.className()+"."+simpleClazzName; this.func = func; int paramIdx = -2; Type paramType = null; @@ -1482,7 +1486,7 @@ public class JavaEmitter implements GlueEmitter { @Override public String toString() { - return String.format("JavaCallback[%s, userParam[idx %d, '%s', %s], %s]", funcName, + return String.format("JavaCallback[%s, %s, userParam[idx %d, '%s', %s], %s]", funcName, fqClazzName, userParamIdx, userParamName, userParamType.getSignature(null).toString(), func.toString(funcName, false, true)); } } @@ -1492,13 +1496,13 @@ public class JavaEmitter implements GlueEmitter { final FunctionSymbol funcSym = new FunctionSymbol("callback", funcType); funcSym.addAliasedName(funcName); final int userParamIdx = cfg.javaCallbackUserParamIdx(funcSym); - final JavaCallback jcb = new JavaCallback(funcName, funcType, userParamIdx); + final JavaCallback jcb = new JavaCallback(cfg, funcName, funcType, userParamIdx); javaCallbackMap.put(funcName, jcb); LOG.log(INFO, "JavaCallback: fSym {0}, userParam {1}", funcSym.getAliasedString(), userParamIdx); LOG.log(INFO, "JavaCallback: Added {0}", jcb); javaUnit.emitln(" /** JavaCallback interface: "+funcName+" -> "+funcType.toString(funcName, false, true)+" */"); - javaUnit.emitln(" public static interface "+funcName+" {"); + javaUnit.emitln(" public static interface "+jcb.simpleClazzName+" {"); generateFunctionInterfaceCode(javaUnit, funcSym, jcb); javaUnit.emitln(" }"); javaUnit.emitln(); @@ -3103,7 +3107,7 @@ public class JavaEmitter implements GlueEmitter { if( isJavaCallbackArg ) { // Replace JavaCallback type with generated interface name - mappedType = JavaType.createForNamedClass(cArgType.getName()); + mappedType = JavaType.createForNamedClass( javaCallback.fqClazzName ); } else if( null != javaCallback && null != javaCallback.userParamName && javaCallback.userParamName.equals( cArgName ) && cArgType.isPointer() && cArgType.getTargetType().isVoid() ) @@ -3291,13 +3295,13 @@ public class JavaEmitter implements GlueEmitter { /** * Converts first letter to upper case. */ - private final String capitalizeString(final String string) { + private static String capitalizeString(final String string) { return Character.toUpperCase(string.charAt(0)) + string.substring(1); } /** * Converts first letter to lower case. */ - private final String decapitalizeString(final String string) { + private static String decapitalizeString(final String string) { return Character.toLowerCase(string.charAt(0)) + string.substring(1); } diff --git a/src/java/com/jogamp/gluegen/JavaType.java b/src/java/com/jogamp/gluegen/JavaType.java index be94431..3bd4943 100644 --- a/src/java/com/jogamp/gluegen/JavaType.java +++ b/src/java/com/jogamp/gluegen/JavaType.java @@ -293,8 +293,7 @@ public class JavaType { } /** - * Returns the descriptor (internal type signature) corresponding to - * this type. + * Returns the descriptor (internal type signature) corresponding to this type. */ public String getDescriptor() { // FIXME: this is not completely accurate at this point (for @@ -318,6 +317,40 @@ public class JavaType { return "ANON_NIO"; } + /** + * Returns the native (JNI) method-name descriptor corresponding to this type, + * i.e. replacing chars {@link #getDescriptor()} as follows + * <ul> + * <li>`_` -> `_1`</li> + * <li>`/` -> `_`</li> + * <li>`;` -> `_2`</li> + * <li>`[` -> `_3`</li> + * </ul> + * @see JNI Spec 2, Chapter 2, Resolving Native Method Names + * @see #toJNIMethodDescriptor(String) + */ + public String getJNIMethodDesciptor() { + return toJNIMethodDescriptor( getDescriptor() ); + } + + /** + * Converts the assumed descriptor (internal type signature) to a native (JNI) method-name descriptor, + * i.e. replacing chars {@link #getDescriptor()} as follows + * <ul> + * <li>`_` -> `_1`</li> + * <li>`/` -> `_`</li> + * <li>`;` -> `_2`</li> + * <li>`[` -> `_3`</li> + * </ul> + * @see JNI Spec 2, Chapter 2, Resolving Native Method Names + */ + public static String toJNIMethodDescriptor(final String descriptor) { + return descriptor.replace("_", "_1") + .replace("/", "_") + .replace(";", "_2") + .replace("[", "_3"); + } + /** Returns the String corresponding to the JNI type for this type, or NULL if it can't be represented (i.e., it's a boxing class that we need to call getBuffer() on.) */ @@ -745,7 +778,7 @@ public class JavaType { return descriptor(clazz.getName()); } - private static String descriptor(final String referenceTypeName) { - return "L" + referenceTypeName.replace('.', '/') + ";"; + private static String descriptor(final String clazzName) { + return "L" + clazzName.replace('.', '/') + ";"; } } |