Skip to content

Commit 47283df

Browse files
committed
Fixed jni.py comment regex and handling of JNI functions with overloads
1 parent 10b58a6 commit 47283df

File tree

2 files changed

+77
-19
lines changed

2 files changed

+77
-19
lines changed

wpiformat/test/test_jni.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,63 @@ def test_jni():
113113
"{" + os.linesep)
114114
test.add_latest_input_as_output(True)
115115

116+
# Check signature that breaks verbose regexes
117+
test.add_input("./NetworkTablesJNI.cpp",
118+
"/*" + os.linesep + \
119+
" * Class: edu_wpi_first_networktables_NetworkTablesJNI" + os.linesep + \
120+
" * Method: getEntry" + os.linesep + \
121+
" * Signature: (ILjava/lang/String;)I" + os.linesep + \
122+
" */" + os.linesep + \
123+
"JNIEXPORT jint JNICALL" + os.linesep + \
124+
"Java_edu_wpi_first_networktables_NetworkTablesJNI_getEntry(JNIEnv* env, jclass," + os.linesep + \
125+
" jint inst," + os.linesep + \
126+
" jstring key) {" + os.linesep)
127+
test.add_output("/*" + os.linesep + \
128+
" * Class: edu_wpi_first_networktables_NetworkTablesJNI" + os.linesep + \
129+
" * Method: getEntry" + os.linesep + \
130+
" * Signature: (ILjava/lang/String;)I" + os.linesep + \
131+
" */" + os.linesep + \
132+
"JNIEXPORT jint JNICALL" + os.linesep + \
133+
"Java_edu_wpi_first_networktables_NetworkTablesJNI_getEntry" + os.linesep + \
134+
" (JNIEnv* env, jclass, jint inst, jstring key)" + os.linesep + \
135+
"{" + os.linesep, True, True)
136+
137+
# Function with array type as argument
138+
test.add_input("./NetworkTablesJNI.cpp",
139+
"/*" + os.linesep + \
140+
" * Class: edu_wpi_first_networktables_NetworkTablesJNI" + os.linesep + \
141+
" * Method: getEntries" + os.linesep + \
142+
" * Signature: (ILjava/lang/String;I)[I" + os.linesep + \
143+
" */" + os.linesep + \
144+
"JNIEXPORT jintArray JNICALL" + os.linesep + \
145+
"Java_edu_wpi_first_networktables_NetworkTablesJNI_getEntries(JNIEnv* env," + os.linesep + \
146+
" jclass, jint inst," + os.linesep + \
147+
" jstring prefix," + os.linesep + \
148+
" jint types) {" + os.linesep)
149+
test.add_output("/*" + os.linesep + \
150+
" * Class: edu_wpi_first_networktables_NetworkTablesJNI" + os.linesep + \
151+
" * Method: getEntries" + os.linesep + \
152+
" * Signature: (ILjava/lang/String;I)[I" + os.linesep + \
153+
" */" + os.linesep + \
154+
"JNIEXPORT jintArray JNICALL" + os.linesep + \
155+
"Java_edu_wpi_first_networktables_NetworkTablesJNI_getEntries" + os.linesep + \
156+
" (JNIEnv* env, jclass, jint inst, jstring prefix, jint types)" + os.linesep + \
157+
"{" + os.linesep, True, True)
158+
159+
# Ensure functions with overloads are handled correctly
160+
test.add_input("./NetworkTablesJNI.cpp",
161+
"/*" + os.linesep + \
162+
" * Class: edu_wpi_first_networktables_NetworkTablesJNI" + os.linesep + \
163+
" * Method: setRaw" + os.linesep + \
164+
" * Signature: (IJ[BZ)Z" + os.linesep + \
165+
" */" + os.linesep + \
166+
"JNIEXPORT jboolean JNICALL" + os.linesep + \
167+
"Java_edu_wpi_first_networktables_NetworkTablesJNI_setRaw__IJ_3BZ" + os.linesep + \
168+
" (JNIEnv* env, jclass, jint entry, jlong time, jbyteArray value," + os.linesep + \
169+
" jboolean force)" + os.linesep + \
170+
"{" + os.linesep)
171+
test.add_latest_input_as_output(True)
172+
116173
# Ensure text before JNIEXPORT and after args and ")" is handled correctly
117174
# as well as two JNI functions in a row
118175
test.add_input("./TestJNI.cpp",

wpiformat/wpiformat/jni.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,43 +31,43 @@ def should_process_file(self, config_file, name):
3131

3232
def map_jni_type(self, type_name):
3333
ret = ""
34-
if type_name.endswith("*"):
34+
if type_name.endswith("*") or type_name.endswith("Array"):
3535
ret += "["
3636

37-
if type_name == "jboolean":
37+
if type_name.startswith("jboolean"):
3838
return ret + "Z"
39-
elif type_name == "jbyte":
39+
elif type_name.startswith("jbyte"):
4040
return ret + "B"
41-
elif type_name == "jchar":
41+
elif type_name.startswith("jchar"):
4242
return ret + "C"
43-
elif type_name == "jshort":
43+
elif type_name.startswith("jshort"):
4444
return ret + "S"
45-
elif type_name == "jint":
45+
elif type_name.startswith("jint"):
4646
return ret + "I"
47-
elif type_name == "jlong":
47+
elif type_name.startswith("jlong"):
4848
return ret + "J"
49-
elif type_name == "jfloat":
49+
elif type_name.startswith("jfloat"):
5050
return ret + "F"
51-
elif type_name == "jdouble":
51+
elif type_name.startswith("jdouble"):
5252
return ret + "D"
53-
elif type_name == "void":
53+
elif type_name.startswith("void"):
5454
return ret + "V"
55-
elif type_name == "jstring":
55+
elif type_name.startswith("jstring"):
5656
return ret + "Ljava/lang/String;"
57-
elif type_name == "jobject":
57+
elif type_name.startswith("jobject"):
5858
return ret + "Ljava/lang/Object;"
5959
else:
6060
return ret + "?"
6161

6262
def run_pipeline(self, config_file, name, lines):
6363
linesep = Task.get_linesep(lines)
6464

65-
regex_str_sig = "(/\*[\w\*:\s\(\)]+\*/\s+)?" + \
66-
"JNIEXPORT\s+ (?P<ret>\w+)\s+ JNICALL\s+ " + \
67-
"(?P<func>Java_\w+)\s* \(\s* " + \
65+
regex_str_sig = "(/\*(?>(.|\n)*?\*/)\s+)?" + \
66+
"JNIEXPORT\s+(?P<ret>\w+)\s+JNICALL\s+" + \
67+
"(?P<func>Java_\w+)\s*\(\s*" + \
6868
"(?P<env_type>JNIEnv\s*\*\s*)" + \
69-
"(?P<env_name>\w+)?,\s* jclass\s*(?P<jclass_name>\w*)?"
70-
regex_sig = regex.compile(regex_str_sig, regex.VERBOSE)
69+
"(?P<env_name>\w+)?,\s*jclass\s*(?P<jclass_name>\w*)?"
70+
regex_sig = regex.compile(regex_str_sig)
7171

7272
regex_str_func = "Java_(?P<class>\w+)_(?P<method>[^_]+)$"
7373
regex_func = regex.compile(regex_str_func)
@@ -94,8 +94,9 @@ def run_pipeline(self, config_file, name, lines):
9494
if match_sig.group("jclass_name"):
9595
jni_args += " " + match_sig.group("jclass_name")
9696

97-
# Write JNI function comment
98-
match = regex_func.search(match_sig.group("func"))
97+
# Write JNI function comment. Splitting at "__" removes overload
98+
# annotation from method comment
99+
match = regex_func.search(match_sig.group("func").split("__")[0])
99100
comment += "/*" + linesep + \
100101
" * Class: " + match.group("class") + linesep + \
101102
" * Method: " + match.group("method") + linesep + \

0 commit comments

Comments
 (0)