From a07e16871cb1dee6870925b8bc12ad7a6e82bc82 Mon Sep 17 00:00:00 2001 From: Jules Villard Date: Tue, 13 Dec 2016 10:33:08 -0800 Subject: [PATCH] [java] silence javalib warnings on stderr when loading class with Java8 Code Summary: Temporarily redirect stderr to /dev/null before calling `Javalib.get_class` so as to avoid getting spammed with "Warning: unexpected attribute: Code" messages when parsing Java files. I suspect that now that Javalib handles Java 8 this issue is more prevalent. An issue/PR should be sent to Javalib too so that it's fixed upstream and we can eventually remove the hack (t15039096). Reviewed By: jberdine Differential Revision: D4319466 fbshipit-source-id: af855ba --- infer/src/base/Utils.ml | 11 +++++++++++ infer/src/base/Utils.mli | 4 ++++ infer/src/java/jClasspath.ml | 7 +++++-- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/infer/src/base/Utils.ml b/infer/src/base/Utils.ml index 37126a967..3f8671a40 100644 --- a/infer/src/base/Utils.ml +++ b/infer/src/base/Utils.ml @@ -328,3 +328,14 @@ let realpath path = ) | Ok path -> path | Error (code, f, arg) -> raise (Unix.Unix_error (code, f, arg)) + + +let suppress_stderr2 f2 x1 x2 = + let orig_stderr = Unix.dup Unix.stderr in + let silent_stderr = Unix.openfile "/dev/null" ~mode:[Unix.O_RDWR] in + let restore_stderr () = + Unix.dup2 ~src:orig_stderr ~dst:Unix.stderr; + Unix.close silent_stderr in + Unix.dup2 ~src:silent_stderr ~dst:Unix.stderr; + let f () = f2 x1 x2 in + protect ~f ~finally:restore_stderr diff --git a/infer/src/base/Utils.mli b/infer/src/base/Utils.mli index 0fbb3e971..f4e7cbe05 100644 --- a/infer/src/base/Utils.mli +++ b/infer/src/base/Utils.mli @@ -75,3 +75,7 @@ val create_dir : string -> unit (** [realpath path] returns path with all symbolic links resolved. It caches results of previous calls to avoid expensive system calls *) val realpath : string -> string + +(** wraps a function expecting 2 arguments in another that temporarily redirects stderr to /dev/null + for the duration of the function call *) +val suppress_stderr2 : ('a -> 'b -> 'c) -> 'a -> 'b -> 'c diff --git a/infer/src/java/jClasspath.ml b/infer/src/java/jClasspath.ml index 56964136f..ab3dc0aca 100644 --- a/infer/src/java/jClasspath.ml +++ b/infer/src/java/jClasspath.ml @@ -14,6 +14,9 @@ open Javalib_pack module L = Logging +(** version of Javalib.get_class that does not spam stderr *) +let javalib_get_class = Utils.suppress_stderr2 Javalib.get_class + let models_specs_filenames = ref String.Set.empty let models_jar = ref "" @@ -297,7 +300,7 @@ let lookup_node cn program = Some (JBasics.ClassMap.find cn (get_classmap program)) with Not_found -> try - let jclass = Javalib.get_class (get_classpath program) cn in + let jclass = javalib_get_class (get_classpath program) cn in add_class cn jclass program; Some jclass with @@ -310,7 +313,7 @@ let collect_classes start_classmap jar_filename = let classpath = Javalib.class_path jar_filename in let collect classmap cn = try - JBasics.ClassMap.add cn (Javalib.get_class classpath cn) classmap + JBasics.ClassMap.add cn (javalib_get_class classpath cn) classmap with JBasics.Class_structure_error _ -> classmap in let classmap =