diff --git a/infer/src/IR/JavaClassName.ml b/infer/src/IR/JavaClassName.ml index 088c7100e..84824cab8 100644 --- a/infer/src/IR/JavaClassName.ml +++ b/infer/src/IR/JavaClassName.ml @@ -40,6 +40,20 @@ let package {package} = package let classname {classname} = classname +let is_anonymous_inner_class_name {classname} = + match String.rsplit2 classname ~on:'$' with + | Some (_, s) -> + let is_int = + try + ignore (int_of_string (String.strip s)) ; + true + with Failure _ -> false + in + is_int + | None -> + false + + let is_external_via_config t = let package = package t in Option.exists ~f:Config.java_package_is_external package diff --git a/infer/src/IR/JavaClassName.mli b/infer/src/IR/JavaClassName.mli index a096c948a..0d42a35e4 100644 --- a/infer/src/IR/JavaClassName.mli +++ b/infer/src/IR/JavaClassName.mli @@ -24,3 +24,7 @@ val classname : t -> string val is_external_via_config : t -> bool (** Considered external based on config flags. *) + +val is_anonymous_inner_class_name : t -> bool +(** True if it is anonymous Java class: + https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html *) diff --git a/infer/src/IR/Procname.ml b/infer/src/IR/Procname.ml index e450103c6..9928cedce 100644 --- a/infer/src/IR/Procname.ml +++ b/infer/src/IR/Procname.ml @@ -171,8 +171,8 @@ module Java = struct let is_constructor {method_name} = String.equal method_name constructor_method_name - let is_anonymous_inner_class_constructor {class_name} = - Typ.Name.Java.is_anonymous_inner_class_name class_name + let is_anonymous_inner_class_constructor_exn {class_name} = + Typ.Name.Java.is_anonymous_inner_class_name_exn class_name let is_static {kind} = match kind with Static -> true | _ -> false diff --git a/infer/src/IR/Procname.mli b/infer/src/IR/Procname.mli index fdbfa7c2c..35ddb31a1 100644 --- a/infer/src/IR/Procname.mli +++ b/infer/src/IR/Procname.mli @@ -69,8 +69,9 @@ module Java : sig val is_autogen_method : t -> bool (** Check if the procedure name is of an auto-generated method containing '$'. *) - val is_anonymous_inner_class_constructor : t -> bool - (** Check if the procedure name is an anonymous inner class constructor. *) + val is_anonymous_inner_class_constructor_exn : t -> bool + (** Check if the procedure name is an anonymous inner class constructor. Throws if it is not a + Java type *) val is_close : t -> bool (** Check if the method name is "close". *) diff --git a/infer/src/IR/Typ.ml b/infer/src/IR/Typ.ml index 4d520e17b..894430b92 100644 --- a/infer/src/IR/Typ.ml +++ b/infer/src/IR/Typ.ml @@ -475,20 +475,9 @@ module Name = struct L.die InternalError "Tried to split a non-java class name into a java split type@." - let is_anonymous_inner_class_name class_name = + let is_anonymous_inner_class_name_exn class_name = let java_class_name = get_java_class_name_exn class_name in - let class_name_no_package = JavaClassName.classname java_class_name in - match String.rsplit2 class_name_no_package ~on:'$' with - | Some (_, s) -> - let is_int = - try - ignore (int_of_string (String.strip s)) ; - true - with Failure _ -> false - in - is_int - | None -> - false + JavaClassName.is_anonymous_inner_class_name java_class_name let is_external t = diff --git a/infer/src/IR/Typ.mli b/infer/src/IR/Typ.mli index 3a0eb4786..1415663f9 100644 --- a/infer/src/IR/Typ.mli +++ b/infer/src/IR/Typ.mli @@ -230,7 +230,8 @@ module Name : sig val is_external : t -> bool (** return true if the typename is in the .inferconfig list of external classes *) - val is_anonymous_inner_class_name : t -> bool + val is_anonymous_inner_class_name_exn : t -> bool + (** Throws if it is not a Java class *) val java_lang_object : t diff --git a/infer/src/nullsafe/typeCheck.ml b/infer/src/nullsafe/typeCheck.ml index 7f2b6a06a..048967bd0 100644 --- a/infer/src/nullsafe/typeCheck.ml +++ b/infer/src/nullsafe/typeCheck.ml @@ -1074,7 +1074,7 @@ let typecheck_sil_call_function find_canonical_duplicate checks tenv instr_ref t drop_unchecked_signature_params callee_attributes callee_annotated_signature in let is_anonymous_inner_class_constructor = - Procname.Java.is_anonymous_inner_class_constructor callee_pname_java + Procname.Java.is_anonymous_inner_class_constructor_exn callee_pname_java in let do_return (ret_ta, ret_typ) typestate' = let mk_return_range () = (ret_typ, ret_ta) in