[java] silence javalib warnings on stderr when loading class with Java8 Code

Summary:
<ugly shameful hack>
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.
</ugly shameful hack>

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
master
Jules Villard 8 years ago committed by Facebook Github Bot
parent ac082cfe01
commit a07e16871c

@ -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

@ -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

@ -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 =

Loading…
Cancel
Save