From 3a856aa6f00f197dbc81d7c04cd17f373413c1c7 Mon Sep 17 00:00:00 2001 From: Jeremy Dubreil Date: Mon, 25 Apr 2016 14:44:06 -0700 Subject: [PATCH] Add example of lazy dynamic dispatch calling a method from the interface Summary: Example of dynamic dispatch with interfaces were already working. Adding some tests now so that we don't break this. Reviewed By: sblackshear Differential Revision: D3220360 fb-gh-sync-id: 11395dd fbshipit-source-id: 11395dd --- .../tracing/LazyDynamicDispatchExample.java | 28 +++++++++++++++---- .../java/tracing/LazyDynamicDispatchTest.java | 3 +- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/infer/tests/codetoanalyze/java/tracing/LazyDynamicDispatchExample.java b/infer/tests/codetoanalyze/java/tracing/LazyDynamicDispatchExample.java index 62e9ceab1..e263928d8 100644 --- a/infer/tests/codetoanalyze/java/tracing/LazyDynamicDispatchExample.java +++ b/infer/tests/codetoanalyze/java/tracing/LazyDynamicDispatchExample.java @@ -9,7 +9,11 @@ package codetoanalyze.java.tracing; -class A { +interface I { + T get(); +} + +class A implements I { public T get() { return new T(); @@ -17,7 +21,7 @@ class A { } -class B extends A { +class B extends A { public T get() { return null; @@ -27,13 +31,27 @@ class B extends A { public class LazyDynamicDispatchExample { - static T foo(A a) { + static T fromSupertype(A a) { return a.get(); } - static void bar() { + static T fromInterface(I i) { + return i.get(); + } + + static void callWithSubtype() { + B b = new B(); + fromSupertype(b).f(); + } + + static void shouldNotReportLocalVarTypeIsKnown() { + A a = new A(); + fromInterface(a).f(); + } + + static void shouldReportLocalVarTypeIsKnown() { B b = new B(); - foo(b).f(); + fromInterface(b).f(); } } diff --git a/infer/tests/endtoend/java/tracing/LazyDynamicDispatchTest.java b/infer/tests/endtoend/java/tracing/LazyDynamicDispatchTest.java index b5e409e30..757057425 100644 --- a/infer/tests/endtoend/java/tracing/LazyDynamicDispatchTest.java +++ b/infer/tests/endtoend/java/tracing/LazyDynamicDispatchTest.java @@ -42,7 +42,8 @@ public class LazyDynamicDispatchTest { public void matchErrors() throws IOException, InterruptedException, InferException { String[] methods = { - "bar" + "callWithSubtype", + "shouldReportLocalVarTypeIsKnown", }; assertThat( "Results should contain " + NPE,