From 9d67b6c07dcd5f0f9ed5faa25b89a33aeee7032d Mon Sep 17 00:00:00 2001 From: Jeremy Dubreil Date: Thu, 17 Aug 2017 21:50:44 -0700 Subject: [PATCH] [infer][java] make the code more consistent to detect that constructor is a creating a resource Summary: Other parts of the code where using the checks in the AndroidFramework module. It is better to have those things in one place. Reviewed By: sblackshear Differential Revision: D5654247 fbshipit-source-id: 2a783e7 --- infer/src/harness/androidFramework.ml | 2 ++ infer/src/harness/androidFramework.mli | 2 ++ infer/src/java/jTrans.ml | 5 +++-- infer/src/java/jTransType.ml | 8 -------- infer/src/java/jTransType.mli | 3 --- 5 files changed, 7 insertions(+), 13 deletions(-) diff --git a/infer/src/harness/androidFramework.ml b/infer/src/harness/androidFramework.ml index a57d6eb04..c81eed2d2 100644 --- a/infer/src/harness/androidFramework.ml +++ b/infer/src/harness/androidFramework.ml @@ -71,6 +71,8 @@ let android_lifecycles = let is_subtype_package_class tenv tname package classname = PatternMatch.is_subtype tenv tname (Typ.Name.Java.from_package_class package classname) +let is_autocloseable tenv tname = is_subtype_package_class tenv tname "java.lang" "AutoCloseable" + let is_context tenv tname = is_subtype_package_class tenv tname "android.content" "Context" let is_application tenv tname = is_subtype_package_class tenv tname "android.app" "Application" diff --git a/infer/src/harness/androidFramework.mli b/infer/src/harness/androidFramework.mli index ea76c9f28..9d93d42c6 100644 --- a/infer/src/harness/androidFramework.mli +++ b/infer/src/harness/androidFramework.mli @@ -14,6 +14,8 @@ open! IStd val get_lifecycles : (string * string * string list) list (** return the complete list of (package, lifecycle_classname, lifecycle_methods) trios *) +val is_autocloseable : Tenv.t -> Typ.Name.t -> bool + val is_context : Tenv.t -> Typ.Name.t -> bool (** return true if [typename] <: android.content.Context *) diff --git a/infer/src/java/jTrans.ml b/infer/src/java/jTrans.ml index 290330a76..61d139c72 100644 --- a/infer/src/java/jTrans.ml +++ b/infer/src/java/jTrans.ml @@ -634,9 +634,10 @@ let method_invocation (context: JContext.t) loc pc var_opt cn ms sil_obj_opt exp | _ when Config.models_mode || JClasspath.is_model callee_procname -> call_instrs + | (_, {Typ.desc= Typ.Tptr ({desc= Tstruct typename}, _)} as exp) :: _ (* add a file attribute when calling the constructor of a subtype of Closeable *) - | (_, typ as exp) :: _ - when Typ.Procname.is_constructor callee_procname && JTransType.is_closeable program tenv typ + when Typ.Procname.is_constructor callee_procname + && AndroidFramework.is_autocloseable tenv typename -> let set_file_attr = let set_builtin = Exp.Const (Const.Cfun BuiltinDecl.__set_file_attribute) in Sil.Call (None, set_builtin, [exp], loc, CallFlags.default) diff --git a/infer/src/java/jTransType.ml b/infer/src/java/jTransType.ml index fad794c05..079e0445a 100644 --- a/infer/src/java/jTransType.ml +++ b/infer/src/java/jTransType.ml @@ -374,14 +374,6 @@ let get_class_type program tenv cn = let is_autogenerated_assert_field field_name = String.equal (Typ.Fieldname.java_get_field field_name) "$assertionsDisabled" -let is_closeable program tenv typ = - let closeable_cn = JBasics.make_cn "java.io.Closeable" in - let closeable_typ = get_class_type program tenv closeable_cn in - let autocloseable_cn = JBasics.make_cn "java.lang.AutoCloseable" in - let autocloseable_typ = get_class_type program tenv autocloseable_cn in - let implements t = Prover.Subtyping_check.check_subtype tenv typ t in - implements closeable_typ || implements autocloseable_typ - (** translate an object type *) let rec object_type program tenv ot = match ot with diff --git a/infer/src/java/jTransType.mli b/infer/src/java/jTransType.mli index f993e12c8..22443470c 100644 --- a/infer/src/java/jTransType.mli +++ b/infer/src/java/jTransType.mli @@ -43,9 +43,6 @@ val get_class_type : JClasspath.program -> Tenv.t -> JBasics.class_name -> Typ.t val is_autogenerated_assert_field : Typ.Fieldname.t -> bool (** return true if [field_name] is the autogenerated C.$assertionsDisabled field for class C *) -val is_closeable : JClasspath.program -> Tenv.t -> Typ.t -> bool -(** [is_closeable program tenv typ] check if typ is an implemtation of the Closeable interface *) - val object_type : JClasspath.program -> Tenv.t -> JBasics.object_type -> Typ.t (** transforms a Java object type to a Typ.t *)