@@ -217,63 +217,52 @@ Java_com_google_devtools_build_lib_unix_NativePosixFiles_symlink(JNIEnv *env,
217
217
link_common (env, oldpath, newpath, ::symlink );
218
218
}
219
219
220
- static jobject NewFileStatus (JNIEnv *env,
221
- const portable_stat_struct &stat_ref) {
222
- static jclass file_status_class = nullptr ;
223
- if (file_status_class == nullptr ) { // note: harmless race condition
224
- jclass local =
225
- env->FindClass (" com/google/devtools/build/lib/unix/FileStatus" );
226
- CHECK (local != nullptr );
227
- file_status_class = static_cast <jclass>(env->NewGlobalRef (local));
228
- }
220
+ namespace {
221
+ static jclass file_status_class = nullptr ;
222
+ static jclass errno_file_status_class = nullptr ;
223
+ static jmethodID file_status_class_ctor = nullptr ;
224
+ static jmethodID errno_file_status_class_no_error_ctor = nullptr ;
225
+ static jmethodID errno_file_status_class_errorno_ctor = nullptr ;
226
+ static jclass dirents_class = nullptr ;
227
+ static jmethodID dirents_ctor = nullptr ;
228
+
229
+ static jclass makeStaticClass (JNIEnv *env, const char *name) {
230
+ jclass lookup_result = env->FindClass (name);
231
+ CHECK (lookup_result != nullptr );
232
+ return static_cast <jclass>(env->NewGlobalRef (lookup_result));
233
+ }
229
234
230
- static jmethodID method = nullptr ;
231
- if (method == nullptr ) { // note: harmless race condition
232
- method = env->GetMethodID (file_status_class, " <init>" , " (IIIIIIIJIJ)V" );
233
- CHECK (method != nullptr );
234
- }
235
+ static jmethodID getConstructorID (JNIEnv *env, jclass clazz,
236
+ const char *parameters) {
237
+ jmethodID method = env->GetMethodID (clazz, " <init>" , parameters);
238
+ CHECK (method != nullptr );
239
+ return method;
240
+ }
235
241
242
+ static jobject NewFileStatus (JNIEnv *env,
243
+ const portable_stat_struct &stat_ref) {
236
244
return env->NewObject (
237
- file_status_class, method , stat_ref.st_mode ,
245
+ file_status_class, file_status_class_ctor , stat_ref.st_mode ,
238
246
StatSeconds (stat_ref, STAT_ATIME), StatNanoSeconds (stat_ref, STAT_ATIME),
239
247
StatSeconds (stat_ref, STAT_MTIME), StatNanoSeconds (stat_ref, STAT_MTIME),
240
248
StatSeconds (stat_ref, STAT_CTIME), StatNanoSeconds (stat_ref, STAT_CTIME),
241
- static_cast <jlong>(stat_ref.st_size ),
242
- static_cast <int >(stat_ref. st_dev ), static_cast < jlong>(stat_ref.st_ino ));
249
+ static_cast <jlong>(stat_ref.st_size ), static_cast < int >(stat_ref. st_dev ),
250
+ static_cast <jlong>(stat_ref.st_ino ));
243
251
}
244
252
245
253
static jobject NewErrnoFileStatus (JNIEnv *env,
246
254
int saved_errno,
247
255
const portable_stat_struct &stat_ref) {
248
- static jclass errno_file_status_class = nullptr ;
249
- if (errno_file_status_class == nullptr ) { // note: harmless race condition
250
- jclass local =
251
- env->FindClass (" com/google/devtools/build/lib/unix/ErrnoFileStatus" );
252
- CHECK (local != nullptr );
253
- errno_file_status_class = static_cast <jclass>(env->NewGlobalRef (local));
254
- }
255
-
256
- static jmethodID no_error_ctor = nullptr ;
257
- if (no_error_ctor == nullptr ) { // note: harmless race condition
258
- no_error_ctor = env->GetMethodID (errno_file_status_class,
259
- " <init>" , " (IIIIIIIJIJ)V" );
260
- CHECK (no_error_ctor != nullptr );
261
- }
262
-
263
- static jmethodID errorno_ctor = nullptr ;
264
- if (errorno_ctor == nullptr ) { // note: harmless race condition
265
- errorno_ctor = env->GetMethodID (errno_file_status_class, " <init>" , " (I)V" );
266
- CHECK (errorno_ctor != nullptr );
267
- }
268
-
269
256
if (saved_errno != 0 ) {
270
- return env->NewObject (errno_file_status_class, errorno_ctor, saved_errno);
257
+ return env->NewObject (errno_file_status_class,
258
+ errno_file_status_class_errorno_ctor, saved_errno);
271
259
}
272
260
return env->NewObject (
273
- errno_file_status_class, no_error_ctor, stat_ref.st_mode ,
274
- StatSeconds (stat_ref, STAT_ATIME), StatNanoSeconds (stat_ref, STAT_ATIME),
275
- StatSeconds (stat_ref, STAT_MTIME), StatNanoSeconds (stat_ref, STAT_MTIME),
276
- StatSeconds (stat_ref, STAT_CTIME), StatNanoSeconds (stat_ref, STAT_CTIME),
261
+ errno_file_status_class, errno_file_status_class_no_error_ctor,
262
+ stat_ref.st_mode , StatSeconds (stat_ref, STAT_ATIME),
263
+ StatNanoSeconds (stat_ref, STAT_ATIME), StatSeconds (stat_ref, STAT_MTIME),
264
+ StatNanoSeconds (stat_ref, STAT_MTIME), StatSeconds (stat_ref, STAT_CTIME),
265
+ StatNanoSeconds (stat_ref, STAT_CTIME),
277
266
static_cast <jlong>(stat_ref.st_size ), static_cast <int >(stat_ref.st_dev ),
278
267
static_cast <jlong>(stat_ref.st_ino ));
279
268
}
@@ -287,6 +276,26 @@ static void SetIntField(JNIEnv *env,
287
276
CHECK (fid != nullptr );
288
277
env->SetIntField (object, fid, val);
289
278
}
279
+ } // namespace
280
+
281
+ extern " C" JNIEXPORT void JNICALL
282
+ Java_com_google_devtools_build_lib_unix_NativePosixFiles_initJNIClasses (
283
+ JNIEnv *env, jclass clazz) {
284
+ file_status_class =
285
+ makeStaticClass (env, " com/google/devtools/build/lib/unix/FileStatus" );
286
+ errno_file_status_class = makeStaticClass (
287
+ env, " com/google/devtools/build/lib/unix/ErrnoFileStatus" );
288
+ file_status_class_ctor =
289
+ getConstructorID (env, file_status_class, " (IIIIIIIJIJ)V" );
290
+ errno_file_status_class_no_error_ctor =
291
+ getConstructorID (env, errno_file_status_class, " (IIIIIIIJIJ)V" );
292
+ errno_file_status_class_errorno_ctor =
293
+ getConstructorID (env, errno_file_status_class, " (I)V" );
294
+ dirents_class = makeStaticClass (
295
+ env, " com/google/devtools/build/lib/unix/NativePosixFiles$Dirents" );
296
+ dirents_ctor =
297
+ getConstructorID (env, dirents_class, " ([Ljava/lang/String;[B)V" );
298
+ }
290
299
291
300
extern " C" JNIEXPORT void JNICALL
292
301
Java_com_google_devtools_build_lib_unix_ErrnoFileStatus_00024ErrnoConstants_initErrnoConstants ( // NOLINT
@@ -299,6 +308,7 @@ Java_com_google_devtools_build_lib_unix_ErrnoFileStatus_00024ErrnoConstants_init
299
308
SetIntField (env, clazz, errno_constants, " ENAMETOOLONG" , ENAMETOOLONG);
300
309
}
301
310
311
+ namespace {
302
312
static jobject StatCommon (JNIEnv *env, jstring path,
303
313
int (*stat_function)(const char *,
304
314
portable_stat_struct *),
@@ -331,6 +341,7 @@ static jobject StatCommon(JNIEnv *env, jstring path,
331
341
? NewFileStatus (env, statbuf)
332
342
: NewErrnoFileStatus (env, saved_errno, statbuf);
333
343
}
344
+ } // namespace
334
345
335
346
/*
336
347
* Class: com.google.devtools.build.lib.unix.NativePosixFiles
@@ -536,25 +547,11 @@ Java_com_google_devtools_build_lib_unix_NativePosixFiles_mkdirs(JNIEnv *env,
536
547
ReleaseStringLatin1Chars (path_chars);
537
548
}
538
549
550
+ namespace {
539
551
static jobject NewDirents (JNIEnv *env,
540
552
jobjectArray names,
541
553
jbyteArray types) {
542
- // See http://java.sun.com/docs/books/jni/html/fldmeth.html#26855
543
- static jclass dirents_class = nullptr ;
544
- if (dirents_class == nullptr ) { // note: harmless race condition
545
- jclass local = env->FindClass (" com/google/devtools/build/lib/unix/NativePosixFiles$Dirents" );
546
- CHECK (local != nullptr );
547
- dirents_class = static_cast <jclass>(env->NewGlobalRef (local));
548
- }
549
-
550
- static jmethodID ctor = nullptr ;
551
- if (ctor == nullptr ) { // note: harmless race condition
552
- ctor =
553
- env->GetMethodID (dirents_class, " <init>" , " ([Ljava/lang/String;[B)V" );
554
- CHECK (ctor != nullptr );
555
- }
556
-
557
- return env->NewObject (dirents_class, ctor, names, types);
554
+ return env->NewObject (dirents_class, dirents_ctor, names, types);
558
555
}
559
556
560
557
static char GetDirentType (struct dirent *entry,
@@ -582,6 +579,7 @@ static char GetDirentType(struct dirent *entry,
582
579
return ' ?' ;
583
580
}
584
581
}
582
+ } // namespace
585
583
586
584
/*
587
585
* Class: com.google.devtools.build.lib.unix.NativePosixFiles
@@ -739,6 +737,7 @@ Java_com_google_devtools_build_lib_unix_NativePosixFiles_mkfifo(JNIEnv *env,
739
737
ReleaseStringLatin1Chars (path_chars);
740
738
}
741
739
740
+ namespace {
742
741
// Posts an exception generated by the DeleteTreesBelow algorithm and its helper
743
742
// functions.
744
743
//
@@ -973,6 +972,7 @@ static int DeleteTreesBelow(JNIEnv* env, std::vector<std::string>* dir_path,
973
972
dir_path->pop_back ();
974
973
return env->ExceptionOccurred () == nullptr ? 0 : -1 ;
975
974
}
975
+ } // namespace
976
976
977
977
/*
978
978
* Class: com.google.devtools.build.lib.unix.NativePosixFiles
@@ -995,6 +995,7 @@ Java_com_google_devtools_build_lib_unix_NativePosixFiles_deleteTreesBelow(
995
995
// //////////////////////////////////////////////////////////////////////
996
996
// Linux extended file attributes
997
997
998
+ namespace {
998
999
typedef ssize_t getxattr_func (const char *path, const char *name,
999
1000
void *value, size_t size, bool *attr_not_found);
1000
1001
@@ -1027,6 +1028,7 @@ static jbyteArray getxattr_common(JNIEnv *env,
1027
1028
ReleaseStringLatin1Chars (name_chars);
1028
1029
return result;
1029
1030
}
1031
+ } // namespace
1030
1032
1031
1033
extern " C" JNIEXPORT jbyteArray JNICALL
1032
1034
Java_com_google_devtools_build_lib_unix_NativePosixFiles_getxattr (JNIEnv *env,
0 commit comments