Cannot pass String as JNI jstring parameter
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 35/100
Direzione di ricerca
Inizia tracciando la conversione degli argomenti di Function tramite convertArgument(), in particolare la gestione di String, OPTION_ALLOW_OBJECTS e JniEnv.CURRENT. Verifica come viene convertito attualmente un Java String prima dell’invocazione e determina se gli oggetti jstring possono passare senza interrompere le conversioni specifiche di JNA. Il lavoro è concluso quando la chiamata JVM_FindLoadedClass segnalata può ricevere un Java String come jstring, con una copertura del comportamento di conversione.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
-
Version of JNA and related jars
JNA 5.3.1 -
Version and vendor of the java virtual machine
Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 1.8.0_181 -
Operating system
Ubuntu 16.04.6 -
System architecture (CPU type, bitness of the JVM)
Intel x64, x64 JVM -
Complete description of the problem
I cannot passStringobjects to a JNI method for invocation, (even using JniEnv.CURRENT and setting OPTION_ALLOW_OBJECTS), since aStringis always converted toNativeString.When trying to call JNI methods, JNA invokes
convertArgument()just before C method invocation, andStringis converted toNativeStringbeforeallowObjectsis checked. This means that it is impossible to pass the JavaString(jstring) object to C (which acceptsjstringas a parameter type). For example, JVM_FindLoadedClass(JNIEnv *env, jobject loader, jstring name); -
Steps to reproduce
public
interface JVM extends com.sun.jna.Library {
Class JVM_FindLoadedClass(JNIEnv env, Object classLoader, String name);
}
Map options = new HashMap();
options.put(Library.OPTION_ALLOW_OBJECTS, Boolean.TRUE);
JVM libjvm = Native.load("jvm", JVM.class, options);
jvm.JVM_FindLoadedClass(JNIEnv.CURRENT, classLoader, className);
It is entirely likely that I don't know what I'm doing, after reading through the documentation and studying the source -- I do not know how to pass a String as a jstring object (not a char* object) to native code. I've tried type converters, but those didn't get me anywhere, since String is always converted to NativeString as an argument...
Because the Function constructor is package scope, as is invoke, I cannot overload invoke to not convert arguments, so I am unable to modify the method invocation (outside of rewriting the class definition at runtime via javassist or something similar)
One solution I thought of, while trying to figure this out, is modify the convertArgument() method so that the check for allowObjects occurs sooner, allowing all Java objects to passed without conversion -- so the check for allowObjects is after JNA specific object types (struct, etc), but before the Java object type conversion (mostly string), instead of where currently is (which is last)....
NOTE: Loading the JVM library for Windows 32 bit, or MACOS is slightly different. The above only loads it on Linux, specifically Ubuntu 16.04.6 x64 with an Oracle JVM (from my testing, this is currently true for all released Oracle JVM's on Linux, but I haven't tested every combination....)
- Lingua principale
- Java
- Stelle
- 8.9k
- Fork
- 1.7k
- Merge medio
- 1g 13h
- PR unite (30g)
- 1
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di java-native-access/jna
-
Difficoltà 4/5 1-2 giorni Idoneità per principianti 48/100
java-native-access/jna#1742 ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 66/100
java-native-access/jna#1738 ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
java-native-access/jna#1736 ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 65/100
java-native-access/jna#1717 ·
-
Windows32Exception - The parameter is incorrectForse già presa @marktech0813 l’ha presa 335 giorni fa. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
java-native-access/jna#1700 · 11 commenti ·
Tutte le issue di java-native-access/jna
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
modelcontextprotocol/java-sdk#1174 · 1 reazione ·
I maintainer di solito rispondono entro 2 giorni
-
module: core
Difficoltà 2/5 1-3 ore Idoneità per principianti 77/100
bigbluebutton/bigbluebutton#26028 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
MetricsHub/metricshub-community#1390 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
apache/rocketmq-dashboard#6331 ·
I maintainer di solito rispondono entro 5 giorni
-
status: team-only type: dependency-upgrade
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
spring-projects/spring-boot#52143 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno