[java] improve function figuring out package in jclasspath

Summary: This function uses uncompiled regexps plus it forgets to close the file when it finds a package declaration.  Fix by using Core operations on strings.

Reviewed By: artempyanykh

Differential Revision: D20192957

fbshipit-source-id: 317caacea
master
Nikos Gorogiannis 5 years ago committed by Facebook Github Bot
parent 634a42b619
commit 54c35bc5c6

@ -64,23 +64,19 @@ type t = string * file_entry String.Map.t * JBasics.ClassSet.t
Only the case where the package is declared in a single line is supported *) Only the case where the package is declared in a single line is supported *)
let read_package_declaration source_file = let read_package_declaration source_file =
let path = SourceFile.to_abs_path source_file in let path = SourceFile.to_abs_path source_file in
let file_in = In_channel.create path in let process_line line =
let remove_trailing_semicolon = Str.replace_first (Str.regexp ";") "" in String.strip line |> String.lsplit2 ~on:';' |> Option.map ~f:fst
let empty_package = "" in |> Option.bind ~f:(String.chop_prefix ~prefix:"package")
let rec loop () = |> Option.map ~f:String.strip
match remove_trailing_semicolon (In_channel.input_line_exn file_in) with in
| exception End_of_file -> let rec loop file_in =
In_channel.close file_in ; empty_package match In_channel.input_line file_in with
| line -> ( | None ->
match Str.split (Str.regexp "[ \t]+") line with None
| [] -> | Some line -> (
(loop [@tailcall]) () match process_line line with Some package -> Some package | None -> loop file_in )
| [hd; package] when String.equal hd "package" ->
package
| _ ->
(loop [@tailcall]) () )
in in
loop () Utils.with_file_in path ~f:loop |> Option.value ~default:""
let add_source_file path map = let add_source_file path map =

Loading…
Cancel
Save